parsermodule.c revision ea7e9f9a83a88325e599d0a7b31122e50495a5aa
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    return (res);
588}
589
590
591/*  PyObject* parser_expr(PyObject* self, PyObject* args)
592 *  PyObject* parser_suite(PyObject* self, PyObject* args)
593 *
594 *  External interfaces to the parser itself.  Which is called determines if
595 *  the parser attempts to recognize an expression ('eval' form) or statement
596 *  suite ('exec' form).  The real work is done by parser_do_parse() above.
597 *
598 */
599static PyObject*
600parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
601{
602    NOTE(ARGUNUSED(self))
603    return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
604}
605
606
607static PyObject*
608parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
609{
610    NOTE(ARGUNUSED(self))
611    return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
612}
613
614
615
616/*  This is the messy part of the code.  Conversion from a tuple to an ST
617 *  object requires that the input tuple be valid without having to rely on
618 *  catching an exception from the compiler.  This is done to allow the
619 *  compiler itself to remain fast, since most of its input will come from
620 *  the parser directly, and therefore be known to be syntactically correct.
621 *  This validation is done to ensure that we don't core dump the compile
622 *  phase, returning an exception instead.
623 *
624 *  Two aspects can be broken out in this code:  creating a node tree from
625 *  the tuple passed in, and verifying that it is indeed valid.  It may be
626 *  advantageous to expand the number of ST types to include funcdefs and
627 *  lambdadefs to take advantage of the optimizer, recognizing those STs
628 *  here.  They are not necessary, and not quite as useful in a raw form.
629 *  For now, let's get expressions and suites working reliably.
630 */
631
632
633static node* build_node_tree(PyObject *tuple);
634static int   validate_expr_tree(node *tree);
635static int   validate_file_input(node *tree);
636static int   validate_encoding_decl(node *tree);
637
638/*  PyObject* parser_tuple2st(PyObject* self, PyObject* args)
639 *
640 *  This is the public function, called from the Python code.  It receives a
641 *  single tuple object from the caller, and creates an ST object if the
642 *  tuple can be validated.  It does this by checking the first code of the
643 *  tuple, and, if acceptable, builds the internal representation.  If this
644 *  step succeeds, the internal representation is validated as fully as
645 *  possible with the various validate_*() routines defined below.
646 *
647 *  This function must be changed if support is to be added for PyST_FRAGMENT
648 *  ST objects.
649 *
650 */
651static PyObject*
652parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
653{
654    NOTE(ARGUNUSED(self))
655    PyObject *st = 0;
656    PyObject *tuple;
657    node *tree;
658
659    static char *keywords[] = {"sequence", NULL};
660
661    if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
662                                     &tuple))
663        return (0);
664    if (!PySequence_Check(tuple)) {
665        PyErr_SetString(PyExc_ValueError,
666                        "sequence2st() requires a single sequence argument");
667        return (0);
668    }
669    /*
670     *  Convert the tree to the internal form before checking it.
671     */
672    tree = build_node_tree(tuple);
673    if (tree != 0) {
674        int start_sym = TYPE(tree);
675        if (start_sym == eval_input) {
676            /*  Might be an eval form.  */
677            if (validate_expr_tree(tree))
678                st = parser_newstobject(tree, PyST_EXPR);
679            else
680                PyNode_Free(tree);
681        }
682        else if (start_sym == file_input) {
683            /*  This looks like an exec form so far.  */
684            if (validate_file_input(tree))
685                st = parser_newstobject(tree, PyST_SUITE);
686            else
687                PyNode_Free(tree);
688        }
689        else if (start_sym == encoding_decl) {
690            /* This looks like an encoding_decl so far. */
691            if (validate_encoding_decl(tree))
692                st = parser_newstobject(tree, PyST_SUITE);
693            else
694                PyNode_Free(tree);
695        }
696        else {
697            /*  This is a fragment, at best. */
698            PyNode_Free(tree);
699            err_string("parse tree does not use a valid start symbol");
700        }
701    }
702    /*  Make sure we throw an exception on all errors.  We should never
703     *  get this, but we'd do well to be sure something is done.
704     */
705    if (st == NULL && !PyErr_Occurred())
706        err_string("unspecified ST error occurred");
707
708    return st;
709}
710
711
712/*  node* build_node_children()
713 *
714 *  Iterate across the children of the current non-terminal node and build
715 *  their structures.  If successful, return the root of this portion of
716 *  the tree, otherwise, 0.  Any required exception will be specified already,
717 *  and no memory will have been deallocated.
718 *
719 */
720static node*
721build_node_children(PyObject *tuple, node *root, int *line_num)
722{
723    Py_ssize_t len = PyObject_Size(tuple);
724    Py_ssize_t i;
725    int  err;
726
727    for (i = 1; i < len; ++i) {
728        /* elem must always be a sequence, however simple */
729        PyObject* elem = PySequence_GetItem(tuple, i);
730        int ok = elem != NULL;
731        long  type = 0;
732        char *strn = 0;
733
734        if (ok)
735            ok = PySequence_Check(elem);
736        if (ok) {
737            PyObject *temp = PySequence_GetItem(elem, 0);
738            if (temp == NULL)
739                ok = 0;
740            else {
741                ok = PyLong_Check(temp);
742                if (ok)
743                    type = PyLong_AS_LONG(temp);
744                Py_DECREF(temp);
745            }
746        }
747        if (!ok) {
748            PyObject *err = Py_BuildValue("os", elem,
749                                          "Illegal node construct.");
750            PyErr_SetObject(parser_error, err);
751            Py_XDECREF(err);
752            Py_XDECREF(elem);
753            return (0);
754        }
755        if (ISTERMINAL(type)) {
756            Py_ssize_t len = PyObject_Size(elem);
757            PyObject *temp;
758            const char *temp_str;
759
760            if ((len != 2) && (len != 3)) {
761                err_string("terminal nodes must have 2 or 3 entries");
762                return 0;
763            }
764            temp = PySequence_GetItem(elem, 1);
765            if (temp == NULL)
766                return 0;
767            if (!PyUnicode_Check(temp)) {
768                PyErr_Format(parser_error,
769                             "second item in terminal node must be a string,"
770                             " found %s",
771                             Py_TYPE(temp)->tp_name);
772                Py_DECREF(temp);
773                Py_DECREF(elem);
774                return 0;
775            }
776            if (len == 3) {
777                PyObject *o = PySequence_GetItem(elem, 2);
778                if (o != NULL) {
779                    if (PyLong_Check(o))
780                        *line_num = PyLong_AS_LONG(o);
781                    else {
782                        PyErr_Format(parser_error,
783                                     "third item in terminal node must be an"
784                                     " integer, found %s",
785                                     Py_TYPE(temp)->tp_name);
786                        Py_DECREF(o);
787                        Py_DECREF(temp);
788                        Py_DECREF(elem);
789                        return 0;
790                    }
791                    Py_DECREF(o);
792                }
793            }
794            temp_str = _PyUnicode_AsStringAndSize(temp, &len);
795            if (temp_str == NULL) {
796                Py_DECREF(temp);
797                Py_XDECREF(elem);
798                return 0;
799            }
800            strn = (char *)PyObject_MALLOC(len + 1);
801            if (strn != NULL)
802                (void) memcpy(strn, temp_str, len + 1);
803            Py_DECREF(temp);
804        }
805        else if (!ISNONTERMINAL(type)) {
806            /*
807             *  It has to be one or the other; this is an error.
808             *  Throw an exception.
809             */
810            PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
811            PyErr_SetObject(parser_error, err);
812            Py_XDECREF(err);
813            Py_XDECREF(elem);
814            return (0);
815        }
816        err = PyNode_AddChild(root, type, strn, *line_num, 0);
817        if (err == E_NOMEM) {
818            Py_XDECREF(elem);
819            PyObject_FREE(strn);
820            return (node *) PyErr_NoMemory();
821        }
822        if (err == E_OVERFLOW) {
823            Py_XDECREF(elem);
824            PyObject_FREE(strn);
825            PyErr_SetString(PyExc_ValueError,
826                            "unsupported number of child nodes");
827            return NULL;
828        }
829
830        if (ISNONTERMINAL(type)) {
831            node* new_child = CHILD(root, i - 1);
832
833            if (new_child != build_node_children(elem, new_child, line_num)) {
834                Py_XDECREF(elem);
835                return (0);
836            }
837        }
838        else if (type == NEWLINE) {     /* It's true:  we increment the     */
839            ++(*line_num);              /* line number *after* the newline! */
840        }
841        Py_XDECREF(elem);
842    }
843    return root;
844}
845
846
847static node*
848build_node_tree(PyObject *tuple)
849{
850    node* res = 0;
851    PyObject *temp = PySequence_GetItem(tuple, 0);
852    long num = -1;
853
854    if (temp != NULL)
855        num = PyLong_AsLong(temp);
856    Py_XDECREF(temp);
857    if (ISTERMINAL(num)) {
858        /*
859         *  The tuple is simple, but it doesn't start with a start symbol.
860         *  Throw an exception now and be done with it.
861         */
862        tuple = Py_BuildValue("os", tuple,
863                    "Illegal syntax-tree; cannot start with terminal symbol.");
864        PyErr_SetObject(parser_error, tuple);
865        Py_XDECREF(tuple);
866    }
867    else if (ISNONTERMINAL(num)) {
868        /*
869         *  Not efficient, but that can be handled later.
870         */
871        int line_num = 0;
872        PyObject *encoding = NULL;
873
874        if (num == encoding_decl) {
875            encoding = PySequence_GetItem(tuple, 2);
876            /* tuple isn't borrowed anymore here, need to DECREF */
877            tuple = PySequence_GetSlice(tuple, 0, 2);
878            if (tuple == NULL)
879                return NULL;
880        }
881        res = PyNode_New(num);
882        if (res != NULL) {
883            if (res != build_node_children(tuple, res, &line_num)) {
884                PyNode_Free(res);
885                res = NULL;
886            }
887            if (res && encoding) {
888                Py_ssize_t len;
889                const char *temp;
890                temp = _PyUnicode_AsStringAndSize(encoding, &len);
891                if (temp == NULL) {
892                    Py_DECREF(res);
893                    Py_DECREF(encoding);
894                    Py_DECREF(tuple);
895                    return NULL;
896                }
897                res->n_str = (char *)PyObject_MALLOC(len + 1);
898                if (res->n_str != NULL && temp != NULL)
899                    (void) memcpy(res->n_str, temp, len + 1);
900                Py_DECREF(encoding);
901                Py_DECREF(tuple);
902            }
903        }
904    }
905    else {
906        /*  The tuple is illegal -- if the number is neither TERMINAL nor
907         *  NONTERMINAL, we can't use it.  Not sure the implementation
908         *  allows this condition, but the API doesn't preclude it.
909         */
910        PyObject *err = Py_BuildValue("os", tuple,
911                                      "Illegal component tuple.");
912        PyErr_SetObject(parser_error, err);
913        Py_XDECREF(err);
914    }
915
916    return (res);
917}
918
919
920/*
921 *  Validation routines used within the validation section:
922 */
923static int validate_terminal(node *terminal, int type, char *string);
924
925#define validate_ampersand(ch)  validate_terminal(ch,      AMPER, "&")
926#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
927#define validate_colon(ch)      validate_terminal(ch,      COLON, ":")
928#define validate_comma(ch)      validate_terminal(ch,      COMMA, ",")
929#define validate_dedent(ch)     validate_terminal(ch,     DEDENT, "")
930#define validate_equal(ch)      validate_terminal(ch,      EQUAL, "=")
931#define validate_indent(ch)     validate_terminal(ch,     INDENT, (char*)NULL)
932#define validate_lparen(ch)     validate_terminal(ch,       LPAR, "(")
933#define validate_newline(ch)    validate_terminal(ch,    NEWLINE, (char*)NULL)
934#define validate_rparen(ch)     validate_terminal(ch,       RPAR, ")")
935#define validate_semi(ch)       validate_terminal(ch,       SEMI, ";")
936#define validate_star(ch)       validate_terminal(ch,       STAR, "*")
937#define validate_vbar(ch)       validate_terminal(ch,       VBAR, "|")
938#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
939#define validate_dot(ch)        validate_terminal(ch,        DOT, ".")
940#define validate_at(ch)         validate_terminal(ch,         AT, "@")
941#define validate_rarrow(ch)     validate_terminal(ch,     RARROW, "->")
942#define validate_name(ch, str)  validate_terminal(ch,       NAME, str)
943
944#define VALIDATER(n)    static int validate_##n(node *tree)
945
946VALIDATER(node);                VALIDATER(small_stmt);
947VALIDATER(class);               VALIDATER(node);
948VALIDATER(parameters);          VALIDATER(suite);
949VALIDATER(testlist);            VALIDATER(varargslist);
950VALIDATER(vfpdef);
951VALIDATER(stmt);                VALIDATER(simple_stmt);
952VALIDATER(expr_stmt);           VALIDATER(power);
953VALIDATER(del_stmt);
954VALIDATER(return_stmt);         VALIDATER(raise_stmt);
955VALIDATER(import_stmt);         VALIDATER(import_stmt);
956VALIDATER(import_name);         VALIDATER(yield_stmt);
957VALIDATER(global_stmt);         VALIDATER(assert_stmt);
958VALIDATER(compound_stmt);       VALIDATER(test_or_star_expr);
959VALIDATER(while);               VALIDATER(for);
960VALIDATER(try);                 VALIDATER(except_clause);
961VALIDATER(test);                VALIDATER(and_test);
962VALIDATER(not_test);            VALIDATER(comparison);
963VALIDATER(comp_op);
964VALIDATER(star_expr);           VALIDATER(expr);
965VALIDATER(xor_expr);            VALIDATER(and_expr);
966VALIDATER(shift_expr);          VALIDATER(arith_expr);
967VALIDATER(term);                VALIDATER(factor);
968VALIDATER(atom);                VALIDATER(lambdef);
969VALIDATER(trailer);             VALIDATER(subscript);
970VALIDATER(subscriptlist);       VALIDATER(sliceop);
971VALIDATER(exprlist);            VALIDATER(dictorsetmaker);
972VALIDATER(arglist);             VALIDATER(argument);
973VALIDATER(comp_for);
974VALIDATER(comp_iter);           VALIDATER(comp_if);
975VALIDATER(testlist_comp);       VALIDATER(yield_expr);
976VALIDATER(or_test);
977VALIDATER(test_nocond);         VALIDATER(lambdef_nocond);
978
979#undef VALIDATER
980
981#define is_even(n)      (((n) & 1) == 0)
982#define is_odd(n)       (((n) & 1) == 1)
983
984
985static int
986validate_ntype(node *n, int t)
987{
988    if (TYPE(n) != t) {
989        PyErr_Format(parser_error, "Expected node type %d, got %d.",
990                     t, TYPE(n));
991        return 0;
992    }
993    return 1;
994}
995
996
997/*  Verifies that the number of child nodes is exactly 'num', raising
998 *  an exception if it isn't.  The exception message does not indicate
999 *  the exact number of nodes, allowing this to be used to raise the
1000 *  "right" exception when the wrong number of nodes is present in a
1001 *  specific variant of a statement's syntax.  This is commonly used
1002 *  in that fashion.
1003 */
1004static int
1005validate_numnodes(node *n, int num, const char *const name)
1006{
1007    if (NCH(n) != num) {
1008        PyErr_Format(parser_error,
1009                     "Illegal number of children for %s node.", name);
1010        return 0;
1011    }
1012    return 1;
1013}
1014
1015
1016static int
1017validate_terminal(node *terminal, int type, char *string)
1018{
1019    int res = (validate_ntype(terminal, type)
1020               && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
1021
1022    if (!res && !PyErr_Occurred()) {
1023        PyErr_Format(parser_error,
1024                     "Illegal terminal: expected \"%s\"", string);
1025    }
1026    return (res);
1027}
1028
1029
1030/*  X (',' X) [',']
1031 */
1032static int
1033validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
1034                        const char *const name)
1035{
1036    int nch = NCH(tree);
1037    int res = (nch && validate_ntype(tree, ntype)
1038               && vfunc(CHILD(tree, 0)));
1039
1040    if (!res && !PyErr_Occurred())
1041        (void) validate_numnodes(tree, 1, name);
1042    else {
1043        if (is_even(nch))
1044            res = validate_comma(CHILD(tree, --nch));
1045        if (res && nch > 1) {
1046            int pos = 1;
1047            for ( ; res && pos < nch; pos += 2)
1048                res = (validate_comma(CHILD(tree, pos))
1049                       && vfunc(CHILD(tree, pos + 1)));
1050        }
1051    }
1052    return (res);
1053}
1054
1055
1056/*  validate_class()
1057 *
1058 *  classdef:
1059 *      'class' NAME ['(' testlist ')'] ':' suite
1060 */
1061static int
1062validate_class(node *tree)
1063{
1064    int nch = NCH(tree);
1065    int res = (validate_ntype(tree, classdef) &&
1066                ((nch == 4) || (nch == 6) || (nch == 7)));
1067
1068    if (res) {
1069        res = (validate_name(CHILD(tree, 0), "class")
1070               && validate_ntype(CHILD(tree, 1), NAME)
1071               && validate_colon(CHILD(tree, nch - 2))
1072               && validate_suite(CHILD(tree, nch - 1)));
1073    }
1074    else {
1075        (void) validate_numnodes(tree, 4, "class");
1076    }
1077
1078    if (res) {
1079        if (nch == 7) {
1080                res = ((validate_lparen(CHILD(tree, 2)) &&
1081                        validate_arglist(CHILD(tree, 3)) &&
1082                        validate_rparen(CHILD(tree, 4))));
1083        }
1084        else if (nch == 6) {
1085                res = (validate_lparen(CHILD(tree,2)) &&
1086                        validate_rparen(CHILD(tree,3)));
1087        }
1088    }
1089    return (res);
1090}
1091
1092
1093/*  if_stmt:
1094 *      'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1095 */
1096static int
1097validate_if(node *tree)
1098{
1099    int nch = NCH(tree);
1100    int res = (validate_ntype(tree, if_stmt)
1101               && (nch >= 4)
1102               && validate_name(CHILD(tree, 0), "if")
1103               && validate_test(CHILD(tree, 1))
1104               && validate_colon(CHILD(tree, 2))
1105               && validate_suite(CHILD(tree, 3)));
1106
1107    if (res && ((nch % 4) == 3)) {
1108        /*  ... 'else' ':' suite  */
1109        res = (validate_name(CHILD(tree, nch - 3), "else")
1110               && validate_colon(CHILD(tree, nch - 2))
1111               && validate_suite(CHILD(tree, nch - 1)));
1112        nch -= 3;
1113    }
1114    else if (!res && !PyErr_Occurred())
1115        (void) validate_numnodes(tree, 4, "if");
1116    if ((nch % 4) != 0)
1117        /* Will catch the case for nch < 4 */
1118        res = validate_numnodes(tree, 0, "if");
1119    else if (res && (nch > 4)) {
1120        /*  ... ('elif' test ':' suite)+ ...  */
1121        int j = 4;
1122        while ((j < nch) && res) {
1123            res = (validate_name(CHILD(tree, j), "elif")
1124                   && validate_colon(CHILD(tree, j + 2))
1125                   && validate_test(CHILD(tree, j + 1))
1126                   && validate_suite(CHILD(tree, j + 3)));
1127            j += 4;
1128        }
1129    }
1130    return (res);
1131}
1132
1133
1134/*  parameters:
1135 *      '(' [varargslist] ')'
1136 *
1137 */
1138static int
1139validate_parameters(node *tree)
1140{
1141    int nch = NCH(tree);
1142    int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
1143
1144    if (res) {
1145        res = (validate_lparen(CHILD(tree, 0))
1146               && validate_rparen(CHILD(tree, nch - 1)));
1147        if (res && (nch == 3))
1148            res = validate_varargslist(CHILD(tree, 1));
1149    }
1150    else {
1151        (void) validate_numnodes(tree, 2, "parameters");
1152    }
1153    return (res);
1154}
1155
1156
1157/*  validate_suite()
1158 *
1159 *  suite:
1160 *      simple_stmt
1161 *    | NEWLINE INDENT stmt+ DEDENT
1162 */
1163static int
1164validate_suite(node *tree)
1165{
1166    int nch = NCH(tree);
1167    int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
1168
1169    if (res && (nch == 1))
1170        res = validate_simple_stmt(CHILD(tree, 0));
1171    else if (res) {
1172        /*  NEWLINE INDENT stmt+ DEDENT  */
1173        res = (validate_newline(CHILD(tree, 0))
1174               && validate_indent(CHILD(tree, 1))
1175               && validate_stmt(CHILD(tree, 2))
1176               && validate_dedent(CHILD(tree, nch - 1)));
1177
1178        if (res && (nch > 4)) {
1179            int i = 3;
1180            --nch;                      /* forget the DEDENT     */
1181            for ( ; res && (i < nch); ++i)
1182                res = validate_stmt(CHILD(tree, i));
1183        }
1184        else if (nch < 4)
1185            res = validate_numnodes(tree, 4, "suite");
1186    }
1187    return (res);
1188}
1189
1190
1191static int
1192validate_testlist(node *tree)
1193{
1194    return (validate_repeating_list(tree, testlist,
1195                                    validate_test, "testlist"));
1196}
1197
1198static int
1199validate_testlist_star_expr(node *tl)
1200{
1201    return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
1202                                    "testlist"));
1203}
1204
1205
1206/* validate either vfpdef or tfpdef.
1207 * vfpdef: NAME
1208 * tfpdef: NAME [':' test]
1209 */
1210static int
1211validate_vfpdef(node *tree)
1212{
1213    int nch = NCH(tree);
1214    if (TYPE(tree) == vfpdef) {
1215        return nch == 1 && validate_name(CHILD(tree, 0), NULL);
1216    }
1217    else if (TYPE(tree) == tfpdef) {
1218        if (nch == 1) {
1219            return validate_name(CHILD(tree, 0), NULL);
1220        }
1221        else if (nch == 3) {
1222            return validate_name(CHILD(tree, 0), NULL) &&
1223                   validate_colon(CHILD(tree, 1)) &&
1224                   validate_test(CHILD(tree, 2));
1225        }
1226    }
1227    return 0;
1228}
1229
1230/* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
1231 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
1232 */
1233static int
1234validate_varargslist_trailer(node *tree, int start)
1235{
1236    int nch = NCH(tree);
1237    int res = 0;
1238
1239    if (nch <= start) {
1240        err_string("expected variable argument trailer for varargslist");
1241        return 0;
1242    }
1243    if (TYPE(CHILD(tree, start)) == STAR) {
1244        /*
1245         * '*' [vfpdef]
1246         */
1247        res = validate_star(CHILD(tree, start++));
1248        if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
1249                                   TYPE(CHILD(tree, start)) == tfpdef))
1250            res = validate_vfpdef(CHILD(tree, start++));
1251        /*
1252         * (',' vfpdef ['=' test])*
1253         */
1254        while (res && start + 1 < nch && (
1255                   TYPE(CHILD(tree, start + 1)) == vfpdef ||
1256                   TYPE(CHILD(tree, start + 1)) == tfpdef)) {
1257            res = (validate_comma(CHILD(tree, start++))
1258                   && validate_vfpdef(CHILD(tree, start++)));
1259            if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
1260                res = (validate_equal(CHILD(tree, start++))
1261                       && validate_test(CHILD(tree, start++)));
1262        }
1263        /*
1264         * [',' '**' vfpdef]
1265         */
1266        if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
1267            res = (validate_comma(CHILD(tree, start++))
1268                   && validate_doublestar(CHILD(tree, start++))
1269                   && validate_vfpdef(CHILD(tree, start++)));
1270    }
1271    else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
1272        /*
1273         * '**' vfpdef
1274         */
1275        if (start + 1 < nch)
1276            res = (validate_doublestar(CHILD(tree, start++))
1277                   && validate_vfpdef(CHILD(tree, start++)));
1278        else {
1279            res = 0;
1280            err_string("expected vfpdef after ** in varargslist trailer");
1281        }
1282    }
1283    else {
1284        res = 0;
1285        err_string("expected * or ** in varargslist trailer");
1286    }
1287
1288    if (res && start != nch) {
1289        res = 0;
1290        err_string("unexpected extra children in varargslist trailer");
1291    }
1292    return res;
1293}
1294
1295
1296/* validate_varargslist()
1297 *
1298 * Validate typedargslist or varargslist.
1299 *
1300 * typedargslist: ((tfpdef ['=' test] ',')*
1301 *                 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
1302 *                  '**' tfpdef)
1303 *                 | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
1304 * tfpdef: NAME [':' test]
1305 * varargslist: ((vfpdef ['=' test] ',')*
1306 *               ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] |
1307 *                '**' vfpdef)
1308 *               | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
1309 * vfpdef: NAME
1310 *
1311 */
1312static int
1313validate_varargslist(node *tree)
1314{
1315    int nch = NCH(tree);
1316    int res = (TYPE(tree) == varargslist ||
1317               TYPE(tree) == typedargslist) &&
1318              (nch != 0);
1319    int sym;
1320    node *ch;
1321    int i = 0;
1322
1323    if (!res)
1324        return 0;
1325    if (nch < 1) {
1326        err_string("varargslist missing child nodes");
1327        return 0;
1328    }
1329    while (i < nch) {
1330        ch = CHILD(tree, i);
1331        sym = TYPE(ch);
1332        if (sym == vfpdef || sym == tfpdef) {
1333            /* validate (vfpdef ['=' test] ',')+ */
1334            res = validate_vfpdef(ch);
1335            ++i;
1336            if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1337                res = (validate_equal(CHILD(tree, i))
1338                       && validate_test(CHILD(tree, i+1)));
1339                if (res)
1340                  i += 2;
1341            }
1342            if (res && i < nch) {
1343                res = validate_comma(CHILD(tree, i));
1344                ++i;
1345            }
1346        } else if (sym == DOUBLESTAR || sym == STAR) {
1347            res = validate_varargslist_trailer(tree, i);
1348            break;
1349        } else {
1350            res = 0;
1351            err_string("illegal formation for varargslist");
1352        }
1353    }
1354    return res;
1355}
1356
1357
1358/*  comp_iter:  comp_for | comp_if
1359 */
1360static int
1361validate_comp_iter(node *tree)
1362{
1363    int res = (validate_ntype(tree, comp_iter)
1364               && validate_numnodes(tree, 1, "comp_iter"));
1365    if (res && TYPE(CHILD(tree, 0)) == comp_for)
1366        res = validate_comp_for(CHILD(tree, 0));
1367    else
1368        res = validate_comp_if(CHILD(tree, 0));
1369
1370    return res;
1371}
1372
1373/*  comp_for:  'for' exprlist 'in' test [comp_iter]
1374 */
1375static int
1376validate_comp_for(node *tree)
1377{
1378    int nch = NCH(tree);
1379    int res;
1380
1381    if (nch == 5)
1382        res = validate_comp_iter(CHILD(tree, 4));
1383    else
1384        res = validate_numnodes(tree, 4, "comp_for");
1385
1386    if (res)
1387        res = (validate_name(CHILD(tree, 0), "for")
1388               && validate_exprlist(CHILD(tree, 1))
1389               && validate_name(CHILD(tree, 2), "in")
1390               && validate_or_test(CHILD(tree, 3)));
1391
1392    return res;
1393}
1394
1395/*  comp_if:  'if' test_nocond [comp_iter]
1396 */
1397static int
1398validate_comp_if(node *tree)
1399{
1400    int nch = NCH(tree);
1401    int res;
1402
1403    if (nch == 3)
1404        res = validate_comp_iter(CHILD(tree, 2));
1405    else
1406        res = validate_numnodes(tree, 2, "comp_if");
1407
1408    if (res)
1409        res = (validate_name(CHILD(tree, 0), "if")
1410               && validate_test_nocond(CHILD(tree, 1)));
1411
1412    return res;
1413}
1414
1415
1416/*  simple_stmt | compound_stmt
1417 *
1418 */
1419static int
1420validate_stmt(node *tree)
1421{
1422    int res = (validate_ntype(tree, stmt)
1423               && validate_numnodes(tree, 1, "stmt"));
1424
1425    if (res) {
1426        tree = CHILD(tree, 0);
1427
1428        if (TYPE(tree) == simple_stmt)
1429            res = validate_simple_stmt(tree);
1430        else
1431            res = validate_compound_stmt(tree);
1432    }
1433    return (res);
1434}
1435
1436
1437/*  small_stmt (';' small_stmt)* [';'] NEWLINE
1438 *
1439 */
1440static int
1441validate_simple_stmt(node *tree)
1442{
1443    int nch = NCH(tree);
1444    int res = (validate_ntype(tree, simple_stmt)
1445               && (nch >= 2)
1446               && validate_small_stmt(CHILD(tree, 0))
1447               && validate_newline(CHILD(tree, nch - 1)));
1448
1449    if (nch < 2)
1450        res = validate_numnodes(tree, 2, "simple_stmt");
1451    --nch;                              /* forget the NEWLINE    */
1452    if (res && is_even(nch))
1453        res = validate_semi(CHILD(tree, --nch));
1454    if (res && (nch > 2)) {
1455        int i;
1456
1457        for (i = 1; res && (i < nch); i += 2)
1458            res = (validate_semi(CHILD(tree, i))
1459                   && validate_small_stmt(CHILD(tree, i + 1)));
1460    }
1461    return (res);
1462}
1463
1464
1465static int
1466validate_small_stmt(node *tree)
1467{
1468    int nch = NCH(tree);
1469    int res = validate_numnodes(tree, 1, "small_stmt");
1470
1471    if (res) {
1472        int ntype = TYPE(CHILD(tree, 0));
1473
1474        if (  (ntype == expr_stmt)
1475              || (ntype == del_stmt)
1476              || (ntype == pass_stmt)
1477              || (ntype == flow_stmt)
1478              || (ntype == import_stmt)
1479              || (ntype == global_stmt)
1480              || (ntype == assert_stmt))
1481            res = validate_node(CHILD(tree, 0));
1482        else {
1483            res = 0;
1484            err_string("illegal small_stmt child type");
1485        }
1486    }
1487    else if (nch == 1) {
1488        res = 0;
1489        PyErr_Format(parser_error,
1490                     "Unrecognized child node of small_stmt: %d.",
1491                     TYPE(CHILD(tree, 0)));
1492    }
1493    return (res);
1494}
1495
1496
1497/*  compound_stmt:
1498 *      if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
1499 */
1500static int
1501validate_compound_stmt(node *tree)
1502{
1503    int res = (validate_ntype(tree, compound_stmt)
1504               && validate_numnodes(tree, 1, "compound_stmt"));
1505    int ntype;
1506
1507    if (!res)
1508        return (0);
1509
1510    tree = CHILD(tree, 0);
1511    ntype = TYPE(tree);
1512    if (  (ntype == if_stmt)
1513          || (ntype == while_stmt)
1514          || (ntype == for_stmt)
1515          || (ntype == try_stmt)
1516          || (ntype == with_stmt)
1517          || (ntype == funcdef)
1518          || (ntype == classdef)
1519          || (ntype == decorated))
1520        res = validate_node(tree);
1521    else {
1522        res = 0;
1523        PyErr_Format(parser_error,
1524                     "Illegal compound statement type: %d.", TYPE(tree));
1525    }
1526    return (res);
1527}
1528
1529static int
1530validate_yield_or_testlist(node *tree, int tse)
1531{
1532    if (TYPE(tree) == yield_expr) {
1533        return validate_yield_expr(tree);
1534    }
1535    else {
1536        if (tse)
1537            return validate_testlist_star_expr(tree);
1538        else
1539            return validate_testlist(tree);
1540    }
1541}
1542
1543static int
1544validate_expr_stmt(node *tree)
1545{
1546    int j;
1547    int nch = NCH(tree);
1548    int res = (validate_ntype(tree, expr_stmt)
1549               && is_odd(nch)
1550               && validate_testlist_star_expr(CHILD(tree, 0)));
1551
1552    if (res && nch == 3
1553        && TYPE(CHILD(tree, 1)) == augassign) {
1554        res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1555            && validate_yield_or_testlist(CHILD(tree, 2), 0);
1556
1557        if (res) {
1558            char *s = STR(CHILD(CHILD(tree, 1), 0));
1559
1560            res = (strcmp(s, "+=") == 0
1561                   || strcmp(s, "-=") == 0
1562                   || strcmp(s, "*=") == 0
1563                   || strcmp(s, "/=") == 0
1564                   || strcmp(s, "//=") == 0
1565                   || 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            if (!res)
1573                err_string("illegal augmented assignment operator");
1574        }
1575    }
1576    else {
1577        for (j = 1; res && (j < nch); j += 2)
1578            res = validate_equal(CHILD(tree, j))
1579                && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
1580    }
1581    return (res);
1582}
1583
1584
1585static int
1586validate_del_stmt(node *tree)
1587{
1588    return (validate_numnodes(tree, 2, "del_stmt")
1589            && validate_name(CHILD(tree, 0), "del")
1590            && validate_exprlist(CHILD(tree, 1)));
1591}
1592
1593
1594static int
1595validate_return_stmt(node *tree)
1596{
1597    int nch = NCH(tree);
1598    int res = (validate_ntype(tree, return_stmt)
1599               && ((nch == 1) || (nch == 2))
1600               && validate_name(CHILD(tree, 0), "return"));
1601
1602    if (res && (nch == 2))
1603        res = validate_testlist(CHILD(tree, 1));
1604
1605    return (res);
1606}
1607
1608
1609static int
1610validate_raise_stmt(node *tree)
1611{
1612    int nch = NCH(tree);
1613    int res = (validate_ntype(tree, raise_stmt)
1614               && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
1615
1616    if (res) {
1617        res = validate_name(CHILD(tree, 0), "raise");
1618        if (res && (nch >= 2))
1619            res = validate_test(CHILD(tree, 1));
1620        if (res && nch > 2) {
1621            res = (validate_comma(CHILD(tree, 2))
1622                   && validate_test(CHILD(tree, 3)));
1623            if (res && (nch > 4))
1624                res = (validate_comma(CHILD(tree, 4))
1625                       && validate_test(CHILD(tree, 5)));
1626        }
1627    }
1628    else
1629        (void) validate_numnodes(tree, 2, "raise");
1630    if (res && (nch == 4))
1631        res = (validate_comma(CHILD(tree, 2))
1632               && validate_test(CHILD(tree, 3)));
1633
1634    return (res);
1635}
1636
1637
1638/* yield_expr: 'yield' [testlist]
1639 */
1640static int
1641validate_yield_expr(node *tree)
1642{
1643    int nch = NCH(tree);
1644    int res = (validate_ntype(tree, yield_expr)
1645               && ((nch == 1) || (nch == 2))
1646               && validate_name(CHILD(tree, 0), "yield"));
1647
1648    if (res && (nch == 2))
1649        res = validate_testlist(CHILD(tree, 1));
1650
1651    return (res);
1652}
1653
1654
1655/* yield_stmt: yield_expr
1656 */
1657static int
1658validate_yield_stmt(node *tree)
1659{
1660    return (validate_ntype(tree, yield_stmt)
1661            && validate_numnodes(tree, 1, "yield_stmt")
1662            && validate_yield_expr(CHILD(tree, 0)));
1663}
1664
1665
1666static int
1667validate_import_as_name(node *tree)
1668{
1669    int nch = NCH(tree);
1670    int ok = validate_ntype(tree, import_as_name);
1671
1672    if (ok) {
1673        if (nch == 1)
1674            ok = validate_name(CHILD(tree, 0), NULL);
1675        else if (nch == 3)
1676            ok = (validate_name(CHILD(tree, 0), NULL)
1677                  && validate_name(CHILD(tree, 1), "as")
1678                  && validate_name(CHILD(tree, 2), NULL));
1679        else
1680            ok = validate_numnodes(tree, 3, "import_as_name");
1681    }
1682    return ok;
1683}
1684
1685
1686/* dotted_name:  NAME ("." NAME)*
1687 */
1688static int
1689validate_dotted_name(node *tree)
1690{
1691    int nch = NCH(tree);
1692    int res = (validate_ntype(tree, dotted_name)
1693               && is_odd(nch)
1694               && validate_name(CHILD(tree, 0), NULL));
1695    int i;
1696
1697    for (i = 1; res && (i < nch); i += 2) {
1698        res = (validate_dot(CHILD(tree, i))
1699               && validate_name(CHILD(tree, i+1), NULL));
1700    }
1701    return res;
1702}
1703
1704
1705/* dotted_as_name:  dotted_name [NAME NAME]
1706 */
1707static int
1708validate_dotted_as_name(node *tree)
1709{
1710    int nch = NCH(tree);
1711    int res = validate_ntype(tree, dotted_as_name);
1712
1713    if (res) {
1714        if (nch == 1)
1715            res = validate_dotted_name(CHILD(tree, 0));
1716        else if (nch == 3)
1717            res = (validate_dotted_name(CHILD(tree, 0))
1718                   && validate_name(CHILD(tree, 1), "as")
1719                   && validate_name(CHILD(tree, 2), NULL));
1720        else {
1721            res = 0;
1722            err_string("illegal number of children for dotted_as_name");
1723        }
1724    }
1725    return res;
1726}
1727
1728
1729/* dotted_as_name (',' dotted_as_name)* */
1730static int
1731validate_dotted_as_names(node *tree)
1732{
1733        int nch = NCH(tree);
1734        int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1735        int i;
1736
1737        for (i = 1; res && (i < nch); i += 2)
1738            res = (validate_comma(CHILD(tree, i))
1739                   && validate_dotted_as_name(CHILD(tree, i + 1)));
1740        return (res);
1741}
1742
1743
1744/* import_as_name (',' import_as_name)* [','] */
1745static int
1746validate_import_as_names(node *tree)
1747{
1748    int nch = NCH(tree);
1749    int res = validate_import_as_name(CHILD(tree, 0));
1750    int i;
1751
1752    for (i = 1; res && (i + 1 < nch); i += 2)
1753        res = (validate_comma(CHILD(tree, i))
1754               && validate_import_as_name(CHILD(tree, i + 1)));
1755    return (res);
1756}
1757
1758
1759/* 'import' dotted_as_names */
1760static int
1761validate_import_name(node *tree)
1762{
1763        return (validate_ntype(tree, import_name)
1764                && validate_numnodes(tree, 2, "import_name")
1765                && validate_name(CHILD(tree, 0), "import")
1766                && validate_dotted_as_names(CHILD(tree, 1)));
1767}
1768
1769/* Helper function to count the number of leading dots (or ellipsis tokens) in
1770 * 'from ...module import name'
1771 */
1772static int
1773count_from_dots(node *tree)
1774{
1775    int i;
1776    for (i = 1; i < NCH(tree); i++)
1777        if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
1778            break;
1779    return i - 1;
1780}
1781
1782/* import_from: ('from' ('.'* dotted_name | '.'+)
1783 *               'import' ('*' | '(' import_as_names ')' | import_as_names))
1784 */
1785static int
1786validate_import_from(node *tree)
1787{
1788        int nch = NCH(tree);
1789        int ndots = count_from_dots(tree);
1790        int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1791        int offset = ndots + havename;
1792        int res = validate_ntype(tree, import_from)
1793                && (offset >= 1)
1794                && (nch >= 3 + offset)
1795                && validate_name(CHILD(tree, 0), "from")
1796                && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1797                && validate_name(CHILD(tree, offset + 1), "import");
1798
1799        if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1800            res = ((nch == offset + 5)
1801                   && validate_lparen(CHILD(tree, offset + 2))
1802                   && validate_import_as_names(CHILD(tree, offset + 3))
1803                   && validate_rparen(CHILD(tree, offset + 4)));
1804        else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1805            res = validate_import_as_names(CHILD(tree, offset + 2));
1806        return (res);
1807}
1808
1809
1810/* import_stmt: import_name | import_from */
1811static int
1812validate_import_stmt(node *tree)
1813{
1814    int nch = NCH(tree);
1815    int res = validate_numnodes(tree, 1, "import_stmt");
1816
1817    if (res) {
1818        int ntype = TYPE(CHILD(tree, 0));
1819
1820        if (ntype == import_name || ntype == import_from)
1821            res = validate_node(CHILD(tree, 0));
1822        else {
1823            res = 0;
1824            err_string("illegal import_stmt child type");
1825        }
1826    }
1827    else if (nch == 1) {
1828        res = 0;
1829        PyErr_Format(parser_error,
1830                     "Unrecognized child node of import_stmt: %d.",
1831                     TYPE(CHILD(tree, 0)));
1832    }
1833    return (res);
1834}
1835
1836
1837
1838
1839static int
1840validate_global_stmt(node *tree)
1841{
1842    int j;
1843    int nch = NCH(tree);
1844    int res = (validate_ntype(tree, global_stmt)
1845               && is_even(nch) && (nch >= 2));
1846
1847    if (!res && !PyErr_Occurred())
1848        err_string("illegal global statement");
1849
1850    if (res)
1851        res = (validate_name(CHILD(tree, 0), "global")
1852               && validate_ntype(CHILD(tree, 1), NAME));
1853    for (j = 2; res && (j < nch); j += 2)
1854        res = (validate_comma(CHILD(tree, j))
1855               && validate_ntype(CHILD(tree, j + 1), NAME));
1856
1857    return (res);
1858}
1859
1860
1861/*  assert_stmt:
1862 *
1863 *  'assert' test [',' test]
1864 */
1865static int
1866validate_assert_stmt(node *tree)
1867{
1868    int nch = NCH(tree);
1869    int res = (validate_ntype(tree, assert_stmt)
1870               && ((nch == 2) || (nch == 4))
1871               && (validate_name(CHILD(tree, 0), "assert"))
1872               && validate_test(CHILD(tree, 1)));
1873
1874    if (!res && !PyErr_Occurred())
1875        err_string("illegal assert statement");
1876    if (res && (nch > 2))
1877        res = (validate_comma(CHILD(tree, 2))
1878               && validate_test(CHILD(tree, 3)));
1879
1880    return (res);
1881}
1882
1883
1884static int
1885validate_while(node *tree)
1886{
1887    int nch = NCH(tree);
1888    int res = (validate_ntype(tree, while_stmt)
1889               && ((nch == 4) || (nch == 7))
1890               && validate_name(CHILD(tree, 0), "while")
1891               && validate_test(CHILD(tree, 1))
1892               && validate_colon(CHILD(tree, 2))
1893               && validate_suite(CHILD(tree, 3)));
1894
1895    if (res && (nch == 7))
1896        res = (validate_name(CHILD(tree, 4), "else")
1897               && validate_colon(CHILD(tree, 5))
1898               && validate_suite(CHILD(tree, 6)));
1899
1900    return (res);
1901}
1902
1903
1904static int
1905validate_for(node *tree)
1906{
1907    int nch = NCH(tree);
1908    int res = (validate_ntype(tree, for_stmt)
1909               && ((nch == 6) || (nch == 9))
1910               && validate_name(CHILD(tree, 0), "for")
1911               && validate_exprlist(CHILD(tree, 1))
1912               && validate_name(CHILD(tree, 2), "in")
1913               && validate_testlist(CHILD(tree, 3))
1914               && validate_colon(CHILD(tree, 4))
1915               && validate_suite(CHILD(tree, 5)));
1916
1917    if (res && (nch == 9))
1918        res = (validate_name(CHILD(tree, 6), "else")
1919               && validate_colon(CHILD(tree, 7))
1920               && validate_suite(CHILD(tree, 8)));
1921
1922    return (res);
1923}
1924
1925
1926/*  try_stmt:
1927 *      'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1928                                                   ['finally' ':' suite]
1929 *    | 'try' ':' suite 'finally' ':' suite
1930 *
1931 */
1932static int
1933validate_try(node *tree)
1934{
1935    int nch = NCH(tree);
1936    int pos = 3;
1937    int res = (validate_ntype(tree, try_stmt)
1938               && (nch >= 6) && ((nch % 3) == 0));
1939
1940    if (res)
1941        res = (validate_name(CHILD(tree, 0), "try")
1942               && validate_colon(CHILD(tree, 1))
1943               && validate_suite(CHILD(tree, 2))
1944               && validate_colon(CHILD(tree, nch - 2))
1945               && validate_suite(CHILD(tree, nch - 1)));
1946    else if (!PyErr_Occurred()) {
1947        const char* name = "except";
1948        if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1949            name = STR(CHILD(tree, nch - 3));
1950
1951        PyErr_Format(parser_error,
1952                     "Illegal number of children for try/%s node.", name);
1953    }
1954    /* Handle try/finally statement */
1955    if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
1956        (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
1957        res = (validate_numnodes(tree, 6, "try/finally")
1958               && validate_colon(CHILD(tree, 4))
1959               && validate_suite(CHILD(tree, 5)));
1960        return (res);
1961    }
1962    /* try/except statement: skip past except_clause sections */
1963    while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
1964        res = (validate_except_clause(CHILD(tree, pos))
1965               && validate_colon(CHILD(tree, pos + 1))
1966               && validate_suite(CHILD(tree, pos + 2)));
1967        pos += 3;
1968    }
1969    /* skip else clause */
1970    if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
1971        (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
1972        res = (validate_colon(CHILD(tree, pos + 1))
1973               && validate_suite(CHILD(tree, pos + 2)));
1974        pos += 3;
1975    }
1976    if (res && pos < nch) {
1977        /* last clause must be a finally */
1978        res = (validate_name(CHILD(tree, pos), "finally")
1979               && validate_numnodes(tree, pos + 3, "try/except/finally")
1980               && validate_colon(CHILD(tree, pos + 1))
1981               && validate_suite(CHILD(tree, pos + 2)));
1982    }
1983    return (res);
1984}
1985
1986
1987static int
1988validate_except_clause(node *tree)
1989{
1990    int nch = NCH(tree);
1991    int res = (validate_ntype(tree, except_clause)
1992               && ((nch == 1) || (nch == 2) || (nch == 4))
1993               && validate_name(CHILD(tree, 0), "except"));
1994
1995    if (res && (nch > 1))
1996        res = validate_test(CHILD(tree, 1));
1997    if (res && (nch == 4))
1998        res = (validate_name(CHILD(tree, 2), "as")
1999               && validate_ntype(CHILD(tree, 3), NAME));
2000
2001    return (res);
2002}
2003
2004
2005static int
2006validate_test(node *tree)
2007{
2008    int nch = NCH(tree);
2009    int res = validate_ntype(tree, test) && is_odd(nch);
2010
2011    if (res && (TYPE(CHILD(tree, 0)) == lambdef))
2012        res = ((nch == 1)
2013               && validate_lambdef(CHILD(tree, 0)));
2014    else if (res) {
2015        res = validate_or_test(CHILD(tree, 0));
2016        res = (res && (nch == 1 || (nch == 5 &&
2017            validate_name(CHILD(tree, 1), "if") &&
2018            validate_or_test(CHILD(tree, 2)) &&
2019            validate_name(CHILD(tree, 3), "else") &&
2020            validate_test(CHILD(tree, 4)))));
2021    }
2022    return (res);
2023}
2024
2025static int
2026validate_test_nocond(node *tree)
2027{
2028    int nch = NCH(tree);
2029    int res = validate_ntype(tree, test_nocond) && (nch == 1);
2030
2031    if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
2032        res = (validate_lambdef_nocond(CHILD(tree, 0)));
2033    else if (res) {
2034        res = (validate_or_test(CHILD(tree, 0)));
2035    }
2036    return (res);
2037}
2038
2039static int
2040validate_or_test(node *tree)
2041{
2042    int nch = NCH(tree);
2043    int res = validate_ntype(tree, or_test) && is_odd(nch);
2044
2045    if (res) {
2046        int pos;
2047        res = validate_and_test(CHILD(tree, 0));
2048        for (pos = 1; res && (pos < nch); pos += 2)
2049            res = (validate_name(CHILD(tree, pos), "or")
2050                   && validate_and_test(CHILD(tree, pos + 1)));
2051    }
2052    return (res);
2053}
2054
2055
2056static int
2057validate_and_test(node *tree)
2058{
2059    int pos;
2060    int nch = NCH(tree);
2061    int res = (validate_ntype(tree, and_test)
2062               && is_odd(nch)
2063               && validate_not_test(CHILD(tree, 0)));
2064
2065    for (pos = 1; res && (pos < nch); pos += 2)
2066        res = (validate_name(CHILD(tree, pos), "and")
2067               && validate_not_test(CHILD(tree, 0)));
2068
2069    return (res);
2070}
2071
2072
2073static int
2074validate_not_test(node *tree)
2075{
2076    int nch = NCH(tree);
2077    int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
2078
2079    if (res) {
2080        if (nch == 2)
2081            res = (validate_name(CHILD(tree, 0), "not")
2082                   && validate_not_test(CHILD(tree, 1)));
2083        else if (nch == 1)
2084            res = validate_comparison(CHILD(tree, 0));
2085    }
2086    return (res);
2087}
2088
2089
2090static int
2091validate_comparison(node *tree)
2092{
2093    int pos;
2094    int nch = NCH(tree);
2095    int res = (validate_ntype(tree, comparison)
2096               && is_odd(nch)
2097               && validate_expr(CHILD(tree, 0)));
2098
2099    for (pos = 1; res && (pos < nch); pos += 2)
2100        res = (validate_comp_op(CHILD(tree, pos))
2101               && validate_expr(CHILD(tree, pos + 1)));
2102
2103    return (res);
2104}
2105
2106
2107static int
2108validate_comp_op(node *tree)
2109{
2110    int res = 0;
2111    int nch = NCH(tree);
2112
2113    if (!validate_ntype(tree, comp_op))
2114        return (0);
2115    if (nch == 1) {
2116        /*
2117         *  Only child will be a terminal with a well-defined symbolic name
2118         *  or a NAME with a string of either 'is' or 'in'
2119         */
2120        tree = CHILD(tree, 0);
2121        switch (TYPE(tree)) {
2122            case LESS:
2123            case GREATER:
2124            case EQEQUAL:
2125            case EQUAL:
2126            case LESSEQUAL:
2127            case GREATEREQUAL:
2128            case NOTEQUAL:
2129              res = 1;
2130              break;
2131            case NAME:
2132              res = ((strcmp(STR(tree), "in") == 0)
2133                     || (strcmp(STR(tree), "is") == 0));
2134              if (!res) {
2135                  PyErr_Format(parser_error,
2136                               "illegal operator '%s'", STR(tree));
2137              }
2138              break;
2139          default:
2140              err_string("illegal comparison operator type");
2141              break;
2142        }
2143    }
2144    else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
2145        res = (validate_ntype(CHILD(tree, 0), NAME)
2146               && validate_ntype(CHILD(tree, 1), NAME)
2147               && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2148                    && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2149                   || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2150                       && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2151        if (!res && !PyErr_Occurred())
2152            err_string("unknown comparison operator");
2153    }
2154    return (res);
2155}
2156
2157
2158static int
2159validate_star_expr(node *tree)
2160{
2161    int res = validate_ntype(tree, star_expr);
2162    if (!res) return res;
2163    if (!validate_numnodes(tree, 2, "star_expr"))
2164        return 0;
2165    return validate_ntype(CHILD(tree, 0), STAR) &&      \
2166        validate_expr(CHILD(tree, 1));
2167}
2168
2169
2170static int
2171validate_expr(node *tree)
2172{
2173    int j;
2174    int nch = NCH(tree);
2175    int res = (validate_ntype(tree, expr)
2176               && is_odd(nch)
2177               && validate_xor_expr(CHILD(tree, 0)));
2178
2179    for (j = 2; res && (j < nch); j += 2)
2180        res = (validate_xor_expr(CHILD(tree, j))
2181               && validate_vbar(CHILD(tree, j - 1)));
2182
2183    return (res);
2184}
2185
2186
2187static int
2188validate_xor_expr(node *tree)
2189{
2190    int j;
2191    int nch = NCH(tree);
2192    int res = (validate_ntype(tree, xor_expr)
2193               && is_odd(nch)
2194               && validate_and_expr(CHILD(tree, 0)));
2195
2196    for (j = 2; res && (j < nch); j += 2)
2197        res = (validate_circumflex(CHILD(tree, j - 1))
2198               && validate_and_expr(CHILD(tree, j)));
2199
2200    return (res);
2201}
2202
2203
2204static int
2205validate_and_expr(node *tree)
2206{
2207    int pos;
2208    int nch = NCH(tree);
2209    int res = (validate_ntype(tree, and_expr)
2210               && is_odd(nch)
2211               && validate_shift_expr(CHILD(tree, 0)));
2212
2213    for (pos = 1; res && (pos < nch); pos += 2)
2214        res = (validate_ampersand(CHILD(tree, pos))
2215               && validate_shift_expr(CHILD(tree, pos + 1)));
2216
2217    return (res);
2218}
2219
2220
2221static int
2222validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
2223 {
2224    int pos = 1;
2225    int nch = NCH(tree);
2226    int res = (is_odd(nch)
2227               && (*termvalid)(CHILD(tree, 0)));
2228
2229    for ( ; res && (pos < nch); pos += 2) {
2230        if (TYPE(CHILD(tree, pos)) != op1)
2231            res = validate_ntype(CHILD(tree, pos), op2);
2232        if (res)
2233            res = (*termvalid)(CHILD(tree, pos + 1));
2234    }
2235    return (res);
2236}
2237
2238
2239static int
2240validate_shift_expr(node *tree)
2241{
2242    return (validate_ntype(tree, shift_expr)
2243            && validate_chain_two_ops(tree, validate_arith_expr,
2244                                      LEFTSHIFT, RIGHTSHIFT));
2245}
2246
2247
2248static int
2249validate_arith_expr(node *tree)
2250{
2251    return (validate_ntype(tree, arith_expr)
2252            && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2253}
2254
2255
2256static int
2257validate_term(node *tree)
2258{
2259    int pos = 1;
2260    int nch = NCH(tree);
2261    int res = (validate_ntype(tree, term)
2262               && is_odd(nch)
2263               && validate_factor(CHILD(tree, 0)));
2264
2265    for ( ; res && (pos < nch); pos += 2)
2266        res = (((TYPE(CHILD(tree, pos)) == STAR)
2267               || (TYPE(CHILD(tree, pos)) == SLASH)
2268               || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
2269               || (TYPE(CHILD(tree, pos)) == PERCENT))
2270               && validate_factor(CHILD(tree, pos + 1)));
2271
2272    return (res);
2273}
2274
2275
2276/*  factor:
2277 *
2278 *  factor: ('+'|'-'|'~') factor | power
2279 */
2280static int
2281validate_factor(node *tree)
2282{
2283    int nch = NCH(tree);
2284    int res = (validate_ntype(tree, factor)
2285               && (((nch == 2)
2286                    && ((TYPE(CHILD(tree, 0)) == PLUS)
2287                        || (TYPE(CHILD(tree, 0)) == MINUS)
2288                        || (TYPE(CHILD(tree, 0)) == TILDE))
2289                    && validate_factor(CHILD(tree, 1)))
2290                   || ((nch == 1)
2291                       && validate_power(CHILD(tree, 0)))));
2292    return (res);
2293}
2294
2295
2296/*  power:
2297 *
2298 *  power: atom trailer* ('**' factor)*
2299 */
2300static int
2301validate_power(node *tree)
2302{
2303    int pos = 1;
2304    int nch = NCH(tree);
2305    int res = (validate_ntype(tree, power) && (nch >= 1)
2306               && validate_atom(CHILD(tree, 0)));
2307
2308    while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
2309        res = validate_trailer(CHILD(tree, pos++));
2310    if (res && (pos < nch)) {
2311        if (!is_even(nch - pos)) {
2312            err_string("illegal number of nodes for 'power'");
2313            return (0);
2314        }
2315        for ( ; res && (pos < (nch - 1)); pos += 2)
2316            res = (validate_doublestar(CHILD(tree, pos))
2317                   && validate_factor(CHILD(tree, pos + 1)));
2318    }
2319    return (res);
2320}
2321
2322
2323static int
2324validate_atom(node *tree)
2325{
2326    int pos;
2327    int nch = NCH(tree);
2328    int res = validate_ntype(tree, atom);
2329
2330    if (res && nch < 1)
2331        res = validate_numnodes(tree, nch+1, "atom");
2332    if (res) {
2333        switch (TYPE(CHILD(tree, 0))) {
2334          case LPAR:
2335            res = ((nch <= 3)
2336                   && (validate_rparen(CHILD(tree, nch - 1))));
2337
2338            if (res && (nch == 3)) {
2339                if (TYPE(CHILD(tree, 1))==yield_expr)
2340                        res = validate_yield_expr(CHILD(tree, 1));
2341                else
2342                        res = validate_testlist_comp(CHILD(tree, 1));
2343            }
2344            break;
2345          case LSQB:
2346            if (nch == 2)
2347                res = validate_ntype(CHILD(tree, 1), RSQB);
2348            else if (nch == 3)
2349                res = (validate_testlist_comp(CHILD(tree, 1))
2350                       && validate_ntype(CHILD(tree, 2), RSQB));
2351            else {
2352                res = 0;
2353                err_string("illegal list display atom");
2354            }
2355            break;
2356          case LBRACE:
2357            res = ((nch <= 3)
2358                   && validate_ntype(CHILD(tree, nch - 1), RBRACE));
2359
2360            if (res && (nch == 3))
2361                res = validate_dictorsetmaker(CHILD(tree, 1));
2362            break;
2363          case NAME:
2364          case NUMBER:
2365            res = (nch == 1);
2366            break;
2367          case STRING:
2368            for (pos = 1; res && (pos < nch); ++pos)
2369                res = validate_ntype(CHILD(tree, pos), STRING);
2370            break;
2371          case DOT:
2372            res = (nch == 3 &&
2373                   validate_ntype(CHILD(tree, 1), DOT) &&
2374                   validate_ntype(CHILD(tree, 2), DOT));
2375            break;
2376          default:
2377            res = 0;
2378            break;
2379        }
2380    }
2381    return (res);
2382}
2383
2384
2385/*  testlist_comp:
2386 *    test ( comp_for | (',' test)* [','] )
2387 */
2388static int
2389validate_testlist_comp(node *tree)
2390{
2391    int nch = NCH(tree);
2392    int ok = nch;
2393
2394    if (nch == 0)
2395        err_string("missing child nodes of testlist_comp");
2396    else {
2397        ok = validate_test_or_star_expr(CHILD(tree, 0));
2398    }
2399
2400    /*
2401     *  comp_for | (',' test)* [',']
2402     */
2403    if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
2404        ok = validate_comp_for(CHILD(tree, 1));
2405    else {
2406        /*  (',' test)* [',']  */
2407        int i = 1;
2408        while (ok && nch - i >= 2) {
2409            ok = (validate_comma(CHILD(tree, i))
2410                  && validate_test_or_star_expr(CHILD(tree, i+1)));
2411            i += 2;
2412        }
2413        if (ok && i == nch-1)
2414            ok = validate_comma(CHILD(tree, i));
2415        else if (i != nch) {
2416            ok = 0;
2417            err_string("illegal trailing nodes for testlist_comp");
2418        }
2419    }
2420    return ok;
2421}
2422
2423/*  decorator:
2424 *    '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2425 */
2426static int
2427validate_decorator(node *tree)
2428{
2429    int ok;
2430    int nch = NCH(tree);
2431    ok = (validate_ntype(tree, decorator) &&
2432          (nch == 3 || nch == 5 || nch == 6) &&
2433          validate_at(CHILD(tree, 0)) &&
2434          validate_dotted_name(CHILD(tree, 1)) &&
2435          validate_newline(RCHILD(tree, -1)));
2436
2437    if (ok && nch != 3) {
2438        ok = (validate_lparen(CHILD(tree, 2)) &&
2439              validate_rparen(RCHILD(tree, -2)));
2440
2441        if (ok && nch == 6)
2442            ok = validate_arglist(CHILD(tree, 3));
2443    }
2444
2445    return ok;
2446}
2447
2448/*  decorators:
2449 *    decorator+
2450 */
2451static int
2452validate_decorators(node *tree)
2453{
2454    int i, nch, ok;
2455    nch = NCH(tree);
2456    ok = validate_ntype(tree, decorators) && nch >= 1;
2457
2458    for (i = 0; ok && i < nch; ++i)
2459        ok = validate_decorator(CHILD(tree, i));
2460
2461    return ok;
2462}
2463
2464/*  with_item:
2465 *   test ['as' expr]
2466 */
2467static int
2468validate_with_item(node *tree)
2469{
2470    int nch = NCH(tree);
2471    int ok = (validate_ntype(tree, with_item)
2472              && (nch == 1 || nch == 3)
2473              && validate_test(CHILD(tree, 0)));
2474    if (ok && nch == 3)
2475        ok = (validate_name(CHILD(tree, 1), "as")
2476              && validate_expr(CHILD(tree, 2)));
2477    return ok;
2478}
2479
2480/*  with_stmt:
2481 *    0      1          ...             -2   -1
2482 *   'with' with_item (',' with_item)* ':' suite
2483 */
2484static int
2485validate_with_stmt(node *tree)
2486{
2487    int i;
2488    int nch = NCH(tree);
2489    int ok = (validate_ntype(tree, with_stmt)
2490        && (nch % 2 == 0)
2491        && validate_name(CHILD(tree, 0), "with")
2492        && validate_colon(RCHILD(tree, -2))
2493        && validate_suite(RCHILD(tree, -1)));
2494    for (i = 1; ok && i < nch - 2; i += 2)
2495        ok = validate_with_item(CHILD(tree, i));
2496    return ok;
2497}
2498
2499/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
2500
2501static int
2502validate_funcdef(node *tree)
2503{
2504    int nch = NCH(tree);
2505    int res = validate_ntype(tree, funcdef);
2506    if (res) {
2507        if (nch == 5) {
2508            res = (validate_name(CHILD(tree, 0), "def")
2509                   && validate_ntype(CHILD(tree, 1), NAME)
2510                   && validate_parameters(CHILD(tree, 2))
2511                   && validate_colon(CHILD(tree, 3))
2512                   && validate_suite(CHILD(tree, 4)));
2513        }
2514        else if (nch == 7) {
2515            res = (validate_name(CHILD(tree, 0), "def")
2516                   && validate_ntype(CHILD(tree, 1), NAME)
2517                   && validate_parameters(CHILD(tree, 2))
2518                   && validate_rarrow(CHILD(tree, 3))
2519                   && validate_test(CHILD(tree, 4))
2520                   && validate_colon(CHILD(tree, 5))
2521                   && validate_suite(CHILD(tree, 6)));
2522        }
2523        else {
2524            res = 0;
2525            err_string("illegal number of children for funcdef");
2526        }
2527    }
2528    return res;
2529}
2530
2531
2532/* decorated
2533 *   decorators (classdef | funcdef)
2534 */
2535static int
2536validate_decorated(node *tree)
2537{
2538    int nch = NCH(tree);
2539    int ok = (validate_ntype(tree, decorated)
2540              && (nch == 2)
2541              && validate_decorators(RCHILD(tree, -2)));
2542    if (TYPE(RCHILD(tree, -1)) == funcdef)
2543        ok = ok && validate_funcdef(RCHILD(tree, -1));
2544    else
2545        ok = ok && validate_class(RCHILD(tree, -1));
2546    return ok;
2547}
2548
2549static int
2550validate_lambdef(node *tree)
2551{
2552    int nch = NCH(tree);
2553    int res = (validate_ntype(tree, lambdef)
2554               && ((nch == 3) || (nch == 4))
2555               && validate_name(CHILD(tree, 0), "lambda")
2556               && validate_colon(CHILD(tree, nch - 2))
2557               && validate_test(CHILD(tree, nch - 1)));
2558
2559    if (res && (nch == 4))
2560        res = validate_varargslist(CHILD(tree, 1));
2561    else if (!res && !PyErr_Occurred())
2562        (void) validate_numnodes(tree, 3, "lambdef");
2563
2564    return (res);
2565}
2566
2567
2568static int
2569validate_lambdef_nocond(node *tree)
2570{
2571    int nch = NCH(tree);
2572    int res = (validate_ntype(tree, lambdef_nocond)
2573               && ((nch == 3) || (nch == 4))
2574               && validate_name(CHILD(tree, 0), "lambda")
2575               && validate_colon(CHILD(tree, nch - 2))
2576               && validate_test(CHILD(tree, nch - 1)));
2577
2578    if (res && (nch == 4))
2579        res = validate_varargslist(CHILD(tree, 1));
2580    else if (!res && !PyErr_Occurred())
2581        (void) validate_numnodes(tree, 3, "lambdef_nocond");
2582
2583    return (res);
2584}
2585
2586
2587/*  arglist:
2588 *
2589 *  (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2590 */
2591static int
2592validate_arglist(node *tree)
2593{
2594    int nch = NCH(tree);
2595    int i = 0;
2596    int ok = 1;
2597
2598    if (nch <= 0)
2599        /* raise the right error from having an invalid number of children */
2600        return validate_numnodes(tree, nch + 1, "arglist");
2601
2602    if (nch > 1) {
2603        for (i=0; i<nch; i++) {
2604            if (TYPE(CHILD(tree, i)) == argument) {
2605                node *ch = CHILD(tree, i);
2606                if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
2607                    err_string("need '(', ')' for generator expression");
2608                    return 0;
2609                }
2610            }
2611        }
2612    }
2613
2614    while (ok && nch-i >= 2) {
2615        /* skip leading (argument ',') */
2616        ok = (validate_argument(CHILD(tree, i))
2617              && validate_comma(CHILD(tree, i+1)));
2618        if (ok)
2619            i += 2;
2620        else
2621            PyErr_Clear();
2622    }
2623    ok = 1;
2624    if (nch-i > 0) {
2625        /*
2626         * argument | '*' test [',' '**' test] | '**' test
2627         */
2628        int sym = TYPE(CHILD(tree, i));
2629
2630        if (sym == argument) {
2631            ok = validate_argument(CHILD(tree, i));
2632            if (ok && i+1 != nch) {
2633                err_string("illegal arglist specification"
2634                           " (extra stuff on end)");
2635                ok = 0;
2636            }
2637        }
2638        else if (sym == STAR) {
2639            ok = validate_star(CHILD(tree, i));
2640            if (ok && (nch-i == 2))
2641                ok = validate_test(CHILD(tree, i+1));
2642            else if (ok && (nch-i == 5))
2643                ok = (validate_test(CHILD(tree, i+1))
2644                      && validate_comma(CHILD(tree, i+2))
2645                      && validate_doublestar(CHILD(tree, i+3))
2646                      && validate_test(CHILD(tree, i+4)));
2647            else {
2648                err_string("illegal use of '*' in arglist");
2649                ok = 0;
2650            }
2651        }
2652        else if (sym == DOUBLESTAR) {
2653            if (nch-i == 2)
2654                ok = (validate_doublestar(CHILD(tree, i))
2655                      && validate_test(CHILD(tree, i+1)));
2656            else {
2657                err_string("illegal use of '**' in arglist");
2658                ok = 0;
2659            }
2660        }
2661        else {
2662            err_string("illegal arglist specification");
2663            ok = 0;
2664        }
2665    }
2666    return (ok);
2667}
2668
2669
2670
2671/*  argument:
2672 *
2673 *  [test '='] test [comp_for]
2674 */
2675static int
2676validate_argument(node *tree)
2677{
2678    int nch = NCH(tree);
2679    int res = (validate_ntype(tree, argument)
2680               && ((nch == 1) || (nch == 2) || (nch == 3))
2681               && validate_test(CHILD(tree, 0)));
2682
2683    if (res && (nch == 2))
2684        res = validate_comp_for(CHILD(tree, 1));
2685    else if (res && (nch == 3))
2686        res = (validate_equal(CHILD(tree, 1))
2687               && validate_test(CHILD(tree, 2)));
2688
2689    return (res);
2690}
2691
2692
2693
2694/*  trailer:
2695 *
2696 *  '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2697 */
2698static int
2699validate_trailer(node *tree)
2700{
2701    int nch = NCH(tree);
2702    int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
2703
2704    if (res) {
2705        switch (TYPE(CHILD(tree, 0))) {
2706          case LPAR:
2707            res = validate_rparen(CHILD(tree, nch - 1));
2708            if (res && (nch == 3))
2709                res = validate_arglist(CHILD(tree, 1));
2710            break;
2711          case LSQB:
2712            res = (validate_numnodes(tree, 3, "trailer")
2713                   && validate_subscriptlist(CHILD(tree, 1))
2714                   && validate_ntype(CHILD(tree, 2), RSQB));
2715            break;
2716          case DOT:
2717            res = (validate_numnodes(tree, 2, "trailer")
2718                   && validate_ntype(CHILD(tree, 1), NAME));
2719            break;
2720          default:
2721            res = 0;
2722            break;
2723        }
2724    }
2725    else {
2726        (void) validate_numnodes(tree, 2, "trailer");
2727    }
2728    return (res);
2729}
2730
2731
2732/*  subscriptlist:
2733 *
2734 *  subscript (',' subscript)* [',']
2735 */
2736static int
2737validate_subscriptlist(node *tree)
2738{
2739    return (validate_repeating_list(tree, subscriptlist,
2740                                    validate_subscript, "subscriptlist"));
2741}
2742
2743
2744/*  subscript:
2745 *
2746 *  '.' '.' '.' | test | [test] ':' [test] [sliceop]
2747 */
2748static int
2749validate_subscript(node *tree)
2750{
2751    int offset = 0;
2752    int nch = NCH(tree);
2753    int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
2754
2755    if (!res) {
2756        if (!PyErr_Occurred())
2757            err_string("invalid number of arguments for subscript node");
2758        return (0);
2759    }
2760    if (TYPE(CHILD(tree, 0)) == DOT)
2761        /* take care of ('.' '.' '.') possibility */
2762        return (validate_numnodes(tree, 3, "subscript")
2763                && validate_dot(CHILD(tree, 0))
2764                && validate_dot(CHILD(tree, 1))
2765                && validate_dot(CHILD(tree, 2)));
2766    if (nch == 1) {
2767        if (TYPE(CHILD(tree, 0)) == test)
2768            res = validate_test(CHILD(tree, 0));
2769        else
2770            res = validate_colon(CHILD(tree, 0));
2771        return (res);
2772    }
2773    /*  Must be [test] ':' [test] [sliceop],
2774     *  but at least one of the optional components will
2775     *  be present, but we don't know which yet.
2776     */
2777    if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
2778        res = validate_test(CHILD(tree, 0));
2779        offset = 1;
2780    }
2781    if (res)
2782        res = validate_colon(CHILD(tree, offset));
2783    if (res) {
2784        int rem = nch - ++offset;
2785        if (rem) {
2786            if (TYPE(CHILD(tree, offset)) == test) {
2787                res = validate_test(CHILD(tree, offset));
2788                ++offset;
2789                --rem;
2790            }
2791            if (res && rem)
2792                res = validate_sliceop(CHILD(tree, offset));
2793        }
2794    }
2795    return (res);
2796}
2797
2798
2799static int
2800validate_sliceop(node *tree)
2801{
2802    int nch = NCH(tree);
2803    int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
2804              && validate_ntype(tree, sliceop);
2805    if (!res && !PyErr_Occurred()) {
2806        res = validate_numnodes(tree, 1, "sliceop");
2807    }
2808    if (res)
2809        res = validate_colon(CHILD(tree, 0));
2810    if (res && (nch == 2))
2811        res = validate_test(CHILD(tree, 1));
2812
2813    return (res);
2814}
2815
2816
2817static int
2818validate_test_or_star_expr(node *n)
2819{
2820    if (TYPE(n) == test)
2821        return validate_test(n);
2822    return validate_star_expr(n);
2823}
2824
2825static int
2826validate_expr_or_star_expr(node *n)
2827{
2828    if (TYPE(n) == expr)
2829        return validate_expr(n);
2830    return validate_star_expr(n);
2831}
2832
2833
2834static int
2835validate_exprlist(node *tree)
2836{
2837    return (validate_repeating_list(tree, exprlist,
2838                                    validate_expr_or_star_expr, "exprlist"));
2839}
2840
2841
2842static int
2843validate_dictorsetmaker(node *tree)
2844{
2845    int nch = NCH(tree);
2846    int res = (validate_ntype(tree, dictorsetmaker)
2847               && (nch >= 3)
2848               && validate_test(CHILD(tree, 0))
2849               && validate_colon(CHILD(tree, 1))
2850               && validate_test(CHILD(tree, 2)));
2851
2852    if (res && ((nch % 4) == 0))
2853        res = validate_comma(CHILD(tree, --nch));
2854    else if (res)
2855        res = ((nch % 4) == 3);
2856
2857    if (res && (nch > 3)) {
2858        int pos = 3;
2859        /*  ( ',' test ':' test )*  */
2860        while (res && (pos < nch)) {
2861            res = (validate_comma(CHILD(tree, pos))
2862                   && validate_test(CHILD(tree, pos + 1))
2863                   && validate_colon(CHILD(tree, pos + 2))
2864                   && validate_test(CHILD(tree, pos + 3)));
2865            pos += 4;
2866        }
2867    }
2868    return (res);
2869}
2870
2871
2872static int
2873validate_eval_input(node *tree)
2874{
2875    int pos;
2876    int nch = NCH(tree);
2877    int res = (validate_ntype(tree, eval_input)
2878               && (nch >= 2)
2879               && validate_testlist(CHILD(tree, 0))
2880               && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
2881
2882    for (pos = 1; res && (pos < (nch - 1)); ++pos)
2883        res = validate_ntype(CHILD(tree, pos), NEWLINE);
2884
2885    return (res);
2886}
2887
2888
2889static int
2890validate_node(node *tree)
2891{
2892    int   nch  = 0;                     /* num. children on current node  */
2893    int   res  = 1;                     /* result value                   */
2894    node* next = 0;                     /* node to process after this one */
2895
2896    while (res && (tree != 0)) {
2897        nch  = NCH(tree);
2898        next = 0;
2899        switch (TYPE(tree)) {
2900            /*
2901             *  Definition nodes.
2902             */
2903          case funcdef:
2904            res = validate_funcdef(tree);
2905            break;
2906          case with_stmt:
2907            res = validate_with_stmt(tree);
2908            break;
2909          case classdef:
2910            res = validate_class(tree);
2911            break;
2912          case decorated:
2913            res = validate_decorated(tree);
2914            break;
2915            /*
2916             *  "Trivial" parse tree nodes.
2917             *  (Why did I call these trivial?)
2918             */
2919          case stmt:
2920            res = validate_stmt(tree);
2921            break;
2922          case small_stmt:
2923            /*
2924             *  expr_stmt | del_stmt | pass_stmt | flow_stmt
2925             *  | import_stmt | global_stmt | assert_stmt
2926             */
2927            res = validate_small_stmt(tree);
2928            break;
2929          case flow_stmt:
2930            res  = (validate_numnodes(tree, 1, "flow_stmt")
2931                    && ((TYPE(CHILD(tree, 0)) == break_stmt)
2932                        || (TYPE(CHILD(tree, 0)) == continue_stmt)
2933                        || (TYPE(CHILD(tree, 0)) == yield_stmt)
2934                        || (TYPE(CHILD(tree, 0)) == return_stmt)
2935                        || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2936            if (res)
2937                next = CHILD(tree, 0);
2938            else if (nch == 1)
2939                err_string("illegal flow_stmt type");
2940            break;
2941          case yield_stmt:
2942            res = validate_yield_stmt(tree);
2943            break;
2944            /*
2945             *  Compound statements.
2946             */
2947          case simple_stmt:
2948            res = validate_simple_stmt(tree);
2949            break;
2950          case compound_stmt:
2951            res = validate_compound_stmt(tree);
2952            break;
2953            /*
2954             *  Fundamental statements.
2955             */
2956          case expr_stmt:
2957            res = validate_expr_stmt(tree);
2958            break;
2959          case del_stmt:
2960            res = validate_del_stmt(tree);
2961            break;
2962          case pass_stmt:
2963            res = (validate_numnodes(tree, 1, "pass")
2964                   && validate_name(CHILD(tree, 0), "pass"));
2965            break;
2966          case break_stmt:
2967            res = (validate_numnodes(tree, 1, "break")
2968                   && validate_name(CHILD(tree, 0), "break"));
2969            break;
2970          case continue_stmt:
2971            res = (validate_numnodes(tree, 1, "continue")
2972                   && validate_name(CHILD(tree, 0), "continue"));
2973            break;
2974          case return_stmt:
2975            res = validate_return_stmt(tree);
2976            break;
2977          case raise_stmt:
2978            res = validate_raise_stmt(tree);
2979            break;
2980          case import_stmt:
2981            res = validate_import_stmt(tree);
2982            break;
2983          case import_name:
2984            res = validate_import_name(tree);
2985            break;
2986          case import_from:
2987            res = validate_import_from(tree);
2988            break;
2989          case global_stmt:
2990            res = validate_global_stmt(tree);
2991            break;
2992          case assert_stmt:
2993            res = validate_assert_stmt(tree);
2994            break;
2995          case if_stmt:
2996            res = validate_if(tree);
2997            break;
2998          case while_stmt:
2999            res = validate_while(tree);
3000            break;
3001          case for_stmt:
3002            res = validate_for(tree);
3003            break;
3004          case try_stmt:
3005            res = validate_try(tree);
3006            break;
3007          case suite:
3008            res = validate_suite(tree);
3009            break;
3010            /*
3011             *  Expression nodes.
3012             */
3013          case testlist:
3014            res = validate_testlist(tree);
3015            break;
3016          case yield_expr:
3017            res = validate_yield_expr(tree);
3018            break;
3019          case test:
3020            res = validate_test(tree);
3021            break;
3022          case and_test:
3023            res = validate_and_test(tree);
3024            break;
3025          case not_test:
3026            res = validate_not_test(tree);
3027            break;
3028          case comparison:
3029            res = validate_comparison(tree);
3030            break;
3031          case exprlist:
3032            res = validate_exprlist(tree);
3033            break;
3034          case comp_op:
3035            res = validate_comp_op(tree);
3036            break;
3037          case expr:
3038            res = validate_expr(tree);
3039            break;
3040          case xor_expr:
3041            res = validate_xor_expr(tree);
3042            break;
3043          case and_expr:
3044            res = validate_and_expr(tree);
3045            break;
3046          case shift_expr:
3047            res = validate_shift_expr(tree);
3048            break;
3049          case arith_expr:
3050            res = validate_arith_expr(tree);
3051            break;
3052          case term:
3053            res = validate_term(tree);
3054            break;
3055          case factor:
3056            res = validate_factor(tree);
3057            break;
3058          case power:
3059            res = validate_power(tree);
3060            break;
3061          case atom:
3062            res = validate_atom(tree);
3063            break;
3064
3065          default:
3066            /* Hopefully never reached! */
3067            err_string("unrecognized node type");
3068            res = 0;
3069            break;
3070        }
3071        tree = next;
3072    }
3073    return (res);
3074}
3075
3076
3077static int
3078validate_expr_tree(node *tree)
3079{
3080    int res = validate_eval_input(tree);
3081
3082    if (!res && !PyErr_Occurred())
3083        err_string("could not validate expression tuple");
3084
3085    return (res);
3086}
3087
3088
3089/*  file_input:
3090 *      (NEWLINE | stmt)* ENDMARKER
3091 */
3092static int
3093validate_file_input(node *tree)
3094{
3095    int j;
3096    int nch = NCH(tree) - 1;
3097    int res = ((nch >= 0)
3098               && validate_ntype(CHILD(tree, nch), ENDMARKER));
3099
3100    for (j = 0; res && (j < nch); ++j) {
3101        if (TYPE(CHILD(tree, j)) == stmt)
3102            res = validate_stmt(CHILD(tree, j));
3103        else
3104            res = validate_newline(CHILD(tree, j));
3105    }
3106    /*  This stays in to prevent any internal failures from getting to the
3107     *  user.  Hopefully, this won't be needed.  If a user reports getting
3108     *  this, we have some debugging to do.
3109     */
3110    if (!res && !PyErr_Occurred())
3111        err_string("VALIDATION FAILURE: report this to the maintainer!");
3112
3113    return (res);
3114}
3115
3116static int
3117validate_encoding_decl(node *tree)
3118{
3119    int nch = NCH(tree);
3120    int res = ((nch == 1)
3121        && validate_file_input(CHILD(tree, 0)));
3122
3123    if (!res && !PyErr_Occurred())
3124        err_string("Error Parsing encoding_decl");
3125
3126    return res;
3127}
3128
3129static PyObject*
3130pickle_constructor = NULL;
3131
3132
3133static PyObject*
3134parser__pickler(PyObject *self, PyObject *args)
3135{
3136    NOTE(ARGUNUSED(self))
3137    PyObject *result = NULL;
3138    PyObject *st = NULL;
3139    PyObject *empty_dict = NULL;
3140
3141    if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
3142        PyObject *newargs;
3143        PyObject *tuple;
3144
3145        if ((empty_dict = PyDict_New()) == NULL)
3146            goto finally;
3147        if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
3148            goto finally;
3149        tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
3150        if (tuple != NULL) {
3151            result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3152            Py_DECREF(tuple);
3153        }
3154        Py_DECREF(empty_dict);
3155        Py_DECREF(newargs);
3156    }
3157  finally:
3158    Py_XDECREF(empty_dict);
3159
3160    return (result);
3161}
3162
3163
3164/*  Functions exported by this module.  Most of this should probably
3165 *  be converted into an ST object with methods, but that is better
3166 *  done directly in Python, allowing subclasses to be created directly.
3167 *  We'd really have to write a wrapper around it all anyway to allow
3168 *  inheritance.
3169 */
3170static PyMethodDef parser_functions[] =  {
3171    {"compilest",      (PyCFunction)parser_compilest,  PUBLIC_METHOD_TYPE,
3172        PyDoc_STR("Compiles an ST object into a code object.")},
3173    {"expr",            (PyCFunction)parser_expr,      PUBLIC_METHOD_TYPE,
3174        PyDoc_STR("Creates an ST object from an expression.")},
3175    {"isexpr",          (PyCFunction)parser_isexpr,    PUBLIC_METHOD_TYPE,
3176        PyDoc_STR("Determines if an ST object was created from an expression.")},
3177    {"issuite",         (PyCFunction)parser_issuite,   PUBLIC_METHOD_TYPE,
3178        PyDoc_STR("Determines if an ST object was created from a suite.")},
3179    {"suite",           (PyCFunction)parser_suite,     PUBLIC_METHOD_TYPE,
3180        PyDoc_STR("Creates an ST object from a suite.")},
3181    {"sequence2st",     (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,
3182        PyDoc_STR("Creates an ST object from a tree representation.")},
3183    {"st2tuple",        (PyCFunction)parser_st2tuple,  PUBLIC_METHOD_TYPE,
3184        PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3185    {"st2list",         (PyCFunction)parser_st2list,   PUBLIC_METHOD_TYPE,
3186        PyDoc_STR("Creates a list-tree representation of an ST.")},
3187    {"tuple2st",        (PyCFunction)parser_tuple2st,  PUBLIC_METHOD_TYPE,
3188        PyDoc_STR("Creates an ST object from a tree representation.")},
3189
3190    /* private stuff: support pickle module */
3191    {"_pickler",        (PyCFunction)parser__pickler,  METH_VARARGS,
3192        PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
3193
3194    {NULL, NULL, 0, NULL}
3195    };
3196
3197
3198
3199static struct PyModuleDef parsermodule = {
3200        PyModuleDef_HEAD_INIT,
3201        "parser",
3202        NULL,
3203        -1,
3204        parser_functions,
3205        NULL,
3206        NULL,
3207        NULL,
3208        NULL
3209};
3210
3211PyMODINIT_FUNC PyInit_parser(void);  /* supply a prototype */
3212
3213PyMODINIT_FUNC
3214PyInit_parser(void)
3215{
3216    PyObject *module, *copyreg;
3217
3218    if (PyType_Ready(&PyST_Type) < 0)
3219        return NULL;
3220    module = PyModule_Create(&parsermodule);
3221    if (module == NULL)
3222        return NULL;
3223
3224    if (parser_error == 0)
3225        parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
3226
3227    if (parser_error == 0)
3228        return NULL;
3229    /* CAUTION:  The code next used to skip bumping the refcount on
3230     * parser_error.  That's a disaster if PyInit_parser() gets called more
3231     * than once.  By incref'ing, we ensure that each module dict that
3232     * gets created owns its reference to the shared parser_error object,
3233     * and the file static parser_error vrbl owns a reference too.
3234     */
3235    Py_INCREF(parser_error);
3236    if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3237        return NULL;
3238
3239    Py_INCREF(&PyST_Type);
3240    PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
3241
3242    PyModule_AddStringConstant(module, "__copyright__",
3243                               parser_copyright_string);
3244    PyModule_AddStringConstant(module, "__doc__",
3245                               parser_doc_string);
3246    PyModule_AddStringConstant(module, "__version__",
3247                               parser_version_string);
3248
3249    /* Register to support pickling.
3250     * If this fails, the import of this module will fail because an
3251     * exception will be raised here; should we clear the exception?
3252     */
3253    copyreg = PyImport_ImportModuleNoBlock("copyreg");
3254    if (copyreg != NULL) {
3255        PyObject *func, *pickler;
3256
3257        func = PyObject_GetAttrString(copyreg, "pickle");
3258        pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3259        pickler = PyObject_GetAttrString(module, "_pickler");
3260        Py_XINCREF(pickle_constructor);
3261        if ((func != NULL) && (pickle_constructor != NULL)
3262            && (pickler != NULL)) {
3263            PyObject *res;
3264
3265            res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3266                                               pickle_constructor, NULL);
3267            Py_XDECREF(res);
3268        }
3269        Py_XDECREF(func);
3270        Py_XDECREF(pickle_constructor);
3271        Py_XDECREF(pickler);
3272        Py_DECREF(copyreg);
3273    }
3274    return module;
3275}
3276