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