parsermodule.c revision 0c31562a913e9a49842bd73c04847861c23774f1
1/*  parsermodule.c
2 *
3 *  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.
10 *
11 *  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.
16 *
17 *  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.
20 *
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(...)".
26 */
27
28#include "Python.h"                     /* general Python API             */
29#include "Python-ast.h"                 /* mod_ty */
30#include "graminit.h"                   /* symbols defined in the grammar */
31#include "node.h"                       /* internal parser structure      */
32#include "errcode.h"                    /* error codes for PyNode_*()     */
33#include "token.h"                      /* token definitions              */
34#include "grammar.h"
35#include "parsetok.h"
36                                        /* ISTERMINAL() / ISNONTERMINAL() */
37#include "compile.h"
38#undef Yield
39#include "ast.h"
40#include "pyarena.h"
41
42extern grammar _PyParser_Grammar; /* From graminit.c */
43
44#ifdef lint
45#include <note.h>
46#else
47#define NOTE(x)
48#endif
49
50/*  String constants used to initialize module attributes.
51 *
52 */
53static char parser_copyright_string[] =
54"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
55University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
56Virginia, USA.  Portions copyright 1991-1995 by Stichting Mathematisch\n\
57Centrum, Amsterdam, The Netherlands.";
58
59
60PyDoc_STRVAR(parser_doc_string,
61"This is an interface to Python's internal parser.");
62
63static char parser_version_string[] = "0.5";
64
65
66typedef PyObject* (*SeqMaker) (Py_ssize_t length);
67typedef int (*SeqInserter) (PyObject* sequence,
68                            Py_ssize_t index,
69                            PyObject* element);
70
71/*  The function below is copyrighted by Stichting Mathematisch Centrum.  The
72 *  original copyright statement is included below, and continues to apply
73 *  in full to the function immediately following.  All other material is
74 *  original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
75 *  Institute and State University.  Changes were made to comply with the
76 *  new naming conventions.  Added arguments to provide support for creating
77 *  lists as well as tuples, and optionally including the line numbers.
78 */
79
80
81static PyObject*
82node2tuple(node *n,                     /* node to convert               */
83           SeqMaker mkseq,              /* create sequence               */
84           SeqInserter addelem,         /* func. to add elem. in seq.    */
85           int lineno,                  /* include line numbers?         */
86           int col_offset)              /* include column offsets?       */
87{
88    if (n == NULL) {
89        Py_INCREF(Py_None);
90        return (Py_None);
91    }
92    if (ISNONTERMINAL(TYPE(n))) {
93        int i;
94        PyObject *v;
95        PyObject *w;
96
97        v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
98        if (v == NULL)
99            return (v);
100        w = PyLong_FromLong(TYPE(n));
101        if (w == NULL) {
102            Py_DECREF(v);
103            return ((PyObject*) NULL);
104        }
105        (void) addelem(v, 0, w);
106        for (i = 0; i < NCH(n); i++) {
107            w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
108            if (w == NULL) {
109                Py_DECREF(v);
110                return ((PyObject*) NULL);
111            }
112            (void) addelem(v, i+1, w);
113        }
114
115        if (TYPE(n) == encoding_decl)
116            (void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
117        return (v);
118    }
119    else if (ISTERMINAL(TYPE(n))) {
120        PyObject *result = mkseq(2 + lineno + col_offset);
121        if (result != NULL) {
122            (void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
123            (void) addelem(result, 1, PyUnicode_FromString(STR(n)));
124            if (lineno == 1)
125                (void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
126            if (col_offset == 1)
127                (void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
128        }
129        return (result);
130    }
131    else {
132        PyErr_SetString(PyExc_SystemError,
133                        "unrecognized parse tree node type");
134        return ((PyObject*) NULL);
135    }
136}
137/*
138 *  End of material copyrighted by Stichting Mathematisch Centrum.
139 */
140
141
142
143/*  There are two types of intermediate objects we're interested in:
144 *  'eval' and 'exec' types.  These constants can be used in the st_type
145 *  field of the object type to identify which any given object represents.
146 *  These should probably go in an external header to allow other extensions
147 *  to use them, but then, we really should be using C++ too.  ;-)
148 */
149
150#define PyST_EXPR  1
151#define PyST_SUITE 2
152
153
154/*  These are the internal objects and definitions required to implement the
155 *  ST type.  Most of the internal names are more reminiscent of the 'old'
156 *  naming style, but the code uses the new naming convention.
157 */
158
159static PyObject*
160parser_error = 0;
161
162
163typedef struct {
164    PyObject_HEAD                       /* standard object header           */
165    node* st_node;                      /* the node* returned by the parser */
166    int   st_type;                      /* EXPR or SUITE ?                  */
167    PyCompilerFlags st_flags;           /* Parser and compiler flags        */
168} PyST_Object;
169
170
171static void parser_free(PyST_Object *st);
172static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
173static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
174static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
175static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
176static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
177static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
178
179#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
180
181static PyMethodDef parser_methods[] = {
182    {"compile",         (PyCFunction)parser_compilest,  PUBLIC_METHOD_TYPE,
183        PyDoc_STR("Compile this ST object into a code object.")},
184    {"isexpr",          (PyCFunction)parser_isexpr,     PUBLIC_METHOD_TYPE,
185        PyDoc_STR("Determines if this ST object was created from an expression.")},
186    {"issuite",         (PyCFunction)parser_issuite,    PUBLIC_METHOD_TYPE,
187        PyDoc_STR("Determines if this ST object was created from a suite.")},
188    {"tolist",          (PyCFunction)parser_st2list,    PUBLIC_METHOD_TYPE,
189        PyDoc_STR("Creates a list-tree representation of this ST.")},
190    {"totuple",         (PyCFunction)parser_st2tuple,   PUBLIC_METHOD_TYPE,
191        PyDoc_STR("Creates a tuple-tree representation of this ST.")},
192
193    {NULL, NULL, 0, NULL}
194};
195
196static
197PyTypeObject PyST_Type = {
198    PyVarObject_HEAD_INIT(NULL, 0)
199    "parser.st",                        /* tp_name              */
200    (int) sizeof(PyST_Object),          /* tp_basicsize         */
201    0,                                  /* tp_itemsize          */
202    (destructor)parser_free,            /* tp_dealloc           */
203    0,                                  /* tp_print             */
204    0,                                  /* tp_getattr           */
205    0,                                  /* tp_setattr           */
206    0,                                  /* tp_reserved          */
207    0,                                  /* tp_repr              */
208    0,                                  /* tp_as_number         */
209    0,                                  /* tp_as_sequence       */
210    0,                                  /* tp_as_mapping        */
211    0,                                  /* tp_hash              */
212    0,                                  /* tp_call              */
213    0,                                  /* tp_str               */
214    0,                                  /* tp_getattro          */
215    0,                                  /* tp_setattro          */
216
217    /* Functions to access object as input/output buffer */
218    0,                                  /* tp_as_buffer         */
219
220    Py_TPFLAGS_DEFAULT,                 /* tp_flags             */
221
222    /* __doc__ */
223    "Intermediate representation of a Python parse tree.",
224    0,                                  /* tp_traverse */
225    0,                                  /* tp_clear */
226    parser_richcompare,                 /* tp_richcompare */
227    0,                                  /* tp_weaklistoffset */
228    0,                                  /* tp_iter */
229    0,                                  /* tp_iternext */
230    parser_methods,                     /* tp_methods */
231};  /* PyST_Type */
232
233
234/* PyST_Type isn't subclassable, so just check ob_type */
235#define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
236
237static int
238parser_compare_nodes(node *left, node *right)
239{
240    int j;
241
242    if (TYPE(left) < TYPE(right))
243        return (-1);
244
245    if (TYPE(right) < TYPE(left))
246        return (1);
247
248    if (ISTERMINAL(TYPE(left)))
249        return (strcmp(STR(left), STR(right)));
250
251    if (NCH(left) < NCH(right))
252        return (-1);
253
254    if (NCH(right) < NCH(left))
255        return (1);
256
257    for (j = 0; j < NCH(left); ++j) {
258        int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
259
260        if (v != 0)
261            return (v);
262    }
263    return (0);
264}
265
266/*  parser_richcompare(PyObject* left, PyObject* right, int op)
267 *
268 *  Comparison function used by the Python operators ==, !=, <, >, <=, >=
269 *  This really just wraps a call to parser_compare_nodes() with some easy
270 *  checks and protection code.
271 *
272 */
273
274#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
275
276static PyObject *
277parser_richcompare(PyObject *left, PyObject *right, int op)
278{
279    int result;
280    PyObject *v;
281
282    /* neither argument should be NULL, unless something's gone wrong */
283    if (left == NULL || right == NULL) {
284        PyErr_BadInternalCall();
285        return NULL;
286    }
287
288    /* both arguments should be instances of PyST_Object */
289    if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
290        v = Py_NotImplemented;
291        goto finished;
292    }
293
294    if (left == right)
295        /* if arguments are identical, they're equal */
296        result = 0;
297    else
298        result = parser_compare_nodes(((PyST_Object *)left)->st_node,
299                                      ((PyST_Object *)right)->st_node);
300
301    /* Convert return value to a Boolean */
302    switch (op) {
303    case Py_EQ:
304        v = TEST_COND(result == 0);
305        break;
306    case Py_NE:
307        v = TEST_COND(result != 0);
308        break;
309    case Py_LE:
310        v = TEST_COND(result <= 0);
311        break;
312    case Py_GE:
313        v = TEST_COND(result >= 0);
314        break;
315    case Py_LT:
316        v = TEST_COND(result < 0);
317        break;
318    case Py_GT:
319        v = TEST_COND(result > 0);
320        break;
321    default:
322        PyErr_BadArgument();
323        return NULL;
324    }
325  finished:
326    Py_INCREF(v);
327    return v;
328}
329
330/*  parser_newstobject(node* st)
331 *
332 *  Allocates a new Python object representing an ST.  This is simply the
333 *  'wrapper' object that holds a node* and allows it to be passed around in
334 *  Python code.
335 *
336 */
337static PyObject*
338parser_newstobject(node *st, int type)
339{
340    PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
341
342    if (o != 0) {
343        o->st_node = st;
344        o->st_type = type;
345        o->st_flags.cf_flags = 0;
346    }
347    else {
348        PyNode_Free(st);
349    }
350    return ((PyObject*)o);
351}
352
353
354/*  void parser_free(PyST_Object* st)
355 *
356 *  This is called by a del statement that reduces the reference count to 0.
357 *
358 */
359static void
360parser_free(PyST_Object *st)
361{
362    PyNode_Free(st->st_node);
363    PyObject_Del(st);
364}
365
366
367/*  parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
368 *
369 *  This provides conversion from a node* to a tuple object that can be
370 *  returned to the Python-level caller.  The ST object is not modified.
371 *
372 */
373static PyObject*
374parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
375{
376    PyObject *line_option = 0;
377    PyObject *col_option = 0;
378    PyObject *res = 0;
379    int ok;
380
381    static char *keywords[] = {"st", "line_info", "col_info", NULL};
382
383    if (self == NULL || PyModule_Check(self)) {
384        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
385                                         &PyST_Type, &self, &line_option,
386                                         &col_option);
387    }
388    else
389        ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
390                                         &line_option, &col_option);
391    if (ok != 0) {
392        int lineno = 0;
393        int col_offset = 0;
394        if (line_option != NULL) {
395            lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
396        }
397        if (col_option != NULL) {
398            col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
399        }
400        /*
401         *  Convert ST into a tuple representation.  Use Guido's function,
402         *  since it's known to work already.
403         */
404        res = node2tuple(((PyST_Object*)self)->st_node,
405                         PyTuple_New, PyTuple_SetItem, lineno, col_offset);
406    }
407    return (res);
408}
409
410
411/*  parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
412 *
413 *  This provides conversion from a node* to a list object that can be
414 *  returned to the Python-level caller.  The ST object is not modified.
415 *
416 */
417static PyObject*
418parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
419{
420    PyObject *line_option = 0;
421    PyObject *col_option = 0;
422    PyObject *res = 0;
423    int ok;
424
425    static char *keywords[] = {"st", "line_info", "col_info", NULL};
426
427    if (self == NULL || PyModule_Check(self))
428        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
429                                         &PyST_Type, &self, &line_option,
430                                         &col_option);
431    else
432        ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
433                                         &line_option, &col_option);
434    if (ok) {
435        int lineno = 0;
436        int col_offset = 0;
437        if (line_option != 0) {
438            lineno = PyObject_IsTrue(line_option) ? 1 : 0;
439        }
440        if (col_option != NULL) {
441            col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
442        }
443        /*
444         *  Convert ST into a tuple representation.  Use Guido's function,
445         *  since it's known to work already.
446         */
447        res = node2tuple(self->st_node,
448                         PyList_New, PyList_SetItem, lineno, col_offset);
449    }
450    return (res);
451}
452
453
454/*  parser_compilest(PyObject* self, PyObject* args)
455 *
456 *  This function creates code objects from the parse tree represented by
457 *  the passed-in data object.  An optional file name is passed in as well.
458 *
459 */
460static PyObject*
461parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
462{
463    PyObject*     res = 0;
464    PyArena*      arena;
465    mod_ty        mod;
466    char*         str = "<syntax-tree>";
467    int ok;
468
469    static char *keywords[] = {"st", "filename", NULL};
470
471    if (self == NULL || PyModule_Check(self))
472        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
473                                         &PyST_Type, &self, &str);
474    else
475        ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
476                                         &str);
477
478    if (ok) {
479        arena = PyArena_New();
480        if (arena) {
481           mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
482           if (mod) {
483               res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
484           }
485           PyArena_Free(arena);
486        }
487    }
488
489    return (res);
490}
491
492
493/*  PyObject* parser_isexpr(PyObject* self, PyObject* args)
494 *  PyObject* parser_issuite(PyObject* self, PyObject* args)
495 *
496 *  Checks the passed-in ST object to determine if it is an expression or
497 *  a statement suite, respectively.  The return is a Python truth value.
498 *
499 */
500static PyObject*
501parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
502{
503    PyObject* res = 0;
504    int ok;
505
506    static char *keywords[] = {"st", NULL};
507
508    if (self == NULL || PyModule_Check(self))
509        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
510                                         &PyST_Type, &self);
511    else
512        ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
513
514    if (ok) {
515        /* Check to see if the ST represents an expression or not. */
516        res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
517        Py_INCREF(res);
518    }
519    return (res);
520}
521
522
523static PyObject*
524parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
525{
526    PyObject* res = 0;
527    int ok;
528
529    static char *keywords[] = {"st", NULL};
530
531    if (self == NULL || PyModule_Check(self))
532        ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
533                                         &PyST_Type, &self);
534    else
535        ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
536
537    if (ok) {
538        /* Check to see if the ST represents an expression or not. */
539        res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
540        Py_INCREF(res);
541    }
542    return (res);
543}
544
545
546/*  err_string(char* message)
547 *
548 *  Sets the error string for an exception of type ParserError.
549 *
550 */
551static void
552err_string(char *message)
553{
554    PyErr_SetString(parser_error, message);
555}
556
557
558/*  PyObject* parser_do_parse(PyObject* args, int type)
559 *
560 *  Internal function to actually execute the parse and return the result if
561 *  successful or set an exception if not.
562 *
563 */
564static PyObject*
565parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
566{
567    char*     string = 0;
568    PyObject* res    = 0;
569    int flags        = 0;
570    perrdetail err;
571
572    static char *keywords[] = {"source", NULL};
573
574    if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
575        node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
576                                                       &_PyParser_Grammar,
577                                                      (type == PyST_EXPR)
578                                                      ? eval_input : file_input,
579                                                      &err, &flags);
580
581	if (n) {
582	    res = parser_newstobject(n, type);
583            if (res)
584                ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
585        }
586        else
587            PyParser_SetError(&err);
588    }
589    return (res);
590}
591
592
593/*  PyObject* parser_expr(PyObject* self, PyObject* args)
594 *  PyObject* parser_suite(PyObject* self, PyObject* args)
595 *
596 *  External interfaces to the parser itself.  Which is called determines if
597 *  the parser attempts to recognize an expression ('eval' form) or statement
598 *  suite ('exec' form).  The real work is done by parser_do_parse() above.
599 *
600 */
601static PyObject*
602parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
603{
604    NOTE(ARGUNUSED(self))
605    return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
606}
607
608
609static PyObject*
610parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
611{
612    NOTE(ARGUNUSED(self))
613    return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
614}
615
616
617
618/*  This is the messy part of the code.  Conversion from a tuple to an ST
619 *  object requires that the input tuple be valid without having to rely on
620 *  catching an exception from the compiler.  This is done to allow the
621 *  compiler itself to remain fast, since most of its input will come from
622 *  the parser directly, and therefore be known to be syntactically correct.
623 *  This validation is done to ensure that we don't core dump the compile
624 *  phase, returning an exception instead.
625 *
626 *  Two aspects can be broken out in this code:  creating a node tree from
627 *  the tuple passed in, and verifying that it is indeed valid.  It may be
628 *  advantageous to expand the number of ST types to include funcdefs and
629 *  lambdadefs to take advantage of the optimizer, recognizing those STs
630 *  here.  They are not necessary, and not quite as useful in a raw form.
631 *  For now, let's get expressions and suites working reliably.
632 */
633
634
635static node* build_node_tree(PyObject *tuple);
636static int   validate_expr_tree(node *tree);
637static int   validate_file_input(node *tree);
638static int   validate_encoding_decl(node *tree);
639
640/*  PyObject* parser_tuple2st(PyObject* self, PyObject* args)
641 *
642 *  This is the public function, called from the Python code.  It receives a
643 *  single tuple object from the caller, and creates an ST object if the
644 *  tuple can be validated.  It does this by checking the first code of the
645 *  tuple, and, if acceptable, builds the internal representation.  If this
646 *  step succeeds, the internal representation is validated as fully as
647 *  possible with the various validate_*() routines defined below.
648 *
649 *  This function must be changed if support is to be added for PyST_FRAGMENT
650 *  ST objects.
651 *
652 */
653static PyObject*
654parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
655{
656    NOTE(ARGUNUSED(self))
657    PyObject *st = 0;
658    PyObject *tuple;
659    node *tree;
660
661    static char *keywords[] = {"sequence", NULL};
662
663    if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
664                                     &tuple))
665        return (0);
666    if (!PySequence_Check(tuple)) {
667        PyErr_SetString(PyExc_ValueError,
668                        "sequence2st() requires a single sequence argument");
669        return (0);
670    }
671    /*
672     *  Convert the tree to the internal form before checking it.
673     */
674    tree = build_node_tree(tuple);
675    if (tree != 0) {
676        int start_sym = TYPE(tree);
677        if (start_sym == eval_input) {
678            /*  Might be an eval form.  */
679            if (validate_expr_tree(tree))
680                st = parser_newstobject(tree, PyST_EXPR);
681            else
682                PyNode_Free(tree);
683        }
684        else if (start_sym == file_input) {
685            /*  This looks like an exec form so far.  */
686            if (validate_file_input(tree))
687                st = parser_newstobject(tree, PyST_SUITE);
688            else
689                PyNode_Free(tree);
690        }
691        else if (start_sym == encoding_decl) {
692            /* This looks like an encoding_decl so far. */
693            if (validate_encoding_decl(tree))
694                st = parser_newstobject(tree, PyST_SUITE);
695            else
696                PyNode_Free(tree);
697        }
698        else {
699            /*  This is a fragment, at best. */
700            PyNode_Free(tree);
701            err_string("parse tree does not use a valid start symbol");
702        }
703    }
704    /*  Make sure we throw an exception on all errors.  We should never
705     *  get this, but we'd do well to be sure something is done.
706     */
707    if (st == NULL && !PyErr_Occurred())
708        err_string("unspecified ST error occurred");
709
710    return st;
711}
712
713
714/*  node* build_node_children()
715 *
716 *  Iterate across the children of the current non-terminal node and build
717 *  their structures.  If successful, return the root of this portion of
718 *  the tree, otherwise, 0.  Any required exception will be specified already,
719 *  and no memory will have been deallocated.
720 *
721 */
722static node*
723build_node_children(PyObject *tuple, node *root, int *line_num)
724{
725    Py_ssize_t len = PyObject_Size(tuple);
726    Py_ssize_t i;
727    int  err;
728
729    for (i = 1; i < len; ++i) {
730        /* elem must always be a sequence, however simple */
731        PyObject* elem = PySequence_GetItem(tuple, i);
732        int ok = elem != NULL;
733        long  type = 0;
734        char *strn = 0;
735
736        if (ok)
737            ok = PySequence_Check(elem);
738        if (ok) {
739            PyObject *temp = PySequence_GetItem(elem, 0);
740            if (temp == NULL)
741                ok = 0;
742            else {
743                ok = PyLong_Check(temp);
744                if (ok)
745                    type = PyLong_AS_LONG(temp);
746                Py_DECREF(temp);
747            }
748        }
749        if (!ok) {
750            PyObject *err = Py_BuildValue("os", elem,
751                                          "Illegal node construct.");
752            PyErr_SetObject(parser_error, err);
753            Py_XDECREF(err);
754            Py_XDECREF(elem);
755            return (0);
756        }
757        if (ISTERMINAL(type)) {
758            Py_ssize_t len = PyObject_Size(elem);
759            PyObject *temp;
760            const char *temp_str;
761
762            if ((len != 2) && (len != 3)) {
763                err_string("terminal nodes must have 2 or 3 entries");
764                return 0;
765            }
766            temp = PySequence_GetItem(elem, 1);
767            if (temp == NULL)
768                return 0;
769            if (!PyUnicode_Check(temp)) {
770                PyErr_Format(parser_error,
771                             "second item in terminal node must be a string,"
772                             " found %s",
773                             Py_TYPE(temp)->tp_name);
774                Py_DECREF(temp);
775                Py_DECREF(elem);
776                return 0;
777            }
778            if (len == 3) {
779                PyObject *o = PySequence_GetItem(elem, 2);
780                if (o != NULL) {
781                    if (PyLong_Check(o))
782                        *line_num = PyLong_AS_LONG(o);
783                    else {
784                        PyErr_Format(parser_error,
785                                     "third item in terminal node must be an"
786                                     " integer, found %s",
787				     Py_TYPE(temp)->tp_name);
788                        Py_DECREF(o);
789                        Py_DECREF(temp);
790                        Py_DECREF(elem);
791                        return 0;
792                    }
793                    Py_DECREF(o);
794                }
795            }
796            temp_str = _PyUnicode_AsStringAndSize(temp, &len);
797            strn = (char *)PyObject_MALLOC(len + 1);
798            if (strn != NULL)
799                (void) memcpy(strn, temp_str, len + 1);
800            Py_DECREF(temp);
801        }
802        else if (!ISNONTERMINAL(type)) {
803            /*
804             *  It has to be one or the other; this is an error.
805             *  Throw an exception.
806             */
807            PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
808            PyErr_SetObject(parser_error, err);
809            Py_XDECREF(err);
810            Py_XDECREF(elem);
811            return (0);
812        }
813        err = PyNode_AddChild(root, type, strn, *line_num, 0);
814        if (err == E_NOMEM) {
815            Py_XDECREF(elem);
816            PyObject_FREE(strn);
817            return (node *) PyErr_NoMemory();
818        }
819        if (err == E_OVERFLOW) {
820            Py_XDECREF(elem);
821            PyObject_FREE(strn);
822            PyErr_SetString(PyExc_ValueError,
823                            "unsupported number of child nodes");
824            return NULL;
825        }
826
827        if (ISNONTERMINAL(type)) {
828            node* new_child = CHILD(root, i - 1);
829
830            if (new_child != build_node_children(elem, new_child, line_num)) {
831                Py_XDECREF(elem);
832                return (0);
833            }
834        }
835        else if (type == NEWLINE) {     /* It's true:  we increment the     */
836            ++(*line_num);              /* line number *after* the newline! */
837        }
838        Py_XDECREF(elem);
839    }
840    return root;
841}
842
843
844static node*
845build_node_tree(PyObject *tuple)
846{
847    node* res = 0;
848    PyObject *temp = PySequence_GetItem(tuple, 0);
849    long num = -1;
850
851    if (temp != NULL)
852        num = PyLong_AsLong(temp);
853    Py_XDECREF(temp);
854    if (ISTERMINAL(num)) {
855        /*
856         *  The tuple is simple, but it doesn't start with a start symbol.
857         *  Throw an exception now and be done with it.
858         */
859        tuple = Py_BuildValue("os", tuple,
860                    "Illegal syntax-tree; cannot start with terminal symbol.");
861        PyErr_SetObject(parser_error, tuple);
862        Py_XDECREF(tuple);
863    }
864    else if (ISNONTERMINAL(num)) {
865        /*
866         *  Not efficient, but that can be handled later.
867         */
868        int line_num = 0;
869        PyObject *encoding = NULL;
870
871        if (num == encoding_decl) {
872            encoding = PySequence_GetItem(tuple, 2);
873            /* tuple isn't borrowed anymore here, need to DECREF */
874            tuple = PySequence_GetSlice(tuple, 0, 2);
875        }
876        res = PyNode_New(num);
877        if (res != NULL) {
878            if (res != build_node_children(tuple, res, &line_num)) {
879                PyNode_Free(res);
880                res = NULL;
881            }
882            if (res && encoding) {
883                Py_ssize_t len;
884                const char *temp;
885                temp = _PyUnicode_AsStringAndSize(encoding, &len);
886                res->n_str = (char *)PyObject_MALLOC(len + 1);
887                if (res->n_str != NULL && temp != NULL)
888                    (void) memcpy(res->n_str, temp, len + 1);
889                Py_DECREF(encoding);
890                Py_DECREF(tuple);
891            }
892        }
893    }
894    else {
895        /*  The tuple is illegal -- if the number is neither TERMINAL nor
896         *  NONTERMINAL, we can't use it.  Not sure the implementation
897         *  allows this condition, but the API doesn't preclude it.
898         */
899        PyObject *err = Py_BuildValue("os", tuple,
900                                      "Illegal component tuple.");
901        PyErr_SetObject(parser_error, err);
902        Py_XDECREF(err);
903    }
904
905    return (res);
906}
907
908
909/*
910 *  Validation routines used within the validation section:
911 */
912static int validate_terminal(node *terminal, int type, char *string);
913
914#define validate_ampersand(ch)  validate_terminal(ch,      AMPER, "&")
915#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
916#define validate_colon(ch)      validate_terminal(ch,      COLON, ":")
917#define validate_comma(ch)      validate_terminal(ch,      COMMA, ",")
918#define validate_dedent(ch)     validate_terminal(ch,     DEDENT, "")
919#define validate_equal(ch)      validate_terminal(ch,      EQUAL, "=")
920#define validate_indent(ch)     validate_terminal(ch,     INDENT, (char*)NULL)
921#define validate_lparen(ch)     validate_terminal(ch,       LPAR, "(")
922#define validate_newline(ch)    validate_terminal(ch,    NEWLINE, (char*)NULL)
923#define validate_rparen(ch)     validate_terminal(ch,       RPAR, ")")
924#define validate_semi(ch)       validate_terminal(ch,       SEMI, ";")
925#define validate_star(ch)       validate_terminal(ch,       STAR, "*")
926#define validate_vbar(ch)       validate_terminal(ch,       VBAR, "|")
927#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
928#define validate_dot(ch)        validate_terminal(ch,        DOT, ".")
929#define validate_at(ch)         validate_terminal(ch,         AT, "@")
930#define validate_name(ch, str)  validate_terminal(ch,       NAME, str)
931
932#define VALIDATER(n)    static int validate_##n(node *tree)
933
934VALIDATER(node);                VALIDATER(small_stmt);
935VALIDATER(class);               VALIDATER(node);
936VALIDATER(parameters);          VALIDATER(suite);
937VALIDATER(testlist);            VALIDATER(varargslist);
938VALIDATER(vfpdef);
939VALIDATER(stmt);                VALIDATER(simple_stmt);
940VALIDATER(expr_stmt);           VALIDATER(power);
941VALIDATER(del_stmt);
942VALIDATER(return_stmt);         VALIDATER(raise_stmt);
943VALIDATER(import_stmt);         VALIDATER(import_stmt);
944VALIDATER(import_name);         VALIDATER(yield_stmt);
945VALIDATER(global_stmt);         VALIDATER(assert_stmt);
946VALIDATER(compound_stmt);
947VALIDATER(while);               VALIDATER(for);
948VALIDATER(try);                 VALIDATER(except_clause);
949VALIDATER(test);                VALIDATER(and_test);
950VALIDATER(not_test);            VALIDATER(comparison);
951VALIDATER(comp_op);
952VALIDATER(star_expr);           VALIDATER(expr);
953VALIDATER(xor_expr);            VALIDATER(and_expr);
954VALIDATER(shift_expr);          VALIDATER(arith_expr);
955VALIDATER(term);                VALIDATER(factor);
956VALIDATER(atom);                VALIDATER(lambdef);
957VALIDATER(trailer);             VALIDATER(subscript);
958VALIDATER(subscriptlist);       VALIDATER(sliceop);
959VALIDATER(exprlist);            VALIDATER(dictorsetmaker);
960VALIDATER(arglist);             VALIDATER(argument);
961VALIDATER(testlist1);           VALIDATER(comp_for);
962VALIDATER(comp_iter);           VALIDATER(comp_if);
963VALIDATER(testlist_comp);       VALIDATER(yield_expr);
964VALIDATER(yield_or_testlist);   VALIDATER(or_test);
965VALIDATER(test_nocond);         VALIDATER(lambdef_nocond);
966
967#undef VALIDATER
968
969#define is_even(n)      (((n) & 1) == 0)
970#define is_odd(n)       (((n) & 1) == 1)
971
972
973static int
974validate_ntype(node *n, int t)
975{
976    if (TYPE(n) != t) {
977        PyErr_Format(parser_error, "Expected node type %d, got %d.",
978                     t, TYPE(n));
979        return 0;
980    }
981    return 1;
982}
983
984
985/*  Verifies that the number of child nodes is exactly 'num', raising
986 *  an exception if it isn't.  The exception message does not indicate
987 *  the exact number of nodes, allowing this to be used to raise the
988 *  "right" exception when the wrong number of nodes is present in a
989 *  specific variant of a statement's syntax.  This is commonly used
990 *  in that fashion.
991 */
992static int
993validate_numnodes(node *n, int num, const char *const name)
994{
995    if (NCH(n) != num) {
996        PyErr_Format(parser_error,
997                     "Illegal number of children for %s node.", name);
998        return 0;
999    }
1000    return 1;
1001}
1002
1003
1004static int
1005validate_terminal(node *terminal, int type, char *string)
1006{
1007    int res = (validate_ntype(terminal, type)
1008               && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
1009
1010    if (!res && !PyErr_Occurred()) {
1011        PyErr_Format(parser_error,
1012                     "Illegal terminal: expected \"%s\"", string);
1013    }
1014    return (res);
1015}
1016
1017
1018/*  X (',' X) [',']
1019 */
1020static int
1021validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
1022                        const char *const name)
1023{
1024    int nch = NCH(tree);
1025    int res = (nch && validate_ntype(tree, ntype)
1026               && vfunc(CHILD(tree, 0)));
1027
1028    if (!res && !PyErr_Occurred())
1029        (void) validate_numnodes(tree, 1, name);
1030    else {
1031        if (is_even(nch))
1032            res = validate_comma(CHILD(tree, --nch));
1033        if (res && nch > 1) {
1034            int pos = 1;
1035            for ( ; res && pos < nch; pos += 2)
1036                res = (validate_comma(CHILD(tree, pos))
1037                       && vfunc(CHILD(tree, pos + 1)));
1038        }
1039    }
1040    return (res);
1041}
1042
1043
1044/*  validate_class()
1045 *
1046 *  classdef:
1047 *      'class' NAME ['(' testlist ')'] ':' suite
1048 */
1049static int
1050validate_class(node *tree)
1051{
1052    int nch = NCH(tree);
1053    int res = (validate_ntype(tree, classdef) &&
1054	       	((nch == 4) || (nch == 6) || (nch == 7)));
1055
1056    if (res) {
1057        res = (validate_name(CHILD(tree, 0), "class")
1058               && validate_ntype(CHILD(tree, 1), NAME)
1059               && validate_colon(CHILD(tree, nch - 2))
1060               && validate_suite(CHILD(tree, nch - 1)));
1061    }
1062    else {
1063        (void) validate_numnodes(tree, 4, "class");
1064    }
1065
1066    if (res) {
1067	if (nch == 7) {
1068		res = ((validate_lparen(CHILD(tree, 2)) &&
1069			validate_arglist(CHILD(tree, 3)) &&
1070			validate_rparen(CHILD(tree, 4))));
1071	}
1072	else if (nch == 6) {
1073		res = (validate_lparen(CHILD(tree,2)) &&
1074			validate_rparen(CHILD(tree,3)));
1075	}
1076    }
1077    return (res);
1078}
1079
1080
1081/*  if_stmt:
1082 *      'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1083 */
1084static int
1085validate_if(node *tree)
1086{
1087    int nch = NCH(tree);
1088    int res = (validate_ntype(tree, if_stmt)
1089               && (nch >= 4)
1090               && validate_name(CHILD(tree, 0), "if")
1091               && validate_test(CHILD(tree, 1))
1092               && validate_colon(CHILD(tree, 2))
1093               && validate_suite(CHILD(tree, 3)));
1094
1095    if (res && ((nch % 4) == 3)) {
1096        /*  ... 'else' ':' suite  */
1097        res = (validate_name(CHILD(tree, nch - 3), "else")
1098               && validate_colon(CHILD(tree, nch - 2))
1099               && validate_suite(CHILD(tree, nch - 1)));
1100        nch -= 3;
1101    }
1102    else if (!res && !PyErr_Occurred())
1103        (void) validate_numnodes(tree, 4, "if");
1104    if ((nch % 4) != 0)
1105        /* Will catch the case for nch < 4 */
1106        res = validate_numnodes(tree, 0, "if");
1107    else if (res && (nch > 4)) {
1108        /*  ... ('elif' test ':' suite)+ ...  */
1109        int j = 4;
1110        while ((j < nch) && res) {
1111            res = (validate_name(CHILD(tree, j), "elif")
1112                   && validate_colon(CHILD(tree, j + 2))
1113                   && validate_test(CHILD(tree, j + 1))
1114                   && validate_suite(CHILD(tree, j + 3)));
1115            j += 4;
1116        }
1117    }
1118    return (res);
1119}
1120
1121
1122/*  parameters:
1123 *      '(' [varargslist] ')'
1124 *
1125 */
1126static int
1127validate_parameters(node *tree)
1128{
1129    int nch = NCH(tree);
1130    int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
1131
1132    if (res) {
1133        res = (validate_lparen(CHILD(tree, 0))
1134               && validate_rparen(CHILD(tree, nch - 1)));
1135        if (res && (nch == 3))
1136            res = validate_varargslist(CHILD(tree, 1));
1137    }
1138    else {
1139        (void) validate_numnodes(tree, 2, "parameters");
1140    }
1141    return (res);
1142}
1143
1144
1145/*  validate_suite()
1146 *
1147 *  suite:
1148 *      simple_stmt
1149 *    | NEWLINE INDENT stmt+ DEDENT
1150 */
1151static int
1152validate_suite(node *tree)
1153{
1154    int nch = NCH(tree);
1155    int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
1156
1157    if (res && (nch == 1))
1158        res = validate_simple_stmt(CHILD(tree, 0));
1159    else if (res) {
1160        /*  NEWLINE INDENT stmt+ DEDENT  */
1161        res = (validate_newline(CHILD(tree, 0))
1162               && validate_indent(CHILD(tree, 1))
1163               && validate_stmt(CHILD(tree, 2))
1164               && validate_dedent(CHILD(tree, nch - 1)));
1165
1166        if (res && (nch > 4)) {
1167            int i = 3;
1168            --nch;                      /* forget the DEDENT     */
1169            for ( ; res && (i < nch); ++i)
1170                res = validate_stmt(CHILD(tree, i));
1171        }
1172        else if (nch < 4)
1173            res = validate_numnodes(tree, 4, "suite");
1174    }
1175    return (res);
1176}
1177
1178
1179static int
1180validate_testlist(node *tree)
1181{
1182    return (validate_repeating_list(tree, testlist,
1183                                    validate_test, "testlist"));
1184}
1185
1186
1187static int
1188validate_testlist1(node *tree)
1189{
1190    return (validate_repeating_list(tree, testlist1,
1191                                    validate_test, "testlist1"));
1192}
1193
1194
1195/* validate either vfpdef or tfpdef.
1196 * vfpdef: NAME
1197 * tfpdef: NAME [':' test]
1198 */
1199static int
1200validate_vfpdef(node *tree)
1201{
1202    int nch = NCH(tree);
1203    if (TYPE(tree) == vfpdef) {
1204        return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1205    }
1206    else if (TYPE(tree) == tfpdef) {
1207        if (nch == 1) {
1208            return validate_name(CHILD(tree, 0), NULL);
1209        }
1210        else if (nch == 3) {
1211            return validate_name(CHILD(tree, 0), NULL) &&
1212                   validate_colon(CHILD(tree, 1)) &&
1213                   validate_test(CHILD(tree, 2));
1214        }
1215    }
1216    return 0;
1217}
1218
1219/* '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
1220 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
1221 */
1222static int
1223validate_varargslist_trailer(node *tree, int start)
1224{
1225    int nch = NCH(tree);
1226    int res = 0, i;
1227    int sym;
1228
1229    if (nch <= start) {
1230        err_string("expected variable argument trailer for varargslist");
1231        return 0;
1232    }
1233    sym = TYPE(CHILD(tree, start));
1234    if (sym == STAR) {
1235        /*
1236         * '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
1237         */
1238        if (nch-start == 2)
1239            res = validate_vfpdef(CHILD(tree, start+1));
1240        else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
1241            res = (validate_vfpdef(CHILD(tree, start+1))
1242                   && validate_comma(CHILD(tree, start+2))
1243                   && validate_doublestar(CHILD(tree, start+3))
1244                   && validate_vfpdef(CHILD(tree, start+4)));
1245        else {
1246            /* skip over vfpdef (',' vfpdef ['=' test])*  */
1247            i = start + 1;
1248	    if (TYPE(CHILD(tree, i)) == vfpdef ||
1249	        TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
1250		i += 1;
1251	    }
1252            while (res && i+1 < nch) { /* validate  (',' vfpdef ['=' test])* */
1253                res = validate_comma(CHILD(tree, i));
1254                if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
1255                    break;
1256                res = res && validate_vfpdef(CHILD(tree, i+1));
1257                if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
1258                    res = res && (i+3 < nch)
1259                          && validate_test(CHILD(tree, i+3));
1260                    i += 4;
1261                }
1262                else {
1263                    i += 2;
1264                }
1265            }
1266            /* [',' '**' vfpdef] */
1267            if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
1268                res = validate_vfpdef(CHILD(tree, i+2));
1269            }
1270        }
1271    }
1272    else if (sym == DOUBLESTAR) {
1273        /*
1274         *  '**' NAME
1275         */
1276        if (nch-start == 2)
1277            res = validate_vfpdef(CHILD(tree, start+1));
1278    }
1279    if (!res)
1280        err_string("illegal variable argument trailer for varargslist");
1281    return res;
1282}
1283
1284
1285/* validate_varargslist()
1286 *
1287 * Validate typedargslist or varargslist.
1288 *
1289 * typedargslist: ((tfpdef ['=' test] ',')*
1290 *                 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1291 *                  '**' tfpdef)
1292 *                 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
1293 * tfpdef: NAME [':' test]
1294 * varargslist: ((vfpdef ['=' test] ',')*
1295 *               ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] |
1296 *                '**' vfpdef)
1297 *               | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
1298 * vfpdef: NAME
1299 *
1300 */
1301static int
1302validate_varargslist(node *tree)
1303{
1304    int nch = NCH(tree);
1305    int res = (TYPE(tree) == varargslist ||
1306               TYPE(tree) == typedargslist) &&
1307              (nch != 0);
1308    int sym;
1309    node *ch;
1310    int i = 0;
1311
1312    if (!res)
1313        return 0;
1314    if (nch < 1) {
1315        err_string("varargslist missing child nodes");
1316        return 0;
1317    }
1318    while (i < nch) {
1319        ch = CHILD(tree, i);
1320        sym = TYPE(ch);
1321        if (sym == vfpdef || sym == tfpdef) {
1322            /* validate (vfpdef ['=' test] ',')+ */
1323            res = validate_vfpdef(ch);
1324            ++i;
1325            if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1326                res = (validate_equal(CHILD(tree, i))
1327                       && validate_test(CHILD(tree, i+1)));
1328                if (res)
1329                  i += 2;
1330            }
1331            if (res && i < nch) {
1332                res = validate_comma(CHILD(tree, i));
1333                ++i;
1334            }
1335        } else if (sym == DOUBLESTAR || sym == STAR) {
1336            res = validate_varargslist_trailer(tree, i);
1337            break;
1338        } else {
1339            res = 0;
1340            err_string("illegal formation for varargslist");
1341        }
1342    }
1343    return res;
1344}
1345
1346
1347/*  comp_iter:  comp_for | comp_if
1348 */
1349static int
1350validate_comp_iter(node *tree)
1351{
1352    int res = (validate_ntype(tree, comp_iter)
1353               && validate_numnodes(tree, 1, "comp_iter"));
1354    if (res && TYPE(CHILD(tree, 0)) == comp_for)
1355        res = validate_comp_for(CHILD(tree, 0));
1356    else
1357        res = validate_comp_if(CHILD(tree, 0));
1358
1359    return res;
1360}
1361
1362/*  comp_for:  'for' exprlist 'in' test [comp_iter]
1363 */
1364static int
1365validate_comp_for(node *tree)
1366{
1367    int nch = NCH(tree);
1368    int res;
1369
1370    if (nch == 5)
1371        res = validate_comp_iter(CHILD(tree, 4));
1372    else
1373        res = validate_numnodes(tree, 4, "comp_for");
1374
1375    if (res)
1376        res = (validate_name(CHILD(tree, 0), "for")
1377               && validate_exprlist(CHILD(tree, 1))
1378               && validate_name(CHILD(tree, 2), "in")
1379               && validate_or_test(CHILD(tree, 3)));
1380
1381    return res;
1382}
1383
1384/*  comp_if:  'if' test_nocond [comp_iter]
1385 */
1386static int
1387validate_comp_if(node *tree)
1388{
1389    int nch = NCH(tree);
1390    int res;
1391
1392    if (nch == 3)
1393        res = validate_comp_iter(CHILD(tree, 2));
1394    else
1395        res = validate_numnodes(tree, 2, "comp_if");
1396
1397    if (res)
1398        res = (validate_name(CHILD(tree, 0), "if")
1399               && validate_test_nocond(CHILD(tree, 1)));
1400
1401    return res;
1402}
1403
1404
1405/*  simple_stmt | compound_stmt
1406 *
1407 */
1408static int
1409validate_stmt(node *tree)
1410{
1411    int res = (validate_ntype(tree, stmt)
1412               && validate_numnodes(tree, 1, "stmt"));
1413
1414    if (res) {
1415        tree = CHILD(tree, 0);
1416
1417        if (TYPE(tree) == simple_stmt)
1418            res = validate_simple_stmt(tree);
1419        else
1420            res = validate_compound_stmt(tree);
1421    }
1422    return (res);
1423}
1424
1425
1426/*  small_stmt (';' small_stmt)* [';'] NEWLINE
1427 *
1428 */
1429static int
1430validate_simple_stmt(node *tree)
1431{
1432    int nch = NCH(tree);
1433    int res = (validate_ntype(tree, simple_stmt)
1434               && (nch >= 2)
1435               && validate_small_stmt(CHILD(tree, 0))
1436               && validate_newline(CHILD(tree, nch - 1)));
1437
1438    if (nch < 2)
1439        res = validate_numnodes(tree, 2, "simple_stmt");
1440    --nch;                              /* forget the NEWLINE    */
1441    if (res && is_even(nch))
1442        res = validate_semi(CHILD(tree, --nch));
1443    if (res && (nch > 2)) {
1444        int i;
1445
1446        for (i = 1; res && (i < nch); i += 2)
1447            res = (validate_semi(CHILD(tree, i))
1448                   && validate_small_stmt(CHILD(tree, i + 1)));
1449    }
1450    return (res);
1451}
1452
1453
1454static int
1455validate_small_stmt(node *tree)
1456{
1457    int nch = NCH(tree);
1458    int res = validate_numnodes(tree, 1, "small_stmt");
1459
1460    if (res) {
1461        int ntype = TYPE(CHILD(tree, 0));
1462
1463        if (  (ntype == expr_stmt)
1464              || (ntype == del_stmt)
1465              || (ntype == pass_stmt)
1466              || (ntype == flow_stmt)
1467              || (ntype == import_stmt)
1468              || (ntype == global_stmt)
1469              || (ntype == assert_stmt))
1470            res = validate_node(CHILD(tree, 0));
1471        else {
1472            res = 0;
1473            err_string("illegal small_stmt child type");
1474        }
1475    }
1476    else if (nch == 1) {
1477        res = 0;
1478        PyErr_Format(parser_error,
1479                     "Unrecognized child node of small_stmt: %d.",
1480                     TYPE(CHILD(tree, 0)));
1481    }
1482    return (res);
1483}
1484
1485
1486/*  compound_stmt:
1487 *      if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
1488 */
1489static int
1490validate_compound_stmt(node *tree)
1491{
1492    int res = (validate_ntype(tree, compound_stmt)
1493               && validate_numnodes(tree, 1, "compound_stmt"));
1494    int ntype;
1495
1496    if (!res)
1497        return (0);
1498
1499    tree = CHILD(tree, 0);
1500    ntype = TYPE(tree);
1501    if (  (ntype == if_stmt)
1502          || (ntype == while_stmt)
1503          || (ntype == for_stmt)
1504          || (ntype == try_stmt)
1505          || (ntype == with_stmt)
1506          || (ntype == funcdef)
1507          || (ntype == classdef)
1508          || (ntype == decorated))
1509        res = validate_node(tree);
1510    else {
1511        res = 0;
1512        PyErr_Format(parser_error,
1513                     "Illegal compound statement type: %d.", TYPE(tree));
1514    }
1515    return (res);
1516}
1517
1518static int
1519validate_yield_or_testlist(node *tree)
1520{
1521	if (TYPE(tree) == yield_expr)
1522		return validate_yield_expr(tree);
1523	else
1524		return validate_testlist(tree);
1525}
1526
1527static int
1528validate_expr_stmt(node *tree)
1529{
1530    int j;
1531    int nch = NCH(tree);
1532    int res = (validate_ntype(tree, expr_stmt)
1533               && is_odd(nch)
1534               && validate_testlist(CHILD(tree, 0)));
1535
1536    if (res && nch == 3
1537        && TYPE(CHILD(tree, 1)) == augassign) {
1538        res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1539		&& validate_yield_or_testlist(CHILD(tree, 2));
1540
1541        if (res) {
1542            char *s = STR(CHILD(CHILD(tree, 1), 0));
1543
1544            res = (strcmp(s, "+=") == 0
1545                   || strcmp(s, "-=") == 0
1546                   || strcmp(s, "*=") == 0
1547                   || strcmp(s, "/=") == 0
1548                   || strcmp(s, "//=") == 0
1549                   || strcmp(s, "%=") == 0
1550                   || strcmp(s, "&=") == 0
1551                   || strcmp(s, "|=") == 0
1552                   || strcmp(s, "^=") == 0
1553                   || strcmp(s, "<<=") == 0
1554                   || strcmp(s, ">>=") == 0
1555                   || strcmp(s, "**=") == 0);
1556            if (!res)
1557                err_string("illegal augmmented assignment operator");
1558        }
1559    }
1560    else {
1561        for (j = 1; res && (j < nch); j += 2)
1562            res = validate_equal(CHILD(tree, j))
1563                   && validate_yield_or_testlist(CHILD(tree, j + 1));
1564    }
1565    return (res);
1566}
1567
1568
1569static int
1570validate_del_stmt(node *tree)
1571{
1572    return (validate_numnodes(tree, 2, "del_stmt")
1573            && validate_name(CHILD(tree, 0), "del")
1574            && validate_exprlist(CHILD(tree, 1)));
1575}
1576
1577
1578static int
1579validate_return_stmt(node *tree)
1580{
1581    int nch = NCH(tree);
1582    int res = (validate_ntype(tree, return_stmt)
1583               && ((nch == 1) || (nch == 2))
1584               && validate_name(CHILD(tree, 0), "return"));
1585
1586    if (res && (nch == 2))
1587        res = validate_testlist(CHILD(tree, 1));
1588
1589    return (res);
1590}
1591
1592
1593static int
1594validate_raise_stmt(node *tree)
1595{
1596    int nch = NCH(tree);
1597    int res = (validate_ntype(tree, raise_stmt)
1598               && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
1599
1600    if (res) {
1601        res = validate_name(CHILD(tree, 0), "raise");
1602        if (res && (nch >= 2))
1603            res = validate_test(CHILD(tree, 1));
1604        if (res && nch > 2) {
1605            res = (validate_comma(CHILD(tree, 2))
1606                   && validate_test(CHILD(tree, 3)));
1607            if (res && (nch > 4))
1608                res = (validate_comma(CHILD(tree, 4))
1609                       && validate_test(CHILD(tree, 5)));
1610        }
1611    }
1612    else
1613        (void) validate_numnodes(tree, 2, "raise");
1614    if (res && (nch == 4))
1615        res = (validate_comma(CHILD(tree, 2))
1616               && validate_test(CHILD(tree, 3)));
1617
1618    return (res);
1619}
1620
1621
1622/* yield_expr: 'yield' [testlist]
1623 */
1624static int
1625validate_yield_expr(node *tree)
1626{
1627    int nch = NCH(tree);
1628    int res = (validate_ntype(tree, yield_expr)
1629               && ((nch == 1) || (nch == 2))
1630               && validate_name(CHILD(tree, 0), "yield"));
1631
1632    if (res && (nch == 2))
1633        res = validate_testlist(CHILD(tree, 1));
1634
1635    return (res);
1636}
1637
1638
1639/* yield_stmt: yield_expr
1640 */
1641static int
1642validate_yield_stmt(node *tree)
1643{
1644    return (validate_ntype(tree, yield_stmt)
1645            && validate_numnodes(tree, 1, "yield_stmt")
1646            && validate_yield_expr(CHILD(tree, 0)));
1647}
1648
1649
1650static int
1651validate_import_as_name(node *tree)
1652{
1653    int nch = NCH(tree);
1654    int ok = validate_ntype(tree, import_as_name);
1655
1656    if (ok) {
1657        if (nch == 1)
1658            ok = validate_name(CHILD(tree, 0), NULL);
1659        else if (nch == 3)
1660            ok = (validate_name(CHILD(tree, 0), NULL)
1661                  && validate_name(CHILD(tree, 1), "as")
1662                  && validate_name(CHILD(tree, 2), NULL));
1663        else
1664            ok = validate_numnodes(tree, 3, "import_as_name");
1665    }
1666    return ok;
1667}
1668
1669
1670/* dotted_name:  NAME ("." NAME)*
1671 */
1672static int
1673validate_dotted_name(node *tree)
1674{
1675    int nch = NCH(tree);
1676    int res = (validate_ntype(tree, dotted_name)
1677               && is_odd(nch)
1678               && validate_name(CHILD(tree, 0), NULL));
1679    int i;
1680
1681    for (i = 1; res && (i < nch); i += 2) {
1682        res = (validate_dot(CHILD(tree, i))
1683               && validate_name(CHILD(tree, i+1), NULL));
1684    }
1685    return res;
1686}
1687
1688
1689/* dotted_as_name:  dotted_name [NAME NAME]
1690 */
1691static int
1692validate_dotted_as_name(node *tree)
1693{
1694    int nch = NCH(tree);
1695    int res = validate_ntype(tree, dotted_as_name);
1696
1697    if (res) {
1698        if (nch == 1)
1699            res = validate_dotted_name(CHILD(tree, 0));
1700        else if (nch == 3)
1701            res = (validate_dotted_name(CHILD(tree, 0))
1702                   && validate_name(CHILD(tree, 1), "as")
1703                   && validate_name(CHILD(tree, 2), NULL));
1704        else {
1705            res = 0;
1706            err_string("illegal number of children for dotted_as_name");
1707        }
1708    }
1709    return res;
1710}
1711
1712
1713/* dotted_as_name (',' dotted_as_name)* */
1714static int
1715validate_dotted_as_names(node *tree)
1716{
1717	int nch = NCH(tree);
1718	int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1719	int i;
1720
1721	for (i = 1; res && (i < nch); i += 2)
1722	    res = (validate_comma(CHILD(tree, i))
1723		   && validate_dotted_as_name(CHILD(tree, i + 1)));
1724	return (res);
1725}
1726
1727
1728/* import_as_name (',' import_as_name)* [','] */
1729static int
1730validate_import_as_names(node *tree)
1731{
1732    int nch = NCH(tree);
1733    int res = validate_import_as_name(CHILD(tree, 0));
1734    int i;
1735
1736    for (i = 1; res && (i + 1 < nch); i += 2)
1737	res = (validate_comma(CHILD(tree, i))
1738	       && validate_import_as_name(CHILD(tree, i + 1)));
1739    return (res);
1740}
1741
1742
1743/* 'import' dotted_as_names */
1744static int
1745validate_import_name(node *tree)
1746{
1747	return (validate_ntype(tree, import_name)
1748		&& validate_numnodes(tree, 2, "import_name")
1749		&& validate_name(CHILD(tree, 0), "import")
1750		&& validate_dotted_as_names(CHILD(tree, 1)));
1751}
1752
1753/* Helper function to count the number of leading dots in
1754 * 'from ...module import name'
1755 */
1756static int
1757count_from_dots(node *tree)
1758{
1759        int i;
1760        for (i = 1; i < NCH(tree); i++)
1761		if (TYPE(CHILD(tree, i)) != DOT)
1762			break;
1763        return i-1;
1764}
1765
1766/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
1767 *     import_as_names
1768 */
1769static int
1770validate_import_from(node *tree)
1771{
1772	int nch = NCH(tree);
1773	int ndots = count_from_dots(tree);
1774	int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1775	int offset = ndots + havename;
1776	int res = validate_ntype(tree, import_from)
1777		&& (nch >= 4 + ndots)
1778		&& validate_name(CHILD(tree, 0), "from")
1779		&& (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1780		&& validate_name(CHILD(tree, offset + 1), "import");
1781
1782	if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1783	    res = ((nch == offset + 5)
1784		   && validate_lparen(CHILD(tree, offset + 2))
1785		   && validate_import_as_names(CHILD(tree, offset + 3))
1786		   && validate_rparen(CHILD(tree, offset + 4)));
1787	else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1788	    res = validate_import_as_names(CHILD(tree, offset + 2));
1789	return (res);
1790}
1791
1792
1793/* import_stmt: import_name | import_from */
1794static int
1795validate_import_stmt(node *tree)
1796{
1797    int nch = NCH(tree);
1798    int res = validate_numnodes(tree, 1, "import_stmt");
1799
1800    if (res) {
1801	int ntype = TYPE(CHILD(tree, 0));
1802
1803	if (ntype == import_name || ntype == import_from)
1804            res = validate_node(CHILD(tree, 0));
1805        else {
1806            res = 0;
1807            err_string("illegal import_stmt child type");
1808        }
1809    }
1810    else if (nch == 1) {
1811        res = 0;
1812        PyErr_Format(parser_error,
1813                     "Unrecognized child node of import_stmt: %d.",
1814                     TYPE(CHILD(tree, 0)));
1815    }
1816    return (res);
1817}
1818
1819
1820
1821
1822static int
1823validate_global_stmt(node *tree)
1824{
1825    int j;
1826    int nch = NCH(tree);
1827    int res = (validate_ntype(tree, global_stmt)
1828               && is_even(nch) && (nch >= 2));
1829
1830    if (!res && !PyErr_Occurred())
1831        err_string("illegal global statement");
1832
1833    if (res)
1834        res = (validate_name(CHILD(tree, 0), "global")
1835               && validate_ntype(CHILD(tree, 1), NAME));
1836    for (j = 2; res && (j < nch); j += 2)
1837        res = (validate_comma(CHILD(tree, j))
1838               && validate_ntype(CHILD(tree, j + 1), NAME));
1839
1840    return (res);
1841}
1842
1843
1844/*  assert_stmt:
1845 *
1846 *  'assert' test [',' test]
1847 */
1848static int
1849validate_assert_stmt(node *tree)
1850{
1851    int nch = NCH(tree);
1852    int res = (validate_ntype(tree, assert_stmt)
1853               && ((nch == 2) || (nch == 4))
1854               && (validate_name(CHILD(tree, 0), "assert"))
1855               && validate_test(CHILD(tree, 1)));
1856
1857    if (!res && !PyErr_Occurred())
1858        err_string("illegal assert statement");
1859    if (res && (nch > 2))
1860        res = (validate_comma(CHILD(tree, 2))
1861               && validate_test(CHILD(tree, 3)));
1862
1863    return (res);
1864}
1865
1866
1867static int
1868validate_while(node *tree)
1869{
1870    int nch = NCH(tree);
1871    int res = (validate_ntype(tree, while_stmt)
1872               && ((nch == 4) || (nch == 7))
1873               && validate_name(CHILD(tree, 0), "while")
1874               && validate_test(CHILD(tree, 1))
1875               && validate_colon(CHILD(tree, 2))
1876               && validate_suite(CHILD(tree, 3)));
1877
1878    if (res && (nch == 7))
1879        res = (validate_name(CHILD(tree, 4), "else")
1880               && validate_colon(CHILD(tree, 5))
1881               && validate_suite(CHILD(tree, 6)));
1882
1883    return (res);
1884}
1885
1886
1887static int
1888validate_for(node *tree)
1889{
1890    int nch = NCH(tree);
1891    int res = (validate_ntype(tree, for_stmt)
1892               && ((nch == 6) || (nch == 9))
1893               && validate_name(CHILD(tree, 0), "for")
1894               && validate_exprlist(CHILD(tree, 1))
1895               && validate_name(CHILD(tree, 2), "in")
1896               && validate_testlist(CHILD(tree, 3))
1897               && validate_colon(CHILD(tree, 4))
1898               && validate_suite(CHILD(tree, 5)));
1899
1900    if (res && (nch == 9))
1901        res = (validate_name(CHILD(tree, 6), "else")
1902               && validate_colon(CHILD(tree, 7))
1903               && validate_suite(CHILD(tree, 8)));
1904
1905    return (res);
1906}
1907
1908
1909/*  try_stmt:
1910 *      'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1911                                                   ['finally' ':' suite]
1912 *    | 'try' ':' suite 'finally' ':' suite
1913 *
1914 */
1915static int
1916validate_try(node *tree)
1917{
1918    int nch = NCH(tree);
1919    int pos = 3;
1920    int res = (validate_ntype(tree, try_stmt)
1921               && (nch >= 6) && ((nch % 3) == 0));
1922
1923    if (res)
1924        res = (validate_name(CHILD(tree, 0), "try")
1925               && validate_colon(CHILD(tree, 1))
1926               && validate_suite(CHILD(tree, 2))
1927               && validate_colon(CHILD(tree, nch - 2))
1928               && validate_suite(CHILD(tree, nch - 1)));
1929    else if (!PyErr_Occurred()) {
1930        const char* name = "except";
1931        if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1932            name = STR(CHILD(tree, nch - 3));
1933
1934        PyErr_Format(parser_error,
1935                     "Illegal number of children for try/%s node.", name);
1936    }
1937    /* Handle try/finally statement */
1938    if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
1939        (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
1940        res = (validate_numnodes(tree, 6, "try/finally")
1941               && validate_colon(CHILD(tree, 4))
1942               && validate_suite(CHILD(tree, 5)));
1943        return (res);
1944    }
1945    /* try/except statement: skip past except_clause sections */
1946    while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
1947        res = (validate_except_clause(CHILD(tree, pos))
1948               && validate_colon(CHILD(tree, pos + 1))
1949               && validate_suite(CHILD(tree, pos + 2)));
1950        pos += 3;
1951    }
1952    /* skip else clause */
1953    if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
1954        (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
1955        res = (validate_colon(CHILD(tree, pos + 1))
1956               && validate_suite(CHILD(tree, pos + 2)));
1957        pos += 3;
1958    }
1959    if (res && pos < nch) {
1960        /* last clause must be a finally */
1961        res = (validate_name(CHILD(tree, pos), "finally")
1962               && validate_numnodes(tree, pos + 3, "try/except/finally")
1963               && validate_colon(CHILD(tree, pos + 1))
1964               && validate_suite(CHILD(tree, pos + 2)));
1965    }
1966    return (res);
1967}
1968
1969
1970static int
1971validate_except_clause(node *tree)
1972{
1973    int nch = NCH(tree);
1974    int res = (validate_ntype(tree, except_clause)
1975               && ((nch == 1) || (nch == 2) || (nch == 4))
1976               && validate_name(CHILD(tree, 0), "except"));
1977
1978    if (res && (nch > 1))
1979        res = validate_test(CHILD(tree, 1));
1980    if (res && (nch == 4))
1981        res = (validate_name(CHILD(tree, 2), "as")
1982               && validate_ntype(CHILD(tree, 3), NAME));
1983
1984    return (res);
1985}
1986
1987
1988static int
1989validate_test(node *tree)
1990{
1991    int nch = NCH(tree);
1992    int res = validate_ntype(tree, test) && is_odd(nch);
1993
1994    if (res && (TYPE(CHILD(tree, 0)) == lambdef))
1995        res = ((nch == 1)
1996               && validate_lambdef(CHILD(tree, 0)));
1997    else if (res) {
1998        res = validate_or_test(CHILD(tree, 0));
1999        res = (res && (nch == 1 || (nch == 5 &&
2000            validate_name(CHILD(tree, 1), "if") &&
2001            validate_or_test(CHILD(tree, 2)) &&
2002            validate_name(CHILD(tree, 3), "else") &&
2003            validate_test(CHILD(tree, 4)))));
2004    }
2005    return (res);
2006}
2007
2008static int
2009validate_test_nocond(node *tree)
2010{
2011    int nch = NCH(tree);
2012    int res = validate_ntype(tree, test_nocond) && (nch == 1);
2013
2014    if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2015        res = (validate_lambdef_nocond(CHILD(tree, 0)));
2016    else if (res) {
2017        res = (validate_or_test(CHILD(tree, 0)));
2018    }
2019    return (res);
2020}
2021
2022static int
2023validate_or_test(node *tree)
2024{
2025    int nch = NCH(tree);
2026    int res = validate_ntype(tree, or_test) && is_odd(nch);
2027
2028    if (res) {
2029        int pos;
2030        res = validate_and_test(CHILD(tree, 0));
2031        for (pos = 1; res && (pos < nch); pos += 2)
2032            res = (validate_name(CHILD(tree, pos), "or")
2033                   && validate_and_test(CHILD(tree, pos + 1)));
2034    }
2035    return (res);
2036}
2037
2038
2039static int
2040validate_and_test(node *tree)
2041{
2042    int pos;
2043    int nch = NCH(tree);
2044    int res = (validate_ntype(tree, and_test)
2045               && is_odd(nch)
2046               && validate_not_test(CHILD(tree, 0)));
2047
2048    for (pos = 1; res && (pos < nch); pos += 2)
2049        res = (validate_name(CHILD(tree, pos), "and")
2050               && validate_not_test(CHILD(tree, 0)));
2051
2052    return (res);
2053}
2054
2055
2056static int
2057validate_not_test(node *tree)
2058{
2059    int nch = NCH(tree);
2060    int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
2061
2062    if (res) {
2063        if (nch == 2)
2064            res = (validate_name(CHILD(tree, 0), "not")
2065                   && validate_not_test(CHILD(tree, 1)));
2066        else if (nch == 1)
2067            res = validate_comparison(CHILD(tree, 0));
2068    }
2069    return (res);
2070}
2071
2072
2073static int
2074validate_comparison(node *tree)
2075{
2076    int pos;
2077    int nch = NCH(tree);
2078    int res = (validate_ntype(tree, comparison)
2079               && is_odd(nch)
2080               && validate_star_expr(CHILD(tree, 0)));
2081
2082    for (pos = 1; res && (pos < nch); pos += 2)
2083        res = (validate_comp_op(CHILD(tree, pos))
2084               && validate_star_expr(CHILD(tree, pos + 1)));
2085
2086    return (res);
2087}
2088
2089
2090static int
2091validate_comp_op(node *tree)
2092{
2093    int res = 0;
2094    int nch = NCH(tree);
2095
2096    if (!validate_ntype(tree, comp_op))
2097        return (0);
2098    if (nch == 1) {
2099        /*
2100         *  Only child will be a terminal with a well-defined symbolic name
2101         *  or a NAME with a string of either 'is' or 'in'
2102         */
2103        tree = CHILD(tree, 0);
2104        switch (TYPE(tree)) {
2105            case LESS:
2106            case GREATER:
2107            case EQEQUAL:
2108            case EQUAL:
2109            case LESSEQUAL:
2110            case GREATEREQUAL:
2111            case NOTEQUAL:
2112              res = 1;
2113              break;
2114            case NAME:
2115              res = ((strcmp(STR(tree), "in") == 0)
2116                     || (strcmp(STR(tree), "is") == 0));
2117              if (!res) {
2118                  PyErr_Format(parser_error,
2119                               "illegal operator '%s'", STR(tree));
2120              }
2121              break;
2122          default:
2123              err_string("illegal comparison operator type");
2124              break;
2125        }
2126    }
2127    else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
2128        res = (validate_ntype(CHILD(tree, 0), NAME)
2129               && validate_ntype(CHILD(tree, 1), NAME)
2130               && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2131                    && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2132                   || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2133                       && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2134        if (!res && !PyErr_Occurred())
2135            err_string("unknown comparison operator");
2136    }
2137    return (res);
2138}
2139
2140
2141static int
2142validate_star_expr(node *tree)
2143{
2144    int res = validate_ntype(tree, star_expr);
2145    if (!res) return res;
2146    if (NCH(tree) == 2) {
2147        return validate_ntype(CHILD(tree, 0), STAR) && \
2148               validate_expr(CHILD(tree, 1));
2149    } else {
2150        return validate_expr(CHILD(tree, 0));
2151    }
2152}
2153
2154
2155static int
2156validate_expr(node *tree)
2157{
2158    int j;
2159    int nch = NCH(tree);
2160    int res = (validate_ntype(tree, expr)
2161               && is_odd(nch)
2162               && validate_xor_expr(CHILD(tree, 0)));
2163
2164    for (j = 2; res && (j < nch); j += 2)
2165        res = (validate_xor_expr(CHILD(tree, j))
2166               && validate_vbar(CHILD(tree, j - 1)));
2167
2168    return (res);
2169}
2170
2171
2172static int
2173validate_xor_expr(node *tree)
2174{
2175    int j;
2176    int nch = NCH(tree);
2177    int res = (validate_ntype(tree, xor_expr)
2178               && is_odd(nch)
2179               && validate_and_expr(CHILD(tree, 0)));
2180
2181    for (j = 2; res && (j < nch); j += 2)
2182        res = (validate_circumflex(CHILD(tree, j - 1))
2183               && validate_and_expr(CHILD(tree, j)));
2184
2185    return (res);
2186}
2187
2188
2189static int
2190validate_and_expr(node *tree)
2191{
2192    int pos;
2193    int nch = NCH(tree);
2194    int res = (validate_ntype(tree, and_expr)
2195               && is_odd(nch)
2196               && validate_shift_expr(CHILD(tree, 0)));
2197
2198    for (pos = 1; res && (pos < nch); pos += 2)
2199        res = (validate_ampersand(CHILD(tree, pos))
2200               && validate_shift_expr(CHILD(tree, pos + 1)));
2201
2202    return (res);
2203}
2204
2205
2206static int
2207validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
2208 {
2209    int pos = 1;
2210    int nch = NCH(tree);
2211    int res = (is_odd(nch)
2212               && (*termvalid)(CHILD(tree, 0)));
2213
2214    for ( ; res && (pos < nch); pos += 2) {
2215        if (TYPE(CHILD(tree, pos)) != op1)
2216            res = validate_ntype(CHILD(tree, pos), op2);
2217        if (res)
2218            res = (*termvalid)(CHILD(tree, pos + 1));
2219    }
2220    return (res);
2221}
2222
2223
2224static int
2225validate_shift_expr(node *tree)
2226{
2227    return (validate_ntype(tree, shift_expr)
2228            && validate_chain_two_ops(tree, validate_arith_expr,
2229                                      LEFTSHIFT, RIGHTSHIFT));
2230}
2231
2232
2233static int
2234validate_arith_expr(node *tree)
2235{
2236    return (validate_ntype(tree, arith_expr)
2237            && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2238}
2239
2240
2241static int
2242validate_term(node *tree)
2243{
2244    int pos = 1;
2245    int nch = NCH(tree);
2246    int res = (validate_ntype(tree, term)
2247               && is_odd(nch)
2248               && validate_factor(CHILD(tree, 0)));
2249
2250    for ( ; res && (pos < nch); pos += 2)
2251        res = (((TYPE(CHILD(tree, pos)) == STAR)
2252               || (TYPE(CHILD(tree, pos)) == SLASH)
2253               || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
2254               || (TYPE(CHILD(tree, pos)) == PERCENT))
2255               && validate_factor(CHILD(tree, pos + 1)));
2256
2257    return (res);
2258}
2259
2260
2261/*  factor:
2262 *
2263 *  factor: ('+'|'-'|'~') factor | power
2264 */
2265static int
2266validate_factor(node *tree)
2267{
2268    int nch = NCH(tree);
2269    int res = (validate_ntype(tree, factor)
2270               && (((nch == 2)
2271                    && ((TYPE(CHILD(tree, 0)) == PLUS)
2272                        || (TYPE(CHILD(tree, 0)) == MINUS)
2273                        || (TYPE(CHILD(tree, 0)) == TILDE))
2274                    && validate_factor(CHILD(tree, 1)))
2275                   || ((nch == 1)
2276                       && validate_power(CHILD(tree, 0)))));
2277    return (res);
2278}
2279
2280
2281/*  power:
2282 *
2283 *  power: atom trailer* ('**' factor)*
2284 */
2285static int
2286validate_power(node *tree)
2287{
2288    int pos = 1;
2289    int nch = NCH(tree);
2290    int res = (validate_ntype(tree, power) && (nch >= 1)
2291               && validate_atom(CHILD(tree, 0)));
2292
2293    while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
2294        res = validate_trailer(CHILD(tree, pos++));
2295    if (res && (pos < nch)) {
2296        if (!is_even(nch - pos)) {
2297            err_string("illegal number of nodes for 'power'");
2298            return (0);
2299        }
2300        for ( ; res && (pos < (nch - 1)); pos += 2)
2301            res = (validate_doublestar(CHILD(tree, pos))
2302                   && validate_factor(CHILD(tree, pos + 1)));
2303    }
2304    return (res);
2305}
2306
2307
2308static int
2309validate_atom(node *tree)
2310{
2311    int pos;
2312    int nch = NCH(tree);
2313    int res = validate_ntype(tree, atom);
2314
2315    if (res && nch < 1)
2316        res = validate_numnodes(tree, nch+1, "atom");
2317    if (res) {
2318        switch (TYPE(CHILD(tree, 0))) {
2319          case LPAR:
2320            res = ((nch <= 3)
2321                   && (validate_rparen(CHILD(tree, nch - 1))));
2322
2323            if (res && (nch == 3)) {
2324		if (TYPE(CHILD(tree, 1))==yield_expr)
2325			res = validate_yield_expr(CHILD(tree, 1));
2326		else
2327                	res = validate_testlist_comp(CHILD(tree, 1));
2328	    }
2329            break;
2330          case LSQB:
2331            if (nch == 2)
2332                res = validate_ntype(CHILD(tree, 1), RSQB);
2333            else if (nch == 3)
2334                res = (validate_testlist_comp(CHILD(tree, 1))
2335                       && validate_ntype(CHILD(tree, 2), RSQB));
2336            else {
2337                res = 0;
2338                err_string("illegal list display atom");
2339            }
2340            break;
2341          case LBRACE:
2342            res = ((nch <= 3)
2343                   && validate_ntype(CHILD(tree, nch - 1), RBRACE));
2344
2345            if (res && (nch == 3))
2346                res = validate_dictorsetmaker(CHILD(tree, 1));
2347            break;
2348          case NAME:
2349          case NUMBER:
2350            res = (nch == 1);
2351            break;
2352          case STRING:
2353            for (pos = 1; res && (pos < nch); ++pos)
2354                res = validate_ntype(CHILD(tree, pos), STRING);
2355            break;
2356	  case DOT:
2357	    res = (nch == 3 &&
2358	           validate_ntype(CHILD(tree, 1), DOT) &&
2359		   validate_ntype(CHILD(tree, 2), DOT));
2360	    break;
2361          default:
2362            res = 0;
2363            break;
2364        }
2365    }
2366    return (res);
2367}
2368
2369
2370/*  testlist_comp:
2371 *    test ( comp_for | (',' test)* [','] )
2372 */
2373static int
2374validate_testlist_comp(node *tree)
2375{
2376    int nch = NCH(tree);
2377    int ok = nch;
2378
2379    if (nch == 0)
2380        err_string("missing child nodes of testlist_comp");
2381    else {
2382        ok = validate_test(CHILD(tree, 0));
2383    }
2384
2385    /*
2386     *  comp_for | (',' test)* [',']
2387     */
2388    if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2389        ok = validate_comp_for(CHILD(tree, 1));
2390    else {
2391        /*  (',' test)* [',']  */
2392        int i = 1;
2393        while (ok && nch - i >= 2) {
2394            ok = (validate_comma(CHILD(tree, i))
2395                  && validate_test(CHILD(tree, i+1)));
2396            i += 2;
2397        }
2398        if (ok && i == nch-1)
2399            ok = validate_comma(CHILD(tree, i));
2400        else if (i != nch) {
2401            ok = 0;
2402            err_string("illegal trailing nodes for testlist_comp");
2403        }
2404    }
2405    return ok;
2406}
2407
2408/*  decorator:
2409 *    '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2410 */
2411static int
2412validate_decorator(node *tree)
2413{
2414    int ok;
2415    int nch = NCH(tree);
2416    ok = (validate_ntype(tree, decorator) &&
2417	  (nch == 3 || nch == 5 || nch == 6) &&
2418	  validate_at(CHILD(tree, 0)) &&
2419	  validate_dotted_name(CHILD(tree, 1)) &&
2420	  validate_newline(RCHILD(tree, -1)));
2421
2422    if (ok && nch != 3) {
2423	ok = (validate_lparen(CHILD(tree, 2)) &&
2424	      validate_rparen(RCHILD(tree, -2)));
2425
2426	if (ok && nch == 6)
2427	    ok = validate_arglist(CHILD(tree, 3));
2428    }
2429
2430    return ok;
2431}
2432
2433/*  decorators:
2434 *    decorator+
2435 */
2436static int
2437validate_decorators(node *tree)
2438{
2439    int i, nch, ok;
2440    nch = NCH(tree);
2441    ok = validate_ntype(tree, decorators) && nch >= 1;
2442
2443    for (i = 0; ok && i < nch; ++i)
2444	ok = validate_decorator(CHILD(tree, i));
2445
2446    return ok;
2447}
2448
2449/*  with_item:
2450 *   test ['as' expr]
2451 */
2452static int
2453validate_with_item(node *tree)
2454{
2455    int nch = NCH(tree);
2456    int ok = (validate_ntype(tree, with_item)
2457              && (nch == 1 || nch == 3)
2458              && validate_test(CHILD(tree, 0)));
2459    if (ok && nch == 3)
2460        ok = (validate_name(CHILD(tree, 1), "as")
2461              && validate_expr(CHILD(tree, 2)));
2462    return ok;
2463}
2464
2465/*  with_stmt:
2466 *    0      1          ...             -2   -1
2467 *   'with' with_item (',' with_item)* ':' suite
2468 */
2469static int
2470validate_with_stmt(node *tree)
2471{
2472    int i;
2473    int nch = NCH(tree);
2474    int ok = (validate_ntype(tree, with_stmt)
2475        && (nch % 2 == 0)
2476        && validate_name(CHILD(tree, 0), "with")
2477        && validate_colon(RCHILD(tree, -2))
2478        && validate_suite(RCHILD(tree, -1)));
2479    for (i = 1; ok && i < nch - 2; i += 2)
2480        ok = validate_with_item(CHILD(tree, i));
2481    return ok;
2482}
2483
2484/*  funcdef:
2485 *
2486 *     -5   -4         -3  -2    -1
2487 *  'def' NAME parameters ':' suite
2488 */
2489static int
2490validate_funcdef(node *tree)
2491{
2492    int nch = NCH(tree);
2493    int ok = (validate_ntype(tree, funcdef)
2494	       && (nch == 5)
2495	       && validate_name(RCHILD(tree, -5), "def")
2496	       && validate_ntype(RCHILD(tree, -4), NAME)
2497	       && validate_colon(RCHILD(tree, -2))
2498	       && validate_parameters(RCHILD(tree, -3))
2499	       && validate_suite(RCHILD(tree, -1)));
2500    return ok;
2501}
2502
2503
2504/* decorated
2505 *   decorators (classdef | funcdef)
2506 */
2507static int
2508validate_decorated(node *tree)
2509{
2510  int nch = NCH(tree);
2511  int ok = (validate_ntype(tree, decorated)
2512	    && (nch == 2)
2513	    && validate_decorators(RCHILD(tree, -2))
2514	    && (validate_funcdef(RCHILD(tree, -1))
2515		|| validate_class(RCHILD(tree, -1)))
2516	    );
2517  return ok;
2518}
2519
2520static int
2521validate_lambdef(node *tree)
2522{
2523    int nch = NCH(tree);
2524    int res = (validate_ntype(tree, lambdef)
2525               && ((nch == 3) || (nch == 4))
2526               && validate_name(CHILD(tree, 0), "lambda")
2527               && validate_colon(CHILD(tree, nch - 2))
2528               && validate_test(CHILD(tree, nch - 1)));
2529
2530    if (res && (nch == 4))
2531        res = validate_varargslist(CHILD(tree, 1));
2532    else if (!res && !PyErr_Occurred())
2533        (void) validate_numnodes(tree, 3, "lambdef");
2534
2535    return (res);
2536}
2537
2538
2539static int
2540validate_lambdef_nocond(node *tree)
2541{
2542    int nch = NCH(tree);
2543    int res = (validate_ntype(tree, lambdef_nocond)
2544               && ((nch == 3) || (nch == 4))
2545               && validate_name(CHILD(tree, 0), "lambda")
2546               && validate_colon(CHILD(tree, nch - 2))
2547               && validate_test(CHILD(tree, nch - 1)));
2548
2549    if (res && (nch == 4))
2550        res = validate_varargslist(CHILD(tree, 1));
2551    else if (!res && !PyErr_Occurred())
2552        (void) validate_numnodes(tree, 3, "lambdef_nocond");
2553
2554    return (res);
2555}
2556
2557
2558/*  arglist:
2559 *
2560 *  (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2561 */
2562static int
2563validate_arglist(node *tree)
2564{
2565    int nch = NCH(tree);
2566    int i = 0;
2567    int ok = 1;
2568
2569    if (nch <= 0)
2570        /* raise the right error from having an invalid number of children */
2571        return validate_numnodes(tree, nch + 1, "arglist");
2572
2573    if (nch > 1) {
2574        for (i=0; i<nch; i++) {
2575            if (TYPE(CHILD(tree, i)) == argument) {
2576                node *ch = CHILD(tree, i);
2577                if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
2578                    err_string("need '(', ')' for generator expression");
2579                    return 0;
2580                }
2581            }
2582        }
2583    }
2584
2585    while (ok && nch-i >= 2) {
2586        /* skip leading (argument ',') */
2587        ok = (validate_argument(CHILD(tree, i))
2588              && validate_comma(CHILD(tree, i+1)));
2589        if (ok)
2590            i += 2;
2591        else
2592            PyErr_Clear();
2593    }
2594    ok = 1;
2595    if (nch-i > 0) {
2596        /*
2597         * argument | '*' test [',' '**' test] | '**' test
2598         */
2599        int sym = TYPE(CHILD(tree, i));
2600
2601        if (sym == argument) {
2602            ok = validate_argument(CHILD(tree, i));
2603            if (ok && i+1 != nch) {
2604                err_string("illegal arglist specification"
2605                           " (extra stuff on end)");
2606                ok = 0;
2607            }
2608        }
2609        else if (sym == STAR) {
2610            ok = validate_star(CHILD(tree, i));
2611            if (ok && (nch-i == 2))
2612                ok = validate_test(CHILD(tree, i+1));
2613            else if (ok && (nch-i == 5))
2614                ok = (validate_test(CHILD(tree, i+1))
2615                      && validate_comma(CHILD(tree, i+2))
2616                      && validate_doublestar(CHILD(tree, i+3))
2617                      && validate_test(CHILD(tree, i+4)));
2618            else {
2619                err_string("illegal use of '*' in arglist");
2620                ok = 0;
2621            }
2622        }
2623        else if (sym == DOUBLESTAR) {
2624            if (nch-i == 2)
2625                ok = (validate_doublestar(CHILD(tree, i))
2626                      && validate_test(CHILD(tree, i+1)));
2627            else {
2628                err_string("illegal use of '**' in arglist");
2629                ok = 0;
2630            }
2631        }
2632        else {
2633            err_string("illegal arglist specification");
2634            ok = 0;
2635        }
2636    }
2637    return (ok);
2638}
2639
2640
2641
2642/*  argument:
2643 *
2644 *  [test '='] test [comp_for]
2645 */
2646static int
2647validate_argument(node *tree)
2648{
2649    int nch = NCH(tree);
2650    int res = (validate_ntype(tree, argument)
2651               && ((nch == 1) || (nch == 2) || (nch == 3))
2652               && validate_test(CHILD(tree, 0)));
2653
2654    if (res && (nch == 2))
2655        res = validate_comp_for(CHILD(tree, 1));
2656    else if (res && (nch == 3))
2657        res = (validate_equal(CHILD(tree, 1))
2658               && validate_test(CHILD(tree, 2)));
2659
2660    return (res);
2661}
2662
2663
2664
2665/*  trailer:
2666 *
2667 *  '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2668 */
2669static int
2670validate_trailer(node *tree)
2671{
2672    int nch = NCH(tree);
2673    int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
2674
2675    if (res) {
2676        switch (TYPE(CHILD(tree, 0))) {
2677          case LPAR:
2678            res = validate_rparen(CHILD(tree, nch - 1));
2679            if (res && (nch == 3))
2680                res = validate_arglist(CHILD(tree, 1));
2681            break;
2682          case LSQB:
2683            res = (validate_numnodes(tree, 3, "trailer")
2684                   && validate_subscriptlist(CHILD(tree, 1))
2685                   && validate_ntype(CHILD(tree, 2), RSQB));
2686            break;
2687          case DOT:
2688            res = (validate_numnodes(tree, 2, "trailer")
2689                   && validate_ntype(CHILD(tree, 1), NAME));
2690            break;
2691          default:
2692            res = 0;
2693            break;
2694        }
2695    }
2696    else {
2697        (void) validate_numnodes(tree, 2, "trailer");
2698    }
2699    return (res);
2700}
2701
2702
2703/*  subscriptlist:
2704 *
2705 *  subscript (',' subscript)* [',']
2706 */
2707static int
2708validate_subscriptlist(node *tree)
2709{
2710    return (validate_repeating_list(tree, subscriptlist,
2711                                    validate_subscript, "subscriptlist"));
2712}
2713
2714
2715/*  subscript:
2716 *
2717 *  '.' '.' '.' | test | [test] ':' [test] [sliceop]
2718 */
2719static int
2720validate_subscript(node *tree)
2721{
2722    int offset = 0;
2723    int nch = NCH(tree);
2724    int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
2725
2726    if (!res) {
2727        if (!PyErr_Occurred())
2728            err_string("invalid number of arguments for subscript node");
2729        return (0);
2730    }
2731    if (TYPE(CHILD(tree, 0)) == DOT)
2732        /* take care of ('.' '.' '.') possibility */
2733        return (validate_numnodes(tree, 3, "subscript")
2734                && validate_dot(CHILD(tree, 0))
2735                && validate_dot(CHILD(tree, 1))
2736                && validate_dot(CHILD(tree, 2)));
2737    if (nch == 1) {
2738        if (TYPE(CHILD(tree, 0)) == test)
2739            res = validate_test(CHILD(tree, 0));
2740        else
2741            res = validate_colon(CHILD(tree, 0));
2742        return (res);
2743    }
2744    /*  Must be [test] ':' [test] [sliceop],
2745     *  but at least one of the optional components will
2746     *  be present, but we don't know which yet.
2747     */
2748    if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
2749        res = validate_test(CHILD(tree, 0));
2750        offset = 1;
2751    }
2752    if (res)
2753        res = validate_colon(CHILD(tree, offset));
2754    if (res) {
2755        int rem = nch - ++offset;
2756        if (rem) {
2757            if (TYPE(CHILD(tree, offset)) == test) {
2758                res = validate_test(CHILD(tree, offset));
2759                ++offset;
2760                --rem;
2761            }
2762            if (res && rem)
2763                res = validate_sliceop(CHILD(tree, offset));
2764        }
2765    }
2766    return (res);
2767}
2768
2769
2770static int
2771validate_sliceop(node *tree)
2772{
2773    int nch = NCH(tree);
2774    int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
2775              && validate_ntype(tree, sliceop);
2776    if (!res && !PyErr_Occurred()) {
2777        res = validate_numnodes(tree, 1, "sliceop");
2778    }
2779    if (res)
2780        res = validate_colon(CHILD(tree, 0));
2781    if (res && (nch == 2))
2782        res = validate_test(CHILD(tree, 1));
2783
2784    return (res);
2785}
2786
2787
2788static int
2789validate_exprlist(node *tree)
2790{
2791    return (validate_repeating_list(tree, exprlist,
2792                                    validate_star_expr, "exprlist"));
2793}
2794
2795
2796static int
2797validate_dictorsetmaker(node *tree)
2798{
2799    int nch = NCH(tree);
2800    int res = (validate_ntype(tree, dictorsetmaker)
2801               && (nch >= 3)
2802               && validate_test(CHILD(tree, 0))
2803               && validate_colon(CHILD(tree, 1))
2804               && validate_test(CHILD(tree, 2)));
2805
2806    if (res && ((nch % 4) == 0))
2807        res = validate_comma(CHILD(tree, --nch));
2808    else if (res)
2809        res = ((nch % 4) == 3);
2810
2811    if (res && (nch > 3)) {
2812        int pos = 3;
2813        /*  ( ',' test ':' test )*  */
2814        while (res && (pos < nch)) {
2815            res = (validate_comma(CHILD(tree, pos))
2816                   && validate_test(CHILD(tree, pos + 1))
2817                   && validate_colon(CHILD(tree, pos + 2))
2818                   && validate_test(CHILD(tree, pos + 3)));
2819            pos += 4;
2820        }
2821    }
2822    return (res);
2823}
2824
2825
2826static int
2827validate_eval_input(node *tree)
2828{
2829    int pos;
2830    int nch = NCH(tree);
2831    int res = (validate_ntype(tree, eval_input)
2832               && (nch >= 2)
2833               && validate_testlist(CHILD(tree, 0))
2834               && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
2835
2836    for (pos = 1; res && (pos < (nch - 1)); ++pos)
2837        res = validate_ntype(CHILD(tree, pos), NEWLINE);
2838
2839    return (res);
2840}
2841
2842
2843static int
2844validate_node(node *tree)
2845{
2846    int   nch  = 0;                     /* num. children on current node  */
2847    int   res  = 1;                     /* result value                   */
2848    node* next = 0;                     /* node to process after this one */
2849
2850    while (res && (tree != 0)) {
2851        nch  = NCH(tree);
2852        next = 0;
2853        switch (TYPE(tree)) {
2854            /*
2855             *  Definition nodes.
2856             */
2857          case funcdef:
2858            res = validate_funcdef(tree);
2859            break;
2860          case with_stmt:
2861            res = validate_with_stmt(tree);
2862            break;
2863          case classdef:
2864            res = validate_class(tree);
2865            break;
2866	  case decorated:
2867	    res = validate_decorated(tree);
2868	    break;
2869            /*
2870             *  "Trivial" parse tree nodes.
2871             *  (Why did I call these trivial?)
2872             */
2873          case stmt:
2874            res = validate_stmt(tree);
2875            break;
2876          case small_stmt:
2877            /*
2878             *  expr_stmt | del_stmt | pass_stmt | flow_stmt
2879             *  | import_stmt | global_stmt | assert_stmt
2880             */
2881            res = validate_small_stmt(tree);
2882            break;
2883          case flow_stmt:
2884            res  = (validate_numnodes(tree, 1, "flow_stmt")
2885                    && ((TYPE(CHILD(tree, 0)) == break_stmt)
2886                        || (TYPE(CHILD(tree, 0)) == continue_stmt)
2887                        || (TYPE(CHILD(tree, 0)) == yield_stmt)
2888                        || (TYPE(CHILD(tree, 0)) == return_stmt)
2889                        || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2890            if (res)
2891                next = CHILD(tree, 0);
2892            else if (nch == 1)
2893                err_string("illegal flow_stmt type");
2894            break;
2895          case yield_stmt:
2896            res = validate_yield_stmt(tree);
2897            break;
2898            /*
2899             *  Compound statements.
2900             */
2901          case simple_stmt:
2902            res = validate_simple_stmt(tree);
2903            break;
2904          case compound_stmt:
2905            res = validate_compound_stmt(tree);
2906            break;
2907            /*
2908             *  Fundamental statements.
2909             */
2910          case expr_stmt:
2911            res = validate_expr_stmt(tree);
2912            break;
2913          case del_stmt:
2914            res = validate_del_stmt(tree);
2915            break;
2916          case pass_stmt:
2917            res = (validate_numnodes(tree, 1, "pass")
2918                   && validate_name(CHILD(tree, 0), "pass"));
2919            break;
2920          case break_stmt:
2921            res = (validate_numnodes(tree, 1, "break")
2922                   && validate_name(CHILD(tree, 0), "break"));
2923            break;
2924          case continue_stmt:
2925            res = (validate_numnodes(tree, 1, "continue")
2926                   && validate_name(CHILD(tree, 0), "continue"));
2927            break;
2928          case return_stmt:
2929            res = validate_return_stmt(tree);
2930            break;
2931          case raise_stmt:
2932            res = validate_raise_stmt(tree);
2933            break;
2934          case import_stmt:
2935            res = validate_import_stmt(tree);
2936            break;
2937	  case import_name:
2938	    res = validate_import_name(tree);
2939	    break;
2940	  case import_from:
2941	    res = validate_import_from(tree);
2942	    break;
2943          case global_stmt:
2944            res = validate_global_stmt(tree);
2945            break;
2946          case assert_stmt:
2947            res = validate_assert_stmt(tree);
2948            break;
2949          case if_stmt:
2950            res = validate_if(tree);
2951            break;
2952          case while_stmt:
2953            res = validate_while(tree);
2954            break;
2955          case for_stmt:
2956            res = validate_for(tree);
2957            break;
2958          case try_stmt:
2959            res = validate_try(tree);
2960            break;
2961          case suite:
2962            res = validate_suite(tree);
2963            break;
2964            /*
2965             *  Expression nodes.
2966             */
2967          case testlist:
2968            res = validate_testlist(tree);
2969            break;
2970          case yield_expr:
2971            res = validate_yield_expr(tree);
2972            break;
2973          case testlist1:
2974            res = validate_testlist1(tree);
2975            break;
2976          case test:
2977            res = validate_test(tree);
2978            break;
2979          case and_test:
2980            res = validate_and_test(tree);
2981            break;
2982          case not_test:
2983            res = validate_not_test(tree);
2984            break;
2985          case comparison:
2986            res = validate_comparison(tree);
2987            break;
2988          case exprlist:
2989            res = validate_exprlist(tree);
2990            break;
2991          case comp_op:
2992            res = validate_comp_op(tree);
2993            break;
2994          case expr:
2995            res = validate_expr(tree);
2996            break;
2997          case xor_expr:
2998            res = validate_xor_expr(tree);
2999            break;
3000          case and_expr:
3001            res = validate_and_expr(tree);
3002            break;
3003          case shift_expr:
3004            res = validate_shift_expr(tree);
3005            break;
3006          case arith_expr:
3007            res = validate_arith_expr(tree);
3008            break;
3009          case term:
3010            res = validate_term(tree);
3011            break;
3012          case factor:
3013            res = validate_factor(tree);
3014            break;
3015          case power:
3016            res = validate_power(tree);
3017            break;
3018          case atom:
3019            res = validate_atom(tree);
3020            break;
3021
3022          default:
3023            /* Hopefully never reached! */
3024            err_string("unrecognized node type");
3025            res = 0;
3026            break;
3027        }
3028        tree = next;
3029    }
3030    return (res);
3031}
3032
3033
3034static int
3035validate_expr_tree(node *tree)
3036{
3037    int res = validate_eval_input(tree);
3038
3039    if (!res && !PyErr_Occurred())
3040        err_string("could not validate expression tuple");
3041
3042    return (res);
3043}
3044
3045
3046/*  file_input:
3047 *      (NEWLINE | stmt)* ENDMARKER
3048 */
3049static int
3050validate_file_input(node *tree)
3051{
3052    int j;
3053    int nch = NCH(tree) - 1;
3054    int res = ((nch >= 0)
3055               && validate_ntype(CHILD(tree, nch), ENDMARKER));
3056
3057    for (j = 0; res && (j < nch); ++j) {
3058        if (TYPE(CHILD(tree, j)) == stmt)
3059            res = validate_stmt(CHILD(tree, j));
3060        else
3061            res = validate_newline(CHILD(tree, j));
3062    }
3063    /*  This stays in to prevent any internal failures from getting to the
3064     *  user.  Hopefully, this won't be needed.  If a user reports getting
3065     *  this, we have some debugging to do.
3066     */
3067    if (!res && !PyErr_Occurred())
3068        err_string("VALIDATION FAILURE: report this to the maintainer!");
3069
3070    return (res);
3071}
3072
3073static int
3074validate_encoding_decl(node *tree)
3075{
3076    int nch = NCH(tree);
3077    int res = ((nch == 1)
3078        && validate_file_input(CHILD(tree, 0)));
3079
3080    if (!res && !PyErr_Occurred())
3081        err_string("Error Parsing encoding_decl");
3082
3083    return res;
3084}
3085
3086static PyObject*
3087pickle_constructor = NULL;
3088
3089
3090static PyObject*
3091parser__pickler(PyObject *self, PyObject *args)
3092{
3093    NOTE(ARGUNUSED(self))
3094    PyObject *result = NULL;
3095    PyObject *st = NULL;
3096    PyObject *empty_dict = NULL;
3097
3098    if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
3099        PyObject *newargs;
3100        PyObject *tuple;
3101
3102        if ((empty_dict = PyDict_New()) == NULL)
3103            goto finally;
3104        if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
3105            goto finally;
3106        tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
3107        if (tuple != NULL) {
3108            result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3109            Py_DECREF(tuple);
3110        }
3111        Py_DECREF(empty_dict);
3112        Py_DECREF(newargs);
3113    }
3114  finally:
3115    Py_XDECREF(empty_dict);
3116
3117    return (result);
3118}
3119
3120
3121/*  Functions exported by this module.  Most of this should probably
3122 *  be converted into an ST object with methods, but that is better
3123 *  done directly in Python, allowing subclasses to be created directly.
3124 *  We'd really have to write a wrapper around it all anyway to allow
3125 *  inheritance.
3126 */
3127static PyMethodDef parser_functions[] =  {
3128    {"compilest",      (PyCFunction)parser_compilest,  PUBLIC_METHOD_TYPE,
3129        PyDoc_STR("Compiles an ST object into a code object.")},
3130    {"expr",            (PyCFunction)parser_expr,      PUBLIC_METHOD_TYPE,
3131        PyDoc_STR("Creates an ST object from an expression.")},
3132    {"isexpr",          (PyCFunction)parser_isexpr,    PUBLIC_METHOD_TYPE,
3133        PyDoc_STR("Determines if an ST object was created from an expression.")},
3134    {"issuite",         (PyCFunction)parser_issuite,   PUBLIC_METHOD_TYPE,
3135        PyDoc_STR("Determines if an ST object was created from a suite.")},
3136    {"suite",           (PyCFunction)parser_suite,     PUBLIC_METHOD_TYPE,
3137        PyDoc_STR("Creates an ST object from a suite.")},
3138    {"sequence2st",     (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,
3139        PyDoc_STR("Creates an ST object from a tree representation.")},
3140    {"st2tuple",        (PyCFunction)parser_st2tuple,  PUBLIC_METHOD_TYPE,
3141        PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3142    {"st2list",         (PyCFunction)parser_st2list,   PUBLIC_METHOD_TYPE,
3143        PyDoc_STR("Creates a list-tree representation of an ST.")},
3144    {"tuple2st",        (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,
3145        PyDoc_STR("Creates an ST object from a tree representation.")},
3146
3147    /* private stuff: support pickle module */
3148    {"_pickler",        (PyCFunction)parser__pickler,  METH_VARARGS,
3149        PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
3150
3151    {NULL, NULL, 0, NULL}
3152    };
3153
3154
3155
3156static struct PyModuleDef parsermodule = {
3157	PyModuleDef_HEAD_INIT,
3158	"parser",
3159	NULL,
3160	-1,
3161	parser_functions,
3162	NULL,
3163	NULL,
3164	NULL,
3165	NULL
3166};
3167
3168PyMODINIT_FUNC PyInit_parser(void);  /* supply a prototype */
3169
3170PyMODINIT_FUNC
3171PyInit_parser(void)
3172{
3173    PyObject *module, *copyreg;
3174
3175    if (PyType_Ready(&PyST_Type) < 0)
3176        return NULL;
3177    module = PyModule_Create(&parsermodule);
3178    if (module == NULL)
3179    	return NULL;
3180
3181    if (parser_error == 0)
3182        parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
3183
3184    if (parser_error == 0)
3185        return NULL;
3186    /* CAUTION:  The code next used to skip bumping the refcount on
3187     * parser_error.  That's a disaster if PyInit_parser() gets called more
3188     * than once.  By incref'ing, we ensure that each module dict that
3189     * gets created owns its reference to the shared parser_error object,
3190     * and the file static parser_error vrbl owns a reference too.
3191     */
3192    Py_INCREF(parser_error);
3193    if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3194        return NULL;
3195
3196    Py_INCREF(&PyST_Type);
3197    PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
3198
3199    PyModule_AddStringConstant(module, "__copyright__",
3200                               parser_copyright_string);
3201    PyModule_AddStringConstant(module, "__doc__",
3202                               parser_doc_string);
3203    PyModule_AddStringConstant(module, "__version__",
3204                               parser_version_string);
3205
3206    /* Register to support pickling.
3207     * If this fails, the import of this module will fail because an
3208     * exception will be raised here; should we clear the exception?
3209     */
3210    copyreg = PyImport_ImportModuleNoBlock("copyreg");
3211    if (copyreg != NULL) {
3212        PyObject *func, *pickler;
3213
3214        func = PyObject_GetAttrString(copyreg, "pickle");
3215        pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3216        pickler = PyObject_GetAttrString(module, "_pickler");
3217        Py_XINCREF(pickle_constructor);
3218        if ((func != NULL) && (pickle_constructor != NULL)
3219            && (pickler != NULL)) {
3220            PyObject *res;
3221
3222            res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3223                                               pickle_constructor, NULL);
3224            Py_XDECREF(res);
3225        }
3226        Py_XDECREF(func);
3227        Py_XDECREF(pickle_constructor);
3228        Py_XDECREF(pickler);
3229        Py_DECREF(copyreg);
3230    }
3231    return module;
3232}
3233