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