blob: 66d17b7dc73c4dd344ba27645b12b64d6158fc17 [file] [log] [blame]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001/* parsermodule.c
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002 *
Guido van Rossum47478871996-08-21 14:32:37 +00003 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
4 * Institute and State University, Blacksburg, Virginia, USA.
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7 * associated with the main Python distribution, with the additional
8 * restriction that this additional notice be included and maintained
9 * on all distributed copies.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000010 *
Guido van Rossum47478871996-08-21 14:32:37 +000011 * This module serves to replace the original parser module written
12 * by Guido. The functionality is not matched precisely, but the
13 * original may be implemented on top of this. This is desirable
14 * since the source of the text to be parsed is now divorced from
15 * this interface.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000016 *
Guido van Rossum47478871996-08-21 14:32:37 +000017 * Unlike the prior interface, the ability to give a parse tree
18 * produced by Python code as a tuple to the compiler is enabled by
19 * this module. See the documentation for more details.
Fred Drake268397f1998-04-29 14:16:32 +000020 *
21 * I've added some annotations that help with the lint code-checking
22 * program, but they're not complete by a long shot. The real errors
23 * that lint detects are gone, but there are still warnings with
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
25 * look like "NOTE(...)".
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000026 */
27
Fred Drakeff9ea482000-04-19 13:54:15 +000028#include "Python.h" /* general Python API */
29#include "graminit.h" /* symbols defined in the grammar */
30#include "node.h" /* internal parser structure */
Fred Drake8b55b2d2001-12-05 22:10:44 +000031#include "errcode.h" /* error codes for PyNode_*() */
Fred Drakeff9ea482000-04-19 13:54:15 +000032#include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34#include "compile.h" /* PyNode_Compile() */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000035
Fred Drake268397f1998-04-29 14:16:32 +000036#ifdef lint
37#include <note.h>
38#else
39#define NOTE(x)
40#endif
41
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000042/* String constants used to initialize module attributes.
43 *
44 */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045static char parser_copyright_string[] =
46"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
Guido van Rossum2a288461996-08-21 21:55:43 +000047University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49Centrum, Amsterdam, The Netherlands.";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000050
51
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000052PyDoc_STRVAR(parser_doc_string,
53"This is an interface to Python's internal parser.");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000055static char parser_version_string[] = "0.5";
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000056
57
Martin v. Löwis18e16552006-02-15 17:27:45 +000058typedef PyObject* (*SeqMaker) (Py_ssize_t length);
Fred Drakeff9ea482000-04-19 13:54:15 +000059typedef int (*SeqInserter) (PyObject* sequence,
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 Py_ssize_t index,
Fred Drakeff9ea482000-04-19 13:54:15 +000061 PyObject* element);
Guido van Rossum47478871996-08-21 14:32:37 +000062
Thomas Wouters7e474022000-07-16 12:04:32 +000063/* The function below is copyrighted by Stichting Mathematisch Centrum. The
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000064 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
Guido van Rossum2a288461996-08-21 21:55:43 +000068 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000070 */
71
Guido van Rossum52f2c051993-11-10 12:53:24 +000072
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +000073static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +000074node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
Guido van Rossum47478871996-08-21 14:32:37 +000079{
Guido van Rossum3d602e31996-07-21 02:33:56 +000080 if (n == NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +000081 Py_INCREF(Py_None);
82 return (Py_None);
Guido van Rossum3d602e31996-07-21 02:33:56 +000083 }
84 if (ISNONTERMINAL(TYPE(n))) {
Fred Drakeff9ea482000-04-19 13:54:15 +000085 int i;
86 PyObject *v;
87 PyObject *w;
Fred Drake268397f1998-04-29 14:16:32 +000088
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000089 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
Fred Drakeff9ea482000-04-19 13:54:15 +000090 if (v == NULL)
91 return (v);
92 w = PyInt_FromLong(TYPE(n));
93 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
96 }
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
103 }
104 (void) addelem(v, i+1, w);
105 }
Tim Peters6a627252003-07-21 14:25:23 +0000106
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 if (TYPE(n) == encoding_decl)
108 (void) addelem(v, i+1, PyString_FromString(STR(n)));
Fred Drakeff9ea482000-04-19 13:54:15 +0000109 return (v);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000110 }
111 else if (ISTERMINAL(TYPE(n))) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 PyObject *result = mkseq(2 + lineno + col_offset);
Fred Drakeff9ea482000-04-19 13:54:15 +0000113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
115 (void) addelem(result, 1, PyString_FromString(STR(n)));
116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 if (col_offset == 1)
119 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
Fred Drakeff9ea482000-04-19 13:54:15 +0000120 }
121 return (result);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000122 }
123 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
Guido van Rossum3d602e31996-07-21 02:33:56 +0000127 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000128}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000129/*
130 * End of material copyrighted by Stichting Mathematisch Centrum.
131 */
Guido van Rossum52f2c051993-11-10 12:53:24 +0000132
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000133
134
135/* There are two types of intermediate objects we're interested in:
Fred Drakec2683dd2001-07-17 19:32:05 +0000136 * 'eval' and 'exec' types. These constants can be used in the st_type
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000140 */
141
Fred Drakec2683dd2001-07-17 19:32:05 +0000142#define PyST_EXPR 1
143#define PyST_SUITE 2
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000144
145
146/* These are the internal objects and definitions required to implement the
Fred Drakec2683dd2001-07-17 19:32:05 +0000147 * ST type. Most of the internal names are more reminiscent of the 'old'
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000148 * naming style, but the code uses the new naming convention.
149 */
150
151static PyObject*
152parser_error = 0;
153
154
Fred Drakec2683dd2001-07-17 19:32:05 +0000155typedef struct {
Fred Drakeff9ea482000-04-19 13:54:15 +0000156 PyObject_HEAD /* standard object header */
Fred Drakec2683dd2001-07-17 19:32:05 +0000157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159} PyST_Object;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000160
161
Jeremy Hylton938ace62002-07-17 16:30:39 +0000162static void parser_free(PyST_Object *st);
163static int parser_compare(PyST_Object *left, PyST_Object *right);
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000164static PyObject *parser_getattr(PyObject *self, char *name);
Fred Drake503d8d61998-04-13 18:45:18 +0000165
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000166
Fred Drake268397f1998-04-29 14:16:32 +0000167static
Fred Drakec2683dd2001-07-17 19:32:05 +0000168PyTypeObject PyST_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000169 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000170 "parser.st", /* tp_name */
Fred Drakec2683dd2001-07-17 19:32:05 +0000171 (int) sizeof(PyST_Object), /* tp_basicsize */
Fred Drakeff9ea482000-04-19 13:54:15 +0000172 0, /* tp_itemsize */
173 (destructor)parser_free, /* tp_dealloc */
174 0, /* tp_print */
175 parser_getattr, /* tp_getattr */
176 0, /* tp_setattr */
177 (cmpfunc)parser_compare, /* tp_compare */
178 0, /* tp_repr */
179 0, /* tp_as_number */
180 0, /* tp_as_sequence */
181 0, /* tp_as_mapping */
182 0, /* tp_hash */
183 0, /* tp_call */
184 0, /* tp_str */
185 0, /* tp_getattro */
186 0, /* tp_setattro */
Fred Drake69b9ae41997-05-23 04:04:17 +0000187
188 /* Functions to access object as input/output buffer */
Fred Drakeff9ea482000-04-19 13:54:15 +0000189 0, /* tp_as_buffer */
Fred Drake69b9ae41997-05-23 04:04:17 +0000190
Fred Drakeff9ea482000-04-19 13:54:15 +0000191 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake69b9ae41997-05-23 04:04:17 +0000192
193 /* __doc__ */
194 "Intermediate representation of a Python parse tree."
Fred Drakec2683dd2001-07-17 19:32:05 +0000195}; /* PyST_Type */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000196
197
198static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000199parser_compare_nodes(node *left, node *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000200{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000201 int j;
Guido van Rossum52f2c051993-11-10 12:53:24 +0000202
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000203 if (TYPE(left) < TYPE(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000204 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000205
206 if (TYPE(right) < TYPE(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000207 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000208
209 if (ISTERMINAL(TYPE(left)))
Fred Drakeff9ea482000-04-19 13:54:15 +0000210 return (strcmp(STR(left), STR(right)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000211
212 if (NCH(left) < NCH(right))
Fred Drakeff9ea482000-04-19 13:54:15 +0000213 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000214
215 if (NCH(right) < NCH(left))
Fred Drakeff9ea482000-04-19 13:54:15 +0000216 return (1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000217
218 for (j = 0; j < NCH(left); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000219 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000220
Fred Drakeff9ea482000-04-19 13:54:15 +0000221 if (v != 0)
222 return (v);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000223 }
224 return (0);
Fred Drakeff9ea482000-04-19 13:54:15 +0000225}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000226
227
Fred Drakec2683dd2001-07-17 19:32:05 +0000228/* int parser_compare(PyST_Object* left, PyST_Object* right)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000229 *
230 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
231 * This really just wraps a call to parser_compare_nodes() with some easy
232 * checks and protection code.
233 *
234 */
235static int
Fred Drakec2683dd2001-07-17 19:32:05 +0000236parser_compare(PyST_Object *left, PyST_Object *right)
Guido van Rossum47478871996-08-21 14:32:37 +0000237{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000238 if (left == right)
Fred Drakeff9ea482000-04-19 13:54:15 +0000239 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000240
241 if ((left == 0) || (right == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +0000242 return (-1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000243
Fred Drakec2683dd2001-07-17 19:32:05 +0000244 return (parser_compare_nodes(left->st_node, right->st_node));
Fred Drakeff9ea482000-04-19 13:54:15 +0000245}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000246
247
Fred Drakec2683dd2001-07-17 19:32:05 +0000248/* parser_newstobject(node* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000249 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000250 * Allocates a new Python object representing an ST. This is simply the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000251 * 'wrapper' object that holds a node* and allows it to be passed around in
252 * Python code.
253 *
254 */
255static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000256parser_newstobject(node *st, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000257{
Fred Drakec2683dd2001-07-17 19:32:05 +0000258 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000259
260 if (o != 0) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000261 o->st_node = st;
262 o->st_type = type;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000263 }
Fred Drake268397f1998-04-29 14:16:32 +0000264 else {
Fred Drakec2683dd2001-07-17 19:32:05 +0000265 PyNode_Free(st);
Fred Drake268397f1998-04-29 14:16:32 +0000266 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000267 return ((PyObject*)o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000268}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000269
270
Fred Drakec2683dd2001-07-17 19:32:05 +0000271/* void parser_free(PyST_Object* st)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000272 *
273 * This is called by a del statement that reduces the reference count to 0.
274 *
275 */
276static void
Fred Drakec2683dd2001-07-17 19:32:05 +0000277parser_free(PyST_Object *st)
Guido van Rossum47478871996-08-21 14:32:37 +0000278{
Fred Drakec2683dd2001-07-17 19:32:05 +0000279 PyNode_Free(st->st_node);
280 PyObject_Del(st);
Fred Drakeff9ea482000-04-19 13:54:15 +0000281}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000282
283
Fred Drakec2683dd2001-07-17 19:32:05 +0000284/* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000285 *
286 * This provides conversion from a node* to a tuple object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000287 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000288 *
289 */
290static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000291parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000292{
Guido van Rossum47478871996-08-21 14:32:37 +0000293 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000294 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000295 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000296 int ok;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000297
Thomas Wouters89f507f2006-12-13 04:49:30 +0000298 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000299
Fred Drake268397f1998-04-29 14:16:32 +0000300 if (self == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000301 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
302 &PyST_Type, &self, &line_option,
303 &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000304 }
Fred Drake503d8d61998-04-13 18:45:18 +0000305 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000306 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
307 &line_option, &col_option);
Fred Drake268397f1998-04-29 14:16:32 +0000308 if (ok != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000309 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000310 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000311 if (line_option != NULL) {
312 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
313 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000314 if (col_option != NULL) {
315 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
316 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000317 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000318 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000319 * since it's known to work already.
320 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000321 res = node2tuple(((PyST_Object*)self)->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000322 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000323 }
324 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000325}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000326
327
Fred Drakec2683dd2001-07-17 19:32:05 +0000328/* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000329 *
Fred Drake2a6875e1999-09-20 22:32:18 +0000330 * This provides conversion from a node* to a list object that can be
Fred Drakec2683dd2001-07-17 19:32:05 +0000331 * returned to the Python-level caller. The ST object is not modified.
Guido van Rossum47478871996-08-21 14:32:37 +0000332 *
333 */
334static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000335parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000336{
Guido van Rossum47478871996-08-21 14:32:37 +0000337 PyObject *line_option = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000338 PyObject *col_option = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000339 PyObject *res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000340 int ok;
Guido van Rossum47478871996-08-21 14:32:37 +0000341
Thomas Wouters89f507f2006-12-13 04:49:30 +0000342 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000343
Fred Drake503d8d61998-04-13 18:45:18 +0000344 if (self == NULL)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000345 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
346 &PyST_Type, &self, &line_option,
347 &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000348 else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000349 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
350 &line_option, &col_option);
Fred Drake503d8d61998-04-13 18:45:18 +0000351 if (ok) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000352 int lineno = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000353 int col_offset = 0;
Fred Drakeff9ea482000-04-19 13:54:15 +0000354 if (line_option != 0) {
355 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
356 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000357 if (col_option != NULL) {
358 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
359 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000360 /*
Fred Drakec2683dd2001-07-17 19:32:05 +0000361 * Convert ST into a tuple representation. Use Guido's function,
Fred Drakeff9ea482000-04-19 13:54:15 +0000362 * since it's known to work already.
363 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000364 res = node2tuple(self->st_node,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000365 PyList_New, PyList_SetItem, lineno, col_offset);
Guido van Rossum47478871996-08-21 14:32:37 +0000366 }
367 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000368}
Guido van Rossum47478871996-08-21 14:32:37 +0000369
370
Fred Drakec2683dd2001-07-17 19:32:05 +0000371/* parser_compilest(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000372 *
373 * This function creates code objects from the parse tree represented by
374 * the passed-in data object. An optional file name is passed in as well.
375 *
376 */
377static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000378parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000379{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000380 PyObject* res = 0;
Fred Drakec2683dd2001-07-17 19:32:05 +0000381 char* str = "<syntax-tree>";
Fred Drake503d8d61998-04-13 18:45:18 +0000382 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000383
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000384 static char *keywords[] = {"ast", "filename", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000385
Fred Drake503d8d61998-04-13 18:45:18 +0000386 if (self == NULL)
Fred Drakec2683dd2001-07-17 19:32:05 +0000387 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
388 &PyST_Type, &self, &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000389 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000390 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
Fred Drake7a15ba51999-09-09 14:21:52 +0000391 &str);
Fred Drake503d8d61998-04-13 18:45:18 +0000392
393 if (ok)
Fred Drakec2683dd2001-07-17 19:32:05 +0000394 res = (PyObject *)PyNode_Compile(self->st_node, str);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000395
396 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000397}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000398
399
400/* PyObject* parser_isexpr(PyObject* self, PyObject* args)
401 * PyObject* parser_issuite(PyObject* self, PyObject* args)
402 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000403 * Checks the passed-in ST object to determine if it is an expression or
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000404 * a statement suite, respectively. The return is a Python truth value.
405 *
406 */
407static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000408parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000409{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000410 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000411 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000412
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000413 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000414
Fred Drake503d8d61998-04-13 18:45:18 +0000415 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000416 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000417 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000418 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000419 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000420
421 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000422 /* Check to see if the ST represents an expression or not. */
423 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
Fred Drakeff9ea482000-04-19 13:54:15 +0000424 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000425 }
426 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000427}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000428
429
430static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000431parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000432{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000433 PyObject* res = 0;
Fred Drake503d8d61998-04-13 18:45:18 +0000434 int ok;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000435
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000436 static char *keywords[] = {"ast", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000437
Fred Drake503d8d61998-04-13 18:45:18 +0000438 if (self == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000439 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
Fred Drakec2683dd2001-07-17 19:32:05 +0000440 &PyST_Type, &self);
Fred Drake503d8d61998-04-13 18:45:18 +0000441 else
Fred Drakeff9ea482000-04-19 13:54:15 +0000442 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
Fred Drake503d8d61998-04-13 18:45:18 +0000443
444 if (ok) {
Fred Drakec2683dd2001-07-17 19:32:05 +0000445 /* Check to see if the ST represents an expression or not. */
446 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
Fred Drakeff9ea482000-04-19 13:54:15 +0000447 Py_INCREF(res);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000448 }
449 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000450}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000451
452
Fred Drake7a15ba51999-09-09 14:21:52 +0000453#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
454
Fred Drake503d8d61998-04-13 18:45:18 +0000455static PyMethodDef
456parser_methods[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +0000457 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000458 PyDoc_STR("Compile this ST object into a code object.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000459 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000460 PyDoc_STR("Determines if this ST object was created from an expression.")},
Fred Drakeff9ea482000-04-19 13:54:15 +0000461 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000462 PyDoc_STR("Determines if this ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000463 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000464 PyDoc_STR("Creates a list-tree representation of this ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +0000465 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +0000466 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
Fred Drake503d8d61998-04-13 18:45:18 +0000467
Fred Drake268397f1998-04-29 14:16:32 +0000468 {NULL, NULL, 0, NULL}
Fred Drake503d8d61998-04-13 18:45:18 +0000469};
470
Fred Drake503d8d61998-04-13 18:45:18 +0000471
472static PyObject*
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000473parser_getattr(PyObject *self, char *name)
Fred Drake503d8d61998-04-13 18:45:18 +0000474{
Fred Drake503d8d61998-04-13 18:45:18 +0000475 return (Py_FindMethod(parser_methods, self, name));
Fred Drakeff9ea482000-04-19 13:54:15 +0000476}
Fred Drake503d8d61998-04-13 18:45:18 +0000477
478
Guido van Rossum3d602e31996-07-21 02:33:56 +0000479/* err_string(char* message)
480 *
481 * Sets the error string for an exception of type ParserError.
482 *
483 */
484static void
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +0000485err_string(char *message)
Guido van Rossum47478871996-08-21 14:32:37 +0000486{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000487 PyErr_SetString(parser_error, message);
Fred Drakeff9ea482000-04-19 13:54:15 +0000488}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000489
490
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000491/* PyObject* parser_do_parse(PyObject* args, int type)
492 *
493 * Internal function to actually execute the parse and return the result if
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000494 * successful or set an exception if not.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000495 *
496 */
497static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +0000498parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
Guido van Rossum47478871996-08-21 14:32:37 +0000499{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000500 char* string = 0;
501 PyObject* res = 0;
502
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000503 static char *keywords[] = {"source", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000504
505 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000506 node* n = PyParser_SimpleParseString(string,
Fred Drakec2683dd2001-07-17 19:32:05 +0000507 (type == PyST_EXPR)
Fred Drakeff9ea482000-04-19 13:54:15 +0000508 ? eval_input : file_input);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000509
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000510 if (n)
511 res = parser_newstobject(n, type);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000512 }
513 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000514}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000515
516
517/* PyObject* parser_expr(PyObject* self, PyObject* args)
518 * PyObject* parser_suite(PyObject* self, PyObject* args)
519 *
520 * External interfaces to the parser itself. Which is called determines if
521 * the parser attempts to recognize an expression ('eval' form) or statement
522 * suite ('exec' form). The real work is done by parser_do_parse() above.
523 *
524 */
525static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000526parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000527{
Fred Drake268397f1998-04-29 14:16:32 +0000528 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000529 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
Fred Drakeff9ea482000-04-19 13:54:15 +0000530}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000531
532
533static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000534parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000535{
Fred Drake268397f1998-04-29 14:16:32 +0000536 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000537 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
Fred Drakeff9ea482000-04-19 13:54:15 +0000538}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000539
540
541
Fred Drakec2683dd2001-07-17 19:32:05 +0000542/* This is the messy part of the code. Conversion from a tuple to an ST
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000543 * object requires that the input tuple be valid without having to rely on
544 * catching an exception from the compiler. This is done to allow the
545 * compiler itself to remain fast, since most of its input will come from
546 * the parser directly, and therefore be known to be syntactically correct.
547 * This validation is done to ensure that we don't core dump the compile
548 * phase, returning an exception instead.
549 *
550 * Two aspects can be broken out in this code: creating a node tree from
551 * the tuple passed in, and verifying that it is indeed valid. It may be
Fred Drakec2683dd2001-07-17 19:32:05 +0000552 * advantageous to expand the number of ST types to include funcdefs and
553 * lambdadefs to take advantage of the optimizer, recognizing those STs
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000554 * here. They are not necessary, and not quite as useful in a raw form.
555 * For now, let's get expressions and suites working reliably.
556 */
557
558
Jeremy Hylton938ace62002-07-17 16:30:39 +0000559static node* build_node_tree(PyObject *tuple);
560static int validate_expr_tree(node *tree);
561static int validate_file_input(node *tree);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000562static int validate_encoding_decl(node *tree);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000563
Fred Drakec2683dd2001-07-17 19:32:05 +0000564/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000565 *
566 * This is the public function, called from the Python code. It receives a
Fred Drakec2683dd2001-07-17 19:32:05 +0000567 * single tuple object from the caller, and creates an ST object if the
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000568 * tuple can be validated. It does this by checking the first code of the
569 * tuple, and, if acceptable, builds the internal representation. If this
570 * step succeeds, the internal representation is validated as fully as
571 * possible with the various validate_*() routines defined below.
572 *
Fred Drakec2683dd2001-07-17 19:32:05 +0000573 * This function must be changed if support is to be added for PyST_FRAGMENT
574 * ST objects.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000575 *
576 */
577static PyObject*
Fred Drakec2683dd2001-07-17 19:32:05 +0000578parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
Guido van Rossum47478871996-08-21 14:32:37 +0000579{
Fred Drake268397f1998-04-29 14:16:32 +0000580 NOTE(ARGUNUSED(self))
Fred Drakec2683dd2001-07-17 19:32:05 +0000581 PyObject *st = 0;
Fred Drake0ac9b072000-09-12 21:58:06 +0000582 PyObject *tuple;
583 node *tree;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000584
Martin v. Löwisb79afb62006-02-27 17:01:22 +0000585 static char *keywords[] = {"sequence", NULL};
Fred Drake7a15ba51999-09-09 14:21:52 +0000586
Fred Drakec2683dd2001-07-17 19:32:05 +0000587 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
Fred Drake7a15ba51999-09-09 14:21:52 +0000588 &tuple))
Fred Drakeff9ea482000-04-19 13:54:15 +0000589 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000590 if (!PySequence_Check(tuple)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000591 PyErr_SetString(PyExc_ValueError,
Fred Drakec2683dd2001-07-17 19:32:05 +0000592 "sequence2st() requires a single sequence argument");
Fred Drakeff9ea482000-04-19 13:54:15 +0000593 return (0);
Guido van Rossum47478871996-08-21 14:32:37 +0000594 }
595 /*
Fred Drake0ac9b072000-09-12 21:58:06 +0000596 * Convert the tree to the internal form before checking it.
Guido van Rossum47478871996-08-21 14:32:37 +0000597 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000598 tree = build_node_tree(tuple);
599 if (tree != 0) {
600 int start_sym = TYPE(tree);
601 if (start_sym == eval_input) {
602 /* Might be an eval form. */
603 if (validate_expr_tree(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000604 st = parser_newstobject(tree, PyST_EXPR);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000605 else
606 PyNode_Free(tree);
Fred Drakeff9ea482000-04-19 13:54:15 +0000607 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000608 else if (start_sym == file_input) {
609 /* This looks like an exec form so far. */
610 if (validate_file_input(tree))
Fred Drakec2683dd2001-07-17 19:32:05 +0000611 st = parser_newstobject(tree, PyST_SUITE);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000612 else
613 PyNode_Free(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +0000614 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000615 else if (start_sym == encoding_decl) {
616 /* This looks like an encoding_decl so far. */
617 if (validate_encoding_decl(tree))
618 st = parser_newstobject(tree, PyST_SUITE);
619 else
620 PyNode_Free(tree);
621 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000622 else {
623 /* This is a fragment, at best. */
624 PyNode_Free(tree);
Fred Drake661ea262000-10-24 19:57:45 +0000625 err_string("parse tree does not use a valid start symbol");
Fred Drake0ac9b072000-09-12 21:58:06 +0000626 }
Guido van Rossum47478871996-08-21 14:32:37 +0000627 }
Guido van Rossum47478871996-08-21 14:32:37 +0000628 /* Make sure we throw an exception on all errors. We should never
629 * get this, but we'd do well to be sure something is done.
630 */
Fred Drakec2683dd2001-07-17 19:32:05 +0000631 if (st == NULL && !PyErr_Occurred())
632 err_string("unspecified ST error occurred");
Guido van Rossum3d602e31996-07-21 02:33:56 +0000633
Fred Drakec2683dd2001-07-17 19:32:05 +0000634 return st;
Fred Drakeff9ea482000-04-19 13:54:15 +0000635}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000636
637
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000638/* node* build_node_children()
639 *
640 * Iterate across the children of the current non-terminal node and build
641 * their structures. If successful, return the root of this portion of
642 * the tree, otherwise, 0. Any required exception will be specified already,
643 * and no memory will have been deallocated.
644 *
645 */
646static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000647build_node_children(PyObject *tuple, node *root, int *line_num)
Guido van Rossum47478871996-08-21 14:32:37 +0000648{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000649 Py_ssize_t len = PyObject_Size(tuple);
650 Py_ssize_t i;
651 int err;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000652
653 for (i = 1; i < len; ++i) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000654 /* elem must always be a sequence, however simple */
Fred Drakeff9ea482000-04-19 13:54:15 +0000655 PyObject* elem = PySequence_GetItem(tuple, i);
656 int ok = elem != NULL;
657 long type = 0;
658 char *strn = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000659
Fred Drakeff9ea482000-04-19 13:54:15 +0000660 if (ok)
661 ok = PySequence_Check(elem);
662 if (ok) {
663 PyObject *temp = PySequence_GetItem(elem, 0);
664 if (temp == NULL)
665 ok = 0;
666 else {
667 ok = PyInt_Check(temp);
668 if (ok)
669 type = PyInt_AS_LONG(temp);
670 Py_DECREF(temp);
671 }
672 }
673 if (!ok) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674 PyObject *err = Py_BuildValue("os", elem,
675 "Illegal node construct.");
676 PyErr_SetObject(parser_error, err);
677 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000678 Py_XDECREF(elem);
679 return (0);
680 }
681 if (ISTERMINAL(type)) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000682 Py_ssize_t len = PyObject_Size(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000683 PyObject *temp;
Guido van Rossum47478871996-08-21 14:32:37 +0000684
Fred Drake0ac9b072000-09-12 21:58:06 +0000685 if ((len != 2) && (len != 3)) {
Fred Drake661ea262000-10-24 19:57:45 +0000686 err_string("terminal nodes must have 2 or 3 entries");
Fred Drake0ac9b072000-09-12 21:58:06 +0000687 return 0;
688 }
689 temp = PySequence_GetItem(elem, 1);
690 if (temp == NULL)
691 return 0;
692 if (!PyString_Check(temp)) {
693 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000694 "second item in terminal node must be a string,"
695 " found %s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000696 Py_Type(temp)->tp_name);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000697 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000698 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000699 return 0;
700 }
701 if (len == 3) {
702 PyObject *o = PySequence_GetItem(elem, 2);
703 if (o != NULL) {
704 if (PyInt_Check(o))
705 *line_num = PyInt_AS_LONG(o);
706 else {
707 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +0000708 "third item in terminal node must be an"
709 " integer, found %s",
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000710 Py_Type(temp)->tp_name);
Fred Drake0ac9b072000-09-12 21:58:06 +0000711 Py_DECREF(o);
712 Py_DECREF(temp);
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000713 Py_DECREF(elem);
Fred Drake0ac9b072000-09-12 21:58:06 +0000714 return 0;
715 }
716 Py_DECREF(o);
Fred Drakeff9ea482000-04-19 13:54:15 +0000717 }
718 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000719 len = PyString_GET_SIZE(temp) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000720 strn = (char *)PyObject_MALLOC(len);
Fred Drake0ac9b072000-09-12 21:58:06 +0000721 if (strn != NULL)
722 (void) memcpy(strn, PyString_AS_STRING(temp), len);
723 Py_DECREF(temp);
Fred Drakeff9ea482000-04-19 13:54:15 +0000724 }
725 else if (!ISNONTERMINAL(type)) {
726 /*
727 * It has to be one or the other; this is an error.
728 * Throw an exception.
729 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000730 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
731 PyErr_SetObject(parser_error, err);
732 Py_XDECREF(err);
Fred Drakeff9ea482000-04-19 13:54:15 +0000733 Py_XDECREF(elem);
734 return (0);
735 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000736 err = PyNode_AddChild(root, type, strn, *line_num, 0);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000737 if (err == E_NOMEM) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000738 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000739 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000740 return (node *) PyErr_NoMemory();
741 }
742 if (err == E_OVERFLOW) {
Neal Norwitz2cde0eb2007-08-11 04:58:43 +0000743 Py_XDECREF(elem);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744 PyObject_FREE(strn);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000745 PyErr_SetString(PyExc_ValueError,
746 "unsupported number of child nodes");
747 return NULL;
748 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000749
Fred Drakeff9ea482000-04-19 13:54:15 +0000750 if (ISNONTERMINAL(type)) {
751 node* new_child = CHILD(root, i - 1);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000752
Fred Drakeff9ea482000-04-19 13:54:15 +0000753 if (new_child != build_node_children(elem, new_child, line_num)) {
754 Py_XDECREF(elem);
755 return (0);
756 }
757 }
758 else if (type == NEWLINE) { /* It's true: we increment the */
759 ++(*line_num); /* line number *after* the newline! */
760 }
761 Py_XDECREF(elem);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000762 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763 return root;
Fred Drakeff9ea482000-04-19 13:54:15 +0000764}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000765
766
767static node*
Fred Drakeff9ea482000-04-19 13:54:15 +0000768build_node_tree(PyObject *tuple)
Guido van Rossum47478871996-08-21 14:32:37 +0000769{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000770 node* res = 0;
Guido van Rossum47478871996-08-21 14:32:37 +0000771 PyObject *temp = PySequence_GetItem(tuple, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +0000772 long num = -1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000773
Guido van Rossum47478871996-08-21 14:32:37 +0000774 if (temp != NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +0000775 num = PyInt_AsLong(temp);
Guido van Rossum47478871996-08-21 14:32:37 +0000776 Py_XDECREF(temp);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000777 if (ISTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000778 /*
779 * The tuple is simple, but it doesn't start with a start symbol.
780 * Throw an exception now and be done with it.
781 */
Fred Drake0ac9b072000-09-12 21:58:06 +0000782 tuple = Py_BuildValue("os", tuple,
Fred Drakec2683dd2001-07-17 19:32:05 +0000783 "Illegal syntax-tree; cannot start with terminal symbol.");
Fred Drakeff9ea482000-04-19 13:54:15 +0000784 PyErr_SetObject(parser_error, tuple);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000785 Py_XDECREF(tuple);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000786 }
787 else if (ISNONTERMINAL(num)) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000788 /*
789 * Not efficient, but that can be handled later.
790 */
791 int line_num = 0;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000792 PyObject *encoding = NULL;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000793
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000794 if (num == encoding_decl) {
795 encoding = PySequence_GetItem(tuple, 2);
796 /* tuple isn't borrowed anymore here, need to DECREF */
797 tuple = PySequence_GetSlice(tuple, 0, 2);
798 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000799 res = PyNode_New(num);
Fred Drake8b55b2d2001-12-05 22:10:44 +0000800 if (res != NULL) {
801 if (res != build_node_children(tuple, res, &line_num)) {
802 PyNode_Free(res);
803 res = NULL;
804 }
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000805 if (res && encoding) {
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000806 Py_ssize_t len;
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000807 len = PyString_GET_SIZE(encoding) + 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808 res->n_str = (char *)PyObject_MALLOC(len);
Michael W. Hudsondf1252d2003-02-08 18:05:10 +0000809 if (res->n_str != NULL)
810 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
811 Py_DECREF(encoding);
812 Py_DECREF(tuple);
813 }
Fred Drakeff9ea482000-04-19 13:54:15 +0000814 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000815 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000816 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000817 /* The tuple is illegal -- if the number is neither TERMINAL nor
Fred Drake0ac9b072000-09-12 21:58:06 +0000818 * NONTERMINAL, we can't use it. Not sure the implementation
819 * allows this condition, but the API doesn't preclude it.
Fred Drakeff9ea482000-04-19 13:54:15 +0000820 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000821 PyObject *err = Py_BuildValue("os", tuple,
822 "Illegal component tuple.");
823 PyErr_SetObject(parser_error, err);
824 Py_XDECREF(err);
825 }
Guido van Rossum3d602e31996-07-21 02:33:56 +0000826
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000827 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000828}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000829
830
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000831/*
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000832 * Validation routines used within the validation section:
833 */
Jeremy Hylton938ace62002-07-17 16:30:39 +0000834static int validate_terminal(node *terminal, int type, char *string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000835
Fred Drakeff9ea482000-04-19 13:54:15 +0000836#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
837#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
838#define validate_colon(ch) validate_terminal(ch, COLON, ":")
839#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
840#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
841#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
842#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
843#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
844#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
845#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
846#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
847#define validate_star(ch) validate_terminal(ch, STAR, "*")
848#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
849#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
850#define validate_dot(ch) validate_terminal(ch, DOT, ".")
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000851#define validate_at(ch) validate_terminal(ch, AT, "@")
Fred Drakeff9ea482000-04-19 13:54:15 +0000852#define validate_name(ch, str) validate_terminal(ch, NAME, str)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000853
Fred Drake0ac9b072000-09-12 21:58:06 +0000854#define VALIDATER(n) static int validate_##n(node *tree)
855
Fred Drakeff9ea482000-04-19 13:54:15 +0000856VALIDATER(node); VALIDATER(small_stmt);
857VALIDATER(class); VALIDATER(node);
858VALIDATER(parameters); VALIDATER(suite);
859VALIDATER(testlist); VALIDATER(varargslist);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000860VALIDATER(vfpdef);
Fred Drakeff9ea482000-04-19 13:54:15 +0000861VALIDATER(stmt); VALIDATER(simple_stmt);
862VALIDATER(expr_stmt); VALIDATER(power);
Guido van Rossum452bf512007-02-09 05:32:43 +0000863VALIDATER(del_stmt);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000864VALIDATER(return_stmt); VALIDATER(raise_stmt);
865VALIDATER(import_stmt); VALIDATER(import_stmt);
866VALIDATER(import_name); VALIDATER(yield_stmt);
867VALIDATER(global_stmt); VALIDATER(assert_stmt);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000868VALIDATER(compound_stmt);
Fred Drakeff9ea482000-04-19 13:54:15 +0000869VALIDATER(while); VALIDATER(for);
870VALIDATER(try); VALIDATER(except_clause);
871VALIDATER(test); VALIDATER(and_test);
872VALIDATER(not_test); VALIDATER(comparison);
Guido van Rossum0368b722007-05-11 16:50:42 +0000873VALIDATER(comp_op);
874VALIDATER(star_expr); VALIDATER(expr);
Fred Drakeff9ea482000-04-19 13:54:15 +0000875VALIDATER(xor_expr); VALIDATER(and_expr);
876VALIDATER(shift_expr); VALIDATER(arith_expr);
877VALIDATER(term); VALIDATER(factor);
878VALIDATER(atom); VALIDATER(lambdef);
879VALIDATER(trailer); VALIDATER(subscript);
880VALIDATER(subscriptlist); VALIDATER(sliceop);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000881VALIDATER(exprlist); VALIDATER(dictorsetmaker);
Fred Drakeff9ea482000-04-19 13:54:15 +0000882VALIDATER(arglist); VALIDATER(argument);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000883VALIDATER(testlist1); VALIDATER(comp_for);
884VALIDATER(comp_iter); VALIDATER(comp_if);
885VALIDATER(testlist_comp); VALIDATER(yield_expr);
886VALIDATER(yield_or_testlist); VALIDATER(or_test);
887VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000888
Fred Drake0ac9b072000-09-12 21:58:06 +0000889#undef VALIDATER
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000890
Fred Drakeff9ea482000-04-19 13:54:15 +0000891#define is_even(n) (((n) & 1) == 0)
892#define is_odd(n) (((n) & 1) == 1)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000893
894
895static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000896validate_ntype(node *n, int t)
Guido van Rossum47478871996-08-21 14:32:37 +0000897{
Fred Drake0ac9b072000-09-12 21:58:06 +0000898 if (TYPE(n) != t) {
899 PyErr_Format(parser_error, "Expected node type %d, got %d.",
900 t, TYPE(n));
901 return 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000902 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000903 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000904}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000905
906
Fred Drakee7ab64e2000-04-25 04:14:46 +0000907/* Verifies that the number of child nodes is exactly 'num', raising
908 * an exception if it isn't. The exception message does not indicate
909 * the exact number of nodes, allowing this to be used to raise the
910 * "right" exception when the wrong number of nodes is present in a
911 * specific variant of a statement's syntax. This is commonly used
912 * in that fashion.
913 */
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000914static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000915validate_numnodes(node *n, int num, const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000916{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000917 if (NCH(n) != num) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000918 PyErr_Format(parser_error,
919 "Illegal number of children for %s node.", name);
920 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +0000921 }
Fred Drake0ac9b072000-09-12 21:58:06 +0000922 return 1;
Fred Drakeff9ea482000-04-19 13:54:15 +0000923}
Guido van Rossum3d602e31996-07-21 02:33:56 +0000924
925
926static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000927validate_terminal(node *terminal, int type, char *string)
Guido van Rossum47478871996-08-21 14:32:37 +0000928{
Guido van Rossum3d602e31996-07-21 02:33:56 +0000929 int res = (validate_ntype(terminal, type)
Fred Drakeff9ea482000-04-19 13:54:15 +0000930 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000931
932 if (!res && !PyErr_Occurred()) {
Fred Drake0ac9b072000-09-12 21:58:06 +0000933 PyErr_Format(parser_error,
934 "Illegal terminal: expected \"%s\"", string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000935 }
936 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000937}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000938
939
Guido van Rossum47478871996-08-21 14:32:37 +0000940/* X (',' X) [',']
941 */
942static int
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000943validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
Fred Drakeff9ea482000-04-19 13:54:15 +0000944 const char *const name)
Guido van Rossum47478871996-08-21 14:32:37 +0000945{
946 int nch = NCH(tree);
947 int res = (nch && validate_ntype(tree, ntype)
Fred Drakeff9ea482000-04-19 13:54:15 +0000948 && vfunc(CHILD(tree, 0)));
Guido van Rossum47478871996-08-21 14:32:37 +0000949
950 if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +0000951 (void) validate_numnodes(tree, 1, name);
Guido van Rossum47478871996-08-21 14:32:37 +0000952 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000953 if (is_even(nch))
954 res = validate_comma(CHILD(tree, --nch));
955 if (res && nch > 1) {
956 int pos = 1;
957 for ( ; res && pos < nch; pos += 2)
958 res = (validate_comma(CHILD(tree, pos))
959 && vfunc(CHILD(tree, pos + 1)));
960 }
Guido van Rossum47478871996-08-21 14:32:37 +0000961 }
962 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +0000963}
Guido van Rossum47478871996-08-21 14:32:37 +0000964
965
Fred Drakecff283c2000-08-21 22:24:43 +0000966/* validate_class()
Guido van Rossum3d602e31996-07-21 02:33:56 +0000967 *
968 * classdef:
Fred Drakeff9ea482000-04-19 13:54:15 +0000969 * 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +0000970 */
Guido van Rossum47478871996-08-21 14:32:37 +0000971static int
Fred Drakeff9ea482000-04-19 13:54:15 +0000972validate_class(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +0000973{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000974 int nch = NCH(tree);
Brett Cannonf4189912005-04-09 02:30:16 +0000975 int res = (validate_ntype(tree, classdef) &&
976 ((nch == 4) || (nch == 6) || (nch == 7)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000977
Guido van Rossum3d602e31996-07-21 02:33:56 +0000978 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +0000979 res = (validate_name(CHILD(tree, 0), "class")
980 && validate_ntype(CHILD(tree, 1), NAME)
981 && validate_colon(CHILD(tree, nch - 2))
982 && validate_suite(CHILD(tree, nch - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +0000983 }
Brett Cannonf4189912005-04-09 02:30:16 +0000984 else {
Fred Drakeff9ea482000-04-19 13:54:15 +0000985 (void) validate_numnodes(tree, 4, "class");
Brett Cannonf4189912005-04-09 02:30:16 +0000986 }
987
988 if (res) {
989 if (nch == 7) {
990 res = ((validate_lparen(CHILD(tree, 2)) &&
991 validate_testlist(CHILD(tree, 3)) &&
992 validate_rparen(CHILD(tree, 4))));
993 }
994 else if (nch == 6) {
995 res = (validate_lparen(CHILD(tree,2)) &&
996 validate_rparen(CHILD(tree,3)));
997 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +0000998 }
999 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001000}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001001
1002
Guido van Rossum3d602e31996-07-21 02:33:56 +00001003/* if_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001004 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001005 */
Guido van Rossum47478871996-08-21 14:32:37 +00001006static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001007validate_if(node *tree)
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001008{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001009 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001010 int res = (validate_ntype(tree, if_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001011 && (nch >= 4)
1012 && validate_name(CHILD(tree, 0), "if")
1013 && validate_test(CHILD(tree, 1))
1014 && validate_colon(CHILD(tree, 2))
1015 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001016
1017 if (res && ((nch % 4) == 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001018 /* ... 'else' ':' suite */
1019 res = (validate_name(CHILD(tree, nch - 3), "else")
1020 && validate_colon(CHILD(tree, nch - 2))
1021 && validate_suite(CHILD(tree, nch - 1)));
1022 nch -= 3;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001023 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001024 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00001025 (void) validate_numnodes(tree, 4, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001026 if ((nch % 4) != 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00001027 /* Will catch the case for nch < 4 */
1028 res = validate_numnodes(tree, 0, "if");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001029 else if (res && (nch > 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001030 /* ... ('elif' test ':' suite)+ ... */
1031 int j = 4;
1032 while ((j < nch) && res) {
1033 res = (validate_name(CHILD(tree, j), "elif")
1034 && validate_colon(CHILD(tree, j + 2))
1035 && validate_test(CHILD(tree, j + 1))
1036 && validate_suite(CHILD(tree, j + 3)));
1037 j += 4;
1038 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001039 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001040 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001041}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001042
1043
Guido van Rossum3d602e31996-07-21 02:33:56 +00001044/* parameters:
Fred Drakeff9ea482000-04-19 13:54:15 +00001045 * '(' [varargslist] ')'
Guido van Rossum3d602e31996-07-21 02:33:56 +00001046 *
1047 */
Guido van Rossum47478871996-08-21 14:32:37 +00001048static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001049validate_parameters(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001050{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001051 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001052 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001053
Guido van Rossum3d602e31996-07-21 02:33:56 +00001054 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001055 res = (validate_lparen(CHILD(tree, 0))
1056 && validate_rparen(CHILD(tree, nch - 1)));
1057 if (res && (nch == 3))
1058 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001059 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001060 else {
1061 (void) validate_numnodes(tree, 2, "parameters");
1062 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001063 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001064}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001065
1066
Fred Drakecff283c2000-08-21 22:24:43 +00001067/* validate_suite()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001068 *
1069 * suite:
Fred Drakeff9ea482000-04-19 13:54:15 +00001070 * simple_stmt
Guido van Rossum3d602e31996-07-21 02:33:56 +00001071 * | NEWLINE INDENT stmt+ DEDENT
1072 */
Guido van Rossum47478871996-08-21 14:32:37 +00001073static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001074validate_suite(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001075{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001076 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001077 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001078
Guido van Rossum3d602e31996-07-21 02:33:56 +00001079 if (res && (nch == 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001080 res = validate_simple_stmt(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001081 else if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001082 /* NEWLINE INDENT stmt+ DEDENT */
1083 res = (validate_newline(CHILD(tree, 0))
1084 && validate_indent(CHILD(tree, 1))
1085 && validate_stmt(CHILD(tree, 2))
1086 && validate_dedent(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001087
Fred Drakeff9ea482000-04-19 13:54:15 +00001088 if (res && (nch > 4)) {
1089 int i = 3;
1090 --nch; /* forget the DEDENT */
1091 for ( ; res && (i < nch); ++i)
1092 res = validate_stmt(CHILD(tree, i));
1093 }
1094 else if (nch < 4)
1095 res = validate_numnodes(tree, 4, "suite");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001096 }
1097 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001098}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001099
1100
Guido van Rossum47478871996-08-21 14:32:37 +00001101static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001102validate_testlist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001103{
Guido van Rossum47478871996-08-21 14:32:37 +00001104 return (validate_repeating_list(tree, testlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00001105 validate_test, "testlist"));
1106}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001107
1108
Guido van Rossum1c917072001-10-15 15:44:05 +00001109static int
Guido van Rossum2d3b9862002-05-24 15:47:06 +00001110validate_testlist1(node *tree)
1111{
1112 return (validate_repeating_list(tree, testlist1,
1113 validate_test, "testlist1"));
1114}
1115
1116
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001117/* validate either vfpdef or tfpdef.
1118 * vfpdef: NAME
1119 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001120 */
1121static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001122validate_vfpdef(node *tree)
Neal Norwitzc1505362006-12-28 06:47:50 +00001123{
1124 int nch = NCH(tree);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001125 if (TYPE(tree) == vfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001126 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1127 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001128 else if (TYPE(tree) == tfpdef) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001129 if (nch == 1) {
1130 return validate_name(CHILD(tree, 0), NULL);
1131 }
1132 else if (nch == 3) {
1133 return validate_name(CHILD(tree, 0), NULL) &&
1134 validate_colon(CHILD(tree, 1)) &&
1135 validate_test(CHILD(tree, 2));
1136 }
1137 }
1138 return 0;
1139}
1140
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001141/* '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
1142 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
Fred Drakecff283c2000-08-21 22:24:43 +00001143 */
1144static int
1145validate_varargslist_trailer(node *tree, int start)
1146{
1147 int nch = NCH(tree);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001148 int res = 0, i;
Fred Drakecff283c2000-08-21 22:24:43 +00001149 int sym;
1150
1151 if (nch <= start) {
1152 err_string("expected variable argument trailer for varargslist");
1153 return 0;
1154 }
1155 sym = TYPE(CHILD(tree, start));
1156 if (sym == STAR) {
1157 /*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001158 * '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
Fred Drakecff283c2000-08-21 22:24:43 +00001159 */
1160 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001161 res = validate_vfpdef(CHILD(tree, start+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001162 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001163 res = (validate_vfpdef(CHILD(tree, start+1))
Fred Drakecff283c2000-08-21 22:24:43 +00001164 && validate_comma(CHILD(tree, start+2))
1165 && validate_doublestar(CHILD(tree, start+3))
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001166 && validate_vfpdef(CHILD(tree, start+4)));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001167 else {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001168 /* skip over vfpdef (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001169 i = start + 1;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001170 if (TYPE(CHILD(tree, i)) == vfpdef ||
1171 TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001172 i += 1;
1173 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001174 while (res && i+1 < nch) { /* validate (',' vfpdef ['=' test])* */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001175 res = validate_comma(CHILD(tree, i));
1176 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
1177 break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001178 res = res && validate_vfpdef(CHILD(tree, i+1));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001179 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
1180 res = res && (i+3 < nch)
1181 && validate_test(CHILD(tree, i+3));
1182 i += 4;
1183 }
1184 else {
1185 i += 2;
1186 }
1187 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001188 /* [',' '**' vfpdef] */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001189 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001190 res = validate_vfpdef(CHILD(tree, i+2));
Guido van Rossum4f72a782006-10-27 23:31:49 +00001191 }
1192 }
Fred Drakecff283c2000-08-21 22:24:43 +00001193 }
1194 else if (sym == DOUBLESTAR) {
1195 /*
1196 * '**' NAME
1197 */
1198 if (nch-start == 2)
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001199 res = validate_vfpdef(CHILD(tree, start+1));
Fred Drakecff283c2000-08-21 22:24:43 +00001200 }
1201 if (!res)
1202 err_string("illegal variable argument trailer for varargslist");
1203 return res;
1204}
1205
1206
Neal Norwitzc1505362006-12-28 06:47:50 +00001207/* validate_varargslist()
Guido van Rossum3d602e31996-07-21 02:33:56 +00001208 *
Neal Norwitzc1505362006-12-28 06:47:50 +00001209 * Validate typedargslist or varargslist.
1210 *
1211 * typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001212 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1213 * '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001214 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001215 * tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +00001216 * varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001217 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
1218 * '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001220 * vfpdef: NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00001221 *
1222 */
Guido van Rossum47478871996-08-21 14:32:37 +00001223static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001224validate_varargslist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001225{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001226 int nch = NCH(tree);
Neal Norwitzc1505362006-12-28 06:47:50 +00001227 int res = (TYPE(tree) == varargslist ||
1228 TYPE(tree) == typedargslist) &&
1229 (nch != 0);
Fred Drakecff283c2000-08-21 22:24:43 +00001230 int sym;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001231 node *ch;
1232 int i = 0;
1233
Fred Drakeb6429a22000-12-11 22:08:27 +00001234 if (!res)
1235 return 0;
Fred Drakecff283c2000-08-21 22:24:43 +00001236 if (nch < 1) {
1237 err_string("varargslist missing child nodes");
1238 return 0;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001239 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001240 while (i < nch) {
1241 ch = CHILD(tree, i);
1242 sym = TYPE(ch);
1243 if (sym == vfpdef || sym == tfpdef) {
1244 /* validate (vfpdef ['=' test] ',')+ */
1245 res = validate_vfpdef(ch);
Fred Drakecff283c2000-08-21 22:24:43 +00001246 ++i;
Fred Drakeb6429a22000-12-11 22:08:27 +00001247 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1248 res = (validate_equal(CHILD(tree, i))
1249 && validate_test(CHILD(tree, i+1)));
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001250 if (res)
1251 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00001252 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001253 if (res && i < nch) {
1254 res = validate_comma(CHILD(tree, i));
1255 ++i;
Fred Drakecff283c2000-08-21 22:24:43 +00001256 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001257 } else if (sym == DOUBLESTAR || sym == STAR) {
1258 res = validate_varargslist_trailer(tree, i);
1259 break;
1260 } else {
1261 res = 0;
1262 err_string("illegal formation for varargslist");
Fred Drakeff9ea482000-04-19 13:54:15 +00001263 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001264 }
Fred Drakecff283c2000-08-21 22:24:43 +00001265 return res;
Fred Drakeff9ea482000-04-19 13:54:15 +00001266}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001267
1268
Nick Coghlan650f0d02007-04-15 12:05:43 +00001269/* comp_iter: comp_for | comp_if
Fred Drakecff283c2000-08-21 22:24:43 +00001270 */
1271static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001272validate_comp_iter(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001273{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001274 int res = (validate_ntype(tree, comp_iter)
1275 && validate_numnodes(tree, 1, "comp_iter"));
1276 if (res && TYPE(CHILD(tree, 0)) == comp_for)
1277 res = validate_comp_for(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001278 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001279 res = validate_comp_if(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001280
1281 return res;
1282}
1283
Nick Coghlan650f0d02007-04-15 12:05:43 +00001284/* comp_for: 'for' exprlist 'in' test [comp_iter]
Raymond Hettinger354433a2004-05-19 08:20:33 +00001285 */
1286static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001287validate_comp_for(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001288{
1289 int nch = NCH(tree);
1290 int res;
1291
1292 if (nch == 5)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001293 res = validate_comp_iter(CHILD(tree, 4));
Fred Drakecff283c2000-08-21 22:24:43 +00001294 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001295 res = validate_numnodes(tree, 4, "comp_for");
Raymond Hettinger354433a2004-05-19 08:20:33 +00001296
1297 if (res)
1298 res = (validate_name(CHILD(tree, 0), "for")
1299 && validate_exprlist(CHILD(tree, 1))
1300 && validate_name(CHILD(tree, 2), "in")
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001301 && validate_or_test(CHILD(tree, 3)));
Raymond Hettinger354433a2004-05-19 08:20:33 +00001302
1303 return res;
1304}
1305
Nick Coghlan650f0d02007-04-15 12:05:43 +00001306/* comp_if: 'if' test_nocond [comp_iter]
Fred Drakecff283c2000-08-21 22:24:43 +00001307 */
1308static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001309validate_comp_if(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00001310{
1311 int nch = NCH(tree);
1312 int res;
1313
1314 if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001315 res = validate_comp_iter(CHILD(tree, 2));
Fred Drakecff283c2000-08-21 22:24:43 +00001316 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00001317 res = validate_numnodes(tree, 2, "comp_if");
Fred Drakecff283c2000-08-21 22:24:43 +00001318
1319 if (res)
1320 res = (validate_name(CHILD(tree, 0), "if")
Nick Coghlan650f0d02007-04-15 12:05:43 +00001321 && validate_test_nocond(CHILD(tree, 1)));
Fred Drakecff283c2000-08-21 22:24:43 +00001322
1323 return res;
1324}
1325
1326
Guido van Rossum3d602e31996-07-21 02:33:56 +00001327/* simple_stmt | compound_stmt
1328 *
1329 */
Guido van Rossum47478871996-08-21 14:32:37 +00001330static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001331validate_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001332{
1333 int res = (validate_ntype(tree, stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001334 && validate_numnodes(tree, 1, "stmt"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001335
Guido van Rossum3d602e31996-07-21 02:33:56 +00001336 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001337 tree = CHILD(tree, 0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001338
Fred Drakeff9ea482000-04-19 13:54:15 +00001339 if (TYPE(tree) == simple_stmt)
1340 res = validate_simple_stmt(tree);
1341 else
1342 res = validate_compound_stmt(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001343 }
1344 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001345}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001346
1347
Guido van Rossum3d602e31996-07-21 02:33:56 +00001348/* small_stmt (';' small_stmt)* [';'] NEWLINE
1349 *
1350 */
Guido van Rossum47478871996-08-21 14:32:37 +00001351static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001352validate_simple_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001353{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001354 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001355 int res = (validate_ntype(tree, simple_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001356 && (nch >= 2)
1357 && validate_small_stmt(CHILD(tree, 0))
1358 && validate_newline(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001359
Guido van Rossum3d602e31996-07-21 02:33:56 +00001360 if (nch < 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001361 res = validate_numnodes(tree, 2, "simple_stmt");
1362 --nch; /* forget the NEWLINE */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001363 if (res && is_even(nch))
Fred Drakeff9ea482000-04-19 13:54:15 +00001364 res = validate_semi(CHILD(tree, --nch));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001365 if (res && (nch > 2)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001366 int i;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001367
Fred Drakeff9ea482000-04-19 13:54:15 +00001368 for (i = 1; res && (i < nch); i += 2)
1369 res = (validate_semi(CHILD(tree, i))
1370 && validate_small_stmt(CHILD(tree, i + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001371 }
1372 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001373}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001374
1375
Guido van Rossum47478871996-08-21 14:32:37 +00001376static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001377validate_small_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001378{
1379 int nch = NCH(tree);
Fred Drake0ac9b072000-09-12 21:58:06 +00001380 int res = validate_numnodes(tree, 1, "small_stmt");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001381
Fred Drake0ac9b072000-09-12 21:58:06 +00001382 if (res) {
1383 int ntype = TYPE(CHILD(tree, 0));
1384
1385 if ( (ntype == expr_stmt)
Fred Drake0ac9b072000-09-12 21:58:06 +00001386 || (ntype == del_stmt)
1387 || (ntype == pass_stmt)
1388 || (ntype == flow_stmt)
1389 || (ntype == import_stmt)
1390 || (ntype == global_stmt)
Georg Brandl7cae87c2006-09-06 06:51:57 +00001391 || (ntype == assert_stmt))
Fred Drake0ac9b072000-09-12 21:58:06 +00001392 res = validate_node(CHILD(tree, 0));
1393 else {
1394 res = 0;
1395 err_string("illegal small_stmt child type");
1396 }
1397 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001398 else if (nch == 1) {
Fred Drake0ac9b072000-09-12 21:58:06 +00001399 res = 0;
1400 PyErr_Format(parser_error,
1401 "Unrecognized child node of small_stmt: %d.",
1402 TYPE(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001403 }
1404 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001405}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001406
1407
1408/* compound_stmt:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001409 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated
Guido van Rossum3d602e31996-07-21 02:33:56 +00001410 */
Guido van Rossum47478871996-08-21 14:32:37 +00001411static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001412validate_compound_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001413{
1414 int res = (validate_ntype(tree, compound_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001415 && validate_numnodes(tree, 1, "compound_stmt"));
Fred Drake0ac9b072000-09-12 21:58:06 +00001416 int ntype;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001417
1418 if (!res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001419 return (0);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001420
1421 tree = CHILD(tree, 0);
Fred Drake0ac9b072000-09-12 21:58:06 +00001422 ntype = TYPE(tree);
1423 if ( (ntype == if_stmt)
1424 || (ntype == while_stmt)
1425 || (ntype == for_stmt)
1426 || (ntype == try_stmt)
1427 || (ntype == funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001428 || (ntype == classdef)
1429 || (ntype == decorated))
Fred Drakeff9ea482000-04-19 13:54:15 +00001430 res = validate_node(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001431 else {
Fred Drake0ac9b072000-09-12 21:58:06 +00001432 res = 0;
1433 PyErr_Format(parser_error,
1434 "Illegal compound statement type: %d.", TYPE(tree));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001435 }
1436 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001437}
Guido van Rossum3d602e31996-07-21 02:33:56 +00001438
Guido van Rossum47478871996-08-21 14:32:37 +00001439static int
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001440validate_yield_or_testlist(node *tree)
1441{
1442 if (TYPE(tree) == yield_expr)
1443 return validate_yield_expr(tree);
1444 else
1445 return validate_testlist(tree);
1446}
1447
1448static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001449validate_expr_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001450{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001451 int j;
1452 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001453 int res = (validate_ntype(tree, expr_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001454 && is_odd(nch)
1455 && validate_testlist(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001456
Fred Drake28f739a2000-08-25 22:42:40 +00001457 if (res && nch == 3
1458 && TYPE(CHILD(tree, 1)) == augassign) {
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001459 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1460 && validate_yield_or_testlist(CHILD(tree, 2));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001461
Fred Drake28f739a2000-08-25 22:42:40 +00001462 if (res) {
1463 char *s = STR(CHILD(CHILD(tree, 1), 0));
1464
1465 res = (strcmp(s, "+=") == 0
1466 || strcmp(s, "-=") == 0
1467 || strcmp(s, "*=") == 0
1468 || strcmp(s, "/=") == 0
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00001469 || strcmp(s, "//=") == 0
Fred Drake28f739a2000-08-25 22:42:40 +00001470 || strcmp(s, "%=") == 0
1471 || strcmp(s, "&=") == 0
1472 || strcmp(s, "|=") == 0
1473 || strcmp(s, "^=") == 0
1474 || strcmp(s, "<<=") == 0
1475 || strcmp(s, ">>=") == 0
1476 || strcmp(s, "**=") == 0);
1477 if (!res)
1478 err_string("illegal augmmented assignment operator");
1479 }
1480 }
1481 else {
1482 for (j = 1; res && (j < nch); j += 2)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001483 res = validate_equal(CHILD(tree, j))
1484 && validate_yield_or_testlist(CHILD(tree, j + 1));
Fred Drake28f739a2000-08-25 22:42:40 +00001485 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001486 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001487}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001488
1489
Guido van Rossum47478871996-08-21 14:32:37 +00001490static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001491validate_del_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001492{
1493 return (validate_numnodes(tree, 2, "del_stmt")
Fred Drakeff9ea482000-04-19 13:54:15 +00001494 && validate_name(CHILD(tree, 0), "del")
1495 && validate_exprlist(CHILD(tree, 1)));
1496}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001497
1498
Guido van Rossum47478871996-08-21 14:32:37 +00001499static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001500validate_return_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001501{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001502 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001503 int res = (validate_ntype(tree, return_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001504 && ((nch == 1) || (nch == 2))
1505 && validate_name(CHILD(tree, 0), "return"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001506
Guido van Rossum3d602e31996-07-21 02:33:56 +00001507 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001508 res = validate_testlist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001509
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001510 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001511}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001512
1513
Guido van Rossum47478871996-08-21 14:32:37 +00001514static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001515validate_raise_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001516{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001517 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001518 int res = (validate_ntype(tree, raise_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001519 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001520
Guido van Rossum3d602e31996-07-21 02:33:56 +00001521 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001522 res = validate_name(CHILD(tree, 0), "raise");
1523 if (res && (nch >= 2))
1524 res = validate_test(CHILD(tree, 1));
1525 if (res && nch > 2) {
1526 res = (validate_comma(CHILD(tree, 2))
1527 && validate_test(CHILD(tree, 3)));
1528 if (res && (nch > 4))
1529 res = (validate_comma(CHILD(tree, 4))
1530 && validate_test(CHILD(tree, 5)));
1531 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001532 }
Guido van Rossum3d602e31996-07-21 02:33:56 +00001533 else
Fred Drakeff9ea482000-04-19 13:54:15 +00001534 (void) validate_numnodes(tree, 2, "raise");
Guido van Rossum3d602e31996-07-21 02:33:56 +00001535 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00001536 res = (validate_comma(CHILD(tree, 2))
1537 && validate_test(CHILD(tree, 3)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001538
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001539 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001540}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001541
1542
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001543/* yield_expr: 'yield' [testlist]
1544 */
1545static int
1546validate_yield_expr(node *tree)
1547{
1548 int nch = NCH(tree);
1549 int res = (validate_ntype(tree, yield_expr)
1550 && ((nch == 1) || (nch == 2))
1551 && validate_name(CHILD(tree, 0), "yield"));
1552
1553 if (res && (nch == 2))
1554 res = validate_testlist(CHILD(tree, 1));
1555
1556 return (res);
1557}
1558
1559
1560/* yield_stmt: yield_expr
Fred Drake02126f22001-07-17 02:59:15 +00001561 */
1562static int
1563validate_yield_stmt(node *tree)
1564{
1565 return (validate_ntype(tree, yield_stmt)
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001566 && validate_numnodes(tree, 1, "yield_stmt")
1567 && validate_yield_expr(CHILD(tree, 0)));
Fred Drake02126f22001-07-17 02:59:15 +00001568}
1569
1570
Fred Drakecff283c2000-08-21 22:24:43 +00001571static int
1572validate_import_as_name(node *tree)
1573{
1574 int nch = NCH(tree);
1575 int ok = validate_ntype(tree, import_as_name);
1576
1577 if (ok) {
1578 if (nch == 1)
1579 ok = validate_name(CHILD(tree, 0), NULL);
1580 else if (nch == 3)
1581 ok = (validate_name(CHILD(tree, 0), NULL)
1582 && validate_name(CHILD(tree, 1), "as")
1583 && validate_name(CHILD(tree, 2), NULL));
1584 else
1585 ok = validate_numnodes(tree, 3, "import_as_name");
1586 }
1587 return ok;
1588}
1589
1590
Fred Drake71137082001-01-07 05:59:59 +00001591/* dotted_name: NAME ("." NAME)*
1592 */
1593static int
1594validate_dotted_name(node *tree)
1595{
1596 int nch = NCH(tree);
1597 int res = (validate_ntype(tree, dotted_name)
1598 && is_odd(nch)
1599 && validate_name(CHILD(tree, 0), NULL));
1600 int i;
1601
1602 for (i = 1; res && (i < nch); i += 2) {
1603 res = (validate_dot(CHILD(tree, i))
1604 && validate_name(CHILD(tree, i+1), NULL));
1605 }
1606 return res;
1607}
1608
1609
Fred Drakecff283c2000-08-21 22:24:43 +00001610/* dotted_as_name: dotted_name [NAME NAME]
1611 */
1612static int
1613validate_dotted_as_name(node *tree)
1614{
1615 int nch = NCH(tree);
1616 int res = validate_ntype(tree, dotted_as_name);
1617
1618 if (res) {
1619 if (nch == 1)
Fred Drake71137082001-01-07 05:59:59 +00001620 res = validate_dotted_name(CHILD(tree, 0));
Fred Drakecff283c2000-08-21 22:24:43 +00001621 else if (nch == 3)
Fred Drake71137082001-01-07 05:59:59 +00001622 res = (validate_dotted_name(CHILD(tree, 0))
Fred Drakecff283c2000-08-21 22:24:43 +00001623 && validate_name(CHILD(tree, 1), "as")
1624 && validate_name(CHILD(tree, 2), NULL));
1625 else {
1626 res = 0;
Fred Drake661ea262000-10-24 19:57:45 +00001627 err_string("illegal number of children for dotted_as_name");
Fred Drakecff283c2000-08-21 22:24:43 +00001628 }
1629 }
1630 return res;
1631}
1632
1633
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001634/* dotted_as_name (',' dotted_as_name)* */
1635static int
1636validate_dotted_as_names(node *tree)
1637{
1638 int nch = NCH(tree);
1639 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1640 int i;
1641
1642 for (i = 1; res && (i < nch); i += 2)
1643 res = (validate_comma(CHILD(tree, i))
1644 && validate_dotted_as_name(CHILD(tree, i + 1)));
1645 return (res);
1646}
1647
1648
1649/* import_as_name (',' import_as_name)* [','] */
1650static int
1651validate_import_as_names(node *tree)
1652{
1653 int nch = NCH(tree);
1654 int res = validate_import_as_name(CHILD(tree, 0));
1655 int i;
1656
1657 for (i = 1; res && (i + 1 < nch); i += 2)
1658 res = (validate_comma(CHILD(tree, i))
1659 && validate_import_as_name(CHILD(tree, i + 1)));
1660 return (res);
1661}
1662
1663
1664/* 'import' dotted_as_names */
1665static int
1666validate_import_name(node *tree)
1667{
1668 return (validate_ntype(tree, import_name)
1669 && validate_numnodes(tree, 2, "import_name")
1670 && validate_name(CHILD(tree, 0), "import")
1671 && validate_dotted_as_names(CHILD(tree, 1)));
1672}
1673
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001674/* Helper function to count the number of leading dots in
1675 * 'from ...module import name'
1676 */
1677static int
1678count_from_dots(node *tree)
1679{
1680 int i;
1681 for (i = 0; i < NCH(tree); i++)
1682 if (TYPE(CHILD(tree, i)) != DOT)
1683 break;
1684 return i;
1685}
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001686
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001687/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001688 * import_as_names
Guido van Rossum3d602e31996-07-21 02:33:56 +00001689 */
Guido van Rossum47478871996-08-21 14:32:37 +00001690static int
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001691validate_import_from(node *tree)
1692{
1693 int nch = NCH(tree);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001694 int ndots = count_from_dots(tree);
1695 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1696 int offset = ndots + havename;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001697 int res = validate_ntype(tree, import_from)
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001698 && (nch >= 4 + ndots)
1699 && validate_name(CHILD(tree, 0), "from")
1700 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1701 && validate_name(CHILD(tree, offset + 1), "import");
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001702
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001703 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1704 res = ((nch == offset + 5)
1705 && validate_lparen(CHILD(tree, offset + 2))
1706 && validate_import_as_names(CHILD(tree, offset + 3))
1707 && validate_rparen(CHILD(tree, offset + 4)));
1708 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1709 res = validate_import_as_names(CHILD(tree, offset + 2));
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001710 return (res);
1711}
1712
1713
1714/* import_stmt: import_name | import_from */
1715static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001716validate_import_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001717{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001718 int nch = NCH(tree);
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001719 int res = validate_numnodes(tree, 1, "import_stmt");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001720
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001721 if (res) {
1722 int ntype = TYPE(CHILD(tree, 0));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001723
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001724 if (ntype == import_name || ntype == import_from)
1725 res = validate_node(CHILD(tree, 0));
Fred Drakeff9ea482000-04-19 13:54:15 +00001726 else {
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001727 res = 0;
1728 err_string("illegal import_stmt child type");
Fred Drakeff9ea482000-04-19 13:54:15 +00001729 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001730 }
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001731 else if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001732 res = 0;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001733 PyErr_Format(parser_error,
1734 "Unrecognized child node of import_stmt: %d.",
1735 TYPE(CHILD(tree, 0)));
1736 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001737 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001738}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001739
1740
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00001741
1742
Guido van Rossum47478871996-08-21 14:32:37 +00001743static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001744validate_global_stmt(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001745{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001746 int j;
1747 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001748 int res = (validate_ntype(tree, global_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001749 && is_even(nch) && (nch >= 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001750
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001751 if (!res && !PyErr_Occurred())
1752 err_string("illegal global statement");
1753
Guido van Rossum3d602e31996-07-21 02:33:56 +00001754 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001755 res = (validate_name(CHILD(tree, 0), "global")
1756 && validate_ntype(CHILD(tree, 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001757 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001758 res = (validate_comma(CHILD(tree, j))
1759 && validate_ntype(CHILD(tree, j + 1), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001760
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001761 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001762}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001763
1764
Guido van Rossum925e5471997-04-02 05:32:13 +00001765/* assert_stmt:
1766 *
1767 * 'assert' test [',' test]
1768 */
1769static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001770validate_assert_stmt(node *tree)
Guido van Rossum925e5471997-04-02 05:32:13 +00001771{
1772 int nch = NCH(tree);
1773 int res = (validate_ntype(tree, assert_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001774 && ((nch == 2) || (nch == 4))
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00001775 && (validate_name(CHILD(tree, 0), "assert"))
Fred Drakeff9ea482000-04-19 13:54:15 +00001776 && validate_test(CHILD(tree, 1)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001777
1778 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00001779 err_string("illegal assert statement");
Guido van Rossum925e5471997-04-02 05:32:13 +00001780 if (res && (nch > 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00001781 res = (validate_comma(CHILD(tree, 2))
1782 && validate_test(CHILD(tree, 3)));
Guido van Rossum925e5471997-04-02 05:32:13 +00001783
1784 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001785}
Guido van Rossum925e5471997-04-02 05:32:13 +00001786
1787
Guido van Rossum47478871996-08-21 14:32:37 +00001788static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001789validate_while(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001790{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001791 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001792 int res = (validate_ntype(tree, while_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001793 && ((nch == 4) || (nch == 7))
1794 && validate_name(CHILD(tree, 0), "while")
1795 && validate_test(CHILD(tree, 1))
1796 && validate_colon(CHILD(tree, 2))
1797 && validate_suite(CHILD(tree, 3)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001798
Guido van Rossum3d602e31996-07-21 02:33:56 +00001799 if (res && (nch == 7))
Fred Drakeff9ea482000-04-19 13:54:15 +00001800 res = (validate_name(CHILD(tree, 4), "else")
1801 && validate_colon(CHILD(tree, 5))
1802 && validate_suite(CHILD(tree, 6)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001803
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001804 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001805}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001806
1807
Guido van Rossum47478871996-08-21 14:32:37 +00001808static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001809validate_for(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001810{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001811 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001812 int res = (validate_ntype(tree, for_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001813 && ((nch == 6) || (nch == 9))
1814 && validate_name(CHILD(tree, 0), "for")
1815 && validate_exprlist(CHILD(tree, 1))
1816 && validate_name(CHILD(tree, 2), "in")
1817 && validate_testlist(CHILD(tree, 3))
1818 && validate_colon(CHILD(tree, 4))
1819 && validate_suite(CHILD(tree, 5)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001820
Guido van Rossum3d602e31996-07-21 02:33:56 +00001821 if (res && (nch == 9))
Fred Drakeff9ea482000-04-19 13:54:15 +00001822 res = (validate_name(CHILD(tree, 6), "else")
1823 && validate_colon(CHILD(tree, 7))
1824 && validate_suite(CHILD(tree, 8)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001825
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001826 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001827}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001828
1829
Guido van Rossum3d602e31996-07-21 02:33:56 +00001830/* try_stmt:
Fred Drakeff9ea482000-04-19 13:54:15 +00001831 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
Guido van Rossum3d602e31996-07-21 02:33:56 +00001832 * | 'try' ':' suite 'finally' ':' suite
1833 *
1834 */
Guido van Rossum47478871996-08-21 14:32:37 +00001835static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00001836validate_try(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001837{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001838 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001839 int pos = 3;
1840 int res = (validate_ntype(tree, try_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00001841 && (nch >= 6) && ((nch % 3) == 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001842
1843 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00001844 res = (validate_name(CHILD(tree, 0), "try")
1845 && validate_colon(CHILD(tree, 1))
1846 && validate_suite(CHILD(tree, 2))
1847 && validate_colon(CHILD(tree, nch - 2))
1848 && validate_suite(CHILD(tree, nch - 1)));
Fred Drake0ac9b072000-09-12 21:58:06 +00001849 else if (!PyErr_Occurred()) {
Fred Drake22269b52000-07-03 18:07:43 +00001850 const char* name = "except";
Fred Drakeff9ea482000-04-19 13:54:15 +00001851 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1852 name = STR(CHILD(tree, nch - 3));
Fred Drake0ac9b072000-09-12 21:58:06 +00001853
1854 PyErr_Format(parser_error,
1855 "Illegal number of children for try/%s node.", name);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001856 }
Fred Drakeff9ea482000-04-19 13:54:15 +00001857 /* Skip past except_clause sections: */
Guido van Rossum3d602e31996-07-21 02:33:56 +00001858 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001859 res = (validate_except_clause(CHILD(tree, pos))
1860 && validate_colon(CHILD(tree, pos + 1))
1861 && validate_suite(CHILD(tree, pos + 2)));
1862 pos += 3;
Guido van Rossum3d602e31996-07-21 02:33:56 +00001863 }
1864 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001865 res = validate_ntype(CHILD(tree, pos), NAME);
1866 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1867 res = (validate_numnodes(tree, 6, "try/finally")
1868 && validate_colon(CHILD(tree, 4))
1869 && validate_suite(CHILD(tree, 5)));
1870 else if (res) {
1871 if (nch == (pos + 3)) {
1872 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1873 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1874 if (!res)
Fred Drake661ea262000-10-24 19:57:45 +00001875 err_string("illegal trailing triple in try statement");
Fred Drakeff9ea482000-04-19 13:54:15 +00001876 }
1877 else if (nch == (pos + 6)) {
1878 res = (validate_name(CHILD(tree, pos), "except")
1879 && validate_colon(CHILD(tree, pos + 1))
1880 && validate_suite(CHILD(tree, pos + 2))
1881 && validate_name(CHILD(tree, pos + 3), "else"));
1882 }
1883 else
1884 res = validate_numnodes(tree, pos + 3, "try/except");
1885 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001886 }
1887 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001888}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001889
1890
Guido van Rossum47478871996-08-21 14:32:37 +00001891static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001892validate_except_clause(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001893{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001894 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001895 int res = (validate_ntype(tree, except_clause)
Fred Drakeff9ea482000-04-19 13:54:15 +00001896 && ((nch == 1) || (nch == 2) || (nch == 4))
1897 && validate_name(CHILD(tree, 0), "except"));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001898
Guido van Rossum3d602e31996-07-21 02:33:56 +00001899 if (res && (nch > 1))
Fred Drakeff9ea482000-04-19 13:54:15 +00001900 res = validate_test(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001901 if (res && (nch == 4))
Guido van Rossumb940e112007-01-10 16:19:56 +00001902 res = (validate_name(CHILD(tree, 2), "as")
1903 && validate_ntype(CHILD(tree, 3), NAME));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001904
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001905 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001906}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001907
1908
Guido van Rossum47478871996-08-21 14:32:37 +00001909static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001910validate_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001911{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001912 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001913 int res = validate_ntype(tree, test) && is_odd(nch);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001914
Guido van Rossum3d602e31996-07-21 02:33:56 +00001915 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
Fred Drakeff9ea482000-04-19 13:54:15 +00001916 res = ((nch == 1)
1917 && validate_lambdef(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001918 else if (res) {
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001919 res = validate_or_test(CHILD(tree, 0));
1920 res = (res && (nch == 1 || (nch == 5 &&
1921 validate_name(CHILD(tree, 1), "if") &&
1922 validate_or_test(CHILD(tree, 2)) &&
1923 validate_name(CHILD(tree, 3), "else") &&
1924 validate_test(CHILD(tree, 4)))));
1925 }
1926 return (res);
1927}
1928
1929static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001930validate_test_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001931{
1932 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001933 int res = validate_ntype(tree, test_nocond) && (nch == 1);
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001934
Nick Coghlan650f0d02007-04-15 12:05:43 +00001935 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
1936 res = (validate_lambdef_nocond(CHILD(tree, 0)));
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00001937 else if (res) {
1938 res = (validate_or_test(CHILD(tree, 0)));
1939 }
1940 return (res);
1941}
1942
1943static int
1944validate_or_test(node *tree)
1945{
1946 int nch = NCH(tree);
1947 int res = validate_ntype(tree, or_test) && is_odd(nch);
1948
1949 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001950 int pos;
1951 res = validate_and_test(CHILD(tree, 0));
1952 for (pos = 1; res && (pos < nch); pos += 2)
1953 res = (validate_name(CHILD(tree, pos), "or")
1954 && validate_and_test(CHILD(tree, pos + 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001955 }
1956 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001957}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001958
1959
Guido van Rossum47478871996-08-21 14:32:37 +00001960static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001961validate_and_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001962{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001963 int pos;
1964 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001965 int res = (validate_ntype(tree, and_test)
Fred Drakeff9ea482000-04-19 13:54:15 +00001966 && is_odd(nch)
1967 && validate_not_test(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001968
Guido van Rossum3d602e31996-07-21 02:33:56 +00001969 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00001970 res = (validate_name(CHILD(tree, pos), "and")
1971 && validate_not_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001972
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001973 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001974}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001975
1976
Guido van Rossum47478871996-08-21 14:32:37 +00001977static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001978validate_not_test(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001979{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001980 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001981 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001982
Guido van Rossum3d602e31996-07-21 02:33:56 +00001983 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00001984 if (nch == 2)
1985 res = (validate_name(CHILD(tree, 0), "not")
1986 && validate_not_test(CHILD(tree, 1)));
1987 else if (nch == 1)
1988 res = validate_comparison(CHILD(tree, 0));
Guido van Rossum3d602e31996-07-21 02:33:56 +00001989 }
1990 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00001991}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001992
1993
Guido van Rossum47478871996-08-21 14:32:37 +00001994static int
Fred Drakeff9ea482000-04-19 13:54:15 +00001995validate_comparison(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00001996{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00001997 int pos;
1998 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00001999 int res = (validate_ntype(tree, comparison)
Fred Drakeff9ea482000-04-19 13:54:15 +00002000 && is_odd(nch)
Guido van Rossum0368b722007-05-11 16:50:42 +00002001 && validate_star_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002002
Guido van Rossum3d602e31996-07-21 02:33:56 +00002003 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002004 res = (validate_comp_op(CHILD(tree, pos))
Guido van Rossum0368b722007-05-11 16:50:42 +00002005 && validate_star_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002006
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002007 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002008}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002009
2010
Guido van Rossum47478871996-08-21 14:32:37 +00002011static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002012validate_comp_op(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002013{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002014 int res = 0;
2015 int nch = NCH(tree);
2016
Guido van Rossum3d602e31996-07-21 02:33:56 +00002017 if (!validate_ntype(tree, comp_op))
Fred Drakeff9ea482000-04-19 13:54:15 +00002018 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002019 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002020 /*
2021 * Only child will be a terminal with a well-defined symbolic name
2022 * or a NAME with a string of either 'is' or 'in'
2023 */
2024 tree = CHILD(tree, 0);
2025 switch (TYPE(tree)) {
2026 case LESS:
2027 case GREATER:
2028 case EQEQUAL:
2029 case EQUAL:
2030 case LESSEQUAL:
2031 case GREATEREQUAL:
2032 case NOTEQUAL:
2033 res = 1;
2034 break;
2035 case NAME:
2036 res = ((strcmp(STR(tree), "in") == 0)
2037 || (strcmp(STR(tree), "is") == 0));
2038 if (!res) {
Fred Drake0ac9b072000-09-12 21:58:06 +00002039 PyErr_Format(parser_error,
Fred Drake661ea262000-10-24 19:57:45 +00002040 "illegal operator '%s'", STR(tree));
Fred Drakeff9ea482000-04-19 13:54:15 +00002041 }
2042 break;
2043 default:
Fred Drake661ea262000-10-24 19:57:45 +00002044 err_string("illegal comparison operator type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002045 break;
2046 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002047 }
Guido van Rossuma376cc51996-12-05 23:43:35 +00002048 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002049 res = (validate_ntype(CHILD(tree, 0), NAME)
2050 && validate_ntype(CHILD(tree, 1), NAME)
2051 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2052 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2053 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2054 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2055 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002056 err_string("unknown comparison operator");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002057 }
2058 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002059}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002060
2061
Guido van Rossum47478871996-08-21 14:32:37 +00002062static int
Guido van Rossum0368b722007-05-11 16:50:42 +00002063validate_star_expr(node *tree)
2064{
2065 int res = validate_ntype(tree, star_expr);
2066 if (!res) return res;
2067 if (NCH(tree) == 2) {
2068 return validate_ntype(CHILD(tree, 0), STAR) && \
2069 validate_expr(CHILD(tree, 1));
2070 } else {
2071 return validate_expr(CHILD(tree, 0));
2072 }
2073}
2074
2075
2076static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002077validate_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002078{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002079 int j;
2080 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002081 int res = (validate_ntype(tree, expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002082 && is_odd(nch)
2083 && validate_xor_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002084
Guido van Rossum3d602e31996-07-21 02:33:56 +00002085 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002086 res = (validate_xor_expr(CHILD(tree, j))
2087 && validate_vbar(CHILD(tree, j - 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002088
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002089 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002090}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002091
2092
Guido van Rossum47478871996-08-21 14:32:37 +00002093static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002094validate_xor_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002095{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002096 int j;
2097 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002098 int res = (validate_ntype(tree, xor_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002099 && is_odd(nch)
2100 && validate_and_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002101
Guido van Rossum3d602e31996-07-21 02:33:56 +00002102 for (j = 2; res && (j < nch); j += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002103 res = (validate_circumflex(CHILD(tree, j - 1))
2104 && validate_and_expr(CHILD(tree, j)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002105
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002106 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002107}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002108
2109
Guido van Rossum47478871996-08-21 14:32:37 +00002110static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002111validate_and_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002112{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002113 int pos;
2114 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002115 int res = (validate_ntype(tree, and_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002116 && is_odd(nch)
2117 && validate_shift_expr(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002118
Guido van Rossum3d602e31996-07-21 02:33:56 +00002119 for (pos = 1; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002120 res = (validate_ampersand(CHILD(tree, pos))
2121 && validate_shift_expr(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002122
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002123 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002124}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002125
2126
2127static int
Peter Schneider-Kamp286da3b2000-07-10 12:43:58 +00002128validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002129 {
2130 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002131 int nch = NCH(tree);
2132 int res = (is_odd(nch)
Fred Drakeff9ea482000-04-19 13:54:15 +00002133 && (*termvalid)(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002134
Guido van Rossum3d602e31996-07-21 02:33:56 +00002135 for ( ; res && (pos < nch); pos += 2) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002136 if (TYPE(CHILD(tree, pos)) != op1)
2137 res = validate_ntype(CHILD(tree, pos), op2);
2138 if (res)
2139 res = (*termvalid)(CHILD(tree, pos + 1));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002140 }
2141 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002142}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002143
2144
Guido van Rossum47478871996-08-21 14:32:37 +00002145static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002146validate_shift_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002147{
2148 return (validate_ntype(tree, shift_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002149 && validate_chain_two_ops(tree, validate_arith_expr,
2150 LEFTSHIFT, RIGHTSHIFT));
2151}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002152
2153
Guido van Rossum47478871996-08-21 14:32:37 +00002154static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002155validate_arith_expr(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002156{
2157 return (validate_ntype(tree, arith_expr)
Fred Drakeff9ea482000-04-19 13:54:15 +00002158 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2159}
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_term(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002164{
2165 int pos = 1;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002166 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002167 int res = (validate_ntype(tree, term)
Fred Drakeff9ea482000-04-19 13:54:15 +00002168 && is_odd(nch)
2169 && validate_factor(CHILD(tree, 0)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002170
Guido van Rossum3d602e31996-07-21 02:33:56 +00002171 for ( ; res && (pos < nch); pos += 2)
Fred Drakeff9ea482000-04-19 13:54:15 +00002172 res = (((TYPE(CHILD(tree, pos)) == STAR)
2173 || (TYPE(CHILD(tree, pos)) == SLASH)
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +00002174 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
Fred Drakeff9ea482000-04-19 13:54:15 +00002175 || (TYPE(CHILD(tree, pos)) == PERCENT))
2176 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002177
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002178 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002179}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002180
2181
Guido van Rossum3d602e31996-07-21 02:33:56 +00002182/* factor:
2183 *
2184 * factor: ('+'|'-'|'~') factor | power
2185 */
Guido van Rossum47478871996-08-21 14:32:37 +00002186static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002187validate_factor(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002188{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002189 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002190 int res = (validate_ntype(tree, factor)
Fred Drakeff9ea482000-04-19 13:54:15 +00002191 && (((nch == 2)
2192 && ((TYPE(CHILD(tree, 0)) == PLUS)
2193 || (TYPE(CHILD(tree, 0)) == MINUS)
2194 || (TYPE(CHILD(tree, 0)) == TILDE))
2195 && validate_factor(CHILD(tree, 1)))
2196 || ((nch == 1)
2197 && validate_power(CHILD(tree, 0)))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002198 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002199}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002200
2201
Guido van Rossum3d602e31996-07-21 02:33:56 +00002202/* power:
2203 *
2204 * power: atom trailer* ('**' factor)*
2205 */
Guido van Rossum47478871996-08-21 14:32:37 +00002206static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002207validate_power(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002208{
2209 int pos = 1;
2210 int nch = NCH(tree);
2211 int res = (validate_ntype(tree, power) && (nch >= 1)
Fred Drakeff9ea482000-04-19 13:54:15 +00002212 && validate_atom(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002213
2214 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
Fred Drakeff9ea482000-04-19 13:54:15 +00002215 res = validate_trailer(CHILD(tree, pos++));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002216 if (res && (pos < nch)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002217 if (!is_even(nch - pos)) {
Fred Drake661ea262000-10-24 19:57:45 +00002218 err_string("illegal number of nodes for 'power'");
Fred Drakeff9ea482000-04-19 13:54:15 +00002219 return (0);
2220 }
2221 for ( ; res && (pos < (nch - 1)); pos += 2)
2222 res = (validate_doublestar(CHILD(tree, pos))
2223 && validate_factor(CHILD(tree, pos + 1)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002224 }
2225 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002226}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002227
2228
Guido van Rossum47478871996-08-21 14:32:37 +00002229static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002230validate_atom(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002231{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002232 int pos;
2233 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002234 int res = validate_ntype(tree, atom);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002235
Fred Drakecff283c2000-08-21 22:24:43 +00002236 if (res && nch < 1)
2237 res = validate_numnodes(tree, nch+1, "atom");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002238 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002239 switch (TYPE(CHILD(tree, 0))) {
2240 case LPAR:
2241 res = ((nch <= 3)
2242 && (validate_rparen(CHILD(tree, nch - 1))));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002243
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002244 if (res && (nch == 3)) {
2245 if (TYPE(CHILD(tree, 1))==yield_expr)
2246 res = validate_yield_expr(CHILD(tree, 1));
2247 else
Nick Coghlan650f0d02007-04-15 12:05:43 +00002248 res = validate_testlist_comp(CHILD(tree, 1));
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002249 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002250 break;
2251 case LSQB:
Fred Drakecff283c2000-08-21 22:24:43 +00002252 if (nch == 2)
2253 res = validate_ntype(CHILD(tree, 1), RSQB);
2254 else if (nch == 3)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002255 res = (validate_testlist_comp(CHILD(tree, 1))
Fred Drakecff283c2000-08-21 22:24:43 +00002256 && validate_ntype(CHILD(tree, 2), RSQB));
2257 else {
2258 res = 0;
2259 err_string("illegal list display atom");
2260 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002261 break;
2262 case LBRACE:
2263 res = ((nch <= 3)
2264 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002265
Fred Drakeff9ea482000-04-19 13:54:15 +00002266 if (res && (nch == 3))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002267 res = validate_dictorsetmaker(CHILD(tree, 1));
Fred Drakeff9ea482000-04-19 13:54:15 +00002268 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002269 case NAME:
2270 case NUMBER:
2271 res = (nch == 1);
2272 break;
2273 case STRING:
2274 for (pos = 1; res && (pos < nch); ++pos)
2275 res = validate_ntype(CHILD(tree, pos), STRING);
2276 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002277 case DOT:
2278 res = (nch == 3 &&
2279 validate_ntype(CHILD(tree, 1), DOT) &&
2280 validate_ntype(CHILD(tree, 2), DOT));
2281 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002282 default:
2283 res = 0;
2284 break;
2285 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002286 }
2287 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002288}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002289
2290
Nick Coghlan650f0d02007-04-15 12:05:43 +00002291/* testlist_comp:
2292 * test ( comp_for | (',' test)* [','] )
Fred Drake85bf3bb2000-08-23 15:35:26 +00002293 */
Fred Drakecff283c2000-08-21 22:24:43 +00002294static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002295validate_testlist_comp(node *tree)
Fred Drakecff283c2000-08-21 22:24:43 +00002296{
2297 int nch = NCH(tree);
2298 int ok = nch;
2299
2300 if (nch == 0)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002301 err_string("missing child nodes of testlist_comp");
2302 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002303 ok = validate_test(CHILD(tree, 0));
Nick Coghlan650f0d02007-04-15 12:05:43 +00002304 }
Fred Drakecff283c2000-08-21 22:24:43 +00002305
2306 /*
Nick Coghlan650f0d02007-04-15 12:05:43 +00002307 * comp_for | (',' test)* [',']
Fred Drakecff283c2000-08-21 22:24:43 +00002308 */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002309 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2310 ok = validate_comp_for(CHILD(tree, 1));
Fred Drakecff283c2000-08-21 22:24:43 +00002311 else {
2312 /* (',' test)* [','] */
2313 int i = 1;
2314 while (ok && nch - i >= 2) {
2315 ok = (validate_comma(CHILD(tree, i))
2316 && validate_test(CHILD(tree, i+1)));
Fred Drake85bf3bb2000-08-23 15:35:26 +00002317 i += 2;
Fred Drakecff283c2000-08-21 22:24:43 +00002318 }
Fred Drake85bf3bb2000-08-23 15:35:26 +00002319 if (ok && i == nch-1)
2320 ok = validate_comma(CHILD(tree, i));
2321 else if (i != nch) {
2322 ok = 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002323 err_string("illegal trailing nodes for testlist_comp");
Raymond Hettinger354433a2004-05-19 08:20:33 +00002324 }
2325 }
2326 return ok;
2327}
Fred Drakecff283c2000-08-21 22:24:43 +00002328
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002329/* decorator:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002330 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002331 */
2332static int
2333validate_decorator(node *tree)
2334{
2335 int ok;
2336 int nch = NCH(tree);
2337 ok = (validate_ntype(tree, decorator) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002338 (nch == 3 || nch == 5 || nch == 6) &&
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002339 validate_at(CHILD(tree, 0)) &&
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002340 validate_dotted_name(CHILD(tree, 1)) &&
2341 validate_newline(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002342
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002343 if (ok && nch != 3) {
2344 ok = (validate_lparen(CHILD(tree, 2)) &&
2345 validate_rparen(RCHILD(tree, -2)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002346
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002347 if (ok && nch == 6)
2348 ok = validate_arglist(CHILD(tree, 3));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002349 }
2350
2351 return ok;
2352}
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002353
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002354/* decorators:
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002355 * decorator+
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002356 */
2357static int
2358validate_decorators(node *tree)
2359{
2360 int i, nch, ok;
2361 nch = NCH(tree);
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002362 ok = validate_ntype(tree, decorators) && nch >= 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002363
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002364 for (i = 0; ok && i < nch; ++i)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002365 ok = validate_decorator(CHILD(tree, i));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002366
2367 return ok;
Michael W. Hudson0ccff072004-08-17 17:29:16 +00002368}
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002369
Guido van Rossum3d602e31996-07-21 02:33:56 +00002370/* funcdef:
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002371 *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002372 * -5 -4 -3 -2 -1
2373 * 'def' NAME parameters ':' suite
Guido van Rossum3d602e31996-07-21 02:33:56 +00002374 */
Guido van Rossum47478871996-08-21 14:32:37 +00002375static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002376validate_funcdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002377{
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002378 int nch = NCH(tree);
2379 int ok = (validate_ntype(tree, funcdef)
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002380 && (nch == 5)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002381 && validate_name(RCHILD(tree, -5), "def")
2382 && validate_ntype(RCHILD(tree, -4), NAME)
2383 && validate_colon(RCHILD(tree, -2))
2384 && validate_parameters(RCHILD(tree, -3))
2385 && validate_suite(RCHILD(tree, -1)));
Anthony Baxterc2a5a632004-08-02 06:10:11 +00002386 return ok;
Fred Drakeff9ea482000-04-19 13:54:15 +00002387}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002388
2389
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002390/* decorated
2391 * decorators (classdef | funcdef)
2392 */
2393static int
2394validate_decorated(node *tree)
2395{
2396 int nch = NCH(tree);
2397 int ok = (validate_ntype(tree, decorated)
2398 && (nch == 2)
2399 && validate_decorators(RCHILD(tree, -2))
2400 && (validate_funcdef(RCHILD(tree, -1))
2401 || validate_class(RCHILD(tree, -1)))
2402 );
2403 return ok;
2404}
2405
Guido van Rossum47478871996-08-21 14:32:37 +00002406static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002407validate_lambdef(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002408{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002409 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002410 int res = (validate_ntype(tree, lambdef)
Fred Drakeff9ea482000-04-19 13:54:15 +00002411 && ((nch == 3) || (nch == 4))
2412 && validate_name(CHILD(tree, 0), "lambda")
2413 && validate_colon(CHILD(tree, nch - 2))
2414 && validate_test(CHILD(tree, nch - 1)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002415
Guido van Rossum3d602e31996-07-21 02:33:56 +00002416 if (res && (nch == 4))
Fred Drakeff9ea482000-04-19 13:54:15 +00002417 res = validate_varargslist(CHILD(tree, 1));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002418 else if (!res && !PyErr_Occurred())
Fred Drakeff9ea482000-04-19 13:54:15 +00002419 (void) validate_numnodes(tree, 3, "lambdef");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002420
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002421 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002422}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002423
2424
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002425static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002426validate_lambdef_nocond(node *tree)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002427{
2428 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002429 int res = (validate_ntype(tree, lambdef_nocond)
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002430 && ((nch == 3) || (nch == 4))
2431 && validate_name(CHILD(tree, 0), "lambda")
2432 && validate_colon(CHILD(tree, nch - 2))
2433 && validate_test(CHILD(tree, nch - 1)));
2434
2435 if (res && (nch == 4))
2436 res = validate_varargslist(CHILD(tree, 1));
2437 else if (!res && !PyErr_Occurred())
Nick Coghlan650f0d02007-04-15 12:05:43 +00002438 (void) validate_numnodes(tree, 3, "lambdef_nocond");
Thomas Wouterse2dd78c2006-02-27 16:25:11 +00002439
2440 return (res);
2441}
2442
2443
Guido van Rossum3d602e31996-07-21 02:33:56 +00002444/* arglist:
2445 *
Fred Drakecff283c2000-08-21 22:24:43 +00002446 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002447 */
Guido van Rossum47478871996-08-21 14:32:37 +00002448static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002449validate_arglist(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002450{
Fred Drakee7ab64e2000-04-25 04:14:46 +00002451 int nch = NCH(tree);
Fred Drakecff283c2000-08-21 22:24:43 +00002452 int i = 0;
2453 int ok = 1;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002454
2455 if (nch <= 0)
2456 /* raise the right error from having an invalid number of children */
2457 return validate_numnodes(tree, nch + 1, "arglist");
2458
Raymond Hettinger354433a2004-05-19 08:20:33 +00002459 if (nch > 1) {
2460 for (i=0; i<nch; i++) {
2461 if (TYPE(CHILD(tree, i)) == argument) {
2462 node *ch = CHILD(tree, i);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002463 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
Raymond Hettinger354433a2004-05-19 08:20:33 +00002464 err_string("need '(', ')' for generator expression");
2465 return 0;
2466 }
2467 }
2468 }
2469 }
2470
Fred Drakecff283c2000-08-21 22:24:43 +00002471 while (ok && nch-i >= 2) {
2472 /* skip leading (argument ',') */
2473 ok = (validate_argument(CHILD(tree, i))
2474 && validate_comma(CHILD(tree, i+1)));
2475 if (ok)
2476 i += 2;
2477 else
2478 PyErr_Clear();
2479 }
2480 ok = 1;
2481 if (nch-i > 0) {
2482 /*
2483 * argument | '*' test [',' '**' test] | '**' test
Fred Drakee7ab64e2000-04-25 04:14:46 +00002484 */
Fred Drakecff283c2000-08-21 22:24:43 +00002485 int sym = TYPE(CHILD(tree, i));
2486
2487 if (sym == argument) {
2488 ok = validate_argument(CHILD(tree, i));
2489 if (ok && i+1 != nch) {
2490 err_string("illegal arglist specification"
2491 " (extra stuff on end)");
2492 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002493 }
Fred Drakecff283c2000-08-21 22:24:43 +00002494 }
2495 else if (sym == STAR) {
2496 ok = validate_star(CHILD(tree, i));
2497 if (ok && (nch-i == 2))
2498 ok = validate_test(CHILD(tree, i+1));
2499 else if (ok && (nch-i == 5))
2500 ok = (validate_test(CHILD(tree, i+1))
2501 && validate_comma(CHILD(tree, i+2))
2502 && validate_doublestar(CHILD(tree, i+3))
2503 && validate_test(CHILD(tree, i+4)));
Fred Drakee7ab64e2000-04-25 04:14:46 +00002504 else {
Fred Drakecff283c2000-08-21 22:24:43 +00002505 err_string("illegal use of '*' in arglist");
2506 ok = 0;
Fred Drakee7ab64e2000-04-25 04:14:46 +00002507 }
2508 }
Fred Drakecff283c2000-08-21 22:24:43 +00002509 else if (sym == DOUBLESTAR) {
2510 if (nch-i == 2)
2511 ok = (validate_doublestar(CHILD(tree, i))
2512 && validate_test(CHILD(tree, i+1)));
2513 else {
2514 err_string("illegal use of '**' in arglist");
2515 ok = 0;
2516 }
2517 }
2518 else {
2519 err_string("illegal arglist specification");
2520 ok = 0;
2521 }
Fred Drakee7ab64e2000-04-25 04:14:46 +00002522 }
2523 return (ok);
Fred Drakeff9ea482000-04-19 13:54:15 +00002524}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002525
2526
2527
2528/* argument:
2529 *
Nick Coghlan650f0d02007-04-15 12:05:43 +00002530 * [test '='] test [comp_for]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002531 */
Guido van Rossum47478871996-08-21 14:32:37 +00002532static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002533validate_argument(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002534{
2535 int nch = NCH(tree);
2536 int res = (validate_ntype(tree, argument)
Raymond Hettinger354433a2004-05-19 08:20:33 +00002537 && ((nch == 1) || (nch == 2) || (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002538 && validate_test(CHILD(tree, 0)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002539
Raymond Hettinger354433a2004-05-19 08:20:33 +00002540 if (res && (nch == 2))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002541 res = validate_comp_for(CHILD(tree, 1));
Raymond Hettinger354433a2004-05-19 08:20:33 +00002542 else if (res && (nch == 3))
Fred Drakeff9ea482000-04-19 13:54:15 +00002543 res = (validate_equal(CHILD(tree, 1))
2544 && validate_test(CHILD(tree, 2)));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002545
2546 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002547}
Guido van Rossum3d602e31996-07-21 02:33:56 +00002548
2549
2550
2551/* trailer:
2552 *
Guido van Rossum47478871996-08-21 14:32:37 +00002553 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Guido van Rossum3d602e31996-07-21 02:33:56 +00002554 */
Guido van Rossum47478871996-08-21 14:32:37 +00002555static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002556validate_trailer(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002557{
2558 int nch = NCH(tree);
2559 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002560
2561 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002562 switch (TYPE(CHILD(tree, 0))) {
2563 case LPAR:
2564 res = validate_rparen(CHILD(tree, nch - 1));
2565 if (res && (nch == 3))
2566 res = validate_arglist(CHILD(tree, 1));
2567 break;
2568 case LSQB:
2569 res = (validate_numnodes(tree, 3, "trailer")
2570 && validate_subscriptlist(CHILD(tree, 1))
2571 && validate_ntype(CHILD(tree, 2), RSQB));
2572 break;
2573 case DOT:
2574 res = (validate_numnodes(tree, 2, "trailer")
2575 && validate_ntype(CHILD(tree, 1), NAME));
2576 break;
2577 default:
2578 res = 0;
2579 break;
2580 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002581 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002582 else {
2583 (void) validate_numnodes(tree, 2, "trailer");
2584 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002585 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002586}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002587
2588
Guido van Rossum47478871996-08-21 14:32:37 +00002589/* subscriptlist:
2590 *
2591 * subscript (',' subscript)* [',']
2592 */
2593static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002594validate_subscriptlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002595{
2596 return (validate_repeating_list(tree, subscriptlist,
Fred Drakeff9ea482000-04-19 13:54:15 +00002597 validate_subscript, "subscriptlist"));
2598}
Guido van Rossum47478871996-08-21 14:32:37 +00002599
2600
Guido van Rossum3d602e31996-07-21 02:33:56 +00002601/* subscript:
2602 *
Guido van Rossum47478871996-08-21 14:32:37 +00002603 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
Guido van Rossum3d602e31996-07-21 02:33:56 +00002604 */
Guido van Rossum47478871996-08-21 14:32:37 +00002605static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002606validate_subscript(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002607{
Guido van Rossum47478871996-08-21 14:32:37 +00002608 int offset = 0;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002609 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002610 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002611
Guido van Rossum47478871996-08-21 14:32:37 +00002612 if (!res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002613 if (!PyErr_Occurred())
2614 err_string("invalid number of arguments for subscript node");
2615 return (0);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002616 }
Guido van Rossum47478871996-08-21 14:32:37 +00002617 if (TYPE(CHILD(tree, 0)) == DOT)
Fred Drakeff9ea482000-04-19 13:54:15 +00002618 /* take care of ('.' '.' '.') possibility */
2619 return (validate_numnodes(tree, 3, "subscript")
2620 && validate_dot(CHILD(tree, 0))
2621 && validate_dot(CHILD(tree, 1))
2622 && validate_dot(CHILD(tree, 2)));
Guido van Rossum47478871996-08-21 14:32:37 +00002623 if (nch == 1) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002624 if (TYPE(CHILD(tree, 0)) == test)
2625 res = validate_test(CHILD(tree, 0));
2626 else
2627 res = validate_colon(CHILD(tree, 0));
2628 return (res);
Guido van Rossum47478871996-08-21 14:32:37 +00002629 }
Fred Drakeff9ea482000-04-19 13:54:15 +00002630 /* Must be [test] ':' [test] [sliceop],
2631 * but at least one of the optional components will
2632 * be present, but we don't know which yet.
Guido van Rossum47478871996-08-21 14:32:37 +00002633 */
2634 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002635 res = validate_test(CHILD(tree, 0));
2636 offset = 1;
Guido van Rossum47478871996-08-21 14:32:37 +00002637 }
2638 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002639 res = validate_colon(CHILD(tree, offset));
Guido van Rossum47478871996-08-21 14:32:37 +00002640 if (res) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002641 int rem = nch - ++offset;
2642 if (rem) {
2643 if (TYPE(CHILD(tree, offset)) == test) {
2644 res = validate_test(CHILD(tree, offset));
2645 ++offset;
2646 --rem;
2647 }
2648 if (res && rem)
2649 res = validate_sliceop(CHILD(tree, offset));
2650 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002651 }
2652 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002653}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002654
2655
Guido van Rossum47478871996-08-21 14:32:37 +00002656static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002657validate_sliceop(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002658{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002659 int nch = NCH(tree);
Guido van Rossum47478871996-08-21 14:32:37 +00002660 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
Fred Drakeff9ea482000-04-19 13:54:15 +00002661 && validate_ntype(tree, sliceop);
Guido van Rossum47478871996-08-21 14:32:37 +00002662 if (!res && !PyErr_Occurred()) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002663 res = validate_numnodes(tree, 1, "sliceop");
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002664 }
Guido van Rossum47478871996-08-21 14:32:37 +00002665 if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002666 res = validate_colon(CHILD(tree, 0));
Guido van Rossum47478871996-08-21 14:32:37 +00002667 if (res && (nch == 2))
Fred Drakeff9ea482000-04-19 13:54:15 +00002668 res = validate_test(CHILD(tree, 1));
Guido van Rossum47478871996-08-21 14:32:37 +00002669
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002670 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002671}
Guido van Rossum47478871996-08-21 14:32:37 +00002672
2673
2674static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002675validate_exprlist(node *tree)
Guido van Rossum47478871996-08-21 14:32:37 +00002676{
2677 return (validate_repeating_list(tree, exprlist,
Guido van Rossum0368b722007-05-11 16:50:42 +00002678 validate_star_expr, "exprlist"));
Fred Drakeff9ea482000-04-19 13:54:15 +00002679}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002680
2681
Guido van Rossum47478871996-08-21 14:32:37 +00002682static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002683validate_dictorsetmaker(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002684{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002685 int nch = NCH(tree);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002686 int res = (validate_ntype(tree, dictorsetmaker)
Fred Drakeff9ea482000-04-19 13:54:15 +00002687 && (nch >= 3)
2688 && validate_test(CHILD(tree, 0))
2689 && validate_colon(CHILD(tree, 1))
2690 && validate_test(CHILD(tree, 2)));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002691
Guido van Rossum3d602e31996-07-21 02:33:56 +00002692 if (res && ((nch % 4) == 0))
Fred Drakeff9ea482000-04-19 13:54:15 +00002693 res = validate_comma(CHILD(tree, --nch));
Guido van Rossum3d602e31996-07-21 02:33:56 +00002694 else if (res)
Fred Drakeff9ea482000-04-19 13:54:15 +00002695 res = ((nch % 4) == 3);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002696
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002697 if (res && (nch > 3)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002698 int pos = 3;
2699 /* ( ',' test ':' test )* */
2700 while (res && (pos < nch)) {
2701 res = (validate_comma(CHILD(tree, pos))
2702 && validate_test(CHILD(tree, pos + 1))
2703 && validate_colon(CHILD(tree, pos + 2))
2704 && validate_test(CHILD(tree, pos + 3)));
2705 pos += 4;
2706 }
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002707 }
2708 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002709}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002710
2711
Guido van Rossum47478871996-08-21 14:32:37 +00002712static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002713validate_eval_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002714{
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002715 int pos;
2716 int nch = NCH(tree);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002717 int res = (validate_ntype(tree, eval_input)
Fred Drakeff9ea482000-04-19 13:54:15 +00002718 && (nch >= 2)
2719 && validate_testlist(CHILD(tree, 0))
2720 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002721
Guido van Rossum3d602e31996-07-21 02:33:56 +00002722 for (pos = 1; res && (pos < (nch - 1)); ++pos)
Fred Drakeff9ea482000-04-19 13:54:15 +00002723 res = validate_ntype(CHILD(tree, pos), NEWLINE);
Guido van Rossum3d602e31996-07-21 02:33:56 +00002724
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002725 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002726}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002727
2728
Guido van Rossum47478871996-08-21 14:32:37 +00002729static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002730validate_node(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002731{
Fred Drakeff9ea482000-04-19 13:54:15 +00002732 int nch = 0; /* num. children on current node */
2733 int res = 1; /* result value */
2734 node* next = 0; /* node to process after this one */
Guido van Rossum3d602e31996-07-21 02:33:56 +00002735
Martin v. Löwisb28f6e72001-06-23 19:55:38 +00002736 while (res && (tree != 0)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002737 nch = NCH(tree);
2738 next = 0;
2739 switch (TYPE(tree)) {
2740 /*
2741 * Definition nodes.
2742 */
2743 case funcdef:
2744 res = validate_funcdef(tree);
2745 break;
2746 case classdef:
2747 res = validate_class(tree);
2748 break;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002749 case decorated:
2750 res = validate_decorated(tree);
2751 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002752 /*
2753 * "Trivial" parse tree nodes.
2754 * (Why did I call these trivial?)
2755 */
2756 case stmt:
2757 res = validate_stmt(tree);
2758 break;
2759 case small_stmt:
2760 /*
Guido van Rossum452bf512007-02-09 05:32:43 +00002761 * expr_stmt | del_stmt | pass_stmt | flow_stmt
Georg Brandl7cae87c2006-09-06 06:51:57 +00002762 * | import_stmt | global_stmt | assert_stmt
Fred Drakeff9ea482000-04-19 13:54:15 +00002763 */
2764 res = validate_small_stmt(tree);
2765 break;
2766 case flow_stmt:
2767 res = (validate_numnodes(tree, 1, "flow_stmt")
2768 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2769 || (TYPE(CHILD(tree, 0)) == continue_stmt)
Fred Drake02126f22001-07-17 02:59:15 +00002770 || (TYPE(CHILD(tree, 0)) == yield_stmt)
Fred Drakeff9ea482000-04-19 13:54:15 +00002771 || (TYPE(CHILD(tree, 0)) == return_stmt)
2772 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2773 if (res)
2774 next = CHILD(tree, 0);
2775 else if (nch == 1)
Fred Drake661ea262000-10-24 19:57:45 +00002776 err_string("illegal flow_stmt type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002777 break;
Fred Drake02126f22001-07-17 02:59:15 +00002778 case yield_stmt:
2779 res = validate_yield_stmt(tree);
2780 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002781 /*
2782 * Compound statements.
2783 */
2784 case simple_stmt:
2785 res = validate_simple_stmt(tree);
2786 break;
2787 case compound_stmt:
2788 res = validate_compound_stmt(tree);
2789 break;
2790 /*
Thomas Wouters7e474022000-07-16 12:04:32 +00002791 * Fundamental statements.
Fred Drakeff9ea482000-04-19 13:54:15 +00002792 */
2793 case expr_stmt:
2794 res = validate_expr_stmt(tree);
2795 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002796 case del_stmt:
2797 res = validate_del_stmt(tree);
2798 break;
2799 case pass_stmt:
2800 res = (validate_numnodes(tree, 1, "pass")
2801 && validate_name(CHILD(tree, 0), "pass"));
2802 break;
2803 case break_stmt:
2804 res = (validate_numnodes(tree, 1, "break")
2805 && validate_name(CHILD(tree, 0), "break"));
2806 break;
2807 case continue_stmt:
2808 res = (validate_numnodes(tree, 1, "continue")
2809 && validate_name(CHILD(tree, 0), "continue"));
2810 break;
2811 case return_stmt:
2812 res = validate_return_stmt(tree);
2813 break;
2814 case raise_stmt:
2815 res = validate_raise_stmt(tree);
2816 break;
2817 case import_stmt:
2818 res = validate_import_stmt(tree);
2819 break;
Anthony Baxter1a4ddae2004-08-31 10:07:13 +00002820 case import_name:
2821 res = validate_import_name(tree);
2822 break;
2823 case import_from:
2824 res = validate_import_from(tree);
2825 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002826 case global_stmt:
2827 res = validate_global_stmt(tree);
2828 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002829 case assert_stmt:
2830 res = validate_assert_stmt(tree);
2831 break;
2832 case if_stmt:
2833 res = validate_if(tree);
2834 break;
2835 case while_stmt:
2836 res = validate_while(tree);
2837 break;
2838 case for_stmt:
2839 res = validate_for(tree);
2840 break;
2841 case try_stmt:
2842 res = validate_try(tree);
2843 break;
2844 case suite:
2845 res = validate_suite(tree);
2846 break;
2847 /*
2848 * Expression nodes.
2849 */
2850 case testlist:
2851 res = validate_testlist(tree);
2852 break;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002853 case yield_expr:
2854 res = validate_yield_expr(tree);
2855 break;
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002856 case testlist1:
2857 res = validate_testlist1(tree);
2858 break;
Fred Drakeff9ea482000-04-19 13:54:15 +00002859 case test:
2860 res = validate_test(tree);
2861 break;
2862 case and_test:
2863 res = validate_and_test(tree);
2864 break;
2865 case not_test:
2866 res = validate_not_test(tree);
2867 break;
2868 case comparison:
2869 res = validate_comparison(tree);
2870 break;
2871 case exprlist:
2872 res = validate_exprlist(tree);
2873 break;
2874 case comp_op:
2875 res = validate_comp_op(tree);
2876 break;
2877 case expr:
2878 res = validate_expr(tree);
2879 break;
2880 case xor_expr:
2881 res = validate_xor_expr(tree);
2882 break;
2883 case and_expr:
2884 res = validate_and_expr(tree);
2885 break;
2886 case shift_expr:
2887 res = validate_shift_expr(tree);
2888 break;
2889 case arith_expr:
2890 res = validate_arith_expr(tree);
2891 break;
2892 case term:
2893 res = validate_term(tree);
2894 break;
2895 case factor:
2896 res = validate_factor(tree);
2897 break;
2898 case power:
2899 res = validate_power(tree);
2900 break;
2901 case atom:
2902 res = validate_atom(tree);
2903 break;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002904
Fred Drakeff9ea482000-04-19 13:54:15 +00002905 default:
2906 /* Hopefully never reached! */
Fred Drake661ea262000-10-24 19:57:45 +00002907 err_string("unrecognized node type");
Fred Drakeff9ea482000-04-19 13:54:15 +00002908 res = 0;
2909 break;
2910 }
2911 tree = next;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002912 }
2913 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002914}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002915
2916
Guido van Rossum47478871996-08-21 14:32:37 +00002917static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002918validate_expr_tree(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002919{
2920 int res = validate_eval_input(tree);
2921
2922 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002923 err_string("could not validate expression tuple");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002924
2925 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002926}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002927
2928
Guido van Rossum3d602e31996-07-21 02:33:56 +00002929/* file_input:
Fred Drakeff9ea482000-04-19 13:54:15 +00002930 * (NEWLINE | stmt)* ENDMARKER
Guido van Rossum3d602e31996-07-21 02:33:56 +00002931 */
Guido van Rossum47478871996-08-21 14:32:37 +00002932static int
Fred Drakeff9ea482000-04-19 13:54:15 +00002933validate_file_input(node *tree)
Guido van Rossum3d602e31996-07-21 02:33:56 +00002934{
Fred Drakec2683dd2001-07-17 19:32:05 +00002935 int j;
Guido van Rossum3d602e31996-07-21 02:33:56 +00002936 int nch = NCH(tree) - 1;
2937 int res = ((nch >= 0)
Fred Drakeff9ea482000-04-19 13:54:15 +00002938 && validate_ntype(CHILD(tree, nch), ENDMARKER));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002939
Fred Drakec2683dd2001-07-17 19:32:05 +00002940 for (j = 0; res && (j < nch); ++j) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002941 if (TYPE(CHILD(tree, j)) == stmt)
2942 res = validate_stmt(CHILD(tree, j));
2943 else
2944 res = validate_newline(CHILD(tree, j));
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002945 }
Thomas Wouters7e474022000-07-16 12:04:32 +00002946 /* This stays in to prevent any internal failures from getting to the
Fred Drakeff9ea482000-04-19 13:54:15 +00002947 * user. Hopefully, this won't be needed. If a user reports getting
2948 * this, we have some debugging to do.
Guido van Rossum3d602e31996-07-21 02:33:56 +00002949 */
2950 if (!res && !PyErr_Occurred())
Fred Drake661ea262000-10-24 19:57:45 +00002951 err_string("VALIDATION FAILURE: report this to the maintainer!");
Guido van Rossum3d602e31996-07-21 02:33:56 +00002952
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002953 return (res);
Fred Drakeff9ea482000-04-19 13:54:15 +00002954}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002955
Michael W. Hudsondf1252d2003-02-08 18:05:10 +00002956static int
2957validate_encoding_decl(node *tree)
2958{
2959 int nch = NCH(tree);
2960 int res = ((nch == 1)
2961 && validate_file_input(CHILD(tree, 0)));
2962
2963 if (!res && !PyErr_Occurred())
2964 err_string("Error Parsing encoding_decl");
2965
2966 return res;
2967}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00002968
Fred Drake43f8f9b1998-04-13 16:25:46 +00002969static PyObject*
2970pickle_constructor = NULL;
2971
2972
2973static PyObject*
Fred Drakeff9ea482000-04-19 13:54:15 +00002974parser__pickler(PyObject *self, PyObject *args)
Fred Drake43f8f9b1998-04-13 16:25:46 +00002975{
Fred Drake268397f1998-04-29 14:16:32 +00002976 NOTE(ARGUNUSED(self))
Fred Drake43f8f9b1998-04-13 16:25:46 +00002977 PyObject *result = NULL;
Fred Drakec2683dd2001-07-17 19:32:05 +00002978 PyObject *st = NULL;
Fred Drake2a6875e1999-09-20 22:32:18 +00002979 PyObject *empty_dict = NULL;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002980
Fred Drakec2683dd2001-07-17 19:32:05 +00002981 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
Fred Drakeff9ea482000-04-19 13:54:15 +00002982 PyObject *newargs;
2983 PyObject *tuple;
Fred Drake43f8f9b1998-04-13 16:25:46 +00002984
Fred Drake2a6875e1999-09-20 22:32:18 +00002985 if ((empty_dict = PyDict_New()) == NULL)
2986 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002987 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
Fred Drakeff9ea482000-04-19 13:54:15 +00002988 goto finally;
Fred Drakec2683dd2001-07-17 19:32:05 +00002989 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002990 if (tuple != NULL) {
2991 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2992 Py_DECREF(tuple);
2993 }
Fred Drake2a6875e1999-09-20 22:32:18 +00002994 Py_DECREF(empty_dict);
Fred Drakeff9ea482000-04-19 13:54:15 +00002995 Py_DECREF(newargs);
Fred Drake43f8f9b1998-04-13 16:25:46 +00002996 }
2997 finally:
Fred Drake2a6875e1999-09-20 22:32:18 +00002998 Py_XDECREF(empty_dict);
2999
Fred Drake43f8f9b1998-04-13 16:25:46 +00003000 return (result);
Fred Drakeff9ea482000-04-19 13:54:15 +00003001}
Fred Drake43f8f9b1998-04-13 16:25:46 +00003002
3003
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003004/* Functions exported by this module. Most of this should probably
Fred Drakec2683dd2001-07-17 19:32:05 +00003005 * be converted into an ST object with methods, but that is better
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003006 * done directly in Python, allowing subclasses to be created directly.
Guido van Rossum3d602e31996-07-21 02:33:56 +00003007 * We'd really have to write a wrapper around it all anyway to allow
3008 * inheritance.
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003009 */
3010static PyMethodDef parser_functions[] = {
Fred Drakec2683dd2001-07-17 19:32:05 +00003011 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003012 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003013 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003014 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003015 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003016 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003017 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003018 PyDoc_STR("Compiles an ST object into a code object.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003019 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003020 PyDoc_STR("Creates an ST object from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003021 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003022 PyDoc_STR("Determines if an ST object was created from an expression.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003023 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003024 PyDoc_STR("Determines if an ST object was created from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003025 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003026 PyDoc_STR("Creates an ST object from a suite.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003027 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003028 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003029 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003030 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003031 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003032 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003033 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003034 PyDoc_STR("Creates a list-tree representation of an ST.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003035 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003036 PyDoc_STR("Creates an ST object from a tree representation.")},
Fred Drakec2683dd2001-07-17 19:32:05 +00003037 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
Neal Norwitz200788c2002-08-13 22:20:41 +00003038 PyDoc_STR("Creates an ST object from a tree representation.")},
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003039
Fred Drake43f8f9b1998-04-13 16:25:46 +00003040 /* private stuff: support pickle module */
Fred Drake13130bc2001-08-15 16:44:56 +00003041 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003042 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
Fred Drake43f8f9b1998-04-13 16:25:46 +00003043
Fred Drake268397f1998-04-29 14:16:32 +00003044 {NULL, NULL, 0, NULL}
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003045 };
3046
3047
Mark Hammond62b1ab12002-07-23 06:31:15 +00003048PyMODINIT_FUNC initparser(void); /* supply a prototype */
Fred Drake28f739a2000-08-25 22:42:40 +00003049
Mark Hammond62b1ab12002-07-23 06:31:15 +00003050PyMODINIT_FUNC
Thomas Wouters5c669862000-07-24 15:49:08 +00003051initparser(void)
Fred Drake28f739a2000-08-25 22:42:40 +00003052{
Fred Drake13130bc2001-08-15 16:44:56 +00003053 PyObject *module, *copyreg;
Fred Drakec2683dd2001-07-17 19:32:05 +00003054
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003055 Py_Type(&PyST_Type) = &PyType_Type;
Guido van Rossumf2b2dac1997-01-23 23:29:44 +00003056 module = Py_InitModule("parser", parser_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003057 if (module == NULL)
3058 return;
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003059
Fred Drake7a15ba51999-09-09 14:21:52 +00003060 if (parser_error == 0)
3061 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003062
Tim Peters6a627252003-07-21 14:25:23 +00003063 if (parser_error == 0)
Fred Drakec2683dd2001-07-17 19:32:05 +00003064 /* caller will check PyErr_Occurred() */
3065 return;
Tim Peters6a627252003-07-21 14:25:23 +00003066 /* CAUTION: The code next used to skip bumping the refcount on
3067 * parser_error. That's a disaster if initparser() gets called more
3068 * than once. By incref'ing, we ensure that each module dict that
3069 * gets created owns its reference to the shared parser_error object,
3070 * and the file static parser_error vrbl owns a reference too.
3071 */
3072 Py_INCREF(parser_error);
3073 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3074 return;
3075
Fred Drakec2683dd2001-07-17 19:32:05 +00003076 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003077 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
Fred Drakec2683dd2001-07-17 19:32:05 +00003078 Py_INCREF(&PyST_Type);
Fred Drake13130bc2001-08-15 16:44:56 +00003079 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
Guido van Rossum3d602e31996-07-21 02:33:56 +00003080
Fred Drake13130bc2001-08-15 16:44:56 +00003081 PyModule_AddStringConstant(module, "__copyright__",
3082 parser_copyright_string);
3083 PyModule_AddStringConstant(module, "__doc__",
3084 parser_doc_string);
3085 PyModule_AddStringConstant(module, "__version__",
3086 parser_version_string);
Guido van Rossumd9e9f9c1995-10-11 17:35:38 +00003087
Fred Drake78bdb9b2001-07-19 20:17:15 +00003088 /* Register to support pickling.
3089 * If this fails, the import of this module will fail because an
3090 * exception will be raised here; should we clear the exception?
3091 */
Fred Drake13130bc2001-08-15 16:44:56 +00003092 copyreg = PyImport_ImportModule("copy_reg");
3093 if (copyreg != NULL) {
Fred Drakeff9ea482000-04-19 13:54:15 +00003094 PyObject *func, *pickler;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003095
Fred Drake13130bc2001-08-15 16:44:56 +00003096 func = PyObject_GetAttrString(copyreg, "pickle");
3097 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3098 pickler = PyObject_GetAttrString(module, "_pickler");
Fred Drakeff9ea482000-04-19 13:54:15 +00003099 Py_XINCREF(pickle_constructor);
3100 if ((func != NULL) && (pickle_constructor != NULL)
3101 && (pickler != NULL)) {
3102 PyObject *res;
Fred Drake43f8f9b1998-04-13 16:25:46 +00003103
Thomas Wouters477c8d52006-05-27 19:21:47 +00003104 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3105 pickle_constructor, NULL);
Fred Drakeff9ea482000-04-19 13:54:15 +00003106 Py_XDECREF(res);
3107 }
3108 Py_XDECREF(func);
Fred Drake13130bc2001-08-15 16:44:56 +00003109 Py_XDECREF(pickle_constructor);
3110 Py_XDECREF(pickler);
3111 Py_DECREF(copyreg);
Fred Drake43f8f9b1998-04-13 16:25:46 +00003112 }
Fred Drakeff9ea482000-04-19 13:54:15 +00003113}