Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Modules / parsermodule.c
blobeb9599c95262b3aba915ff70b241ac90ca9646c3
1 /* parsermodule.c
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.
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.
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.
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(...)".
28 #include "Python.h" /* general Python API */
29 #include "graminit.h" /* symbols defined in the grammar */
30 #include "node.h" /* internal parser structure */
31 #include "errcode.h" /* error codes for PyNode_*() */
32 #include "token.h" /* token definitions */
33 /* ISTERMINAL() / ISNONTERMINAL() */
34 #include "compile.h" /* PyNode_Compile() */
36 #ifdef lint
37 #include <note.h>
38 #else
39 #define NOTE(x)
40 #endif
42 #ifdef macintosh
43 char *strdup(char *);
44 #endif
46 /* String constants used to initialize module attributes.
49 static char parser_copyright_string[] =
50 "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
51 University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52 Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53 Centrum, Amsterdam, The Netherlands.";
56 PyDoc_STRVAR(parser_doc_string,
57 "This is an interface to Python's internal parser.");
59 static char parser_version_string[] = "0.5";
62 typedef PyObject* (*SeqMaker) (int length);
63 typedef int (*SeqInserter) (PyObject* sequence,
64 int index,
65 PyObject* element);
67 /* The function below is copyrighted by Stichting Mathematisch Centrum. The
68 * original copyright statement is included below, and continues to apply
69 * in full to the function immediately following. All other material is
70 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
71 * Institute and State University. Changes were made to comply with the
72 * new naming conventions. Added arguments to provide support for creating
73 * lists as well as tuples, and optionally including the line numbers.
77 static PyObject*
78 node2tuple(node *n, /* node to convert */
79 SeqMaker mkseq, /* create sequence */
80 SeqInserter addelem, /* func. to add elem. in seq. */
81 int lineno) /* include line numbers? */
83 if (n == NULL) {
84 Py_INCREF(Py_None);
85 return (Py_None);
87 if (ISNONTERMINAL(TYPE(n))) {
88 int i;
89 PyObject *v;
90 PyObject *w;
92 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
93 if (v == NULL)
94 return (v);
95 w = PyInt_FromLong(TYPE(n));
96 if (w == NULL) {
97 Py_DECREF(v);
98 return ((PyObject*) NULL);
100 (void) addelem(v, 0, w);
101 for (i = 0; i < NCH(n); i++) {
102 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
103 if (w == NULL) {
104 Py_DECREF(v);
105 return ((PyObject*) NULL);
107 (void) addelem(v, i+1, w);
110 if (TYPE(n) == encoding_decl)
111 (void) addelem(v, i+1, PyString_FromString(STR(n)));
112 return (v);
114 else if (ISTERMINAL(TYPE(n))) {
115 PyObject *result = mkseq(2 + lineno);
116 if (result != NULL) {
117 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
118 (void) addelem(result, 1, PyString_FromString(STR(n)));
119 if (lineno == 1)
120 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
122 return (result);
124 else {
125 PyErr_SetString(PyExc_SystemError,
126 "unrecognized parse tree node type");
127 return ((PyObject*) NULL);
131 * End of material copyrighted by Stichting Mathematisch Centrum.
136 /* There are two types of intermediate objects we're interested in:
137 * 'eval' and 'exec' types. These constants can be used in the st_type
138 * field of the object type to identify which any given object represents.
139 * These should probably go in an external header to allow other extensions
140 * to use them, but then, we really should be using C++ too. ;-)
143 #define PyST_EXPR 1
144 #define PyST_SUITE 2
147 /* These are the internal objects and definitions required to implement the
148 * ST type. Most of the internal names are more reminiscent of the 'old'
149 * naming style, but the code uses the new naming convention.
152 static PyObject*
153 parser_error = 0;
156 typedef struct {
157 PyObject_HEAD /* standard object header */
158 node* st_node; /* the node* returned by the parser */
159 int st_type; /* EXPR or SUITE ? */
160 } PyST_Object;
163 static void parser_free(PyST_Object *st);
164 static int parser_compare(PyST_Object *left, PyST_Object *right);
165 static PyObject *parser_getattr(PyObject *self, char *name);
168 static
169 PyTypeObject PyST_Type = {
170 PyObject_HEAD_INIT(NULL)
172 "parser.st", /* tp_name */
173 (int) sizeof(PyST_Object), /* tp_basicsize */
174 0, /* tp_itemsize */
175 (destructor)parser_free, /* tp_dealloc */
176 0, /* tp_print */
177 parser_getattr, /* tp_getattr */
178 0, /* tp_setattr */
179 (cmpfunc)parser_compare, /* tp_compare */
180 0, /* tp_repr */
181 0, /* tp_as_number */
182 0, /* tp_as_sequence */
183 0, /* tp_as_mapping */
184 0, /* tp_hash */
185 0, /* tp_call */
186 0, /* tp_str */
187 0, /* tp_getattro */
188 0, /* tp_setattro */
190 /* Functions to access object as input/output buffer */
191 0, /* tp_as_buffer */
193 Py_TPFLAGS_DEFAULT, /* tp_flags */
195 /* __doc__ */
196 "Intermediate representation of a Python parse tree."
197 }; /* PyST_Type */
200 static int
201 parser_compare_nodes(node *left, node *right)
203 int j;
205 if (TYPE(left) < TYPE(right))
206 return (-1);
208 if (TYPE(right) < TYPE(left))
209 return (1);
211 if (ISTERMINAL(TYPE(left)))
212 return (strcmp(STR(left), STR(right)));
214 if (NCH(left) < NCH(right))
215 return (-1);
217 if (NCH(right) < NCH(left))
218 return (1);
220 for (j = 0; j < NCH(left); ++j) {
221 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
223 if (v != 0)
224 return (v);
226 return (0);
230 /* int parser_compare(PyST_Object* left, PyST_Object* right)
232 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
233 * This really just wraps a call to parser_compare_nodes() with some easy
234 * checks and protection code.
237 static int
238 parser_compare(PyST_Object *left, PyST_Object *right)
240 if (left == right)
241 return (0);
243 if ((left == 0) || (right == 0))
244 return (-1);
246 return (parser_compare_nodes(left->st_node, right->st_node));
250 /* parser_newstobject(node* st)
252 * Allocates a new Python object representing an ST. This is simply the
253 * 'wrapper' object that holds a node* and allows it to be passed around in
254 * Python code.
257 static PyObject*
258 parser_newstobject(node *st, int type)
260 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
262 if (o != 0) {
263 o->st_node = st;
264 o->st_type = type;
266 else {
267 PyNode_Free(st);
269 return ((PyObject*)o);
273 /* void parser_free(PyST_Object* st)
275 * This is called by a del statement that reduces the reference count to 0.
278 static void
279 parser_free(PyST_Object *st)
281 PyNode_Free(st->st_node);
282 PyObject_Del(st);
286 /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
288 * This provides conversion from a node* to a tuple object that can be
289 * returned to the Python-level caller. The ST object is not modified.
292 static PyObject*
293 parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
295 PyObject *line_option = 0;
296 PyObject *res = 0;
297 int ok;
299 static char *keywords[] = {"ast", "line_info", NULL};
301 if (self == NULL) {
302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2tuple", keywords,
303 &PyST_Type, &self, &line_option);
305 else
306 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
307 &line_option);
308 if (ok != 0) {
309 int lineno = 0;
310 if (line_option != NULL) {
311 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
314 * Convert ST into a tuple representation. Use Guido's function,
315 * since it's known to work already.
317 res = node2tuple(((PyST_Object*)self)->st_node,
318 PyTuple_New, PyTuple_SetItem, lineno);
320 return (res);
324 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
326 * This provides conversion from a node* to a list object that can be
327 * returned to the Python-level caller. The ST object is not modified.
330 static PyObject*
331 parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
333 PyObject *line_option = 0;
334 PyObject *res = 0;
335 int ok;
337 static char *keywords[] = {"ast", "line_info", NULL};
339 if (self == NULL)
340 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:st2list", keywords,
341 &PyST_Type, &self, &line_option);
342 else
343 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
344 &line_option);
345 if (ok) {
346 int lineno = 0;
347 if (line_option != 0) {
348 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
351 * Convert ST into a tuple representation. Use Guido's function,
352 * since it's known to work already.
354 res = node2tuple(self->st_node,
355 PyList_New, PyList_SetItem, lineno);
357 return (res);
361 /* parser_compilest(PyObject* self, PyObject* args)
363 * This function creates code objects from the parse tree represented by
364 * the passed-in data object. An optional file name is passed in as well.
367 static PyObject*
368 parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
370 PyObject* res = 0;
371 char* str = "<syntax-tree>";
372 int ok;
374 static char *keywords[] = {"ast", "filename", NULL};
376 if (self == NULL)
377 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
378 &PyST_Type, &self, &str);
379 else
380 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
381 &str);
383 if (ok)
384 res = (PyObject *)PyNode_Compile(self->st_node, str);
386 return (res);
390 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
391 * PyObject* parser_issuite(PyObject* self, PyObject* args)
393 * Checks the passed-in ST object to determine if it is an expression or
394 * a statement suite, respectively. The return is a Python truth value.
397 static PyObject*
398 parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
400 PyObject* res = 0;
401 int ok;
403 static char *keywords[] = {"ast", NULL};
405 if (self == NULL)
406 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
407 &PyST_Type, &self);
408 else
409 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
411 if (ok) {
412 /* Check to see if the ST represents an expression or not. */
413 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
414 Py_INCREF(res);
416 return (res);
420 static PyObject*
421 parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
423 PyObject* res = 0;
424 int ok;
426 static char *keywords[] = {"ast", NULL};
428 if (self == NULL)
429 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
430 &PyST_Type, &self);
431 else
432 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
434 if (ok) {
435 /* Check to see if the ST represents an expression or not. */
436 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
437 Py_INCREF(res);
439 return (res);
443 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
445 static PyMethodDef
446 parser_methods[] = {
447 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
448 PyDoc_STR("Compile this ST object into a code object.")},
449 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
450 PyDoc_STR("Determines if this ST object was created from an expression.")},
451 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
452 PyDoc_STR("Determines if this ST object was created from a suite.")},
453 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
454 PyDoc_STR("Creates a list-tree representation of this ST.")},
455 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
456 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
458 {NULL, NULL, 0, NULL}
462 static PyObject*
463 parser_getattr(PyObject *self, char *name)
465 return (Py_FindMethod(parser_methods, self, name));
469 /* err_string(char* message)
471 * Sets the error string for an exception of type ParserError.
474 static void
475 err_string(char *message)
477 PyErr_SetString(parser_error, message);
481 /* PyObject* parser_do_parse(PyObject* args, int type)
483 * Internal function to actually execute the parse and return the result if
484 * successful or set an exception if not.
487 static PyObject*
488 parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
490 char* string = 0;
491 PyObject* res = 0;
493 static char *keywords[] = {"source", NULL};
495 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
496 node* n = PyParser_SimpleParseString(string,
497 (type == PyST_EXPR)
498 ? eval_input : file_input);
500 if (n)
501 res = parser_newstobject(n, type);
503 return (res);
507 /* PyObject* parser_expr(PyObject* self, PyObject* args)
508 * PyObject* parser_suite(PyObject* self, PyObject* args)
510 * External interfaces to the parser itself. Which is called determines if
511 * the parser attempts to recognize an expression ('eval' form) or statement
512 * suite ('exec' form). The real work is done by parser_do_parse() above.
515 static PyObject*
516 parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
518 NOTE(ARGUNUSED(self))
519 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
523 static PyObject*
524 parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
526 NOTE(ARGUNUSED(self))
527 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
532 /* This is the messy part of the code. Conversion from a tuple to an ST
533 * object requires that the input tuple be valid without having to rely on
534 * catching an exception from the compiler. This is done to allow the
535 * compiler itself to remain fast, since most of its input will come from
536 * the parser directly, and therefore be known to be syntactically correct.
537 * This validation is done to ensure that we don't core dump the compile
538 * phase, returning an exception instead.
540 * Two aspects can be broken out in this code: creating a node tree from
541 * the tuple passed in, and verifying that it is indeed valid. It may be
542 * advantageous to expand the number of ST types to include funcdefs and
543 * lambdadefs to take advantage of the optimizer, recognizing those STs
544 * here. They are not necessary, and not quite as useful in a raw form.
545 * For now, let's get expressions and suites working reliably.
549 static node* build_node_tree(PyObject *tuple);
550 static int validate_expr_tree(node *tree);
551 static int validate_file_input(node *tree);
552 static int validate_encoding_decl(node *tree);
554 /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
556 * This is the public function, called from the Python code. It receives a
557 * single tuple object from the caller, and creates an ST object if the
558 * tuple can be validated. It does this by checking the first code of the
559 * tuple, and, if acceptable, builds the internal representation. If this
560 * step succeeds, the internal representation is validated as fully as
561 * possible with the various validate_*() routines defined below.
563 * This function must be changed if support is to be added for PyST_FRAGMENT
564 * ST objects.
567 static PyObject*
568 parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
570 NOTE(ARGUNUSED(self))
571 PyObject *st = 0;
572 PyObject *tuple;
573 node *tree;
575 static char *keywords[] = {"sequence", NULL};
577 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
578 &tuple))
579 return (0);
580 if (!PySequence_Check(tuple)) {
581 PyErr_SetString(PyExc_ValueError,
582 "sequence2st() requires a single sequence argument");
583 return (0);
586 * Convert the tree to the internal form before checking it.
588 tree = build_node_tree(tuple);
589 if (tree != 0) {
590 int start_sym = TYPE(tree);
591 if (start_sym == eval_input) {
592 /* Might be an eval form. */
593 if (validate_expr_tree(tree))
594 st = parser_newstobject(tree, PyST_EXPR);
595 else
596 PyNode_Free(tree);
598 else if (start_sym == file_input) {
599 /* This looks like an exec form so far. */
600 if (validate_file_input(tree))
601 st = parser_newstobject(tree, PyST_SUITE);
602 else
603 PyNode_Free(tree);
605 else if (start_sym == encoding_decl) {
606 /* This looks like an encoding_decl so far. */
607 if (validate_encoding_decl(tree))
608 st = parser_newstobject(tree, PyST_SUITE);
609 else
610 PyNode_Free(tree);
612 else {
613 /* This is a fragment, at best. */
614 PyNode_Free(tree);
615 err_string("parse tree does not use a valid start symbol");
618 /* Make sure we throw an exception on all errors. We should never
619 * get this, but we'd do well to be sure something is done.
621 if (st == NULL && !PyErr_Occurred())
622 err_string("unspecified ST error occurred");
624 return st;
628 /* node* build_node_children()
630 * Iterate across the children of the current non-terminal node and build
631 * their structures. If successful, return the root of this portion of
632 * the tree, otherwise, 0. Any required exception will be specified already,
633 * and no memory will have been deallocated.
636 static node*
637 build_node_children(PyObject *tuple, node *root, int *line_num)
639 int len = PyObject_Size(tuple);
640 int i, err;
642 for (i = 1; i < len; ++i) {
643 /* elem must always be a sequence, however simple */
644 PyObject* elem = PySequence_GetItem(tuple, i);
645 int ok = elem != NULL;
646 long type = 0;
647 char *strn = 0;
649 if (ok)
650 ok = PySequence_Check(elem);
651 if (ok) {
652 PyObject *temp = PySequence_GetItem(elem, 0);
653 if (temp == NULL)
654 ok = 0;
655 else {
656 ok = PyInt_Check(temp);
657 if (ok)
658 type = PyInt_AS_LONG(temp);
659 Py_DECREF(temp);
662 if (!ok) {
663 PyErr_SetObject(parser_error,
664 Py_BuildValue("os", elem,
665 "Illegal node construct."));
666 Py_XDECREF(elem);
667 return (0);
669 if (ISTERMINAL(type)) {
670 int len = PyObject_Size(elem);
671 PyObject *temp;
673 if ((len != 2) && (len != 3)) {
674 err_string("terminal nodes must have 2 or 3 entries");
675 return 0;
677 temp = PySequence_GetItem(elem, 1);
678 if (temp == NULL)
679 return 0;
680 if (!PyString_Check(temp)) {
681 PyErr_Format(parser_error,
682 "second item in terminal node must be a string,"
683 " found %s",
684 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
685 Py_DECREF(temp);
686 return 0;
688 if (len == 3) {
689 PyObject *o = PySequence_GetItem(elem, 2);
690 if (o != NULL) {
691 if (PyInt_Check(o))
692 *line_num = PyInt_AS_LONG(o);
693 else {
694 PyErr_Format(parser_error,
695 "third item in terminal node must be an"
696 " integer, found %s",
697 ((PyTypeObject*)PyObject_Type(temp))->tp_name);
698 Py_DECREF(o);
699 Py_DECREF(temp);
700 return 0;
702 Py_DECREF(o);
705 len = PyString_GET_SIZE(temp) + 1;
706 strn = (char *)PyMem_MALLOC(len);
707 if (strn != NULL)
708 (void) memcpy(strn, PyString_AS_STRING(temp), len);
709 Py_DECREF(temp);
711 else if (!ISNONTERMINAL(type)) {
713 * It has to be one or the other; this is an error.
714 * Throw an exception.
716 PyErr_SetObject(parser_error,
717 Py_BuildValue("os", elem, "unknown node type."));
718 Py_XDECREF(elem);
719 return (0);
721 err = PyNode_AddChild(root, type, strn, *line_num);
722 if (err == E_NOMEM) {
723 PyMem_DEL(strn);
724 return (node *) PyErr_NoMemory();
726 if (err == E_OVERFLOW) {
727 PyMem_DEL(strn);
728 PyErr_SetString(PyExc_ValueError,
729 "unsupported number of child nodes");
730 return NULL;
733 if (ISNONTERMINAL(type)) {
734 node* new_child = CHILD(root, i - 1);
736 if (new_child != build_node_children(elem, new_child, line_num)) {
737 Py_XDECREF(elem);
738 return (0);
741 else if (type == NEWLINE) { /* It's true: we increment the */
742 ++(*line_num); /* line number *after* the newline! */
744 Py_XDECREF(elem);
746 return (root);
750 static node*
751 build_node_tree(PyObject *tuple)
753 node* res = 0;
754 PyObject *temp = PySequence_GetItem(tuple, 0);
755 long num = -1;
757 if (temp != NULL)
758 num = PyInt_AsLong(temp);
759 Py_XDECREF(temp);
760 if (ISTERMINAL(num)) {
762 * The tuple is simple, but it doesn't start with a start symbol.
763 * Throw an exception now and be done with it.
765 tuple = Py_BuildValue("os", tuple,
766 "Illegal syntax-tree; cannot start with terminal symbol.");
767 PyErr_SetObject(parser_error, tuple);
769 else if (ISNONTERMINAL(num)) {
771 * Not efficient, but that can be handled later.
773 int line_num = 0;
774 PyObject *encoding = NULL;
776 if (num == encoding_decl) {
777 encoding = PySequence_GetItem(tuple, 2);
778 /* tuple isn't borrowed anymore here, need to DECREF */
779 tuple = PySequence_GetSlice(tuple, 0, 2);
781 res = PyNode_New(num);
782 if (res != NULL) {
783 if (res != build_node_children(tuple, res, &line_num)) {
784 PyNode_Free(res);
785 res = NULL;
787 if (res && encoding) {
788 int len;
789 len = PyString_GET_SIZE(encoding) + 1;
790 res->n_str = (char *)PyMem_MALLOC(len);
791 if (res->n_str != NULL)
792 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
793 Py_DECREF(encoding);
794 Py_DECREF(tuple);
798 else
799 /* The tuple is illegal -- if the number is neither TERMINAL nor
800 * NONTERMINAL, we can't use it. Not sure the implementation
801 * allows this condition, but the API doesn't preclude it.
803 PyErr_SetObject(parser_error,
804 Py_BuildValue("os", tuple,
805 "Illegal component tuple."));
807 return (res);
812 * Validation routines used within the validation section:
814 static int validate_terminal(node *terminal, int type, char *string);
816 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
817 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
818 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
819 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
820 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
821 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
822 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
823 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
824 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
825 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
826 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
827 #define validate_star(ch) validate_terminal(ch, STAR, "*")
828 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
829 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
830 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
831 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
833 #define VALIDATER(n) static int validate_##n(node *tree)
835 VALIDATER(node); VALIDATER(small_stmt);
836 VALIDATER(class); VALIDATER(node);
837 VALIDATER(parameters); VALIDATER(suite);
838 VALIDATER(testlist); VALIDATER(varargslist);
839 VALIDATER(fpdef); VALIDATER(fplist);
840 VALIDATER(stmt); VALIDATER(simple_stmt);
841 VALIDATER(expr_stmt); VALIDATER(power);
842 VALIDATER(print_stmt); VALIDATER(del_stmt);
843 VALIDATER(return_stmt); VALIDATER(list_iter);
844 VALIDATER(raise_stmt); VALIDATER(import_stmt);
845 VALIDATER(global_stmt); VALIDATER(list_if);
846 VALIDATER(assert_stmt); VALIDATER(list_for);
847 VALIDATER(exec_stmt); VALIDATER(compound_stmt);
848 VALIDATER(while); VALIDATER(for);
849 VALIDATER(try); VALIDATER(except_clause);
850 VALIDATER(test); VALIDATER(and_test);
851 VALIDATER(not_test); VALIDATER(comparison);
852 VALIDATER(comp_op); VALIDATER(expr);
853 VALIDATER(xor_expr); VALIDATER(and_expr);
854 VALIDATER(shift_expr); VALIDATER(arith_expr);
855 VALIDATER(term); VALIDATER(factor);
856 VALIDATER(atom); VALIDATER(lambdef);
857 VALIDATER(trailer); VALIDATER(subscript);
858 VALIDATER(subscriptlist); VALIDATER(sliceop);
859 VALIDATER(exprlist); VALIDATER(dictmaker);
860 VALIDATER(arglist); VALIDATER(argument);
861 VALIDATER(listmaker); VALIDATER(yield_stmt);
862 VALIDATER(testlist1);
864 #undef VALIDATER
866 #define is_even(n) (((n) & 1) == 0)
867 #define is_odd(n) (((n) & 1) == 1)
870 static int
871 validate_ntype(node *n, int t)
873 if (TYPE(n) != t) {
874 PyErr_Format(parser_error, "Expected node type %d, got %d.",
875 t, TYPE(n));
876 return 0;
878 return 1;
882 /* Verifies that the number of child nodes is exactly 'num', raising
883 * an exception if it isn't. The exception message does not indicate
884 * the exact number of nodes, allowing this to be used to raise the
885 * "right" exception when the wrong number of nodes is present in a
886 * specific variant of a statement's syntax. This is commonly used
887 * in that fashion.
889 static int
890 validate_numnodes(node *n, int num, const char *const name)
892 if (NCH(n) != num) {
893 PyErr_Format(parser_error,
894 "Illegal number of children for %s node.", name);
895 return 0;
897 return 1;
901 static int
902 validate_terminal(node *terminal, int type, char *string)
904 int res = (validate_ntype(terminal, type)
905 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
907 if (!res && !PyErr_Occurred()) {
908 PyErr_Format(parser_error,
909 "Illegal terminal: expected \"%s\"", string);
911 return (res);
915 /* X (',' X) [',']
917 static int
918 validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
919 const char *const name)
921 int nch = NCH(tree);
922 int res = (nch && validate_ntype(tree, ntype)
923 && vfunc(CHILD(tree, 0)));
925 if (!res && !PyErr_Occurred())
926 (void) validate_numnodes(tree, 1, name);
927 else {
928 if (is_even(nch))
929 res = validate_comma(CHILD(tree, --nch));
930 if (res && nch > 1) {
931 int pos = 1;
932 for ( ; res && pos < nch; pos += 2)
933 res = (validate_comma(CHILD(tree, pos))
934 && vfunc(CHILD(tree, pos + 1)));
937 return (res);
941 /* validate_class()
943 * classdef:
944 * 'class' NAME ['(' testlist ')'] ':' suite
946 static int
947 validate_class(node *tree)
949 int nch = NCH(tree);
950 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
952 if (res) {
953 res = (validate_name(CHILD(tree, 0), "class")
954 && validate_ntype(CHILD(tree, 1), NAME)
955 && validate_colon(CHILD(tree, nch - 2))
956 && validate_suite(CHILD(tree, nch - 1)));
958 else
959 (void) validate_numnodes(tree, 4, "class");
960 if (res && (nch == 7)) {
961 res = (validate_lparen(CHILD(tree, 2))
962 && validate_testlist(CHILD(tree, 3))
963 && validate_rparen(CHILD(tree, 4)));
965 return (res);
969 /* if_stmt:
970 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
972 static int
973 validate_if(node *tree)
975 int nch = NCH(tree);
976 int res = (validate_ntype(tree, if_stmt)
977 && (nch >= 4)
978 && validate_name(CHILD(tree, 0), "if")
979 && validate_test(CHILD(tree, 1))
980 && validate_colon(CHILD(tree, 2))
981 && validate_suite(CHILD(tree, 3)));
983 if (res && ((nch % 4) == 3)) {
984 /* ... 'else' ':' suite */
985 res = (validate_name(CHILD(tree, nch - 3), "else")
986 && validate_colon(CHILD(tree, nch - 2))
987 && validate_suite(CHILD(tree, nch - 1)));
988 nch -= 3;
990 else if (!res && !PyErr_Occurred())
991 (void) validate_numnodes(tree, 4, "if");
992 if ((nch % 4) != 0)
993 /* Will catch the case for nch < 4 */
994 res = validate_numnodes(tree, 0, "if");
995 else if (res && (nch > 4)) {
996 /* ... ('elif' test ':' suite)+ ... */
997 int j = 4;
998 while ((j < nch) && res) {
999 res = (validate_name(CHILD(tree, j), "elif")
1000 && validate_colon(CHILD(tree, j + 2))
1001 && validate_test(CHILD(tree, j + 1))
1002 && validate_suite(CHILD(tree, j + 3)));
1003 j += 4;
1006 return (res);
1010 /* parameters:
1011 * '(' [varargslist] ')'
1014 static int
1015 validate_parameters(node *tree)
1017 int nch = NCH(tree);
1018 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
1020 if (res) {
1021 res = (validate_lparen(CHILD(tree, 0))
1022 && validate_rparen(CHILD(tree, nch - 1)));
1023 if (res && (nch == 3))
1024 res = validate_varargslist(CHILD(tree, 1));
1026 else {
1027 (void) validate_numnodes(tree, 2, "parameters");
1029 return (res);
1033 /* validate_suite()
1035 * suite:
1036 * simple_stmt
1037 * | NEWLINE INDENT stmt+ DEDENT
1039 static int
1040 validate_suite(node *tree)
1042 int nch = NCH(tree);
1043 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
1045 if (res && (nch == 1))
1046 res = validate_simple_stmt(CHILD(tree, 0));
1047 else if (res) {
1048 /* NEWLINE INDENT stmt+ DEDENT */
1049 res = (validate_newline(CHILD(tree, 0))
1050 && validate_indent(CHILD(tree, 1))
1051 && validate_stmt(CHILD(tree, 2))
1052 && validate_dedent(CHILD(tree, nch - 1)));
1054 if (res && (nch > 4)) {
1055 int i = 3;
1056 --nch; /* forget the DEDENT */
1057 for ( ; res && (i < nch); ++i)
1058 res = validate_stmt(CHILD(tree, i));
1060 else if (nch < 4)
1061 res = validate_numnodes(tree, 4, "suite");
1063 return (res);
1067 static int
1068 validate_testlist(node *tree)
1070 return (validate_repeating_list(tree, testlist,
1071 validate_test, "testlist"));
1075 static int
1076 validate_testlist1(node *tree)
1078 return (validate_repeating_list(tree, testlist1,
1079 validate_test, "testlist1"));
1083 static int
1084 validate_testlist_safe(node *tree)
1086 return (validate_repeating_list(tree, testlist_safe,
1087 validate_test, "testlist_safe"));
1091 /* '*' NAME [',' '**' NAME] | '**' NAME
1093 static int
1094 validate_varargslist_trailer(node *tree, int start)
1096 int nch = NCH(tree);
1097 int res = 0;
1098 int sym;
1100 if (nch <= start) {
1101 err_string("expected variable argument trailer for varargslist");
1102 return 0;
1104 sym = TYPE(CHILD(tree, start));
1105 if (sym == STAR) {
1107 * ('*' NAME [',' '**' NAME]
1109 if (nch-start == 2)
1110 res = validate_name(CHILD(tree, start+1), NULL);
1111 else if (nch-start == 5)
1112 res = (validate_name(CHILD(tree, start+1), NULL)
1113 && validate_comma(CHILD(tree, start+2))
1114 && validate_doublestar(CHILD(tree, start+3))
1115 && validate_name(CHILD(tree, start+4), NULL));
1117 else if (sym == DOUBLESTAR) {
1119 * '**' NAME
1121 if (nch-start == 2)
1122 res = validate_name(CHILD(tree, start+1), NULL);
1124 if (!res)
1125 err_string("illegal variable argument trailer for varargslist");
1126 return res;
1130 /* validate_varargslist()
1132 * varargslist:
1133 * (fpdef ['=' test] ',')*
1134 * ('*' NAME [',' '**' NAME]
1135 * | '**' NAME)
1136 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1139 static int
1140 validate_varargslist(node *tree)
1142 int nch = NCH(tree);
1143 int res = validate_ntype(tree, varargslist) && (nch != 0);
1144 int sym;
1146 if (!res)
1147 return 0;
1148 if (nch < 1) {
1149 err_string("varargslist missing child nodes");
1150 return 0;
1152 sym = TYPE(CHILD(tree, 0));
1153 if (sym == STAR || sym == DOUBLESTAR)
1154 /* whole thing matches:
1155 * '*' NAME [',' '**' NAME] | '**' NAME
1157 res = validate_varargslist_trailer(tree, 0);
1158 else if (sym == fpdef) {
1159 int i = 0;
1161 sym = TYPE(CHILD(tree, nch-1));
1162 if (sym == NAME) {
1164 * (fpdef ['=' test] ',')+
1165 * ('*' NAME [',' '**' NAME]
1166 * | '**' NAME)
1168 /* skip over (fpdef ['=' test] ',')+ */
1169 while (res && (i+2 <= nch)) {
1170 res = validate_fpdef(CHILD(tree, i));
1171 ++i;
1172 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1173 res = (validate_equal(CHILD(tree, i))
1174 && validate_test(CHILD(tree, i+1)));
1175 if (res)
1176 i += 2;
1178 if (res && i < nch) {
1179 res = validate_comma(CHILD(tree, i));
1180 ++i;
1181 if (res && i < nch
1182 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1183 || TYPE(CHILD(tree, i)) == STAR))
1184 break;
1187 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1188 * i --^^^
1190 if (res)
1191 res = validate_varargslist_trailer(tree, i);
1193 else {
1195 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1197 /* strip trailing comma node */
1198 if (sym == COMMA) {
1199 res = validate_comma(CHILD(tree, nch-1));
1200 if (!res)
1201 return 0;
1202 --nch;
1205 * fpdef ['=' test] (',' fpdef ['=' test])*
1207 res = validate_fpdef(CHILD(tree, 0));
1208 ++i;
1209 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1210 res = (validate_equal(CHILD(tree, i))
1211 && validate_test(CHILD(tree, i+1)));
1212 i += 2;
1215 * ... (',' fpdef ['=' test])*
1216 * i ---^^^
1218 while (res && (nch - i) >= 2) {
1219 res = (validate_comma(CHILD(tree, i))
1220 && validate_fpdef(CHILD(tree, i+1)));
1221 i += 2;
1222 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1223 res = (validate_equal(CHILD(tree, i))
1224 && validate_test(CHILD(tree, i+1)));
1225 i += 2;
1228 if (res && nch - i != 0) {
1229 res = 0;
1230 err_string("illegal formation for varargslist");
1234 return res;
1238 /* list_iter: list_for | list_if
1240 static int
1241 validate_list_iter(node *tree)
1243 int res = (validate_ntype(tree, list_iter)
1244 && validate_numnodes(tree, 1, "list_iter"));
1245 if (res && TYPE(CHILD(tree, 0)) == list_for)
1246 res = validate_list_for(CHILD(tree, 0));
1247 else
1248 res = validate_list_if(CHILD(tree, 0));
1250 return res;
1253 /* list_for: 'for' exprlist 'in' testlist [list_iter]
1255 static int
1256 validate_list_for(node *tree)
1258 int nch = NCH(tree);
1259 int res;
1261 if (nch == 5)
1262 res = validate_list_iter(CHILD(tree, 4));
1263 else
1264 res = validate_numnodes(tree, 4, "list_for");
1266 if (res)
1267 res = (validate_name(CHILD(tree, 0), "for")
1268 && validate_exprlist(CHILD(tree, 1))
1269 && validate_name(CHILD(tree, 2), "in")
1270 && validate_testlist_safe(CHILD(tree, 3)));
1272 return res;
1275 /* list_if: 'if' test [list_iter]
1277 static int
1278 validate_list_if(node *tree)
1280 int nch = NCH(tree);
1281 int res;
1283 if (nch == 3)
1284 res = validate_list_iter(CHILD(tree, 2));
1285 else
1286 res = validate_numnodes(tree, 2, "list_if");
1288 if (res)
1289 res = (validate_name(CHILD(tree, 0), "if")
1290 && validate_test(CHILD(tree, 1)));
1292 return res;
1296 /* validate_fpdef()
1298 * fpdef:
1299 * NAME
1300 * | '(' fplist ')'
1302 static int
1303 validate_fpdef(node *tree)
1305 int nch = NCH(tree);
1306 int res = validate_ntype(tree, fpdef);
1308 if (res) {
1309 if (nch == 1)
1310 res = validate_ntype(CHILD(tree, 0), NAME);
1311 else if (nch == 3)
1312 res = (validate_lparen(CHILD(tree, 0))
1313 && validate_fplist(CHILD(tree, 1))
1314 && validate_rparen(CHILD(tree, 2)));
1315 else
1316 res = validate_numnodes(tree, 1, "fpdef");
1318 return (res);
1322 static int
1323 validate_fplist(node *tree)
1325 return (validate_repeating_list(tree, fplist,
1326 validate_fpdef, "fplist"));
1330 /* simple_stmt | compound_stmt
1333 static int
1334 validate_stmt(node *tree)
1336 int res = (validate_ntype(tree, stmt)
1337 && validate_numnodes(tree, 1, "stmt"));
1339 if (res) {
1340 tree = CHILD(tree, 0);
1342 if (TYPE(tree) == simple_stmt)
1343 res = validate_simple_stmt(tree);
1344 else
1345 res = validate_compound_stmt(tree);
1347 return (res);
1351 /* small_stmt (';' small_stmt)* [';'] NEWLINE
1354 static int
1355 validate_simple_stmt(node *tree)
1357 int nch = NCH(tree);
1358 int res = (validate_ntype(tree, simple_stmt)
1359 && (nch >= 2)
1360 && validate_small_stmt(CHILD(tree, 0))
1361 && validate_newline(CHILD(tree, nch - 1)));
1363 if (nch < 2)
1364 res = validate_numnodes(tree, 2, "simple_stmt");
1365 --nch; /* forget the NEWLINE */
1366 if (res && is_even(nch))
1367 res = validate_semi(CHILD(tree, --nch));
1368 if (res && (nch > 2)) {
1369 int i;
1371 for (i = 1; res && (i < nch); i += 2)
1372 res = (validate_semi(CHILD(tree, i))
1373 && validate_small_stmt(CHILD(tree, i + 1)));
1375 return (res);
1379 static int
1380 validate_small_stmt(node *tree)
1382 int nch = NCH(tree);
1383 int res = validate_numnodes(tree, 1, "small_stmt");
1385 if (res) {
1386 int ntype = TYPE(CHILD(tree, 0));
1388 if ( (ntype == expr_stmt)
1389 || (ntype == print_stmt)
1390 || (ntype == del_stmt)
1391 || (ntype == pass_stmt)
1392 || (ntype == flow_stmt)
1393 || (ntype == import_stmt)
1394 || (ntype == global_stmt)
1395 || (ntype == assert_stmt)
1396 || (ntype == exec_stmt))
1397 res = validate_node(CHILD(tree, 0));
1398 else {
1399 res = 0;
1400 err_string("illegal small_stmt child type");
1403 else if (nch == 1) {
1404 res = 0;
1405 PyErr_Format(parser_error,
1406 "Unrecognized child node of small_stmt: %d.",
1407 TYPE(CHILD(tree, 0)));
1409 return (res);
1413 /* compound_stmt:
1414 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1416 static int
1417 validate_compound_stmt(node *tree)
1419 int res = (validate_ntype(tree, compound_stmt)
1420 && validate_numnodes(tree, 1, "compound_stmt"));
1421 int ntype;
1423 if (!res)
1424 return (0);
1426 tree = CHILD(tree, 0);
1427 ntype = TYPE(tree);
1428 if ( (ntype == if_stmt)
1429 || (ntype == while_stmt)
1430 || (ntype == for_stmt)
1431 || (ntype == try_stmt)
1432 || (ntype == funcdef)
1433 || (ntype == classdef))
1434 res = validate_node(tree);
1435 else {
1436 res = 0;
1437 PyErr_Format(parser_error,
1438 "Illegal compound statement type: %d.", TYPE(tree));
1440 return (res);
1444 static int
1445 validate_expr_stmt(node *tree)
1447 int j;
1448 int nch = NCH(tree);
1449 int res = (validate_ntype(tree, expr_stmt)
1450 && is_odd(nch)
1451 && validate_testlist(CHILD(tree, 0)));
1453 if (res && nch == 3
1454 && TYPE(CHILD(tree, 1)) == augassign) {
1455 res = (validate_numnodes(CHILD(tree, 1), 1, "augassign")
1456 && validate_testlist(CHILD(tree, 2)));
1458 if (res) {
1459 char *s = STR(CHILD(CHILD(tree, 1), 0));
1461 res = (strcmp(s, "+=") == 0
1462 || strcmp(s, "-=") == 0
1463 || strcmp(s, "*=") == 0
1464 || strcmp(s, "/=") == 0
1465 || strcmp(s, "//=") == 0
1466 || strcmp(s, "%=") == 0
1467 || strcmp(s, "&=") == 0
1468 || strcmp(s, "|=") == 0
1469 || strcmp(s, "^=") == 0
1470 || strcmp(s, "<<=") == 0
1471 || strcmp(s, ">>=") == 0
1472 || strcmp(s, "**=") == 0);
1473 if (!res)
1474 err_string("illegal augmmented assignment operator");
1477 else {
1478 for (j = 1; res && (j < nch); j += 2)
1479 res = (validate_equal(CHILD(tree, j))
1480 && validate_testlist(CHILD(tree, j + 1)));
1482 return (res);
1486 /* print_stmt:
1488 * 'print' ( [ test (',' test)* [','] ]
1489 * | '>>' test [ (',' test)+ [','] ] )
1491 static int
1492 validate_print_stmt(node *tree)
1494 int nch = NCH(tree);
1495 int res = (validate_ntype(tree, print_stmt)
1496 && (nch > 0)
1497 && validate_name(CHILD(tree, 0), "print"));
1499 if (res && nch > 1) {
1500 int sym = TYPE(CHILD(tree, 1));
1501 int i = 1;
1502 int allow_trailing_comma = 1;
1504 if (sym == test)
1505 res = validate_test(CHILD(tree, i++));
1506 else {
1507 if (nch < 3)
1508 res = validate_numnodes(tree, 3, "print_stmt");
1509 else {
1510 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1511 && validate_test(CHILD(tree, i+1)));
1512 i += 2;
1513 allow_trailing_comma = 0;
1516 if (res) {
1517 /* ... (',' test)* [','] */
1518 while (res && i+2 <= nch) {
1519 res = (validate_comma(CHILD(tree, i))
1520 && validate_test(CHILD(tree, i+1)));
1521 allow_trailing_comma = 1;
1522 i += 2;
1524 if (res && !allow_trailing_comma)
1525 res = validate_numnodes(tree, i, "print_stmt");
1526 else if (res && i < nch)
1527 res = validate_comma(CHILD(tree, i));
1530 return (res);
1534 static int
1535 validate_del_stmt(node *tree)
1537 return (validate_numnodes(tree, 2, "del_stmt")
1538 && validate_name(CHILD(tree, 0), "del")
1539 && validate_exprlist(CHILD(tree, 1)));
1543 static int
1544 validate_return_stmt(node *tree)
1546 int nch = NCH(tree);
1547 int res = (validate_ntype(tree, return_stmt)
1548 && ((nch == 1) || (nch == 2))
1549 && validate_name(CHILD(tree, 0), "return"));
1551 if (res && (nch == 2))
1552 res = validate_testlist(CHILD(tree, 1));
1554 return (res);
1558 static int
1559 validate_raise_stmt(node *tree)
1561 int nch = NCH(tree);
1562 int res = (validate_ntype(tree, raise_stmt)
1563 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
1565 if (res) {
1566 res = validate_name(CHILD(tree, 0), "raise");
1567 if (res && (nch >= 2))
1568 res = validate_test(CHILD(tree, 1));
1569 if (res && nch > 2) {
1570 res = (validate_comma(CHILD(tree, 2))
1571 && validate_test(CHILD(tree, 3)));
1572 if (res && (nch > 4))
1573 res = (validate_comma(CHILD(tree, 4))
1574 && validate_test(CHILD(tree, 5)));
1577 else
1578 (void) validate_numnodes(tree, 2, "raise");
1579 if (res && (nch == 4))
1580 res = (validate_comma(CHILD(tree, 2))
1581 && validate_test(CHILD(tree, 3)));
1583 return (res);
1587 /* yield_stmt: 'yield' testlist
1589 static int
1590 validate_yield_stmt(node *tree)
1592 return (validate_ntype(tree, yield_stmt)
1593 && validate_numnodes(tree, 2, "yield_stmt")
1594 && validate_name(CHILD(tree, 0), "yield")
1595 && validate_testlist(CHILD(tree, 1)));
1599 static int
1600 validate_import_as_name(node *tree)
1602 int nch = NCH(tree);
1603 int ok = validate_ntype(tree, import_as_name);
1605 if (ok) {
1606 if (nch == 1)
1607 ok = validate_name(CHILD(tree, 0), NULL);
1608 else if (nch == 3)
1609 ok = (validate_name(CHILD(tree, 0), NULL)
1610 && validate_name(CHILD(tree, 1), "as")
1611 && validate_name(CHILD(tree, 2), NULL));
1612 else
1613 ok = validate_numnodes(tree, 3, "import_as_name");
1615 return ok;
1619 /* dotted_name: NAME ("." NAME)*
1621 static int
1622 validate_dotted_name(node *tree)
1624 int nch = NCH(tree);
1625 int res = (validate_ntype(tree, dotted_name)
1626 && is_odd(nch)
1627 && validate_name(CHILD(tree, 0), NULL));
1628 int i;
1630 for (i = 1; res && (i < nch); i += 2) {
1631 res = (validate_dot(CHILD(tree, i))
1632 && validate_name(CHILD(tree, i+1), NULL));
1634 return res;
1638 /* dotted_as_name: dotted_name [NAME NAME]
1640 static int
1641 validate_dotted_as_name(node *tree)
1643 int nch = NCH(tree);
1644 int res = validate_ntype(tree, dotted_as_name);
1646 if (res) {
1647 if (nch == 1)
1648 res = validate_dotted_name(CHILD(tree, 0));
1649 else if (nch == 3)
1650 res = (validate_dotted_name(CHILD(tree, 0))
1651 && validate_name(CHILD(tree, 1), "as")
1652 && validate_name(CHILD(tree, 2), NULL));
1653 else {
1654 res = 0;
1655 err_string("illegal number of children for dotted_as_name");
1658 return res;
1662 /* import_stmt:
1664 * 'import' dotted_as_name (',' dotted_as_name)*
1665 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
1667 static int
1668 validate_import_stmt(node *tree)
1670 int nch = NCH(tree);
1671 int res = (validate_ntype(tree, import_stmt)
1672 && (nch >= 2) && is_even(nch)
1673 && validate_ntype(CHILD(tree, 0), NAME));
1675 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
1676 int j;
1678 res = validate_dotted_as_name(CHILD(tree, 1));
1679 for (j = 2; res && (j < nch); j += 2)
1680 res = (validate_comma(CHILD(tree, j))
1681 && validate_dotted_as_name(CHILD(tree, j + 1)));
1683 else if (res && (res = validate_name(CHILD(tree, 0), "from"))) {
1684 res = ((nch >= 4) && is_even(nch)
1685 && validate_dotted_name(CHILD(tree, 1))
1686 && validate_name(CHILD(tree, 2), "import"));
1687 if (nch == 4) {
1688 if (TYPE(CHILD(tree, 3)) == import_as_name)
1689 res = validate_import_as_name(CHILD(tree, 3));
1690 else
1691 res = validate_star(CHILD(tree, 3));
1693 else {
1694 /* 'from' dotted_name 'import' import_as_name
1695 * (',' import_as_name)+
1697 int j;
1698 res = validate_import_as_name(CHILD(tree, 3));
1699 for (j = 4; res && (j < nch); j += 2)
1700 res = (validate_comma(CHILD(tree, j))
1701 && validate_import_as_name(CHILD(tree, j + 1)));
1704 else
1705 res = 0;
1707 return (res);
1711 static int
1712 validate_global_stmt(node *tree)
1714 int j;
1715 int nch = NCH(tree);
1716 int res = (validate_ntype(tree, global_stmt)
1717 && is_even(nch) && (nch >= 2));
1719 if (!res && !PyErr_Occurred())
1720 err_string("illegal global statement");
1722 if (res)
1723 res = (validate_name(CHILD(tree, 0), "global")
1724 && validate_ntype(CHILD(tree, 1), NAME));
1725 for (j = 2; res && (j < nch); j += 2)
1726 res = (validate_comma(CHILD(tree, j))
1727 && validate_ntype(CHILD(tree, j + 1), NAME));
1729 return (res);
1733 /* exec_stmt:
1735 * 'exec' expr ['in' test [',' test]]
1737 static int
1738 validate_exec_stmt(node *tree)
1740 int nch = NCH(tree);
1741 int res = (validate_ntype(tree, exec_stmt)
1742 && ((nch == 2) || (nch == 4) || (nch == 6))
1743 && validate_name(CHILD(tree, 0), "exec")
1744 && validate_expr(CHILD(tree, 1)));
1746 if (!res && !PyErr_Occurred())
1747 err_string("illegal exec statement");
1748 if (res && (nch > 2))
1749 res = (validate_name(CHILD(tree, 2), "in")
1750 && validate_test(CHILD(tree, 3)));
1751 if (res && (nch == 6))
1752 res = (validate_comma(CHILD(tree, 4))
1753 && validate_test(CHILD(tree, 5)));
1755 return (res);
1759 /* assert_stmt:
1761 * 'assert' test [',' test]
1763 static int
1764 validate_assert_stmt(node *tree)
1766 int nch = NCH(tree);
1767 int res = (validate_ntype(tree, assert_stmt)
1768 && ((nch == 2) || (nch == 4))
1769 && (validate_name(CHILD(tree, 0), "assert"))
1770 && validate_test(CHILD(tree, 1)));
1772 if (!res && !PyErr_Occurred())
1773 err_string("illegal assert statement");
1774 if (res && (nch > 2))
1775 res = (validate_comma(CHILD(tree, 2))
1776 && validate_test(CHILD(tree, 3)));
1778 return (res);
1782 static int
1783 validate_while(node *tree)
1785 int nch = NCH(tree);
1786 int res = (validate_ntype(tree, while_stmt)
1787 && ((nch == 4) || (nch == 7))
1788 && validate_name(CHILD(tree, 0), "while")
1789 && validate_test(CHILD(tree, 1))
1790 && validate_colon(CHILD(tree, 2))
1791 && validate_suite(CHILD(tree, 3)));
1793 if (res && (nch == 7))
1794 res = (validate_name(CHILD(tree, 4), "else")
1795 && validate_colon(CHILD(tree, 5))
1796 && validate_suite(CHILD(tree, 6)));
1798 return (res);
1802 static int
1803 validate_for(node *tree)
1805 int nch = NCH(tree);
1806 int res = (validate_ntype(tree, for_stmt)
1807 && ((nch == 6) || (nch == 9))
1808 && validate_name(CHILD(tree, 0), "for")
1809 && validate_exprlist(CHILD(tree, 1))
1810 && validate_name(CHILD(tree, 2), "in")
1811 && validate_testlist(CHILD(tree, 3))
1812 && validate_colon(CHILD(tree, 4))
1813 && validate_suite(CHILD(tree, 5)));
1815 if (res && (nch == 9))
1816 res = (validate_name(CHILD(tree, 6), "else")
1817 && validate_colon(CHILD(tree, 7))
1818 && validate_suite(CHILD(tree, 8)));
1820 return (res);
1824 /* try_stmt:
1825 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1826 * | 'try' ':' suite 'finally' ':' suite
1829 static int
1830 validate_try(node *tree)
1832 int nch = NCH(tree);
1833 int pos = 3;
1834 int res = (validate_ntype(tree, try_stmt)
1835 && (nch >= 6) && ((nch % 3) == 0));
1837 if (res)
1838 res = (validate_name(CHILD(tree, 0), "try")
1839 && validate_colon(CHILD(tree, 1))
1840 && validate_suite(CHILD(tree, 2))
1841 && validate_colon(CHILD(tree, nch - 2))
1842 && validate_suite(CHILD(tree, nch - 1)));
1843 else if (!PyErr_Occurred()) {
1844 const char* name = "except";
1845 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
1846 name = STR(CHILD(tree, nch - 3));
1848 PyErr_Format(parser_error,
1849 "Illegal number of children for try/%s node.", name);
1851 /* Skip past except_clause sections: */
1852 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
1853 res = (validate_except_clause(CHILD(tree, pos))
1854 && validate_colon(CHILD(tree, pos + 1))
1855 && validate_suite(CHILD(tree, pos + 2)));
1856 pos += 3;
1858 if (res && (pos < nch)) {
1859 res = validate_ntype(CHILD(tree, pos), NAME);
1860 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
1861 res = (validate_numnodes(tree, 6, "try/finally")
1862 && validate_colon(CHILD(tree, 4))
1863 && validate_suite(CHILD(tree, 5)));
1864 else if (res) {
1865 if (nch == (pos + 3)) {
1866 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
1867 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
1868 if (!res)
1869 err_string("illegal trailing triple in try statement");
1871 else if (nch == (pos + 6)) {
1872 res = (validate_name(CHILD(tree, pos), "except")
1873 && validate_colon(CHILD(tree, pos + 1))
1874 && validate_suite(CHILD(tree, pos + 2))
1875 && validate_name(CHILD(tree, pos + 3), "else"));
1877 else
1878 res = validate_numnodes(tree, pos + 3, "try/except");
1881 return (res);
1885 static int
1886 validate_except_clause(node *tree)
1888 int nch = NCH(tree);
1889 int res = (validate_ntype(tree, except_clause)
1890 && ((nch == 1) || (nch == 2) || (nch == 4))
1891 && validate_name(CHILD(tree, 0), "except"));
1893 if (res && (nch > 1))
1894 res = validate_test(CHILD(tree, 1));
1895 if (res && (nch == 4))
1896 res = (validate_comma(CHILD(tree, 2))
1897 && validate_test(CHILD(tree, 3)));
1899 return (res);
1903 static int
1904 validate_test(node *tree)
1906 int nch = NCH(tree);
1907 int res = validate_ntype(tree, test) && is_odd(nch);
1909 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
1910 res = ((nch == 1)
1911 && validate_lambdef(CHILD(tree, 0)));
1912 else if (res) {
1913 int pos;
1914 res = validate_and_test(CHILD(tree, 0));
1915 for (pos = 1; res && (pos < nch); pos += 2)
1916 res = (validate_name(CHILD(tree, pos), "or")
1917 && validate_and_test(CHILD(tree, pos + 1)));
1919 return (res);
1923 static int
1924 validate_and_test(node *tree)
1926 int pos;
1927 int nch = NCH(tree);
1928 int res = (validate_ntype(tree, and_test)
1929 && is_odd(nch)
1930 && validate_not_test(CHILD(tree, 0)));
1932 for (pos = 1; res && (pos < nch); pos += 2)
1933 res = (validate_name(CHILD(tree, pos), "and")
1934 && validate_not_test(CHILD(tree, 0)));
1936 return (res);
1940 static int
1941 validate_not_test(node *tree)
1943 int nch = NCH(tree);
1944 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
1946 if (res) {
1947 if (nch == 2)
1948 res = (validate_name(CHILD(tree, 0), "not")
1949 && validate_not_test(CHILD(tree, 1)));
1950 else if (nch == 1)
1951 res = validate_comparison(CHILD(tree, 0));
1953 return (res);
1957 static int
1958 validate_comparison(node *tree)
1960 int pos;
1961 int nch = NCH(tree);
1962 int res = (validate_ntype(tree, comparison)
1963 && is_odd(nch)
1964 && validate_expr(CHILD(tree, 0)));
1966 for (pos = 1; res && (pos < nch); pos += 2)
1967 res = (validate_comp_op(CHILD(tree, pos))
1968 && validate_expr(CHILD(tree, pos + 1)));
1970 return (res);
1974 static int
1975 validate_comp_op(node *tree)
1977 int res = 0;
1978 int nch = NCH(tree);
1980 if (!validate_ntype(tree, comp_op))
1981 return (0);
1982 if (nch == 1) {
1984 * Only child will be a terminal with a well-defined symbolic name
1985 * or a NAME with a string of either 'is' or 'in'
1987 tree = CHILD(tree, 0);
1988 switch (TYPE(tree)) {
1989 case LESS:
1990 case GREATER:
1991 case EQEQUAL:
1992 case EQUAL:
1993 case LESSEQUAL:
1994 case GREATEREQUAL:
1995 case NOTEQUAL:
1996 res = 1;
1997 break;
1998 case NAME:
1999 res = ((strcmp(STR(tree), "in") == 0)
2000 || (strcmp(STR(tree), "is") == 0));
2001 if (!res) {
2002 PyErr_Format(parser_error,
2003 "illegal operator '%s'", STR(tree));
2005 break;
2006 default:
2007 err_string("illegal comparison operator type");
2008 break;
2011 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
2012 res = (validate_ntype(CHILD(tree, 0), NAME)
2013 && validate_ntype(CHILD(tree, 1), NAME)
2014 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2015 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2016 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2017 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2018 if (!res && !PyErr_Occurred())
2019 err_string("unknown comparison operator");
2021 return (res);
2025 static int
2026 validate_expr(node *tree)
2028 int j;
2029 int nch = NCH(tree);
2030 int res = (validate_ntype(tree, expr)
2031 && is_odd(nch)
2032 && validate_xor_expr(CHILD(tree, 0)));
2034 for (j = 2; res && (j < nch); j += 2)
2035 res = (validate_xor_expr(CHILD(tree, j))
2036 && validate_vbar(CHILD(tree, j - 1)));
2038 return (res);
2042 static int
2043 validate_xor_expr(node *tree)
2045 int j;
2046 int nch = NCH(tree);
2047 int res = (validate_ntype(tree, xor_expr)
2048 && is_odd(nch)
2049 && validate_and_expr(CHILD(tree, 0)));
2051 for (j = 2; res && (j < nch); j += 2)
2052 res = (validate_circumflex(CHILD(tree, j - 1))
2053 && validate_and_expr(CHILD(tree, j)));
2055 return (res);
2059 static int
2060 validate_and_expr(node *tree)
2062 int pos;
2063 int nch = NCH(tree);
2064 int res = (validate_ntype(tree, and_expr)
2065 && is_odd(nch)
2066 && validate_shift_expr(CHILD(tree, 0)));
2068 for (pos = 1; res && (pos < nch); pos += 2)
2069 res = (validate_ampersand(CHILD(tree, pos))
2070 && validate_shift_expr(CHILD(tree, pos + 1)));
2072 return (res);
2076 static int
2077 validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
2079 int pos = 1;
2080 int nch = NCH(tree);
2081 int res = (is_odd(nch)
2082 && (*termvalid)(CHILD(tree, 0)));
2084 for ( ; res && (pos < nch); pos += 2) {
2085 if (TYPE(CHILD(tree, pos)) != op1)
2086 res = validate_ntype(CHILD(tree, pos), op2);
2087 if (res)
2088 res = (*termvalid)(CHILD(tree, pos + 1));
2090 return (res);
2094 static int
2095 validate_shift_expr(node *tree)
2097 return (validate_ntype(tree, shift_expr)
2098 && validate_chain_two_ops(tree, validate_arith_expr,
2099 LEFTSHIFT, RIGHTSHIFT));
2103 static int
2104 validate_arith_expr(node *tree)
2106 return (validate_ntype(tree, arith_expr)
2107 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2111 static int
2112 validate_term(node *tree)
2114 int pos = 1;
2115 int nch = NCH(tree);
2116 int res = (validate_ntype(tree, term)
2117 && is_odd(nch)
2118 && validate_factor(CHILD(tree, 0)));
2120 for ( ; res && (pos < nch); pos += 2)
2121 res = (((TYPE(CHILD(tree, pos)) == STAR)
2122 || (TYPE(CHILD(tree, pos)) == SLASH)
2123 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
2124 || (TYPE(CHILD(tree, pos)) == PERCENT))
2125 && validate_factor(CHILD(tree, pos + 1)));
2127 return (res);
2131 /* factor:
2133 * factor: ('+'|'-'|'~') factor | power
2135 static int
2136 validate_factor(node *tree)
2138 int nch = NCH(tree);
2139 int res = (validate_ntype(tree, factor)
2140 && (((nch == 2)
2141 && ((TYPE(CHILD(tree, 0)) == PLUS)
2142 || (TYPE(CHILD(tree, 0)) == MINUS)
2143 || (TYPE(CHILD(tree, 0)) == TILDE))
2144 && validate_factor(CHILD(tree, 1)))
2145 || ((nch == 1)
2146 && validate_power(CHILD(tree, 0)))));
2147 return (res);
2151 /* power:
2153 * power: atom trailer* ('**' factor)*
2155 static int
2156 validate_power(node *tree)
2158 int pos = 1;
2159 int nch = NCH(tree);
2160 int res = (validate_ntype(tree, power) && (nch >= 1)
2161 && validate_atom(CHILD(tree, 0)));
2163 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
2164 res = validate_trailer(CHILD(tree, pos++));
2165 if (res && (pos < nch)) {
2166 if (!is_even(nch - pos)) {
2167 err_string("illegal number of nodes for 'power'");
2168 return (0);
2170 for ( ; res && (pos < (nch - 1)); pos += 2)
2171 res = (validate_doublestar(CHILD(tree, pos))
2172 && validate_factor(CHILD(tree, pos + 1)));
2174 return (res);
2178 static int
2179 validate_atom(node *tree)
2181 int pos;
2182 int nch = NCH(tree);
2183 int res = validate_ntype(tree, atom);
2185 if (res && nch < 1)
2186 res = validate_numnodes(tree, nch+1, "atom");
2187 if (res) {
2188 switch (TYPE(CHILD(tree, 0))) {
2189 case LPAR:
2190 res = ((nch <= 3)
2191 && (validate_rparen(CHILD(tree, nch - 1))));
2193 if (res && (nch == 3))
2194 res = validate_testlist(CHILD(tree, 1));
2195 break;
2196 case LSQB:
2197 if (nch == 2)
2198 res = validate_ntype(CHILD(tree, 1), RSQB);
2199 else if (nch == 3)
2200 res = (validate_listmaker(CHILD(tree, 1))
2201 && validate_ntype(CHILD(tree, 2), RSQB));
2202 else {
2203 res = 0;
2204 err_string("illegal list display atom");
2206 break;
2207 case LBRACE:
2208 res = ((nch <= 3)
2209 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
2211 if (res && (nch == 3))
2212 res = validate_dictmaker(CHILD(tree, 1));
2213 break;
2214 case BACKQUOTE:
2215 res = ((nch == 3)
2216 && validate_testlist1(CHILD(tree, 1))
2217 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2218 break;
2219 case NAME:
2220 case NUMBER:
2221 res = (nch == 1);
2222 break;
2223 case STRING:
2224 for (pos = 1; res && (pos < nch); ++pos)
2225 res = validate_ntype(CHILD(tree, pos), STRING);
2226 break;
2227 default:
2228 res = 0;
2229 break;
2232 return (res);
2236 /* listmaker:
2237 * test ( list_for | (',' test)* [','] )
2239 static int
2240 validate_listmaker(node *tree)
2242 int nch = NCH(tree);
2243 int ok = nch;
2245 if (nch == 0)
2246 err_string("missing child nodes of listmaker");
2247 else
2248 ok = validate_test(CHILD(tree, 0));
2251 * list_iter | (',' test)* [',']
2253 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2254 ok = validate_list_for(CHILD(tree, 1));
2255 else {
2256 /* (',' test)* [','] */
2257 int i = 1;
2258 while (ok && nch - i >= 2) {
2259 ok = (validate_comma(CHILD(tree, i))
2260 && validate_test(CHILD(tree, i+1)));
2261 i += 2;
2263 if (ok && i == nch-1)
2264 ok = validate_comma(CHILD(tree, i));
2265 else if (i != nch) {
2266 ok = 0;
2267 err_string("illegal trailing nodes for listmaker");
2270 return ok;
2274 /* funcdef:
2275 * 'def' NAME parameters ':' suite
2278 static int
2279 validate_funcdef(node *tree)
2281 return (validate_ntype(tree, funcdef)
2282 && validate_numnodes(tree, 5, "funcdef")
2283 && validate_name(CHILD(tree, 0), "def")
2284 && validate_ntype(CHILD(tree, 1), NAME)
2285 && validate_colon(CHILD(tree, 3))
2286 && validate_parameters(CHILD(tree, 2))
2287 && validate_suite(CHILD(tree, 4)));
2291 static int
2292 validate_lambdef(node *tree)
2294 int nch = NCH(tree);
2295 int res = (validate_ntype(tree, lambdef)
2296 && ((nch == 3) || (nch == 4))
2297 && validate_name(CHILD(tree, 0), "lambda")
2298 && validate_colon(CHILD(tree, nch - 2))
2299 && validate_test(CHILD(tree, nch - 1)));
2301 if (res && (nch == 4))
2302 res = validate_varargslist(CHILD(tree, 1));
2303 else if (!res && !PyErr_Occurred())
2304 (void) validate_numnodes(tree, 3, "lambdef");
2306 return (res);
2310 /* arglist:
2312 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2314 static int
2315 validate_arglist(node *tree)
2317 int nch = NCH(tree);
2318 int i = 0;
2319 int ok = 1;
2321 if (nch <= 0)
2322 /* raise the right error from having an invalid number of children */
2323 return validate_numnodes(tree, nch + 1, "arglist");
2325 while (ok && nch-i >= 2) {
2326 /* skip leading (argument ',') */
2327 ok = (validate_argument(CHILD(tree, i))
2328 && validate_comma(CHILD(tree, i+1)));
2329 if (ok)
2330 i += 2;
2331 else
2332 PyErr_Clear();
2334 ok = 1;
2335 if (nch-i > 0) {
2337 * argument | '*' test [',' '**' test] | '**' test
2339 int sym = TYPE(CHILD(tree, i));
2341 if (sym == argument) {
2342 ok = validate_argument(CHILD(tree, i));
2343 if (ok && i+1 != nch) {
2344 err_string("illegal arglist specification"
2345 " (extra stuff on end)");
2346 ok = 0;
2349 else if (sym == STAR) {
2350 ok = validate_star(CHILD(tree, i));
2351 if (ok && (nch-i == 2))
2352 ok = validate_test(CHILD(tree, i+1));
2353 else if (ok && (nch-i == 5))
2354 ok = (validate_test(CHILD(tree, i+1))
2355 && validate_comma(CHILD(tree, i+2))
2356 && validate_doublestar(CHILD(tree, i+3))
2357 && validate_test(CHILD(tree, i+4)));
2358 else {
2359 err_string("illegal use of '*' in arglist");
2360 ok = 0;
2363 else if (sym == DOUBLESTAR) {
2364 if (nch-i == 2)
2365 ok = (validate_doublestar(CHILD(tree, i))
2366 && validate_test(CHILD(tree, i+1)));
2367 else {
2368 err_string("illegal use of '**' in arglist");
2369 ok = 0;
2372 else {
2373 err_string("illegal arglist specification");
2374 ok = 0;
2377 return (ok);
2382 /* argument:
2384 * [test '='] test
2386 static int
2387 validate_argument(node *tree)
2389 int nch = NCH(tree);
2390 int res = (validate_ntype(tree, argument)
2391 && ((nch == 1) || (nch == 3))
2392 && validate_test(CHILD(tree, 0)));
2394 if (res && (nch == 3))
2395 res = (validate_equal(CHILD(tree, 1))
2396 && validate_test(CHILD(tree, 2)));
2398 return (res);
2403 /* trailer:
2405 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2407 static int
2408 validate_trailer(node *tree)
2410 int nch = NCH(tree);
2411 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
2413 if (res) {
2414 switch (TYPE(CHILD(tree, 0))) {
2415 case LPAR:
2416 res = validate_rparen(CHILD(tree, nch - 1));
2417 if (res && (nch == 3))
2418 res = validate_arglist(CHILD(tree, 1));
2419 break;
2420 case LSQB:
2421 res = (validate_numnodes(tree, 3, "trailer")
2422 && validate_subscriptlist(CHILD(tree, 1))
2423 && validate_ntype(CHILD(tree, 2), RSQB));
2424 break;
2425 case DOT:
2426 res = (validate_numnodes(tree, 2, "trailer")
2427 && validate_ntype(CHILD(tree, 1), NAME));
2428 break;
2429 default:
2430 res = 0;
2431 break;
2434 else {
2435 (void) validate_numnodes(tree, 2, "trailer");
2437 return (res);
2441 /* subscriptlist:
2443 * subscript (',' subscript)* [',']
2445 static int
2446 validate_subscriptlist(node *tree)
2448 return (validate_repeating_list(tree, subscriptlist,
2449 validate_subscript, "subscriptlist"));
2453 /* subscript:
2455 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
2457 static int
2458 validate_subscript(node *tree)
2460 int offset = 0;
2461 int nch = NCH(tree);
2462 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
2464 if (!res) {
2465 if (!PyErr_Occurred())
2466 err_string("invalid number of arguments for subscript node");
2467 return (0);
2469 if (TYPE(CHILD(tree, 0)) == DOT)
2470 /* take care of ('.' '.' '.') possibility */
2471 return (validate_numnodes(tree, 3, "subscript")
2472 && validate_dot(CHILD(tree, 0))
2473 && validate_dot(CHILD(tree, 1))
2474 && validate_dot(CHILD(tree, 2)));
2475 if (nch == 1) {
2476 if (TYPE(CHILD(tree, 0)) == test)
2477 res = validate_test(CHILD(tree, 0));
2478 else
2479 res = validate_colon(CHILD(tree, 0));
2480 return (res);
2482 /* Must be [test] ':' [test] [sliceop],
2483 * but at least one of the optional components will
2484 * be present, but we don't know which yet.
2486 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
2487 res = validate_test(CHILD(tree, 0));
2488 offset = 1;
2490 if (res)
2491 res = validate_colon(CHILD(tree, offset));
2492 if (res) {
2493 int rem = nch - ++offset;
2494 if (rem) {
2495 if (TYPE(CHILD(tree, offset)) == test) {
2496 res = validate_test(CHILD(tree, offset));
2497 ++offset;
2498 --rem;
2500 if (res && rem)
2501 res = validate_sliceop(CHILD(tree, offset));
2504 return (res);
2508 static int
2509 validate_sliceop(node *tree)
2511 int nch = NCH(tree);
2512 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
2513 && validate_ntype(tree, sliceop);
2514 if (!res && !PyErr_Occurred()) {
2515 res = validate_numnodes(tree, 1, "sliceop");
2517 if (res)
2518 res = validate_colon(CHILD(tree, 0));
2519 if (res && (nch == 2))
2520 res = validate_test(CHILD(tree, 1));
2522 return (res);
2526 static int
2527 validate_exprlist(node *tree)
2529 return (validate_repeating_list(tree, exprlist,
2530 validate_expr, "exprlist"));
2534 static int
2535 validate_dictmaker(node *tree)
2537 int nch = NCH(tree);
2538 int res = (validate_ntype(tree, dictmaker)
2539 && (nch >= 3)
2540 && validate_test(CHILD(tree, 0))
2541 && validate_colon(CHILD(tree, 1))
2542 && validate_test(CHILD(tree, 2)));
2544 if (res && ((nch % 4) == 0))
2545 res = validate_comma(CHILD(tree, --nch));
2546 else if (res)
2547 res = ((nch % 4) == 3);
2549 if (res && (nch > 3)) {
2550 int pos = 3;
2551 /* ( ',' test ':' test )* */
2552 while (res && (pos < nch)) {
2553 res = (validate_comma(CHILD(tree, pos))
2554 && validate_test(CHILD(tree, pos + 1))
2555 && validate_colon(CHILD(tree, pos + 2))
2556 && validate_test(CHILD(tree, pos + 3)));
2557 pos += 4;
2560 return (res);
2564 static int
2565 validate_eval_input(node *tree)
2567 int pos;
2568 int nch = NCH(tree);
2569 int res = (validate_ntype(tree, eval_input)
2570 && (nch >= 2)
2571 && validate_testlist(CHILD(tree, 0))
2572 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
2574 for (pos = 1; res && (pos < (nch - 1)); ++pos)
2575 res = validate_ntype(CHILD(tree, pos), NEWLINE);
2577 return (res);
2581 static int
2582 validate_node(node *tree)
2584 int nch = 0; /* num. children on current node */
2585 int res = 1; /* result value */
2586 node* next = 0; /* node to process after this one */
2588 while (res && (tree != 0)) {
2589 nch = NCH(tree);
2590 next = 0;
2591 switch (TYPE(tree)) {
2593 * Definition nodes.
2595 case funcdef:
2596 res = validate_funcdef(tree);
2597 break;
2598 case classdef:
2599 res = validate_class(tree);
2600 break;
2602 * "Trivial" parse tree nodes.
2603 * (Why did I call these trivial?)
2605 case stmt:
2606 res = validate_stmt(tree);
2607 break;
2608 case small_stmt:
2610 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2611 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2613 res = validate_small_stmt(tree);
2614 break;
2615 case flow_stmt:
2616 res = (validate_numnodes(tree, 1, "flow_stmt")
2617 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2618 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2619 || (TYPE(CHILD(tree, 0)) == yield_stmt)
2620 || (TYPE(CHILD(tree, 0)) == return_stmt)
2621 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2622 if (res)
2623 next = CHILD(tree, 0);
2624 else if (nch == 1)
2625 err_string("illegal flow_stmt type");
2626 break;
2627 case yield_stmt:
2628 res = validate_yield_stmt(tree);
2629 break;
2631 * Compound statements.
2633 case simple_stmt:
2634 res = validate_simple_stmt(tree);
2635 break;
2636 case compound_stmt:
2637 res = validate_compound_stmt(tree);
2638 break;
2640 * Fundamental statements.
2642 case expr_stmt:
2643 res = validate_expr_stmt(tree);
2644 break;
2645 case print_stmt:
2646 res = validate_print_stmt(tree);
2647 break;
2648 case del_stmt:
2649 res = validate_del_stmt(tree);
2650 break;
2651 case pass_stmt:
2652 res = (validate_numnodes(tree, 1, "pass")
2653 && validate_name(CHILD(tree, 0), "pass"));
2654 break;
2655 case break_stmt:
2656 res = (validate_numnodes(tree, 1, "break")
2657 && validate_name(CHILD(tree, 0), "break"));
2658 break;
2659 case continue_stmt:
2660 res = (validate_numnodes(tree, 1, "continue")
2661 && validate_name(CHILD(tree, 0), "continue"));
2662 break;
2663 case return_stmt:
2664 res = validate_return_stmt(tree);
2665 break;
2666 case raise_stmt:
2667 res = validate_raise_stmt(tree);
2668 break;
2669 case import_stmt:
2670 res = validate_import_stmt(tree);
2671 break;
2672 case global_stmt:
2673 res = validate_global_stmt(tree);
2674 break;
2675 case exec_stmt:
2676 res = validate_exec_stmt(tree);
2677 break;
2678 case assert_stmt:
2679 res = validate_assert_stmt(tree);
2680 break;
2681 case if_stmt:
2682 res = validate_if(tree);
2683 break;
2684 case while_stmt:
2685 res = validate_while(tree);
2686 break;
2687 case for_stmt:
2688 res = validate_for(tree);
2689 break;
2690 case try_stmt:
2691 res = validate_try(tree);
2692 break;
2693 case suite:
2694 res = validate_suite(tree);
2695 break;
2697 * Expression nodes.
2699 case testlist:
2700 res = validate_testlist(tree);
2701 break;
2702 case testlist1:
2703 res = validate_testlist1(tree);
2704 break;
2705 case test:
2706 res = validate_test(tree);
2707 break;
2708 case and_test:
2709 res = validate_and_test(tree);
2710 break;
2711 case not_test:
2712 res = validate_not_test(tree);
2713 break;
2714 case comparison:
2715 res = validate_comparison(tree);
2716 break;
2717 case exprlist:
2718 res = validate_exprlist(tree);
2719 break;
2720 case comp_op:
2721 res = validate_comp_op(tree);
2722 break;
2723 case expr:
2724 res = validate_expr(tree);
2725 break;
2726 case xor_expr:
2727 res = validate_xor_expr(tree);
2728 break;
2729 case and_expr:
2730 res = validate_and_expr(tree);
2731 break;
2732 case shift_expr:
2733 res = validate_shift_expr(tree);
2734 break;
2735 case arith_expr:
2736 res = validate_arith_expr(tree);
2737 break;
2738 case term:
2739 res = validate_term(tree);
2740 break;
2741 case factor:
2742 res = validate_factor(tree);
2743 break;
2744 case power:
2745 res = validate_power(tree);
2746 break;
2747 case atom:
2748 res = validate_atom(tree);
2749 break;
2751 default:
2752 /* Hopefully never reached! */
2753 err_string("unrecognized node type");
2754 res = 0;
2755 break;
2757 tree = next;
2759 return (res);
2763 static int
2764 validate_expr_tree(node *tree)
2766 int res = validate_eval_input(tree);
2768 if (!res && !PyErr_Occurred())
2769 err_string("could not validate expression tuple");
2771 return (res);
2775 /* file_input:
2776 * (NEWLINE | stmt)* ENDMARKER
2778 static int
2779 validate_file_input(node *tree)
2781 int j;
2782 int nch = NCH(tree) - 1;
2783 int res = ((nch >= 0)
2784 && validate_ntype(CHILD(tree, nch), ENDMARKER));
2786 for (j = 0; res && (j < nch); ++j) {
2787 if (TYPE(CHILD(tree, j)) == stmt)
2788 res = validate_stmt(CHILD(tree, j));
2789 else
2790 res = validate_newline(CHILD(tree, j));
2792 /* This stays in to prevent any internal failures from getting to the
2793 * user. Hopefully, this won't be needed. If a user reports getting
2794 * this, we have some debugging to do.
2796 if (!res && !PyErr_Occurred())
2797 err_string("VALIDATION FAILURE: report this to the maintainer!");
2799 return (res);
2802 static int
2803 validate_encoding_decl(node *tree)
2805 int nch = NCH(tree);
2806 int res = ((nch == 1)
2807 && validate_file_input(CHILD(tree, 0)));
2809 if (!res && !PyErr_Occurred())
2810 err_string("Error Parsing encoding_decl");
2812 return res;
2815 static PyObject*
2816 pickle_constructor = NULL;
2819 static PyObject*
2820 parser__pickler(PyObject *self, PyObject *args)
2822 NOTE(ARGUNUSED(self))
2823 PyObject *result = NULL;
2824 PyObject *st = NULL;
2825 PyObject *empty_dict = NULL;
2827 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
2828 PyObject *newargs;
2829 PyObject *tuple;
2831 if ((empty_dict = PyDict_New()) == NULL)
2832 goto finally;
2833 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
2834 goto finally;
2835 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
2836 if (tuple != NULL) {
2837 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
2838 Py_DECREF(tuple);
2840 Py_DECREF(empty_dict);
2841 Py_DECREF(newargs);
2843 finally:
2844 Py_XDECREF(empty_dict);
2846 return (result);
2850 /* Functions exported by this module. Most of this should probably
2851 * be converted into an ST object with methods, but that is better
2852 * done directly in Python, allowing subclasses to be created directly.
2853 * We'd really have to write a wrapper around it all anyway to allow
2854 * inheritance.
2856 static PyMethodDef parser_functions[] = {
2857 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2858 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
2859 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2860 PyDoc_STR("Creates a list-tree representation of an ST.")},
2861 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2862 PyDoc_STR("Compiles an ST object into a code object.")},
2863 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
2864 PyDoc_STR("Compiles an ST object into a code object.")},
2865 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
2866 PyDoc_STR("Creates an ST object from an expression.")},
2867 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
2868 PyDoc_STR("Determines if an ST object was created from an expression.")},
2869 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
2870 PyDoc_STR("Determines if an ST object was created from a suite.")},
2871 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
2872 PyDoc_STR("Creates an ST object from a suite.")},
2873 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2874 PyDoc_STR("Creates an ST object from a tree representation.")},
2875 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2876 PyDoc_STR("Creates an ST object from a tree representation.")},
2877 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
2878 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
2879 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
2880 PyDoc_STR("Creates a list-tree representation of an ST.")},
2881 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2882 PyDoc_STR("Creates an ST object from a tree representation.")},
2883 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
2884 PyDoc_STR("Creates an ST object from a tree representation.")},
2886 /* private stuff: support pickle module */
2887 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
2888 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
2890 {NULL, NULL, 0, NULL}
2894 PyMODINIT_FUNC initparser(void); /* supply a prototype */
2896 PyMODINIT_FUNC
2897 initparser(void)
2899 PyObject *module, *copyreg;
2901 PyST_Type.ob_type = &PyType_Type;
2902 module = Py_InitModule("parser", parser_functions);
2904 if (parser_error == 0)
2905 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
2907 if ((parser_error == 0)
2908 || (PyModule_AddObject(module, "ParserError", parser_error) != 0)) {
2909 /* caller will check PyErr_Occurred() */
2910 return;
2912 Py_INCREF(&PyST_Type);
2913 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
2914 Py_INCREF(&PyST_Type);
2915 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
2917 PyModule_AddStringConstant(module, "__copyright__",
2918 parser_copyright_string);
2919 PyModule_AddStringConstant(module, "__doc__",
2920 parser_doc_string);
2921 PyModule_AddStringConstant(module, "__version__",
2922 parser_version_string);
2924 /* Register to support pickling.
2925 * If this fails, the import of this module will fail because an
2926 * exception will be raised here; should we clear the exception?
2928 copyreg = PyImport_ImportModule("copy_reg");
2929 if (copyreg != NULL) {
2930 PyObject *func, *pickler;
2932 func = PyObject_GetAttrString(copyreg, "pickle");
2933 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
2934 pickler = PyObject_GetAttrString(module, "_pickler");
2935 Py_XINCREF(pickle_constructor);
2936 if ((func != NULL) && (pickle_constructor != NULL)
2937 && (pickler != NULL)) {
2938 PyObject *res;
2940 res = PyObject_CallFunction(func, "OOO", &PyST_Type, pickler,
2941 pickle_constructor);
2942 Py_XDECREF(res);
2944 Py_XDECREF(func);
2945 Py_XDECREF(pickle_constructor);
2946 Py_XDECREF(pickler);
2947 Py_DECREF(copyreg);