Remove tuple parameter unpacking in aifc to silence warnings under -3.
[python.git] / Modules / parsermodule.c
blob915bbdc7b2e29db3bef9fa94166803ef02508c9c
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 /* String constants used to initialize module attributes.
45 static char parser_copyright_string[] =
46 "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
47 University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
48 Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
49 Centrum, Amsterdam, The Netherlands.";
52 PyDoc_STRVAR(parser_doc_string,
53 "This is an interface to Python's internal parser.");
55 static char parser_version_string[] = "0.5";
58 typedef PyObject* (*SeqMaker) (Py_ssize_t length);
59 typedef int (*SeqInserter) (PyObject* sequence,
60 Py_ssize_t index,
61 PyObject* element);
63 /* The function below is copyrighted by Stichting Mathematisch Centrum. The
64 * original copyright statement is included below, and continues to apply
65 * in full to the function immediately following. All other material is
66 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
67 * Institute and State University. Changes were made to comply with the
68 * new naming conventions. Added arguments to provide support for creating
69 * lists as well as tuples, and optionally including the line numbers.
73 static PyObject*
74 node2tuple(node *n, /* node to convert */
75 SeqMaker mkseq, /* create sequence */
76 SeqInserter addelem, /* func. to add elem. in seq. */
77 int lineno, /* include line numbers? */
78 int col_offset) /* include column offsets? */
80 if (n == NULL) {
81 Py_INCREF(Py_None);
82 return (Py_None);
84 if (ISNONTERMINAL(TYPE(n))) {
85 int i;
86 PyObject *v;
87 PyObject *w;
89 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
90 if (v == NULL)
91 return (v);
92 w = PyInt_FromLong(TYPE(n));
93 if (w == NULL) {
94 Py_DECREF(v);
95 return ((PyObject*) NULL);
97 (void) addelem(v, 0, w);
98 for (i = 0; i < NCH(n); i++) {
99 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
100 if (w == NULL) {
101 Py_DECREF(v);
102 return ((PyObject*) NULL);
104 (void) addelem(v, i+1, w);
107 if (TYPE(n) == encoding_decl)
108 (void) addelem(v, i+1, PyString_FromString(STR(n)));
109 return (v);
111 else if (ISTERMINAL(TYPE(n))) {
112 PyObject *result = mkseq(2 + lineno + col_offset);
113 if (result != NULL) {
114 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
115 (void) addelem(result, 1, PyString_FromString(STR(n)));
116 if (lineno == 1)
117 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
118 if (col_offset == 1)
119 (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset));
121 return (result);
123 else {
124 PyErr_SetString(PyExc_SystemError,
125 "unrecognized parse tree node type");
126 return ((PyObject*) NULL);
130 * End of material copyrighted by Stichting Mathematisch Centrum.
135 /* There are two types of intermediate objects we're interested in:
136 * 'eval' and 'exec' types. These constants can be used in the st_type
137 * field of the object type to identify which any given object represents.
138 * These should probably go in an external header to allow other extensions
139 * to use them, but then, we really should be using C++ too. ;-)
142 #define PyST_EXPR 1
143 #define PyST_SUITE 2
146 /* These are the internal objects and definitions required to implement the
147 * ST type. Most of the internal names are more reminiscent of the 'old'
148 * naming style, but the code uses the new naming convention.
151 static PyObject*
152 parser_error = 0;
155 typedef struct {
156 PyObject_HEAD /* standard object header */
157 node* st_node; /* the node* returned by the parser */
158 int st_type; /* EXPR or SUITE ? */
159 } PyST_Object;
162 static void parser_free(PyST_Object *st);
163 static int parser_compare(PyST_Object *left, PyST_Object *right);
164 static PyObject *parser_getattr(PyObject *self, char *name);
167 static
168 PyTypeObject PyST_Type = {
169 PyVarObject_HEAD_INIT(NULL, 0)
170 "parser.st", /* tp_name */
171 (int) sizeof(PyST_Object), /* tp_basicsize */
172 0, /* tp_itemsize */
173 (destructor)parser_free, /* tp_dealloc */
174 0, /* tp_print */
175 parser_getattr, /* tp_getattr */
176 0, /* tp_setattr */
177 (cmpfunc)parser_compare, /* tp_compare */
178 0, /* tp_repr */
179 0, /* tp_as_number */
180 0, /* tp_as_sequence */
181 0, /* tp_as_mapping */
182 0, /* tp_hash */
183 0, /* tp_call */
184 0, /* tp_str */
185 0, /* tp_getattro */
186 0, /* tp_setattro */
188 /* Functions to access object as input/output buffer */
189 0, /* tp_as_buffer */
191 Py_TPFLAGS_DEFAULT, /* tp_flags */
193 /* __doc__ */
194 "Intermediate representation of a Python parse tree."
195 }; /* PyST_Type */
198 static int
199 parser_compare_nodes(node *left, node *right)
201 int j;
203 if (TYPE(left) < TYPE(right))
204 return (-1);
206 if (TYPE(right) < TYPE(left))
207 return (1);
209 if (ISTERMINAL(TYPE(left)))
210 return (strcmp(STR(left), STR(right)));
212 if (NCH(left) < NCH(right))
213 return (-1);
215 if (NCH(right) < NCH(left))
216 return (1);
218 for (j = 0; j < NCH(left); ++j) {
219 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
221 if (v != 0)
222 return (v);
224 return (0);
228 /* int parser_compare(PyST_Object* left, PyST_Object* right)
230 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
231 * This really just wraps a call to parser_compare_nodes() with some easy
232 * checks and protection code.
235 static int
236 parser_compare(PyST_Object *left, PyST_Object *right)
238 if (left == right)
239 return (0);
241 if ((left == 0) || (right == 0))
242 return (-1);
244 return (parser_compare_nodes(left->st_node, right->st_node));
248 /* parser_newstobject(node* st)
250 * Allocates a new Python object representing an ST. This is simply the
251 * 'wrapper' object that holds a node* and allows it to be passed around in
252 * Python code.
255 static PyObject*
256 parser_newstobject(node *st, int type)
258 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
260 if (o != 0) {
261 o->st_node = st;
262 o->st_type = type;
264 else {
265 PyNode_Free(st);
267 return ((PyObject*)o);
271 /* void parser_free(PyST_Object* st)
273 * This is called by a del statement that reduces the reference count to 0.
276 static void
277 parser_free(PyST_Object *st)
279 PyNode_Free(st->st_node);
280 PyObject_Del(st);
284 /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
286 * This provides conversion from a node* to a tuple object that can be
287 * returned to the Python-level caller. The ST object is not modified.
290 static PyObject*
291 parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
293 PyObject *line_option = 0;
294 PyObject *col_option = 0;
295 PyObject *res = 0;
296 int ok;
298 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
300 if (self == NULL) {
301 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
302 &PyST_Type, &self, &line_option,
303 &col_option);
305 else
306 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
307 &line_option, &col_option);
308 if (ok != 0) {
309 int lineno = 0;
310 int col_offset = 0;
311 if (line_option != NULL) {
312 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
314 if (col_option != NULL) {
315 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
318 * Convert ST into a tuple representation. Use Guido's function,
319 * since it's known to work already.
321 res = node2tuple(((PyST_Object*)self)->st_node,
322 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
324 return (res);
327 static PyObject*
328 parser_ast2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
330 if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
331 return NULL;
332 return parser_st2tuple(self, args, kw);
336 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
338 * This provides conversion from a node* to a list object that can be
339 * returned to the Python-level caller. The ST object is not modified.
342 static PyObject*
343 parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
345 PyObject *line_option = 0;
346 PyObject *col_option = 0;
347 PyObject *res = 0;
348 int ok;
350 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
352 if (self == NULL)
353 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
354 &PyST_Type, &self, &line_option,
355 &col_option);
356 else
357 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
358 &line_option, &col_option);
359 if (ok) {
360 int lineno = 0;
361 int col_offset = 0;
362 if (line_option != 0) {
363 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
365 if (col_option != NULL) {
366 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
369 * Convert ST into a tuple representation. Use Guido's function,
370 * since it's known to work already.
372 res = node2tuple(self->st_node,
373 PyList_New, PyList_SetItem, lineno, col_offset);
375 return (res);
378 static PyObject*
379 parser_ast2list(PyST_Object *self, PyObject *args, PyObject *kw)
381 if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
382 return NULL;
383 return parser_st2list(self, args, kw);
387 /* parser_compilest(PyObject* self, PyObject* args)
389 * This function creates code objects from the parse tree represented by
390 * the passed-in data object. An optional file name is passed in as well.
393 static PyObject*
394 parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
396 PyObject* res = 0;
397 char* str = "<syntax-tree>";
398 int ok;
400 static char *keywords[] = {"ast", "filename", NULL};
402 if (self == NULL)
403 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
404 &PyST_Type, &self, &str);
405 else
406 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
407 &str);
409 if (ok)
410 res = (PyObject *)PyNode_Compile(self->st_node, str);
412 return (res);
415 static PyObject*
416 parser_compileast(PyST_Object *self, PyObject *args, PyObject *kw)
418 if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
419 return NULL;
420 return parser_compilest(self, args, kw);
424 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
425 * PyObject* parser_issuite(PyObject* self, PyObject* args)
427 * Checks the passed-in ST object to determine if it is an expression or
428 * a statement suite, respectively. The return is a Python truth value.
431 static PyObject*
432 parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
434 PyObject* res = 0;
435 int ok;
437 static char *keywords[] = {"ast", NULL};
439 if (self == NULL)
440 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
441 &PyST_Type, &self);
442 else
443 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
445 if (ok) {
446 /* Check to see if the ST represents an expression or not. */
447 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
448 Py_INCREF(res);
450 return (res);
454 static PyObject*
455 parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
457 PyObject* res = 0;
458 int ok;
460 static char *keywords[] = {"ast", NULL};
462 if (self == NULL)
463 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
464 &PyST_Type, &self);
465 else
466 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
468 if (ok) {
469 /* Check to see if the ST represents an expression or not. */
470 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
471 Py_INCREF(res);
473 return (res);
477 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
479 static PyMethodDef
480 parser_methods[] = {
481 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
482 PyDoc_STR("Compile this ST object into a code object.")},
483 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
484 PyDoc_STR("Determines if this ST object was created from an expression.")},
485 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
486 PyDoc_STR("Determines if this ST object was created from a suite.")},
487 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
488 PyDoc_STR("Creates a list-tree representation of this ST.")},
489 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
490 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
492 {NULL, NULL, 0, NULL}
496 static PyObject*
497 parser_getattr(PyObject *self, char *name)
499 return (Py_FindMethod(parser_methods, self, name));
503 /* err_string(char* message)
505 * Sets the error string for an exception of type ParserError.
508 static void
509 err_string(char *message)
511 PyErr_SetString(parser_error, message);
515 /* PyObject* parser_do_parse(PyObject* args, int type)
517 * Internal function to actually execute the parse and return the result if
518 * successful or set an exception if not.
521 static PyObject*
522 parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
524 char* string = 0;
525 PyObject* res = 0;
527 static char *keywords[] = {"source", NULL};
529 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
530 node* n = PyParser_SimpleParseString(string,
531 (type == PyST_EXPR)
532 ? eval_input : file_input);
534 if (n)
535 res = parser_newstobject(n, type);
537 return (res);
541 /* PyObject* parser_expr(PyObject* self, PyObject* args)
542 * PyObject* parser_suite(PyObject* self, PyObject* args)
544 * External interfaces to the parser itself. Which is called determines if
545 * the parser attempts to recognize an expression ('eval' form) or statement
546 * suite ('exec' form). The real work is done by parser_do_parse() above.
549 static PyObject*
550 parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
552 NOTE(ARGUNUSED(self))
553 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
557 static PyObject*
558 parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
560 NOTE(ARGUNUSED(self))
561 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
566 /* This is the messy part of the code. Conversion from a tuple to an ST
567 * object requires that the input tuple be valid without having to rely on
568 * catching an exception from the compiler. This is done to allow the
569 * compiler itself to remain fast, since most of its input will come from
570 * the parser directly, and therefore be known to be syntactically correct.
571 * This validation is done to ensure that we don't core dump the compile
572 * phase, returning an exception instead.
574 * Two aspects can be broken out in this code: creating a node tree from
575 * the tuple passed in, and verifying that it is indeed valid. It may be
576 * advantageous to expand the number of ST types to include funcdefs and
577 * lambdadefs to take advantage of the optimizer, recognizing those STs
578 * here. They are not necessary, and not quite as useful in a raw form.
579 * For now, let's get expressions and suites working reliably.
583 static node* build_node_tree(PyObject *tuple);
584 static int validate_expr_tree(node *tree);
585 static int validate_file_input(node *tree);
586 static int validate_encoding_decl(node *tree);
588 /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
590 * This is the public function, called from the Python code. It receives a
591 * single tuple object from the caller, and creates an ST object if the
592 * tuple can be validated. It does this by checking the first code of the
593 * tuple, and, if acceptable, builds the internal representation. If this
594 * step succeeds, the internal representation is validated as fully as
595 * possible with the various validate_*() routines defined below.
597 * This function must be changed if support is to be added for PyST_FRAGMENT
598 * ST objects.
601 static PyObject*
602 parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
604 NOTE(ARGUNUSED(self))
605 PyObject *st = 0;
606 PyObject *tuple;
607 node *tree;
609 static char *keywords[] = {"sequence", NULL};
611 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
612 &tuple))
613 return (0);
614 if (!PySequence_Check(tuple)) {
615 PyErr_SetString(PyExc_ValueError,
616 "sequence2st() requires a single sequence argument");
617 return (0);
620 * Convert the tree to the internal form before checking it.
622 tree = build_node_tree(tuple);
623 if (tree != 0) {
624 int start_sym = TYPE(tree);
625 if (start_sym == eval_input) {
626 /* Might be an eval form. */
627 if (validate_expr_tree(tree))
628 st = parser_newstobject(tree, PyST_EXPR);
629 else
630 PyNode_Free(tree);
632 else if (start_sym == file_input) {
633 /* This looks like an exec form so far. */
634 if (validate_file_input(tree))
635 st = parser_newstobject(tree, PyST_SUITE);
636 else
637 PyNode_Free(tree);
639 else if (start_sym == encoding_decl) {
640 /* This looks like an encoding_decl so far. */
641 if (validate_encoding_decl(tree))
642 st = parser_newstobject(tree, PyST_SUITE);
643 else
644 PyNode_Free(tree);
646 else {
647 /* This is a fragment, at best. */
648 PyNode_Free(tree);
649 err_string("parse tree does not use a valid start symbol");
652 /* Make sure we throw an exception on all errors. We should never
653 * get this, but we'd do well to be sure something is done.
655 if (st == NULL && !PyErr_Occurred())
656 err_string("unspecified ST error occurred");
658 return st;
661 static PyObject*
662 parser_tuple2ast(PyST_Object *self, PyObject *args, PyObject *kw)
664 if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
665 return NULL;
666 return parser_tuple2st(self, args, kw);
670 /* node* build_node_children()
672 * Iterate across the children of the current non-terminal node and build
673 * their structures. If successful, return the root of this portion of
674 * the tree, otherwise, 0. Any required exception will be specified already,
675 * and no memory will have been deallocated.
678 static node*
679 build_node_children(PyObject *tuple, node *root, int *line_num)
681 Py_ssize_t len = PyObject_Size(tuple);
682 Py_ssize_t i;
683 int err;
685 for (i = 1; i < len; ++i) {
686 /* elem must always be a sequence, however simple */
687 PyObject* elem = PySequence_GetItem(tuple, i);
688 int ok = elem != NULL;
689 long type = 0;
690 char *strn = 0;
692 if (ok)
693 ok = PySequence_Check(elem);
694 if (ok) {
695 PyObject *temp = PySequence_GetItem(elem, 0);
696 if (temp == NULL)
697 ok = 0;
698 else {
699 ok = PyInt_Check(temp);
700 if (ok)
701 type = PyInt_AS_LONG(temp);
702 Py_DECREF(temp);
705 if (!ok) {
706 PyObject *err = Py_BuildValue("os", elem,
707 "Illegal node construct.");
708 PyErr_SetObject(parser_error, err);
709 Py_XDECREF(err);
710 Py_XDECREF(elem);
711 return (0);
713 if (ISTERMINAL(type)) {
714 Py_ssize_t len = PyObject_Size(elem);
715 PyObject *temp;
717 if ((len != 2) && (len != 3)) {
718 err_string("terminal nodes must have 2 or 3 entries");
719 return 0;
721 temp = PySequence_GetItem(elem, 1);
722 if (temp == NULL)
723 return 0;
724 if (!PyString_Check(temp)) {
725 PyErr_Format(parser_error,
726 "second item in terminal node must be a string,"
727 " found %s",
728 Py_TYPE(temp)->tp_name);
729 Py_DECREF(temp);
730 return 0;
732 if (len == 3) {
733 PyObject *o = PySequence_GetItem(elem, 2);
734 if (o != NULL) {
735 if (PyInt_Check(o))
736 *line_num = PyInt_AS_LONG(o);
737 else {
738 PyErr_Format(parser_error,
739 "third item in terminal node must be an"
740 " integer, found %s",
741 Py_TYPE(temp)->tp_name);
742 Py_DECREF(o);
743 Py_DECREF(temp);
744 return 0;
746 Py_DECREF(o);
749 len = PyString_GET_SIZE(temp) + 1;
750 strn = (char *)PyObject_MALLOC(len);
751 if (strn != NULL)
752 (void) memcpy(strn, PyString_AS_STRING(temp), len);
753 Py_DECREF(temp);
755 else if (!ISNONTERMINAL(type)) {
757 * It has to be one or the other; this is an error.
758 * Throw an exception.
760 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
761 PyErr_SetObject(parser_error, err);
762 Py_XDECREF(err);
763 Py_XDECREF(elem);
764 return (0);
766 err = PyNode_AddChild(root, type, strn, *line_num, 0);
767 if (err == E_NOMEM) {
768 PyObject_FREE(strn);
769 return (node *) PyErr_NoMemory();
771 if (err == E_OVERFLOW) {
772 PyObject_FREE(strn);
773 PyErr_SetString(PyExc_ValueError,
774 "unsupported number of child nodes");
775 return NULL;
778 if (ISNONTERMINAL(type)) {
779 node* new_child = CHILD(root, i - 1);
781 if (new_child != build_node_children(elem, new_child, line_num)) {
782 Py_XDECREF(elem);
783 return (0);
786 else if (type == NEWLINE) { /* It's true: we increment the */
787 ++(*line_num); /* line number *after* the newline! */
789 Py_XDECREF(elem);
791 return root;
795 static node*
796 build_node_tree(PyObject *tuple)
798 node* res = 0;
799 PyObject *temp = PySequence_GetItem(tuple, 0);
800 long num = -1;
802 if (temp != NULL)
803 num = PyInt_AsLong(temp);
804 Py_XDECREF(temp);
805 if (ISTERMINAL(num)) {
807 * The tuple is simple, but it doesn't start with a start symbol.
808 * Throw an exception now and be done with it.
810 tuple = Py_BuildValue("os", tuple,
811 "Illegal syntax-tree; cannot start with terminal symbol.");
812 PyErr_SetObject(parser_error, tuple);
813 Py_XDECREF(tuple);
815 else if (ISNONTERMINAL(num)) {
817 * Not efficient, but that can be handled later.
819 int line_num = 0;
820 PyObject *encoding = NULL;
822 if (num == encoding_decl) {
823 encoding = PySequence_GetItem(tuple, 2);
824 /* tuple isn't borrowed anymore here, need to DECREF */
825 tuple = PySequence_GetSlice(tuple, 0, 2);
827 res = PyNode_New(num);
828 if (res != NULL) {
829 if (res != build_node_children(tuple, res, &line_num)) {
830 PyNode_Free(res);
831 res = NULL;
833 if (res && encoding) {
834 Py_ssize_t len;
835 len = PyString_GET_SIZE(encoding) + 1;
836 res->n_str = (char *)PyObject_MALLOC(len);
837 if (res->n_str != NULL)
838 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
839 Py_DECREF(encoding);
840 Py_DECREF(tuple);
844 else {
845 /* The tuple is illegal -- if the number is neither TERMINAL nor
846 * NONTERMINAL, we can't use it. Not sure the implementation
847 * allows this condition, but the API doesn't preclude it.
849 PyObject *err = Py_BuildValue("os", tuple,
850 "Illegal component tuple.");
851 PyErr_SetObject(parser_error, err);
852 Py_XDECREF(err);
855 return (res);
860 * Validation routines used within the validation section:
862 static int validate_terminal(node *terminal, int type, char *string);
864 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
865 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
866 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
867 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
868 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
869 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
870 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
871 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
872 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
873 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
874 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
875 #define validate_star(ch) validate_terminal(ch, STAR, "*")
876 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
877 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
878 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
879 #define validate_at(ch) validate_terminal(ch, AT, "@")
880 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
882 #define VALIDATER(n) static int validate_##n(node *tree)
884 VALIDATER(node); VALIDATER(small_stmt);
885 VALIDATER(class); VALIDATER(node);
886 VALIDATER(parameters); VALIDATER(suite);
887 VALIDATER(testlist); VALIDATER(varargslist);
888 VALIDATER(fpdef); VALIDATER(fplist);
889 VALIDATER(stmt); VALIDATER(simple_stmt);
890 VALIDATER(expr_stmt); VALIDATER(power);
891 VALIDATER(print_stmt); VALIDATER(del_stmt);
892 VALIDATER(return_stmt); VALIDATER(list_iter);
893 VALIDATER(raise_stmt); VALIDATER(import_stmt);
894 VALIDATER(import_name); VALIDATER(import_from);
895 VALIDATER(global_stmt); VALIDATER(list_if);
896 VALIDATER(assert_stmt); VALIDATER(list_for);
897 VALIDATER(exec_stmt); VALIDATER(compound_stmt);
898 VALIDATER(while); VALIDATER(for);
899 VALIDATER(try); VALIDATER(except_clause);
900 VALIDATER(test); VALIDATER(and_test);
901 VALIDATER(not_test); VALIDATER(comparison);
902 VALIDATER(comp_op); VALIDATER(expr);
903 VALIDATER(xor_expr); VALIDATER(and_expr);
904 VALIDATER(shift_expr); VALIDATER(arith_expr);
905 VALIDATER(term); VALIDATER(factor);
906 VALIDATER(atom); VALIDATER(lambdef);
907 VALIDATER(trailer); VALIDATER(subscript);
908 VALIDATER(subscriptlist); VALIDATER(sliceop);
909 VALIDATER(exprlist); VALIDATER(dictmaker);
910 VALIDATER(arglist); VALIDATER(argument);
911 VALIDATER(listmaker); VALIDATER(yield_stmt);
912 VALIDATER(testlist1); VALIDATER(gen_for);
913 VALIDATER(gen_iter); VALIDATER(gen_if);
914 VALIDATER(testlist_gexp); VALIDATER(yield_expr);
915 VALIDATER(yield_or_testlist); VALIDATER(or_test);
916 VALIDATER(old_test); VALIDATER(old_lambdef);
918 #undef VALIDATER
920 #define is_even(n) (((n) & 1) == 0)
921 #define is_odd(n) (((n) & 1) == 1)
924 static int
925 validate_ntype(node *n, int t)
927 if (TYPE(n) != t) {
928 PyErr_Format(parser_error, "Expected node type %d, got %d.",
929 t, TYPE(n));
930 return 0;
932 return 1;
936 /* Verifies that the number of child nodes is exactly 'num', raising
937 * an exception if it isn't. The exception message does not indicate
938 * the exact number of nodes, allowing this to be used to raise the
939 * "right" exception when the wrong number of nodes is present in a
940 * specific variant of a statement's syntax. This is commonly used
941 * in that fashion.
943 static int
944 validate_numnodes(node *n, int num, const char *const name)
946 if (NCH(n) != num) {
947 PyErr_Format(parser_error,
948 "Illegal number of children for %s node.", name);
949 return 0;
951 return 1;
955 static int
956 validate_terminal(node *terminal, int type, char *string)
958 int res = (validate_ntype(terminal, type)
959 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
961 if (!res && !PyErr_Occurred()) {
962 PyErr_Format(parser_error,
963 "Illegal terminal: expected \"%s\"", string);
965 return (res);
969 /* X (',' X) [',']
971 static int
972 validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
973 const char *const name)
975 int nch = NCH(tree);
976 int res = (nch && validate_ntype(tree, ntype)
977 && vfunc(CHILD(tree, 0)));
979 if (!res && !PyErr_Occurred())
980 (void) validate_numnodes(tree, 1, name);
981 else {
982 if (is_even(nch))
983 res = validate_comma(CHILD(tree, --nch));
984 if (res && nch > 1) {
985 int pos = 1;
986 for ( ; res && pos < nch; pos += 2)
987 res = (validate_comma(CHILD(tree, pos))
988 && vfunc(CHILD(tree, pos + 1)));
991 return (res);
995 /* validate_class()
997 * classdef:
998 * 'class' NAME ['(' testlist ')'] ':' suite
1000 static int
1001 validate_class(node *tree)
1003 int nch = NCH(tree);
1004 int res = (validate_ntype(tree, classdef) &&
1005 ((nch == 4) || (nch == 6) || (nch == 7)));
1007 if (res) {
1008 res = (validate_name(CHILD(tree, 0), "class")
1009 && validate_ntype(CHILD(tree, 1), NAME)
1010 && validate_colon(CHILD(tree, nch - 2))
1011 && validate_suite(CHILD(tree, nch - 1)));
1013 else {
1014 (void) validate_numnodes(tree, 4, "class");
1017 if (res) {
1018 if (nch == 7) {
1019 res = ((validate_lparen(CHILD(tree, 2)) &&
1020 validate_testlist(CHILD(tree, 3)) &&
1021 validate_rparen(CHILD(tree, 4))));
1023 else if (nch == 6) {
1024 res = (validate_lparen(CHILD(tree,2)) &&
1025 validate_rparen(CHILD(tree,3)));
1028 return (res);
1032 /* if_stmt:
1033 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1035 static int
1036 validate_if(node *tree)
1038 int nch = NCH(tree);
1039 int res = (validate_ntype(tree, if_stmt)
1040 && (nch >= 4)
1041 && validate_name(CHILD(tree, 0), "if")
1042 && validate_test(CHILD(tree, 1))
1043 && validate_colon(CHILD(tree, 2))
1044 && validate_suite(CHILD(tree, 3)));
1046 if (res && ((nch % 4) == 3)) {
1047 /* ... 'else' ':' suite */
1048 res = (validate_name(CHILD(tree, nch - 3), "else")
1049 && validate_colon(CHILD(tree, nch - 2))
1050 && validate_suite(CHILD(tree, nch - 1)));
1051 nch -= 3;
1053 else if (!res && !PyErr_Occurred())
1054 (void) validate_numnodes(tree, 4, "if");
1055 if ((nch % 4) != 0)
1056 /* Will catch the case for nch < 4 */
1057 res = validate_numnodes(tree, 0, "if");
1058 else if (res && (nch > 4)) {
1059 /* ... ('elif' test ':' suite)+ ... */
1060 int j = 4;
1061 while ((j < nch) && res) {
1062 res = (validate_name(CHILD(tree, j), "elif")
1063 && validate_colon(CHILD(tree, j + 2))
1064 && validate_test(CHILD(tree, j + 1))
1065 && validate_suite(CHILD(tree, j + 3)));
1066 j += 4;
1069 return (res);
1073 /* parameters:
1074 * '(' [varargslist] ')'
1077 static int
1078 validate_parameters(node *tree)
1080 int nch = NCH(tree);
1081 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
1083 if (res) {
1084 res = (validate_lparen(CHILD(tree, 0))
1085 && validate_rparen(CHILD(tree, nch - 1)));
1086 if (res && (nch == 3))
1087 res = validate_varargslist(CHILD(tree, 1));
1089 else {
1090 (void) validate_numnodes(tree, 2, "parameters");
1092 return (res);
1096 /* validate_suite()
1098 * suite:
1099 * simple_stmt
1100 * | NEWLINE INDENT stmt+ DEDENT
1102 static int
1103 validate_suite(node *tree)
1105 int nch = NCH(tree);
1106 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
1108 if (res && (nch == 1))
1109 res = validate_simple_stmt(CHILD(tree, 0));
1110 else if (res) {
1111 /* NEWLINE INDENT stmt+ DEDENT */
1112 res = (validate_newline(CHILD(tree, 0))
1113 && validate_indent(CHILD(tree, 1))
1114 && validate_stmt(CHILD(tree, 2))
1115 && validate_dedent(CHILD(tree, nch - 1)));
1117 if (res && (nch > 4)) {
1118 int i = 3;
1119 --nch; /* forget the DEDENT */
1120 for ( ; res && (i < nch); ++i)
1121 res = validate_stmt(CHILD(tree, i));
1123 else if (nch < 4)
1124 res = validate_numnodes(tree, 4, "suite");
1126 return (res);
1130 static int
1131 validate_testlist(node *tree)
1133 return (validate_repeating_list(tree, testlist,
1134 validate_test, "testlist"));
1138 static int
1139 validate_testlist1(node *tree)
1141 return (validate_repeating_list(tree, testlist1,
1142 validate_test, "testlist1"));
1146 static int
1147 validate_testlist_safe(node *tree)
1149 return (validate_repeating_list(tree, testlist_safe,
1150 validate_old_test, "testlist_safe"));
1154 /* '*' NAME [',' '**' NAME] | '**' NAME
1156 static int
1157 validate_varargslist_trailer(node *tree, int start)
1159 int nch = NCH(tree);
1160 int res = 0;
1161 int sym;
1163 if (nch <= start) {
1164 err_string("expected variable argument trailer for varargslist");
1165 return 0;
1167 sym = TYPE(CHILD(tree, start));
1168 if (sym == STAR) {
1170 * ('*' NAME [',' '**' NAME]
1172 if (nch-start == 2)
1173 res = validate_name(CHILD(tree, start+1), NULL);
1174 else if (nch-start == 5)
1175 res = (validate_name(CHILD(tree, start+1), NULL)
1176 && validate_comma(CHILD(tree, start+2))
1177 && validate_doublestar(CHILD(tree, start+3))
1178 && validate_name(CHILD(tree, start+4), NULL));
1180 else if (sym == DOUBLESTAR) {
1182 * '**' NAME
1184 if (nch-start == 2)
1185 res = validate_name(CHILD(tree, start+1), NULL);
1187 if (!res)
1188 err_string("illegal variable argument trailer for varargslist");
1189 return res;
1193 /* validate_varargslist()
1195 * varargslist:
1196 * (fpdef ['=' test] ',')*
1197 * ('*' NAME [',' '**' NAME]
1198 * | '**' NAME)
1199 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1202 static int
1203 validate_varargslist(node *tree)
1205 int nch = NCH(tree);
1206 int res = validate_ntype(tree, varargslist) && (nch != 0);
1207 int sym;
1209 if (!res)
1210 return 0;
1211 if (nch < 1) {
1212 err_string("varargslist missing child nodes");
1213 return 0;
1215 sym = TYPE(CHILD(tree, 0));
1216 if (sym == STAR || sym == DOUBLESTAR)
1217 /* whole thing matches:
1218 * '*' NAME [',' '**' NAME] | '**' NAME
1220 res = validate_varargslist_trailer(tree, 0);
1221 else if (sym == fpdef) {
1222 int i = 0;
1224 sym = TYPE(CHILD(tree, nch-1));
1225 if (sym == NAME) {
1227 * (fpdef ['=' test] ',')+
1228 * ('*' NAME [',' '**' NAME]
1229 * | '**' NAME)
1231 /* skip over (fpdef ['=' test] ',')+ */
1232 while (res && (i+2 <= nch)) {
1233 res = validate_fpdef(CHILD(tree, i));
1234 ++i;
1235 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1236 res = (validate_equal(CHILD(tree, i))
1237 && validate_test(CHILD(tree, i+1)));
1238 if (res)
1239 i += 2;
1241 if (res && i < nch) {
1242 res = validate_comma(CHILD(tree, i));
1243 ++i;
1244 if (res && i < nch
1245 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1246 || TYPE(CHILD(tree, i)) == STAR))
1247 break;
1250 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1251 * i --^^^
1253 if (res)
1254 res = validate_varargslist_trailer(tree, i);
1256 else {
1258 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1260 /* strip trailing comma node */
1261 if (sym == COMMA) {
1262 res = validate_comma(CHILD(tree, nch-1));
1263 if (!res)
1264 return 0;
1265 --nch;
1268 * fpdef ['=' test] (',' fpdef ['=' test])*
1270 res = validate_fpdef(CHILD(tree, 0));
1271 ++i;
1272 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1273 res = (validate_equal(CHILD(tree, i))
1274 && validate_test(CHILD(tree, i+1)));
1275 i += 2;
1278 * ... (',' fpdef ['=' test])*
1279 * i ---^^^
1281 while (res && (nch - i) >= 2) {
1282 res = (validate_comma(CHILD(tree, i))
1283 && validate_fpdef(CHILD(tree, i+1)));
1284 i += 2;
1285 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1286 res = (validate_equal(CHILD(tree, i))
1287 && validate_test(CHILD(tree, i+1)));
1288 i += 2;
1291 if (res && nch - i != 0) {
1292 res = 0;
1293 err_string("illegal formation for varargslist");
1297 return res;
1301 /* list_iter: list_for | list_if
1303 static int
1304 validate_list_iter(node *tree)
1306 int res = (validate_ntype(tree, list_iter)
1307 && validate_numnodes(tree, 1, "list_iter"));
1308 if (res && TYPE(CHILD(tree, 0)) == list_for)
1309 res = validate_list_for(CHILD(tree, 0));
1310 else
1311 res = validate_list_if(CHILD(tree, 0));
1313 return res;
1316 /* gen_iter: gen_for | gen_if
1318 static int
1319 validate_gen_iter(node *tree)
1321 int res = (validate_ntype(tree, gen_iter)
1322 && validate_numnodes(tree, 1, "gen_iter"));
1323 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1324 res = validate_gen_for(CHILD(tree, 0));
1325 else
1326 res = validate_gen_if(CHILD(tree, 0));
1328 return res;
1331 /* list_for: 'for' exprlist 'in' testlist [list_iter]
1333 static int
1334 validate_list_for(node *tree)
1336 int nch = NCH(tree);
1337 int res;
1339 if (nch == 5)
1340 res = validate_list_iter(CHILD(tree, 4));
1341 else
1342 res = validate_numnodes(tree, 4, "list_for");
1344 if (res)
1345 res = (validate_name(CHILD(tree, 0), "for")
1346 && validate_exprlist(CHILD(tree, 1))
1347 && validate_name(CHILD(tree, 2), "in")
1348 && validate_testlist_safe(CHILD(tree, 3)));
1350 return res;
1353 /* gen_for: 'for' exprlist 'in' test [gen_iter]
1355 static int
1356 validate_gen_for(node *tree)
1358 int nch = NCH(tree);
1359 int res;
1361 if (nch == 5)
1362 res = validate_gen_iter(CHILD(tree, 4));
1363 else
1364 res = validate_numnodes(tree, 4, "gen_for");
1366 if (res)
1367 res = (validate_name(CHILD(tree, 0), "for")
1368 && validate_exprlist(CHILD(tree, 1))
1369 && validate_name(CHILD(tree, 2), "in")
1370 && validate_or_test(CHILD(tree, 3)));
1372 return res;
1375 /* list_if: 'if' old_test [list_iter]
1377 static int
1378 validate_list_if(node *tree)
1380 int nch = NCH(tree);
1381 int res;
1383 if (nch == 3)
1384 res = validate_list_iter(CHILD(tree, 2));
1385 else
1386 res = validate_numnodes(tree, 2, "list_if");
1388 if (res)
1389 res = (validate_name(CHILD(tree, 0), "if")
1390 && validate_old_test(CHILD(tree, 1)));
1392 return res;
1395 /* gen_if: 'if' old_test [gen_iter]
1397 static int
1398 validate_gen_if(node *tree)
1400 int nch = NCH(tree);
1401 int res;
1403 if (nch == 3)
1404 res = validate_gen_iter(CHILD(tree, 2));
1405 else
1406 res = validate_numnodes(tree, 2, "gen_if");
1408 if (res)
1409 res = (validate_name(CHILD(tree, 0), "if")
1410 && validate_old_test(CHILD(tree, 1)));
1412 return res;
1415 /* validate_fpdef()
1417 * fpdef:
1418 * NAME
1419 * | '(' fplist ')'
1421 static int
1422 validate_fpdef(node *tree)
1424 int nch = NCH(tree);
1425 int res = validate_ntype(tree, fpdef);
1427 if (res) {
1428 if (nch == 1)
1429 res = validate_ntype(CHILD(tree, 0), NAME);
1430 else if (nch == 3)
1431 res = (validate_lparen(CHILD(tree, 0))
1432 && validate_fplist(CHILD(tree, 1))
1433 && validate_rparen(CHILD(tree, 2)));
1434 else
1435 res = validate_numnodes(tree, 1, "fpdef");
1437 return (res);
1441 static int
1442 validate_fplist(node *tree)
1444 return (validate_repeating_list(tree, fplist,
1445 validate_fpdef, "fplist"));
1449 /* simple_stmt | compound_stmt
1452 static int
1453 validate_stmt(node *tree)
1455 int res = (validate_ntype(tree, stmt)
1456 && validate_numnodes(tree, 1, "stmt"));
1458 if (res) {
1459 tree = CHILD(tree, 0);
1461 if (TYPE(tree) == simple_stmt)
1462 res = validate_simple_stmt(tree);
1463 else
1464 res = validate_compound_stmt(tree);
1466 return (res);
1470 /* small_stmt (';' small_stmt)* [';'] NEWLINE
1473 static int
1474 validate_simple_stmt(node *tree)
1476 int nch = NCH(tree);
1477 int res = (validate_ntype(tree, simple_stmt)
1478 && (nch >= 2)
1479 && validate_small_stmt(CHILD(tree, 0))
1480 && validate_newline(CHILD(tree, nch - 1)));
1482 if (nch < 2)
1483 res = validate_numnodes(tree, 2, "simple_stmt");
1484 --nch; /* forget the NEWLINE */
1485 if (res && is_even(nch))
1486 res = validate_semi(CHILD(tree, --nch));
1487 if (res && (nch > 2)) {
1488 int i;
1490 for (i = 1; res && (i < nch); i += 2)
1491 res = (validate_semi(CHILD(tree, i))
1492 && validate_small_stmt(CHILD(tree, i + 1)));
1494 return (res);
1498 static int
1499 validate_small_stmt(node *tree)
1501 int nch = NCH(tree);
1502 int res = validate_numnodes(tree, 1, "small_stmt");
1504 if (res) {
1505 int ntype = TYPE(CHILD(tree, 0));
1507 if ( (ntype == expr_stmt)
1508 || (ntype == print_stmt)
1509 || (ntype == del_stmt)
1510 || (ntype == pass_stmt)
1511 || (ntype == flow_stmt)
1512 || (ntype == import_stmt)
1513 || (ntype == global_stmt)
1514 || (ntype == assert_stmt)
1515 || (ntype == exec_stmt))
1516 res = validate_node(CHILD(tree, 0));
1517 else {
1518 res = 0;
1519 err_string("illegal small_stmt child type");
1522 else if (nch == 1) {
1523 res = 0;
1524 PyErr_Format(parser_error,
1525 "Unrecognized child node of small_stmt: %d.",
1526 TYPE(CHILD(tree, 0)));
1528 return (res);
1532 /* compound_stmt:
1533 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated
1535 static int
1536 validate_compound_stmt(node *tree)
1538 int res = (validate_ntype(tree, compound_stmt)
1539 && validate_numnodes(tree, 1, "compound_stmt"));
1540 int ntype;
1542 if (!res)
1543 return (0);
1545 tree = CHILD(tree, 0);
1546 ntype = TYPE(tree);
1547 if ( (ntype == if_stmt)
1548 || (ntype == while_stmt)
1549 || (ntype == for_stmt)
1550 || (ntype == try_stmt)
1551 || (ntype == funcdef)
1552 || (ntype == classdef)
1553 || (ntype == decorated))
1554 res = validate_node(tree);
1555 else {
1556 res = 0;
1557 PyErr_Format(parser_error,
1558 "Illegal compound statement type: %d.", TYPE(tree));
1560 return (res);
1563 static int
1564 validate_yield_or_testlist(node *tree)
1566 if (TYPE(tree) == yield_expr)
1567 return validate_yield_expr(tree);
1568 else
1569 return validate_testlist(tree);
1572 static int
1573 validate_expr_stmt(node *tree)
1575 int j;
1576 int nch = NCH(tree);
1577 int res = (validate_ntype(tree, expr_stmt)
1578 && is_odd(nch)
1579 && validate_testlist(CHILD(tree, 0)));
1581 if (res && nch == 3
1582 && TYPE(CHILD(tree, 1)) == augassign) {
1583 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1584 && validate_yield_or_testlist(CHILD(tree, 2));
1586 if (res) {
1587 char *s = STR(CHILD(CHILD(tree, 1), 0));
1589 res = (strcmp(s, "+=") == 0
1590 || strcmp(s, "-=") == 0
1591 || strcmp(s, "*=") == 0
1592 || strcmp(s, "/=") == 0
1593 || strcmp(s, "//=") == 0
1594 || strcmp(s, "%=") == 0
1595 || strcmp(s, "&=") == 0
1596 || strcmp(s, "|=") == 0
1597 || strcmp(s, "^=") == 0
1598 || strcmp(s, "<<=") == 0
1599 || strcmp(s, ">>=") == 0
1600 || strcmp(s, "**=") == 0);
1601 if (!res)
1602 err_string("illegal augmmented assignment operator");
1605 else {
1606 for (j = 1; res && (j < nch); j += 2)
1607 res = validate_equal(CHILD(tree, j))
1608 && validate_yield_or_testlist(CHILD(tree, j + 1));
1610 return (res);
1614 /* print_stmt:
1616 * 'print' ( [ test (',' test)* [','] ]
1617 * | '>>' test [ (',' test)+ [','] ] )
1619 static int
1620 validate_print_stmt(node *tree)
1622 int nch = NCH(tree);
1623 int res = (validate_ntype(tree, print_stmt)
1624 && (nch > 0)
1625 && validate_name(CHILD(tree, 0), "print"));
1627 if (res && nch > 1) {
1628 int sym = TYPE(CHILD(tree, 1));
1629 int i = 1;
1630 int allow_trailing_comma = 1;
1632 if (sym == test)
1633 res = validate_test(CHILD(tree, i++));
1634 else {
1635 if (nch < 3)
1636 res = validate_numnodes(tree, 3, "print_stmt");
1637 else {
1638 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1639 && validate_test(CHILD(tree, i+1)));
1640 i += 2;
1641 allow_trailing_comma = 0;
1644 if (res) {
1645 /* ... (',' test)* [','] */
1646 while (res && i+2 <= nch) {
1647 res = (validate_comma(CHILD(tree, i))
1648 && validate_test(CHILD(tree, i+1)));
1649 allow_trailing_comma = 1;
1650 i += 2;
1652 if (res && !allow_trailing_comma)
1653 res = validate_numnodes(tree, i, "print_stmt");
1654 else if (res && i < nch)
1655 res = validate_comma(CHILD(tree, i));
1658 return (res);
1662 static int
1663 validate_del_stmt(node *tree)
1665 return (validate_numnodes(tree, 2, "del_stmt")
1666 && validate_name(CHILD(tree, 0), "del")
1667 && validate_exprlist(CHILD(tree, 1)));
1671 static int
1672 validate_return_stmt(node *tree)
1674 int nch = NCH(tree);
1675 int res = (validate_ntype(tree, return_stmt)
1676 && ((nch == 1) || (nch == 2))
1677 && validate_name(CHILD(tree, 0), "return"));
1679 if (res && (nch == 2))
1680 res = validate_testlist(CHILD(tree, 1));
1682 return (res);
1686 static int
1687 validate_raise_stmt(node *tree)
1689 int nch = NCH(tree);
1690 int res = (validate_ntype(tree, raise_stmt)
1691 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
1693 if (res) {
1694 res = validate_name(CHILD(tree, 0), "raise");
1695 if (res && (nch >= 2))
1696 res = validate_test(CHILD(tree, 1));
1697 if (res && nch > 2) {
1698 res = (validate_comma(CHILD(tree, 2))
1699 && validate_test(CHILD(tree, 3)));
1700 if (res && (nch > 4))
1701 res = (validate_comma(CHILD(tree, 4))
1702 && validate_test(CHILD(tree, 5)));
1705 else
1706 (void) validate_numnodes(tree, 2, "raise");
1707 if (res && (nch == 4))
1708 res = (validate_comma(CHILD(tree, 2))
1709 && validate_test(CHILD(tree, 3)));
1711 return (res);
1715 /* yield_expr: 'yield' [testlist]
1717 static int
1718 validate_yield_expr(node *tree)
1720 int nch = NCH(tree);
1721 int res = (validate_ntype(tree, yield_expr)
1722 && ((nch == 1) || (nch == 2))
1723 && validate_name(CHILD(tree, 0), "yield"));
1725 if (res && (nch == 2))
1726 res = validate_testlist(CHILD(tree, 1));
1728 return (res);
1732 /* yield_stmt: yield_expr
1734 static int
1735 validate_yield_stmt(node *tree)
1737 return (validate_ntype(tree, yield_stmt)
1738 && validate_numnodes(tree, 1, "yield_stmt")
1739 && validate_yield_expr(CHILD(tree, 0)));
1743 static int
1744 validate_import_as_name(node *tree)
1746 int nch = NCH(tree);
1747 int ok = validate_ntype(tree, import_as_name);
1749 if (ok) {
1750 if (nch == 1)
1751 ok = validate_name(CHILD(tree, 0), NULL);
1752 else if (nch == 3)
1753 ok = (validate_name(CHILD(tree, 0), NULL)
1754 && validate_name(CHILD(tree, 1), "as")
1755 && validate_name(CHILD(tree, 2), NULL));
1756 else
1757 ok = validate_numnodes(tree, 3, "import_as_name");
1759 return ok;
1763 /* dotted_name: NAME ("." NAME)*
1765 static int
1766 validate_dotted_name(node *tree)
1768 int nch = NCH(tree);
1769 int res = (validate_ntype(tree, dotted_name)
1770 && is_odd(nch)
1771 && validate_name(CHILD(tree, 0), NULL));
1772 int i;
1774 for (i = 1; res && (i < nch); i += 2) {
1775 res = (validate_dot(CHILD(tree, i))
1776 && validate_name(CHILD(tree, i+1), NULL));
1778 return res;
1782 /* dotted_as_name: dotted_name [NAME NAME]
1784 static int
1785 validate_dotted_as_name(node *tree)
1787 int nch = NCH(tree);
1788 int res = validate_ntype(tree, dotted_as_name);
1790 if (res) {
1791 if (nch == 1)
1792 res = validate_dotted_name(CHILD(tree, 0));
1793 else if (nch == 3)
1794 res = (validate_dotted_name(CHILD(tree, 0))
1795 && validate_name(CHILD(tree, 1), "as")
1796 && validate_name(CHILD(tree, 2), NULL));
1797 else {
1798 res = 0;
1799 err_string("illegal number of children for dotted_as_name");
1802 return res;
1806 /* dotted_as_name (',' dotted_as_name)* */
1807 static int
1808 validate_dotted_as_names(node *tree)
1810 int nch = NCH(tree);
1811 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1812 int i;
1814 for (i = 1; res && (i < nch); i += 2)
1815 res = (validate_comma(CHILD(tree, i))
1816 && validate_dotted_as_name(CHILD(tree, i + 1)));
1817 return (res);
1821 /* import_as_name (',' import_as_name)* [','] */
1822 static int
1823 validate_import_as_names(node *tree)
1825 int nch = NCH(tree);
1826 int res = validate_import_as_name(CHILD(tree, 0));
1827 int i;
1829 for (i = 1; res && (i + 1 < nch); i += 2)
1830 res = (validate_comma(CHILD(tree, i))
1831 && validate_import_as_name(CHILD(tree, i + 1)));
1832 return (res);
1836 /* 'import' dotted_as_names */
1837 static int
1838 validate_import_name(node *tree)
1840 return (validate_ntype(tree, import_name)
1841 && validate_numnodes(tree, 2, "import_name")
1842 && validate_name(CHILD(tree, 0), "import")
1843 && validate_dotted_as_names(CHILD(tree, 1)));
1846 /* Helper function to count the number of leading dots in
1847 * 'from ...module import name'
1849 static int
1850 count_from_dots(node *tree)
1852 int i;
1853 for (i = 0; i < NCH(tree); i++)
1854 if (TYPE(CHILD(tree, i)) != DOT)
1855 break;
1856 return i;
1859 /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
1860 * import_as_names
1862 static int
1863 validate_import_from(node *tree)
1865 int nch = NCH(tree);
1866 int ndots = count_from_dots(tree);
1867 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1868 int offset = ndots + havename;
1869 int res = validate_ntype(tree, import_from)
1870 && (nch >= 4 + ndots)
1871 && validate_name(CHILD(tree, 0), "from")
1872 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1873 && validate_name(CHILD(tree, offset + 1), "import");
1875 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1876 res = ((nch == offset + 5)
1877 && validate_lparen(CHILD(tree, offset + 2))
1878 && validate_import_as_names(CHILD(tree, offset + 3))
1879 && validate_rparen(CHILD(tree, offset + 4)));
1880 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1881 res = validate_import_as_names(CHILD(tree, offset + 2));
1882 return (res);
1886 /* import_stmt: import_name | import_from */
1887 static int
1888 validate_import_stmt(node *tree)
1890 int nch = NCH(tree);
1891 int res = validate_numnodes(tree, 1, "import_stmt");
1893 if (res) {
1894 int ntype = TYPE(CHILD(tree, 0));
1896 if (ntype == import_name || ntype == import_from)
1897 res = validate_node(CHILD(tree, 0));
1898 else {
1899 res = 0;
1900 err_string("illegal import_stmt child type");
1903 else if (nch == 1) {
1904 res = 0;
1905 PyErr_Format(parser_error,
1906 "Unrecognized child node of import_stmt: %d.",
1907 TYPE(CHILD(tree, 0)));
1909 return (res);
1915 static int
1916 validate_global_stmt(node *tree)
1918 int j;
1919 int nch = NCH(tree);
1920 int res = (validate_ntype(tree, global_stmt)
1921 && is_even(nch) && (nch >= 2));
1923 if (!res && !PyErr_Occurred())
1924 err_string("illegal global statement");
1926 if (res)
1927 res = (validate_name(CHILD(tree, 0), "global")
1928 && validate_ntype(CHILD(tree, 1), NAME));
1929 for (j = 2; res && (j < nch); j += 2)
1930 res = (validate_comma(CHILD(tree, j))
1931 && validate_ntype(CHILD(tree, j + 1), NAME));
1933 return (res);
1937 /* exec_stmt:
1939 * 'exec' expr ['in' test [',' test]]
1941 static int
1942 validate_exec_stmt(node *tree)
1944 int nch = NCH(tree);
1945 int res = (validate_ntype(tree, exec_stmt)
1946 && ((nch == 2) || (nch == 4) || (nch == 6))
1947 && validate_name(CHILD(tree, 0), "exec")
1948 && validate_expr(CHILD(tree, 1)));
1950 if (!res && !PyErr_Occurred())
1951 err_string("illegal exec statement");
1952 if (res && (nch > 2))
1953 res = (validate_name(CHILD(tree, 2), "in")
1954 && validate_test(CHILD(tree, 3)));
1955 if (res && (nch == 6))
1956 res = (validate_comma(CHILD(tree, 4))
1957 && validate_test(CHILD(tree, 5)));
1959 return (res);
1963 /* assert_stmt:
1965 * 'assert' test [',' test]
1967 static int
1968 validate_assert_stmt(node *tree)
1970 int nch = NCH(tree);
1971 int res = (validate_ntype(tree, assert_stmt)
1972 && ((nch == 2) || (nch == 4))
1973 && (validate_name(CHILD(tree, 0), "assert"))
1974 && validate_test(CHILD(tree, 1)));
1976 if (!res && !PyErr_Occurred())
1977 err_string("illegal assert statement");
1978 if (res && (nch > 2))
1979 res = (validate_comma(CHILD(tree, 2))
1980 && validate_test(CHILD(tree, 3)));
1982 return (res);
1986 static int
1987 validate_while(node *tree)
1989 int nch = NCH(tree);
1990 int res = (validate_ntype(tree, while_stmt)
1991 && ((nch == 4) || (nch == 7))
1992 && validate_name(CHILD(tree, 0), "while")
1993 && validate_test(CHILD(tree, 1))
1994 && validate_colon(CHILD(tree, 2))
1995 && validate_suite(CHILD(tree, 3)));
1997 if (res && (nch == 7))
1998 res = (validate_name(CHILD(tree, 4), "else")
1999 && validate_colon(CHILD(tree, 5))
2000 && validate_suite(CHILD(tree, 6)));
2002 return (res);
2006 static int
2007 validate_for(node *tree)
2009 int nch = NCH(tree);
2010 int res = (validate_ntype(tree, for_stmt)
2011 && ((nch == 6) || (nch == 9))
2012 && validate_name(CHILD(tree, 0), "for")
2013 && validate_exprlist(CHILD(tree, 1))
2014 && validate_name(CHILD(tree, 2), "in")
2015 && validate_testlist(CHILD(tree, 3))
2016 && validate_colon(CHILD(tree, 4))
2017 && validate_suite(CHILD(tree, 5)));
2019 if (res && (nch == 9))
2020 res = (validate_name(CHILD(tree, 6), "else")
2021 && validate_colon(CHILD(tree, 7))
2022 && validate_suite(CHILD(tree, 8)));
2024 return (res);
2028 /* try_stmt:
2029 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2030 * | 'try' ':' suite 'finally' ':' suite
2033 static int
2034 validate_try(node *tree)
2036 int nch = NCH(tree);
2037 int pos = 3;
2038 int res = (validate_ntype(tree, try_stmt)
2039 && (nch >= 6) && ((nch % 3) == 0));
2041 if (res)
2042 res = (validate_name(CHILD(tree, 0), "try")
2043 && validate_colon(CHILD(tree, 1))
2044 && validate_suite(CHILD(tree, 2))
2045 && validate_colon(CHILD(tree, nch - 2))
2046 && validate_suite(CHILD(tree, nch - 1)));
2047 else if (!PyErr_Occurred()) {
2048 const char* name = "except";
2049 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2050 name = STR(CHILD(tree, nch - 3));
2052 PyErr_Format(parser_error,
2053 "Illegal number of children for try/%s node.", name);
2055 /* Skip past except_clause sections: */
2056 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
2057 res = (validate_except_clause(CHILD(tree, pos))
2058 && validate_colon(CHILD(tree, pos + 1))
2059 && validate_suite(CHILD(tree, pos + 2)));
2060 pos += 3;
2062 if (res && (pos < nch)) {
2063 res = validate_ntype(CHILD(tree, pos), NAME);
2064 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
2065 res = (validate_numnodes(tree, 6, "try/finally")
2066 && validate_colon(CHILD(tree, 4))
2067 && validate_suite(CHILD(tree, 5)));
2068 else if (res) {
2069 if (nch == (pos + 3)) {
2070 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
2071 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
2072 if (!res)
2073 err_string("illegal trailing triple in try statement");
2075 else if (nch == (pos + 6)) {
2076 res = (validate_name(CHILD(tree, pos), "except")
2077 && validate_colon(CHILD(tree, pos + 1))
2078 && validate_suite(CHILD(tree, pos + 2))
2079 && validate_name(CHILD(tree, pos + 3), "else"));
2081 else
2082 res = validate_numnodes(tree, pos + 3, "try/except");
2085 return (res);
2089 static int
2090 validate_except_clause(node *tree)
2092 int nch = NCH(tree);
2093 int res = (validate_ntype(tree, except_clause)
2094 && ((nch == 1) || (nch == 2) || (nch == 4))
2095 && validate_name(CHILD(tree, 0), "except"));
2097 if (res && (nch > 1))
2098 res = validate_test(CHILD(tree, 1));
2099 if (res && (nch == 4))
2100 res = (validate_comma(CHILD(tree, 2))
2101 && validate_test(CHILD(tree, 3)));
2103 return (res);
2107 static int
2108 validate_test(node *tree)
2110 int nch = NCH(tree);
2111 int res = validate_ntype(tree, test) && is_odd(nch);
2113 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
2114 res = ((nch == 1)
2115 && validate_lambdef(CHILD(tree, 0)));
2116 else if (res) {
2117 res = validate_or_test(CHILD(tree, 0));
2118 res = (res && (nch == 1 || (nch == 5 &&
2119 validate_name(CHILD(tree, 1), "if") &&
2120 validate_or_test(CHILD(tree, 2)) &&
2121 validate_name(CHILD(tree, 3), "else") &&
2122 validate_test(CHILD(tree, 4)))));
2124 return (res);
2127 static int
2128 validate_old_test(node *tree)
2130 int nch = NCH(tree);
2131 int res = validate_ntype(tree, old_test) && (nch == 1);
2133 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2134 res = (validate_old_lambdef(CHILD(tree, 0)));
2135 else if (res) {
2136 res = (validate_or_test(CHILD(tree, 0)));
2138 return (res);
2141 static int
2142 validate_or_test(node *tree)
2144 int nch = NCH(tree);
2145 int res = validate_ntype(tree, or_test) && is_odd(nch);
2147 if (res) {
2148 int pos;
2149 res = validate_and_test(CHILD(tree, 0));
2150 for (pos = 1; res && (pos < nch); pos += 2)
2151 res = (validate_name(CHILD(tree, pos), "or")
2152 && validate_and_test(CHILD(tree, pos + 1)));
2154 return (res);
2158 static int
2159 validate_and_test(node *tree)
2161 int pos;
2162 int nch = NCH(tree);
2163 int res = (validate_ntype(tree, and_test)
2164 && is_odd(nch)
2165 && validate_not_test(CHILD(tree, 0)));
2167 for (pos = 1; res && (pos < nch); pos += 2)
2168 res = (validate_name(CHILD(tree, pos), "and")
2169 && validate_not_test(CHILD(tree, 0)));
2171 return (res);
2175 static int
2176 validate_not_test(node *tree)
2178 int nch = NCH(tree);
2179 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
2181 if (res) {
2182 if (nch == 2)
2183 res = (validate_name(CHILD(tree, 0), "not")
2184 && validate_not_test(CHILD(tree, 1)));
2185 else if (nch == 1)
2186 res = validate_comparison(CHILD(tree, 0));
2188 return (res);
2192 static int
2193 validate_comparison(node *tree)
2195 int pos;
2196 int nch = NCH(tree);
2197 int res = (validate_ntype(tree, comparison)
2198 && is_odd(nch)
2199 && validate_expr(CHILD(tree, 0)));
2201 for (pos = 1; res && (pos < nch); pos += 2)
2202 res = (validate_comp_op(CHILD(tree, pos))
2203 && validate_expr(CHILD(tree, pos + 1)));
2205 return (res);
2209 static int
2210 validate_comp_op(node *tree)
2212 int res = 0;
2213 int nch = NCH(tree);
2215 if (!validate_ntype(tree, comp_op))
2216 return (0);
2217 if (nch == 1) {
2219 * Only child will be a terminal with a well-defined symbolic name
2220 * or a NAME with a string of either 'is' or 'in'
2222 tree = CHILD(tree, 0);
2223 switch (TYPE(tree)) {
2224 case LESS:
2225 case GREATER:
2226 case EQEQUAL:
2227 case EQUAL:
2228 case LESSEQUAL:
2229 case GREATEREQUAL:
2230 case NOTEQUAL:
2231 res = 1;
2232 break;
2233 case NAME:
2234 res = ((strcmp(STR(tree), "in") == 0)
2235 || (strcmp(STR(tree), "is") == 0));
2236 if (!res) {
2237 PyErr_Format(parser_error,
2238 "illegal operator '%s'", STR(tree));
2240 break;
2241 default:
2242 err_string("illegal comparison operator type");
2243 break;
2246 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
2247 res = (validate_ntype(CHILD(tree, 0), NAME)
2248 && validate_ntype(CHILD(tree, 1), NAME)
2249 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2250 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2251 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2252 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2253 if (!res && !PyErr_Occurred())
2254 err_string("unknown comparison operator");
2256 return (res);
2260 static int
2261 validate_expr(node *tree)
2263 int j;
2264 int nch = NCH(tree);
2265 int res = (validate_ntype(tree, expr)
2266 && is_odd(nch)
2267 && validate_xor_expr(CHILD(tree, 0)));
2269 for (j = 2; res && (j < nch); j += 2)
2270 res = (validate_xor_expr(CHILD(tree, j))
2271 && validate_vbar(CHILD(tree, j - 1)));
2273 return (res);
2277 static int
2278 validate_xor_expr(node *tree)
2280 int j;
2281 int nch = NCH(tree);
2282 int res = (validate_ntype(tree, xor_expr)
2283 && is_odd(nch)
2284 && validate_and_expr(CHILD(tree, 0)));
2286 for (j = 2; res && (j < nch); j += 2)
2287 res = (validate_circumflex(CHILD(tree, j - 1))
2288 && validate_and_expr(CHILD(tree, j)));
2290 return (res);
2294 static int
2295 validate_and_expr(node *tree)
2297 int pos;
2298 int nch = NCH(tree);
2299 int res = (validate_ntype(tree, and_expr)
2300 && is_odd(nch)
2301 && validate_shift_expr(CHILD(tree, 0)));
2303 for (pos = 1; res && (pos < nch); pos += 2)
2304 res = (validate_ampersand(CHILD(tree, pos))
2305 && validate_shift_expr(CHILD(tree, pos + 1)));
2307 return (res);
2311 static int
2312 validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
2314 int pos = 1;
2315 int nch = NCH(tree);
2316 int res = (is_odd(nch)
2317 && (*termvalid)(CHILD(tree, 0)));
2319 for ( ; res && (pos < nch); pos += 2) {
2320 if (TYPE(CHILD(tree, pos)) != op1)
2321 res = validate_ntype(CHILD(tree, pos), op2);
2322 if (res)
2323 res = (*termvalid)(CHILD(tree, pos + 1));
2325 return (res);
2329 static int
2330 validate_shift_expr(node *tree)
2332 return (validate_ntype(tree, shift_expr)
2333 && validate_chain_two_ops(tree, validate_arith_expr,
2334 LEFTSHIFT, RIGHTSHIFT));
2338 static int
2339 validate_arith_expr(node *tree)
2341 return (validate_ntype(tree, arith_expr)
2342 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2346 static int
2347 validate_term(node *tree)
2349 int pos = 1;
2350 int nch = NCH(tree);
2351 int res = (validate_ntype(tree, term)
2352 && is_odd(nch)
2353 && validate_factor(CHILD(tree, 0)));
2355 for ( ; res && (pos < nch); pos += 2)
2356 res = (((TYPE(CHILD(tree, pos)) == STAR)
2357 || (TYPE(CHILD(tree, pos)) == SLASH)
2358 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
2359 || (TYPE(CHILD(tree, pos)) == PERCENT))
2360 && validate_factor(CHILD(tree, pos + 1)));
2362 return (res);
2366 /* factor:
2368 * factor: ('+'|'-'|'~') factor | power
2370 static int
2371 validate_factor(node *tree)
2373 int nch = NCH(tree);
2374 int res = (validate_ntype(tree, factor)
2375 && (((nch == 2)
2376 && ((TYPE(CHILD(tree, 0)) == PLUS)
2377 || (TYPE(CHILD(tree, 0)) == MINUS)
2378 || (TYPE(CHILD(tree, 0)) == TILDE))
2379 && validate_factor(CHILD(tree, 1)))
2380 || ((nch == 1)
2381 && validate_power(CHILD(tree, 0)))));
2382 return (res);
2386 /* power:
2388 * power: atom trailer* ('**' factor)*
2390 static int
2391 validate_power(node *tree)
2393 int pos = 1;
2394 int nch = NCH(tree);
2395 int res = (validate_ntype(tree, power) && (nch >= 1)
2396 && validate_atom(CHILD(tree, 0)));
2398 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
2399 res = validate_trailer(CHILD(tree, pos++));
2400 if (res && (pos < nch)) {
2401 if (!is_even(nch - pos)) {
2402 err_string("illegal number of nodes for 'power'");
2403 return (0);
2405 for ( ; res && (pos < (nch - 1)); pos += 2)
2406 res = (validate_doublestar(CHILD(tree, pos))
2407 && validate_factor(CHILD(tree, pos + 1)));
2409 return (res);
2413 static int
2414 validate_atom(node *tree)
2416 int pos;
2417 int nch = NCH(tree);
2418 int res = validate_ntype(tree, atom);
2420 if (res && nch < 1)
2421 res = validate_numnodes(tree, nch+1, "atom");
2422 if (res) {
2423 switch (TYPE(CHILD(tree, 0))) {
2424 case LPAR:
2425 res = ((nch <= 3)
2426 && (validate_rparen(CHILD(tree, nch - 1))));
2428 if (res && (nch == 3)) {
2429 if (TYPE(CHILD(tree, 1))==yield_expr)
2430 res = validate_yield_expr(CHILD(tree, 1));
2431 else
2432 res = validate_testlist_gexp(CHILD(tree, 1));
2434 break;
2435 case LSQB:
2436 if (nch == 2)
2437 res = validate_ntype(CHILD(tree, 1), RSQB);
2438 else if (nch == 3)
2439 res = (validate_listmaker(CHILD(tree, 1))
2440 && validate_ntype(CHILD(tree, 2), RSQB));
2441 else {
2442 res = 0;
2443 err_string("illegal list display atom");
2445 break;
2446 case LBRACE:
2447 res = ((nch <= 3)
2448 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
2450 if (res && (nch == 3))
2451 res = validate_dictmaker(CHILD(tree, 1));
2452 break;
2453 case BACKQUOTE:
2454 res = ((nch == 3)
2455 && validate_testlist1(CHILD(tree, 1))
2456 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2457 break;
2458 case NAME:
2459 case NUMBER:
2460 res = (nch == 1);
2461 break;
2462 case STRING:
2463 for (pos = 1; res && (pos < nch); ++pos)
2464 res = validate_ntype(CHILD(tree, pos), STRING);
2465 break;
2466 default:
2467 res = 0;
2468 break;
2471 return (res);
2475 /* listmaker:
2476 * test ( list_for | (',' test)* [','] )
2478 static int
2479 validate_listmaker(node *tree)
2481 int nch = NCH(tree);
2482 int ok = nch;
2484 if (nch == 0)
2485 err_string("missing child nodes of listmaker");
2486 else
2487 ok = validate_test(CHILD(tree, 0));
2490 * list_for | (',' test)* [',']
2492 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2493 ok = validate_list_for(CHILD(tree, 1));
2494 else {
2495 /* (',' test)* [','] */
2496 int i = 1;
2497 while (ok && nch - i >= 2) {
2498 ok = (validate_comma(CHILD(tree, i))
2499 && validate_test(CHILD(tree, i+1)));
2500 i += 2;
2502 if (ok && i == nch-1)
2503 ok = validate_comma(CHILD(tree, i));
2504 else if (i != nch) {
2505 ok = 0;
2506 err_string("illegal trailing nodes for listmaker");
2509 return ok;
2512 /* testlist_gexp:
2513 * test ( gen_for | (',' test)* [','] )
2515 static int
2516 validate_testlist_gexp(node *tree)
2518 int nch = NCH(tree);
2519 int ok = nch;
2521 if (nch == 0)
2522 err_string("missing child nodes of testlist_gexp");
2523 else {
2524 ok = validate_test(CHILD(tree, 0));
2528 * gen_for | (',' test)* [',']
2530 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2531 ok = validate_gen_for(CHILD(tree, 1));
2532 else {
2533 /* (',' test)* [','] */
2534 int i = 1;
2535 while (ok && nch - i >= 2) {
2536 ok = (validate_comma(CHILD(tree, i))
2537 && validate_test(CHILD(tree, i+1)));
2538 i += 2;
2540 if (ok && i == nch-1)
2541 ok = validate_comma(CHILD(tree, i));
2542 else if (i != nch) {
2543 ok = 0;
2544 err_string("illegal trailing nodes for testlist_gexp");
2547 return ok;
2550 /* decorator:
2551 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2553 static int
2554 validate_decorator(node *tree)
2556 int ok;
2557 int nch = NCH(tree);
2558 ok = (validate_ntype(tree, decorator) &&
2559 (nch == 3 || nch == 5 || nch == 6) &&
2560 validate_at(CHILD(tree, 0)) &&
2561 validate_dotted_name(CHILD(tree, 1)) &&
2562 validate_newline(RCHILD(tree, -1)));
2564 if (ok && nch != 3) {
2565 ok = (validate_lparen(CHILD(tree, 2)) &&
2566 validate_rparen(RCHILD(tree, -2)));
2568 if (ok && nch == 6)
2569 ok = validate_arglist(CHILD(tree, 3));
2572 return ok;
2575 /* decorators:
2576 * decorator+
2578 static int
2579 validate_decorators(node *tree)
2581 int i, nch, ok;
2582 nch = NCH(tree);
2583 ok = validate_ntype(tree, decorators) && nch >= 1;
2585 for (i = 0; ok && i < nch; ++i)
2586 ok = validate_decorator(CHILD(tree, i));
2588 return ok;
2591 /* funcdef:
2593 * -5 -4 -3 -2 -1
2594 * 'def' NAME parameters ':' suite
2596 static int
2597 validate_funcdef(node *tree)
2599 int nch = NCH(tree);
2600 int ok = (validate_ntype(tree, funcdef)
2601 && (nch == 5)
2602 && validate_name(RCHILD(tree, -5), "def")
2603 && validate_ntype(RCHILD(tree, -4), NAME)
2604 && validate_colon(RCHILD(tree, -2))
2605 && validate_parameters(RCHILD(tree, -3))
2606 && validate_suite(RCHILD(tree, -1)));
2607 return ok;
2611 /* decorated
2612 * decorators (classdef | funcdef)
2614 static int
2615 validate_decorated(node *tree)
2617 int nch = NCH(tree);
2618 int ok = (validate_ntype(tree, decorated)
2619 && (nch == 2)
2620 && validate_decorators(RCHILD(tree, -2))
2621 && (validate_funcdef(RCHILD(tree, -1))
2622 || validate_class(RCHILD(tree, -1)))
2624 return ok;
2627 static int
2628 validate_lambdef(node *tree)
2630 int nch = NCH(tree);
2631 int res = (validate_ntype(tree, lambdef)
2632 && ((nch == 3) || (nch == 4))
2633 && validate_name(CHILD(tree, 0), "lambda")
2634 && validate_colon(CHILD(tree, nch - 2))
2635 && validate_test(CHILD(tree, nch - 1)));
2637 if (res && (nch == 4))
2638 res = validate_varargslist(CHILD(tree, 1));
2639 else if (!res && !PyErr_Occurred())
2640 (void) validate_numnodes(tree, 3, "lambdef");
2642 return (res);
2646 static int
2647 validate_old_lambdef(node *tree)
2649 int nch = NCH(tree);
2650 int res = (validate_ntype(tree, old_lambdef)
2651 && ((nch == 3) || (nch == 4))
2652 && validate_name(CHILD(tree, 0), "lambda")
2653 && validate_colon(CHILD(tree, nch - 2))
2654 && validate_test(CHILD(tree, nch - 1)));
2656 if (res && (nch == 4))
2657 res = validate_varargslist(CHILD(tree, 1));
2658 else if (!res && !PyErr_Occurred())
2659 (void) validate_numnodes(tree, 3, "old_lambdef");
2661 return (res);
2665 /* arglist:
2667 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2669 static int
2670 validate_arglist(node *tree)
2672 int nch = NCH(tree);
2673 int i = 0;
2674 int ok = 1;
2676 if (nch <= 0)
2677 /* raise the right error from having an invalid number of children */
2678 return validate_numnodes(tree, nch + 1, "arglist");
2680 if (nch > 1) {
2681 for (i=0; i<nch; i++) {
2682 if (TYPE(CHILD(tree, i)) == argument) {
2683 node *ch = CHILD(tree, i);
2684 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2685 err_string("need '(', ')' for generator expression");
2686 return 0;
2692 while (ok && nch-i >= 2) {
2693 /* skip leading (argument ',') */
2694 ok = (validate_argument(CHILD(tree, i))
2695 && validate_comma(CHILD(tree, i+1)));
2696 if (ok)
2697 i += 2;
2698 else
2699 PyErr_Clear();
2701 ok = 1;
2702 if (nch-i > 0) {
2704 * argument | '*' test [',' '**' test] | '**' test
2706 int sym = TYPE(CHILD(tree, i));
2708 if (sym == argument) {
2709 ok = validate_argument(CHILD(tree, i));
2710 if (ok && i+1 != nch) {
2711 err_string("illegal arglist specification"
2712 " (extra stuff on end)");
2713 ok = 0;
2716 else if (sym == STAR) {
2717 ok = validate_star(CHILD(tree, i));
2718 if (ok && (nch-i == 2))
2719 ok = validate_test(CHILD(tree, i+1));
2720 else if (ok && (nch-i == 5))
2721 ok = (validate_test(CHILD(tree, i+1))
2722 && validate_comma(CHILD(tree, i+2))
2723 && validate_doublestar(CHILD(tree, i+3))
2724 && validate_test(CHILD(tree, i+4)));
2725 else {
2726 err_string("illegal use of '*' in arglist");
2727 ok = 0;
2730 else if (sym == DOUBLESTAR) {
2731 if (nch-i == 2)
2732 ok = (validate_doublestar(CHILD(tree, i))
2733 && validate_test(CHILD(tree, i+1)));
2734 else {
2735 err_string("illegal use of '**' in arglist");
2736 ok = 0;
2739 else {
2740 err_string("illegal arglist specification");
2741 ok = 0;
2744 return (ok);
2749 /* argument:
2751 * [test '='] test [gen_for]
2753 static int
2754 validate_argument(node *tree)
2756 int nch = NCH(tree);
2757 int res = (validate_ntype(tree, argument)
2758 && ((nch == 1) || (nch == 2) || (nch == 3))
2759 && validate_test(CHILD(tree, 0)));
2761 if (res && (nch == 2))
2762 res = validate_gen_for(CHILD(tree, 1));
2763 else if (res && (nch == 3))
2764 res = (validate_equal(CHILD(tree, 1))
2765 && validate_test(CHILD(tree, 2)));
2767 return (res);
2772 /* trailer:
2774 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2776 static int
2777 validate_trailer(node *tree)
2779 int nch = NCH(tree);
2780 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
2782 if (res) {
2783 switch (TYPE(CHILD(tree, 0))) {
2784 case LPAR:
2785 res = validate_rparen(CHILD(tree, nch - 1));
2786 if (res && (nch == 3))
2787 res = validate_arglist(CHILD(tree, 1));
2788 break;
2789 case LSQB:
2790 res = (validate_numnodes(tree, 3, "trailer")
2791 && validate_subscriptlist(CHILD(tree, 1))
2792 && validate_ntype(CHILD(tree, 2), RSQB));
2793 break;
2794 case DOT:
2795 res = (validate_numnodes(tree, 2, "trailer")
2796 && validate_ntype(CHILD(tree, 1), NAME));
2797 break;
2798 default:
2799 res = 0;
2800 break;
2803 else {
2804 (void) validate_numnodes(tree, 2, "trailer");
2806 return (res);
2810 /* subscriptlist:
2812 * subscript (',' subscript)* [',']
2814 static int
2815 validate_subscriptlist(node *tree)
2817 return (validate_repeating_list(tree, subscriptlist,
2818 validate_subscript, "subscriptlist"));
2822 /* subscript:
2824 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
2826 static int
2827 validate_subscript(node *tree)
2829 int offset = 0;
2830 int nch = NCH(tree);
2831 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
2833 if (!res) {
2834 if (!PyErr_Occurred())
2835 err_string("invalid number of arguments for subscript node");
2836 return (0);
2838 if (TYPE(CHILD(tree, 0)) == DOT)
2839 /* take care of ('.' '.' '.') possibility */
2840 return (validate_numnodes(tree, 3, "subscript")
2841 && validate_dot(CHILD(tree, 0))
2842 && validate_dot(CHILD(tree, 1))
2843 && validate_dot(CHILD(tree, 2)));
2844 if (nch == 1) {
2845 if (TYPE(CHILD(tree, 0)) == test)
2846 res = validate_test(CHILD(tree, 0));
2847 else
2848 res = validate_colon(CHILD(tree, 0));
2849 return (res);
2851 /* Must be [test] ':' [test] [sliceop],
2852 * but at least one of the optional components will
2853 * be present, but we don't know which yet.
2855 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
2856 res = validate_test(CHILD(tree, 0));
2857 offset = 1;
2859 if (res)
2860 res = validate_colon(CHILD(tree, offset));
2861 if (res) {
2862 int rem = nch - ++offset;
2863 if (rem) {
2864 if (TYPE(CHILD(tree, offset)) == test) {
2865 res = validate_test(CHILD(tree, offset));
2866 ++offset;
2867 --rem;
2869 if (res && rem)
2870 res = validate_sliceop(CHILD(tree, offset));
2873 return (res);
2877 static int
2878 validate_sliceop(node *tree)
2880 int nch = NCH(tree);
2881 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
2882 && validate_ntype(tree, sliceop);
2883 if (!res && !PyErr_Occurred()) {
2884 res = validate_numnodes(tree, 1, "sliceop");
2886 if (res)
2887 res = validate_colon(CHILD(tree, 0));
2888 if (res && (nch == 2))
2889 res = validate_test(CHILD(tree, 1));
2891 return (res);
2895 static int
2896 validate_exprlist(node *tree)
2898 return (validate_repeating_list(tree, exprlist,
2899 validate_expr, "exprlist"));
2903 static int
2904 validate_dictmaker(node *tree)
2906 int nch = NCH(tree);
2907 int res = (validate_ntype(tree, dictmaker)
2908 && (nch >= 3)
2909 && validate_test(CHILD(tree, 0))
2910 && validate_colon(CHILD(tree, 1))
2911 && validate_test(CHILD(tree, 2)));
2913 if (res && ((nch % 4) == 0))
2914 res = validate_comma(CHILD(tree, --nch));
2915 else if (res)
2916 res = ((nch % 4) == 3);
2918 if (res && (nch > 3)) {
2919 int pos = 3;
2920 /* ( ',' test ':' test )* */
2921 while (res && (pos < nch)) {
2922 res = (validate_comma(CHILD(tree, pos))
2923 && validate_test(CHILD(tree, pos + 1))
2924 && validate_colon(CHILD(tree, pos + 2))
2925 && validate_test(CHILD(tree, pos + 3)));
2926 pos += 4;
2929 return (res);
2933 static int
2934 validate_eval_input(node *tree)
2936 int pos;
2937 int nch = NCH(tree);
2938 int res = (validate_ntype(tree, eval_input)
2939 && (nch >= 2)
2940 && validate_testlist(CHILD(tree, 0))
2941 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
2943 for (pos = 1; res && (pos < (nch - 1)); ++pos)
2944 res = validate_ntype(CHILD(tree, pos), NEWLINE);
2946 return (res);
2950 static int
2951 validate_node(node *tree)
2953 int nch = 0; /* num. children on current node */
2954 int res = 1; /* result value */
2955 node* next = 0; /* node to process after this one */
2957 while (res && (tree != 0)) {
2958 nch = NCH(tree);
2959 next = 0;
2960 switch (TYPE(tree)) {
2962 * Definition nodes.
2964 case funcdef:
2965 res = validate_funcdef(tree);
2966 break;
2967 case classdef:
2968 res = validate_class(tree);
2969 break;
2970 case decorated:
2971 res = validate_decorated(tree);
2972 break;
2974 * "Trivial" parse tree nodes.
2975 * (Why did I call these trivial?)
2977 case stmt:
2978 res = validate_stmt(tree);
2979 break;
2980 case small_stmt:
2982 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2983 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2985 res = validate_small_stmt(tree);
2986 break;
2987 case flow_stmt:
2988 res = (validate_numnodes(tree, 1, "flow_stmt")
2989 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2990 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2991 || (TYPE(CHILD(tree, 0)) == yield_stmt)
2992 || (TYPE(CHILD(tree, 0)) == return_stmt)
2993 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2994 if (res)
2995 next = CHILD(tree, 0);
2996 else if (nch == 1)
2997 err_string("illegal flow_stmt type");
2998 break;
2999 case yield_stmt:
3000 res = validate_yield_stmt(tree);
3001 break;
3003 * Compound statements.
3005 case simple_stmt:
3006 res = validate_simple_stmt(tree);
3007 break;
3008 case compound_stmt:
3009 res = validate_compound_stmt(tree);
3010 break;
3012 * Fundamental statements.
3014 case expr_stmt:
3015 res = validate_expr_stmt(tree);
3016 break;
3017 case print_stmt:
3018 res = validate_print_stmt(tree);
3019 break;
3020 case del_stmt:
3021 res = validate_del_stmt(tree);
3022 break;
3023 case pass_stmt:
3024 res = (validate_numnodes(tree, 1, "pass")
3025 && validate_name(CHILD(tree, 0), "pass"));
3026 break;
3027 case break_stmt:
3028 res = (validate_numnodes(tree, 1, "break")
3029 && validate_name(CHILD(tree, 0), "break"));
3030 break;
3031 case continue_stmt:
3032 res = (validate_numnodes(tree, 1, "continue")
3033 && validate_name(CHILD(tree, 0), "continue"));
3034 break;
3035 case return_stmt:
3036 res = validate_return_stmt(tree);
3037 break;
3038 case raise_stmt:
3039 res = validate_raise_stmt(tree);
3040 break;
3041 case import_stmt:
3042 res = validate_import_stmt(tree);
3043 break;
3044 case import_name:
3045 res = validate_import_name(tree);
3046 break;
3047 case import_from:
3048 res = validate_import_from(tree);
3049 break;
3050 case global_stmt:
3051 res = validate_global_stmt(tree);
3052 break;
3053 case exec_stmt:
3054 res = validate_exec_stmt(tree);
3055 break;
3056 case assert_stmt:
3057 res = validate_assert_stmt(tree);
3058 break;
3059 case if_stmt:
3060 res = validate_if(tree);
3061 break;
3062 case while_stmt:
3063 res = validate_while(tree);
3064 break;
3065 case for_stmt:
3066 res = validate_for(tree);
3067 break;
3068 case try_stmt:
3069 res = validate_try(tree);
3070 break;
3071 case suite:
3072 res = validate_suite(tree);
3073 break;
3075 * Expression nodes.
3077 case testlist:
3078 res = validate_testlist(tree);
3079 break;
3080 case yield_expr:
3081 res = validate_yield_expr(tree);
3082 break;
3083 case testlist1:
3084 res = validate_testlist1(tree);
3085 break;
3086 case test:
3087 res = validate_test(tree);
3088 break;
3089 case and_test:
3090 res = validate_and_test(tree);
3091 break;
3092 case not_test:
3093 res = validate_not_test(tree);
3094 break;
3095 case comparison:
3096 res = validate_comparison(tree);
3097 break;
3098 case exprlist:
3099 res = validate_exprlist(tree);
3100 break;
3101 case comp_op:
3102 res = validate_comp_op(tree);
3103 break;
3104 case expr:
3105 res = validate_expr(tree);
3106 break;
3107 case xor_expr:
3108 res = validate_xor_expr(tree);
3109 break;
3110 case and_expr:
3111 res = validate_and_expr(tree);
3112 break;
3113 case shift_expr:
3114 res = validate_shift_expr(tree);
3115 break;
3116 case arith_expr:
3117 res = validate_arith_expr(tree);
3118 break;
3119 case term:
3120 res = validate_term(tree);
3121 break;
3122 case factor:
3123 res = validate_factor(tree);
3124 break;
3125 case power:
3126 res = validate_power(tree);
3127 break;
3128 case atom:
3129 res = validate_atom(tree);
3130 break;
3132 default:
3133 /* Hopefully never reached! */
3134 err_string("unrecognized node type");
3135 res = 0;
3136 break;
3138 tree = next;
3140 return (res);
3144 static int
3145 validate_expr_tree(node *tree)
3147 int res = validate_eval_input(tree);
3149 if (!res && !PyErr_Occurred())
3150 err_string("could not validate expression tuple");
3152 return (res);
3156 /* file_input:
3157 * (NEWLINE | stmt)* ENDMARKER
3159 static int
3160 validate_file_input(node *tree)
3162 int j;
3163 int nch = NCH(tree) - 1;
3164 int res = ((nch >= 0)
3165 && validate_ntype(CHILD(tree, nch), ENDMARKER));
3167 for (j = 0; res && (j < nch); ++j) {
3168 if (TYPE(CHILD(tree, j)) == stmt)
3169 res = validate_stmt(CHILD(tree, j));
3170 else
3171 res = validate_newline(CHILD(tree, j));
3173 /* This stays in to prevent any internal failures from getting to the
3174 * user. Hopefully, this won't be needed. If a user reports getting
3175 * this, we have some debugging to do.
3177 if (!res && !PyErr_Occurred())
3178 err_string("VALIDATION FAILURE: report this to the maintainer!");
3180 return (res);
3183 static int
3184 validate_encoding_decl(node *tree)
3186 int nch = NCH(tree);
3187 int res = ((nch == 1)
3188 && validate_file_input(CHILD(tree, 0)));
3190 if (!res && !PyErr_Occurred())
3191 err_string("Error Parsing encoding_decl");
3193 return res;
3196 static PyObject*
3197 pickle_constructor = NULL;
3200 static PyObject*
3201 parser__pickler(PyObject *self, PyObject *args)
3203 NOTE(ARGUNUSED(self))
3204 PyObject *result = NULL;
3205 PyObject *st = NULL;
3206 PyObject *empty_dict = NULL;
3208 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
3209 PyObject *newargs;
3210 PyObject *tuple;
3212 if ((empty_dict = PyDict_New()) == NULL)
3213 goto finally;
3214 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
3215 goto finally;
3216 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
3217 if (tuple != NULL) {
3218 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3219 Py_DECREF(tuple);
3221 Py_DECREF(empty_dict);
3222 Py_DECREF(newargs);
3224 finally:
3225 Py_XDECREF(empty_dict);
3227 return (result);
3231 /* Functions exported by this module. Most of this should probably
3232 * be converted into an ST object with methods, but that is better
3233 * done directly in Python, allowing subclasses to be created directly.
3234 * We'd really have to write a wrapper around it all anyway to allow
3235 * inheritance.
3237 static PyMethodDef parser_functions[] = {
3238 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
3239 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3240 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
3241 PyDoc_STR("Creates a list-tree representation of an ST.")},
3242 {"compileast", (PyCFunction)parser_compileast,PUBLIC_METHOD_TYPE,
3243 PyDoc_STR("Compiles an ST object into a code object.")},
3244 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
3245 PyDoc_STR("Compiles an ST object into a code object.")},
3246 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
3247 PyDoc_STR("Creates an ST object from an expression.")},
3248 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
3249 PyDoc_STR("Determines if an ST object was created from an expression.")},
3250 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
3251 PyDoc_STR("Determines if an ST object was created from a suite.")},
3252 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
3253 PyDoc_STR("Creates an ST object from a suite.")},
3254 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
3255 PyDoc_STR("Creates an ST object from a tree representation.")},
3256 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
3257 PyDoc_STR("Creates an ST object from a tree representation.")},
3258 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
3259 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3260 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
3261 PyDoc_STR("Creates a list-tree representation of an ST.")},
3262 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
3263 PyDoc_STR("Creates an ST object from a tree representation.")},
3264 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
3265 PyDoc_STR("Creates an ST object from a tree representation.")},
3267 /* private stuff: support pickle module */
3268 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
3269 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
3271 {NULL, NULL, 0, NULL}
3275 PyMODINIT_FUNC initparser(void); /* supply a prototype */
3277 PyMODINIT_FUNC
3278 initparser(void)
3280 PyObject *module, *copyreg;
3282 Py_TYPE(&PyST_Type) = &PyType_Type;
3283 module = Py_InitModule("parser", parser_functions);
3284 if (module == NULL)
3285 return;
3287 if (parser_error == 0)
3288 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
3290 if (parser_error == 0)
3291 /* caller will check PyErr_Occurred() */
3292 return;
3293 /* CAUTION: The code next used to skip bumping the refcount on
3294 * parser_error. That's a disaster if initparser() gets called more
3295 * than once. By incref'ing, we ensure that each module dict that
3296 * gets created owns its reference to the shared parser_error object,
3297 * and the file static parser_error vrbl owns a reference too.
3299 Py_INCREF(parser_error);
3300 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3301 return;
3303 Py_INCREF(&PyST_Type);
3304 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
3305 Py_INCREF(&PyST_Type);
3306 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
3308 PyModule_AddStringConstant(module, "__copyright__",
3309 parser_copyright_string);
3310 PyModule_AddStringConstant(module, "__doc__",
3311 parser_doc_string);
3312 PyModule_AddStringConstant(module, "__version__",
3313 parser_version_string);
3315 /* Register to support pickling.
3316 * If this fails, the import of this module will fail because an
3317 * exception will be raised here; should we clear the exception?
3319 copyreg = PyImport_ImportModuleNoBlock("copy_reg");
3320 if (copyreg != NULL) {
3321 PyObject *func, *pickler;
3323 func = PyObject_GetAttrString(copyreg, "pickle");
3324 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3325 pickler = PyObject_GetAttrString(module, "_pickler");
3326 Py_XINCREF(pickle_constructor);
3327 if ((func != NULL) && (pickle_constructor != NULL)
3328 && (pickler != NULL)) {
3329 PyObject *res;
3331 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3332 pickle_constructor, NULL);
3333 Py_XDECREF(res);
3335 Py_XDECREF(func);
3336 Py_XDECREF(pickle_constructor);
3337 Py_XDECREF(pickler);
3338 Py_DECREF(copyreg);