Minor fix for currentframe (SF #1652788).
[python.git] / Modules / parsermodule.c
blob23364fe240ff263cb4b54c9d1e8c65698b961d3d
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 PyObject_HEAD_INIT(NULL)
171 "parser.st", /* tp_name */
172 (int) sizeof(PyST_Object), /* tp_basicsize */
173 0, /* tp_itemsize */
174 (destructor)parser_free, /* tp_dealloc */
175 0, /* tp_print */
176 parser_getattr, /* tp_getattr */
177 0, /* tp_setattr */
178 (cmpfunc)parser_compare, /* tp_compare */
179 0, /* tp_repr */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
183 0, /* tp_hash */
184 0, /* tp_call */
185 0, /* tp_str */
186 0, /* tp_getattro */
187 0, /* tp_setattro */
189 /* Functions to access object as input/output buffer */
190 0, /* tp_as_buffer */
192 Py_TPFLAGS_DEFAULT, /* tp_flags */
194 /* __doc__ */
195 "Intermediate representation of a Python parse tree."
196 }; /* PyST_Type */
199 static int
200 parser_compare_nodes(node *left, node *right)
202 int j;
204 if (TYPE(left) < TYPE(right))
205 return (-1);
207 if (TYPE(right) < TYPE(left))
208 return (1);
210 if (ISTERMINAL(TYPE(left)))
211 return (strcmp(STR(left), STR(right)));
213 if (NCH(left) < NCH(right))
214 return (-1);
216 if (NCH(right) < NCH(left))
217 return (1);
219 for (j = 0; j < NCH(left); ++j) {
220 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
222 if (v != 0)
223 return (v);
225 return (0);
229 /* int parser_compare(PyST_Object* left, PyST_Object* right)
231 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
232 * This really just wraps a call to parser_compare_nodes() with some easy
233 * checks and protection code.
236 static int
237 parser_compare(PyST_Object *left, PyST_Object *right)
239 if (left == right)
240 return (0);
242 if ((left == 0) || (right == 0))
243 return (-1);
245 return (parser_compare_nodes(left->st_node, right->st_node));
249 /* parser_newstobject(node* st)
251 * Allocates a new Python object representing an ST. This is simply the
252 * 'wrapper' object that holds a node* and allows it to be passed around in
253 * Python code.
256 static PyObject*
257 parser_newstobject(node *st, int type)
259 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
261 if (o != 0) {
262 o->st_node = st;
263 o->st_type = type;
265 else {
266 PyNode_Free(st);
268 return ((PyObject*)o);
272 /* void parser_free(PyST_Object* st)
274 * This is called by a del statement that reduces the reference count to 0.
277 static void
278 parser_free(PyST_Object *st)
280 PyNode_Free(st->st_node);
281 PyObject_Del(st);
285 /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
287 * This provides conversion from a node* to a tuple object that can be
288 * returned to the Python-level caller. The ST object is not modified.
291 static PyObject*
292 parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
294 PyObject *line_option = 0;
295 PyObject *col_option = 0;
296 PyObject *res = 0;
297 int ok;
299 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
301 if (self == NULL) {
302 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
303 &PyST_Type, &self, &line_option,
304 &col_option);
306 else
307 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
308 &line_option, &col_option);
309 if (ok != 0) {
310 int lineno = 0;
311 int col_offset = 0;
312 if (line_option != NULL) {
313 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
315 if (col_option != NULL) {
316 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
319 * Convert ST into a tuple representation. Use Guido's function,
320 * since it's known to work already.
322 res = node2tuple(((PyST_Object*)self)->st_node,
323 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
325 return (res);
329 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
331 * This provides conversion from a node* to a list object that can be
332 * returned to the Python-level caller. The ST object is not modified.
335 static PyObject*
336 parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
338 PyObject *line_option = 0;
339 PyObject *col_option = 0;
340 PyObject *res = 0;
341 int ok;
343 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
345 if (self == NULL)
346 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
347 &PyST_Type, &self, &line_option,
348 &col_option);
349 else
350 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
351 &line_option, &col_option);
352 if (ok) {
353 int lineno = 0;
354 int col_offset = 0;
355 if (line_option != 0) {
356 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
358 if (col_option != NULL) {
359 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
362 * Convert ST into a tuple representation. Use Guido's function,
363 * since it's known to work already.
365 res = node2tuple(self->st_node,
366 PyList_New, PyList_SetItem, lineno, col_offset);
368 return (res);
372 /* parser_compilest(PyObject* self, PyObject* args)
374 * This function creates code objects from the parse tree represented by
375 * the passed-in data object. An optional file name is passed in as well.
378 static PyObject*
379 parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
381 PyObject* res = 0;
382 char* str = "<syntax-tree>";
383 int ok;
385 static char *keywords[] = {"ast", "filename", NULL};
387 if (self == NULL)
388 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
389 &PyST_Type, &self, &str);
390 else
391 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
392 &str);
394 if (ok)
395 res = (PyObject *)PyNode_Compile(self->st_node, str);
397 return (res);
401 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
402 * PyObject* parser_issuite(PyObject* self, PyObject* args)
404 * Checks the passed-in ST object to determine if it is an expression or
405 * a statement suite, respectively. The return is a Python truth value.
408 static PyObject*
409 parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
411 PyObject* res = 0;
412 int ok;
414 static char *keywords[] = {"ast", NULL};
416 if (self == NULL)
417 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
418 &PyST_Type, &self);
419 else
420 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
422 if (ok) {
423 /* Check to see if the ST represents an expression or not. */
424 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
425 Py_INCREF(res);
427 return (res);
431 static PyObject*
432 parser_issuite(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!:issuite", keywords,
441 &PyST_Type, &self);
442 else
443 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &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_False : Py_True;
448 Py_INCREF(res);
450 return (res);
454 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
456 static PyMethodDef
457 parser_methods[] = {
458 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
459 PyDoc_STR("Compile this ST object into a code object.")},
460 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
461 PyDoc_STR("Determines if this ST object was created from an expression.")},
462 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
463 PyDoc_STR("Determines if this ST object was created from a suite.")},
464 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
465 PyDoc_STR("Creates a list-tree representation of this ST.")},
466 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
467 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
469 {NULL, NULL, 0, NULL}
473 static PyObject*
474 parser_getattr(PyObject *self, char *name)
476 return (Py_FindMethod(parser_methods, self, name));
480 /* err_string(char* message)
482 * Sets the error string for an exception of type ParserError.
485 static void
486 err_string(char *message)
488 PyErr_SetString(parser_error, message);
492 /* PyObject* parser_do_parse(PyObject* args, int type)
494 * Internal function to actually execute the parse and return the result if
495 * successful or set an exception if not.
498 static PyObject*
499 parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
501 char* string = 0;
502 PyObject* res = 0;
504 static char *keywords[] = {"source", NULL};
506 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
507 node* n = PyParser_SimpleParseString(string,
508 (type == PyST_EXPR)
509 ? eval_input : file_input);
511 if (n)
512 res = parser_newstobject(n, type);
514 return (res);
518 /* PyObject* parser_expr(PyObject* self, PyObject* args)
519 * PyObject* parser_suite(PyObject* self, PyObject* args)
521 * External interfaces to the parser itself. Which is called determines if
522 * the parser attempts to recognize an expression ('eval' form) or statement
523 * suite ('exec' form). The real work is done by parser_do_parse() above.
526 static PyObject*
527 parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
529 NOTE(ARGUNUSED(self))
530 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
534 static PyObject*
535 parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
537 NOTE(ARGUNUSED(self))
538 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
543 /* This is the messy part of the code. Conversion from a tuple to an ST
544 * object requires that the input tuple be valid without having to rely on
545 * catching an exception from the compiler. This is done to allow the
546 * compiler itself to remain fast, since most of its input will come from
547 * the parser directly, and therefore be known to be syntactically correct.
548 * This validation is done to ensure that we don't core dump the compile
549 * phase, returning an exception instead.
551 * Two aspects can be broken out in this code: creating a node tree from
552 * the tuple passed in, and verifying that it is indeed valid. It may be
553 * advantageous to expand the number of ST types to include funcdefs and
554 * lambdadefs to take advantage of the optimizer, recognizing those STs
555 * here. They are not necessary, and not quite as useful in a raw form.
556 * For now, let's get expressions and suites working reliably.
560 static node* build_node_tree(PyObject *tuple);
561 static int validate_expr_tree(node *tree);
562 static int validate_file_input(node *tree);
563 static int validate_encoding_decl(node *tree);
565 /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
567 * This is the public function, called from the Python code. It receives a
568 * single tuple object from the caller, and creates an ST object if the
569 * tuple can be validated. It does this by checking the first code of the
570 * tuple, and, if acceptable, builds the internal representation. If this
571 * step succeeds, the internal representation is validated as fully as
572 * possible with the various validate_*() routines defined below.
574 * This function must be changed if support is to be added for PyST_FRAGMENT
575 * ST objects.
578 static PyObject*
579 parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
581 NOTE(ARGUNUSED(self))
582 PyObject *st = 0;
583 PyObject *tuple;
584 node *tree;
586 static char *keywords[] = {"sequence", NULL};
588 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
589 &tuple))
590 return (0);
591 if (!PySequence_Check(tuple)) {
592 PyErr_SetString(PyExc_ValueError,
593 "sequence2st() requires a single sequence argument");
594 return (0);
597 * Convert the tree to the internal form before checking it.
599 tree = build_node_tree(tuple);
600 if (tree != 0) {
601 int start_sym = TYPE(tree);
602 if (start_sym == eval_input) {
603 /* Might be an eval form. */
604 if (validate_expr_tree(tree))
605 st = parser_newstobject(tree, PyST_EXPR);
606 else
607 PyNode_Free(tree);
609 else if (start_sym == file_input) {
610 /* This looks like an exec form so far. */
611 if (validate_file_input(tree))
612 st = parser_newstobject(tree, PyST_SUITE);
613 else
614 PyNode_Free(tree);
616 else if (start_sym == encoding_decl) {
617 /* This looks like an encoding_decl so far. */
618 if (validate_encoding_decl(tree))
619 st = parser_newstobject(tree, PyST_SUITE);
620 else
621 PyNode_Free(tree);
623 else {
624 /* This is a fragment, at best. */
625 PyNode_Free(tree);
626 err_string("parse tree does not use a valid start symbol");
629 /* Make sure we throw an exception on all errors. We should never
630 * get this, but we'd do well to be sure something is done.
632 if (st == NULL && !PyErr_Occurred())
633 err_string("unspecified ST error occurred");
635 return st;
639 /* node* build_node_children()
641 * Iterate across the children of the current non-terminal node and build
642 * their structures. If successful, return the root of this portion of
643 * the tree, otherwise, 0. Any required exception will be specified already,
644 * and no memory will have been deallocated.
647 static node*
648 build_node_children(PyObject *tuple, node *root, int *line_num)
650 Py_ssize_t len = PyObject_Size(tuple);
651 Py_ssize_t i;
652 int err;
654 for (i = 1; i < len; ++i) {
655 /* elem must always be a sequence, however simple */
656 PyObject* elem = PySequence_GetItem(tuple, i);
657 int ok = elem != NULL;
658 long type = 0;
659 char *strn = 0;
661 if (ok)
662 ok = PySequence_Check(elem);
663 if (ok) {
664 PyObject *temp = PySequence_GetItem(elem, 0);
665 if (temp == NULL)
666 ok = 0;
667 else {
668 ok = PyInt_Check(temp);
669 if (ok)
670 type = PyInt_AS_LONG(temp);
671 Py_DECREF(temp);
674 if (!ok) {
675 PyObject *err = Py_BuildValue("os", elem,
676 "Illegal node construct.");
677 PyErr_SetObject(parser_error, err);
678 Py_XDECREF(err);
679 Py_XDECREF(elem);
680 return (0);
682 if (ISTERMINAL(type)) {
683 Py_ssize_t len = PyObject_Size(elem);
684 PyObject *temp;
686 if ((len != 2) && (len != 3)) {
687 err_string("terminal nodes must have 2 or 3 entries");
688 return 0;
690 temp = PySequence_GetItem(elem, 1);
691 if (temp == NULL)
692 return 0;
693 if (!PyString_Check(temp)) {
694 PyErr_Format(parser_error,
695 "second item in terminal node must be a string,"
696 " found %s",
697 temp->ob_type->tp_name);
698 Py_DECREF(temp);
699 return 0;
701 if (len == 3) {
702 PyObject *o = PySequence_GetItem(elem, 2);
703 if (o != NULL) {
704 if (PyInt_Check(o))
705 *line_num = PyInt_AS_LONG(o);
706 else {
707 PyErr_Format(parser_error,
708 "third item in terminal node must be an"
709 " integer, found %s",
710 temp->ob_type->tp_name);
711 Py_DECREF(o);
712 Py_DECREF(temp);
713 return 0;
715 Py_DECREF(o);
718 len = PyString_GET_SIZE(temp) + 1;
719 strn = (char *)PyObject_MALLOC(len);
720 if (strn != NULL)
721 (void) memcpy(strn, PyString_AS_STRING(temp), len);
722 Py_DECREF(temp);
724 else if (!ISNONTERMINAL(type)) {
726 * It has to be one or the other; this is an error.
727 * Throw an exception.
729 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
730 PyErr_SetObject(parser_error, err);
731 Py_XDECREF(err);
732 Py_XDECREF(elem);
733 return (0);
735 err = PyNode_AddChild(root, type, strn, *line_num, 0);
736 if (err == E_NOMEM) {
737 PyObject_FREE(strn);
738 return (node *) PyErr_NoMemory();
740 if (err == E_OVERFLOW) {
741 PyObject_FREE(strn);
742 PyErr_SetString(PyExc_ValueError,
743 "unsupported number of child nodes");
744 return NULL;
747 if (ISNONTERMINAL(type)) {
748 node* new_child = CHILD(root, i - 1);
750 if (new_child != build_node_children(elem, new_child, line_num)) {
751 Py_XDECREF(elem);
752 return (0);
755 else if (type == NEWLINE) { /* It's true: we increment the */
756 ++(*line_num); /* line number *after* the newline! */
758 Py_XDECREF(elem);
760 return root;
764 static node*
765 build_node_tree(PyObject *tuple)
767 node* res = 0;
768 PyObject *temp = PySequence_GetItem(tuple, 0);
769 long num = -1;
771 if (temp != NULL)
772 num = PyInt_AsLong(temp);
773 Py_XDECREF(temp);
774 if (ISTERMINAL(num)) {
776 * The tuple is simple, but it doesn't start with a start symbol.
777 * Throw an exception now and be done with it.
779 tuple = Py_BuildValue("os", tuple,
780 "Illegal syntax-tree; cannot start with terminal symbol.");
781 PyErr_SetObject(parser_error, tuple);
782 Py_XDECREF(tuple);
784 else if (ISNONTERMINAL(num)) {
786 * Not efficient, but that can be handled later.
788 int line_num = 0;
789 PyObject *encoding = NULL;
791 if (num == encoding_decl) {
792 encoding = PySequence_GetItem(tuple, 2);
793 /* tuple isn't borrowed anymore here, need to DECREF */
794 tuple = PySequence_GetSlice(tuple, 0, 2);
796 res = PyNode_New(num);
797 if (res != NULL) {
798 if (res != build_node_children(tuple, res, &line_num)) {
799 PyNode_Free(res);
800 res = NULL;
802 if (res && encoding) {
803 Py_ssize_t len;
804 len = PyString_GET_SIZE(encoding) + 1;
805 res->n_str = (char *)PyObject_MALLOC(len);
806 if (res->n_str != NULL)
807 (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
808 Py_DECREF(encoding);
809 Py_DECREF(tuple);
813 else {
814 /* The tuple is illegal -- if the number is neither TERMINAL nor
815 * NONTERMINAL, we can't use it. Not sure the implementation
816 * allows this condition, but the API doesn't preclude it.
818 PyObject *err = Py_BuildValue("os", tuple,
819 "Illegal component tuple.");
820 PyErr_SetObject(parser_error, err);
821 Py_XDECREF(err);
824 return (res);
829 * Validation routines used within the validation section:
831 static int validate_terminal(node *terminal, int type, char *string);
833 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
834 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
835 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
836 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
837 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
838 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
839 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
840 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
841 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
842 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
843 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
844 #define validate_star(ch) validate_terminal(ch, STAR, "*")
845 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
846 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
847 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
848 #define validate_at(ch) validate_terminal(ch, AT, "@")
849 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
851 #define VALIDATER(n) static int validate_##n(node *tree)
853 VALIDATER(node); VALIDATER(small_stmt);
854 VALIDATER(class); VALIDATER(node);
855 VALIDATER(parameters); VALIDATER(suite);
856 VALIDATER(testlist); VALIDATER(varargslist);
857 VALIDATER(fpdef); VALIDATER(fplist);
858 VALIDATER(stmt); VALIDATER(simple_stmt);
859 VALIDATER(expr_stmt); VALIDATER(power);
860 VALIDATER(print_stmt); VALIDATER(del_stmt);
861 VALIDATER(return_stmt); VALIDATER(list_iter);
862 VALIDATER(raise_stmt); VALIDATER(import_stmt);
863 VALIDATER(import_name); VALIDATER(import_from);
864 VALIDATER(global_stmt); VALIDATER(list_if);
865 VALIDATER(assert_stmt); VALIDATER(list_for);
866 VALIDATER(exec_stmt); VALIDATER(compound_stmt);
867 VALIDATER(while); VALIDATER(for);
868 VALIDATER(try); VALIDATER(except_clause);
869 VALIDATER(test); VALIDATER(and_test);
870 VALIDATER(not_test); VALIDATER(comparison);
871 VALIDATER(comp_op); VALIDATER(expr);
872 VALIDATER(xor_expr); VALIDATER(and_expr);
873 VALIDATER(shift_expr); VALIDATER(arith_expr);
874 VALIDATER(term); VALIDATER(factor);
875 VALIDATER(atom); VALIDATER(lambdef);
876 VALIDATER(trailer); VALIDATER(subscript);
877 VALIDATER(subscriptlist); VALIDATER(sliceop);
878 VALIDATER(exprlist); VALIDATER(dictmaker);
879 VALIDATER(arglist); VALIDATER(argument);
880 VALIDATER(listmaker); VALIDATER(yield_stmt);
881 VALIDATER(testlist1); VALIDATER(gen_for);
882 VALIDATER(gen_iter); VALIDATER(gen_if);
883 VALIDATER(testlist_gexp); VALIDATER(yield_expr);
884 VALIDATER(yield_or_testlist); VALIDATER(or_test);
885 VALIDATER(old_test); VALIDATER(old_lambdef);
887 #undef VALIDATER
889 #define is_even(n) (((n) & 1) == 0)
890 #define is_odd(n) (((n) & 1) == 1)
893 static int
894 validate_ntype(node *n, int t)
896 if (TYPE(n) != t) {
897 PyErr_Format(parser_error, "Expected node type %d, got %d.",
898 t, TYPE(n));
899 return 0;
901 return 1;
905 /* Verifies that the number of child nodes is exactly 'num', raising
906 * an exception if it isn't. The exception message does not indicate
907 * the exact number of nodes, allowing this to be used to raise the
908 * "right" exception when the wrong number of nodes is present in a
909 * specific variant of a statement's syntax. This is commonly used
910 * in that fashion.
912 static int
913 validate_numnodes(node *n, int num, const char *const name)
915 if (NCH(n) != num) {
916 PyErr_Format(parser_error,
917 "Illegal number of children for %s node.", name);
918 return 0;
920 return 1;
924 static int
925 validate_terminal(node *terminal, int type, char *string)
927 int res = (validate_ntype(terminal, type)
928 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
930 if (!res && !PyErr_Occurred()) {
931 PyErr_Format(parser_error,
932 "Illegal terminal: expected \"%s\"", string);
934 return (res);
938 /* X (',' X) [',']
940 static int
941 validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
942 const char *const name)
944 int nch = NCH(tree);
945 int res = (nch && validate_ntype(tree, ntype)
946 && vfunc(CHILD(tree, 0)));
948 if (!res && !PyErr_Occurred())
949 (void) validate_numnodes(tree, 1, name);
950 else {
951 if (is_even(nch))
952 res = validate_comma(CHILD(tree, --nch));
953 if (res && nch > 1) {
954 int pos = 1;
955 for ( ; res && pos < nch; pos += 2)
956 res = (validate_comma(CHILD(tree, pos))
957 && vfunc(CHILD(tree, pos + 1)));
960 return (res);
964 /* validate_class()
966 * classdef:
967 * 'class' NAME ['(' testlist ')'] ':' suite
969 static int
970 validate_class(node *tree)
972 int nch = NCH(tree);
973 int res = (validate_ntype(tree, classdef) &&
974 ((nch == 4) || (nch == 6) || (nch == 7)));
976 if (res) {
977 res = (validate_name(CHILD(tree, 0), "class")
978 && validate_ntype(CHILD(tree, 1), NAME)
979 && validate_colon(CHILD(tree, nch - 2))
980 && validate_suite(CHILD(tree, nch - 1)));
982 else {
983 (void) validate_numnodes(tree, 4, "class");
986 if (res) {
987 if (nch == 7) {
988 res = ((validate_lparen(CHILD(tree, 2)) &&
989 validate_testlist(CHILD(tree, 3)) &&
990 validate_rparen(CHILD(tree, 4))));
992 else if (nch == 6) {
993 res = (validate_lparen(CHILD(tree,2)) &&
994 validate_rparen(CHILD(tree,3)));
997 return (res);
1001 /* if_stmt:
1002 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1004 static int
1005 validate_if(node *tree)
1007 int nch = NCH(tree);
1008 int res = (validate_ntype(tree, if_stmt)
1009 && (nch >= 4)
1010 && validate_name(CHILD(tree, 0), "if")
1011 && validate_test(CHILD(tree, 1))
1012 && validate_colon(CHILD(tree, 2))
1013 && validate_suite(CHILD(tree, 3)));
1015 if (res && ((nch % 4) == 3)) {
1016 /* ... 'else' ':' suite */
1017 res = (validate_name(CHILD(tree, nch - 3), "else")
1018 && validate_colon(CHILD(tree, nch - 2))
1019 && validate_suite(CHILD(tree, nch - 1)));
1020 nch -= 3;
1022 else if (!res && !PyErr_Occurred())
1023 (void) validate_numnodes(tree, 4, "if");
1024 if ((nch % 4) != 0)
1025 /* Will catch the case for nch < 4 */
1026 res = validate_numnodes(tree, 0, "if");
1027 else if (res && (nch > 4)) {
1028 /* ... ('elif' test ':' suite)+ ... */
1029 int j = 4;
1030 while ((j < nch) && res) {
1031 res = (validate_name(CHILD(tree, j), "elif")
1032 && validate_colon(CHILD(tree, j + 2))
1033 && validate_test(CHILD(tree, j + 1))
1034 && validate_suite(CHILD(tree, j + 3)));
1035 j += 4;
1038 return (res);
1042 /* parameters:
1043 * '(' [varargslist] ')'
1046 static int
1047 validate_parameters(node *tree)
1049 int nch = NCH(tree);
1050 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
1052 if (res) {
1053 res = (validate_lparen(CHILD(tree, 0))
1054 && validate_rparen(CHILD(tree, nch - 1)));
1055 if (res && (nch == 3))
1056 res = validate_varargslist(CHILD(tree, 1));
1058 else {
1059 (void) validate_numnodes(tree, 2, "parameters");
1061 return (res);
1065 /* validate_suite()
1067 * suite:
1068 * simple_stmt
1069 * | NEWLINE INDENT stmt+ DEDENT
1071 static int
1072 validate_suite(node *tree)
1074 int nch = NCH(tree);
1075 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
1077 if (res && (nch == 1))
1078 res = validate_simple_stmt(CHILD(tree, 0));
1079 else if (res) {
1080 /* NEWLINE INDENT stmt+ DEDENT */
1081 res = (validate_newline(CHILD(tree, 0))
1082 && validate_indent(CHILD(tree, 1))
1083 && validate_stmt(CHILD(tree, 2))
1084 && validate_dedent(CHILD(tree, nch - 1)));
1086 if (res && (nch > 4)) {
1087 int i = 3;
1088 --nch; /* forget the DEDENT */
1089 for ( ; res && (i < nch); ++i)
1090 res = validate_stmt(CHILD(tree, i));
1092 else if (nch < 4)
1093 res = validate_numnodes(tree, 4, "suite");
1095 return (res);
1099 static int
1100 validate_testlist(node *tree)
1102 return (validate_repeating_list(tree, testlist,
1103 validate_test, "testlist"));
1107 static int
1108 validate_testlist1(node *tree)
1110 return (validate_repeating_list(tree, testlist1,
1111 validate_test, "testlist1"));
1115 static int
1116 validate_testlist_safe(node *tree)
1118 return (validate_repeating_list(tree, testlist_safe,
1119 validate_old_test, "testlist_safe"));
1123 /* '*' NAME [',' '**' NAME] | '**' NAME
1125 static int
1126 validate_varargslist_trailer(node *tree, int start)
1128 int nch = NCH(tree);
1129 int res = 0;
1130 int sym;
1132 if (nch <= start) {
1133 err_string("expected variable argument trailer for varargslist");
1134 return 0;
1136 sym = TYPE(CHILD(tree, start));
1137 if (sym == STAR) {
1139 * ('*' NAME [',' '**' NAME]
1141 if (nch-start == 2)
1142 res = validate_name(CHILD(tree, start+1), NULL);
1143 else if (nch-start == 5)
1144 res = (validate_name(CHILD(tree, start+1), NULL)
1145 && validate_comma(CHILD(tree, start+2))
1146 && validate_doublestar(CHILD(tree, start+3))
1147 && validate_name(CHILD(tree, start+4), NULL));
1149 else if (sym == DOUBLESTAR) {
1151 * '**' NAME
1153 if (nch-start == 2)
1154 res = validate_name(CHILD(tree, start+1), NULL);
1156 if (!res)
1157 err_string("illegal variable argument trailer for varargslist");
1158 return res;
1162 /* validate_varargslist()
1164 * varargslist:
1165 * (fpdef ['=' test] ',')*
1166 * ('*' NAME [',' '**' NAME]
1167 * | '**' NAME)
1168 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1171 static int
1172 validate_varargslist(node *tree)
1174 int nch = NCH(tree);
1175 int res = validate_ntype(tree, varargslist) && (nch != 0);
1176 int sym;
1178 if (!res)
1179 return 0;
1180 if (nch < 1) {
1181 err_string("varargslist missing child nodes");
1182 return 0;
1184 sym = TYPE(CHILD(tree, 0));
1185 if (sym == STAR || sym == DOUBLESTAR)
1186 /* whole thing matches:
1187 * '*' NAME [',' '**' NAME] | '**' NAME
1189 res = validate_varargslist_trailer(tree, 0);
1190 else if (sym == fpdef) {
1191 int i = 0;
1193 sym = TYPE(CHILD(tree, nch-1));
1194 if (sym == NAME) {
1196 * (fpdef ['=' test] ',')+
1197 * ('*' NAME [',' '**' NAME]
1198 * | '**' NAME)
1200 /* skip over (fpdef ['=' test] ',')+ */
1201 while (res && (i+2 <= nch)) {
1202 res = validate_fpdef(CHILD(tree, i));
1203 ++i;
1204 if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
1205 res = (validate_equal(CHILD(tree, i))
1206 && validate_test(CHILD(tree, i+1)));
1207 if (res)
1208 i += 2;
1210 if (res && i < nch) {
1211 res = validate_comma(CHILD(tree, i));
1212 ++i;
1213 if (res && i < nch
1214 && (TYPE(CHILD(tree, i)) == DOUBLESTAR
1215 || TYPE(CHILD(tree, i)) == STAR))
1216 break;
1219 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1220 * i --^^^
1222 if (res)
1223 res = validate_varargslist_trailer(tree, i);
1225 else {
1227 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1229 /* strip trailing comma node */
1230 if (sym == COMMA) {
1231 res = validate_comma(CHILD(tree, nch-1));
1232 if (!res)
1233 return 0;
1234 --nch;
1237 * fpdef ['=' test] (',' fpdef ['=' test])*
1239 res = validate_fpdef(CHILD(tree, 0));
1240 ++i;
1241 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
1242 res = (validate_equal(CHILD(tree, i))
1243 && validate_test(CHILD(tree, i+1)));
1244 i += 2;
1247 * ... (',' fpdef ['=' test])*
1248 * i ---^^^
1250 while (res && (nch - i) >= 2) {
1251 res = (validate_comma(CHILD(tree, i))
1252 && validate_fpdef(CHILD(tree, i+1)));
1253 i += 2;
1254 if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
1255 res = (validate_equal(CHILD(tree, i))
1256 && validate_test(CHILD(tree, i+1)));
1257 i += 2;
1260 if (res && nch - i != 0) {
1261 res = 0;
1262 err_string("illegal formation for varargslist");
1266 return res;
1270 /* list_iter: list_for | list_if
1272 static int
1273 validate_list_iter(node *tree)
1275 int res = (validate_ntype(tree, list_iter)
1276 && validate_numnodes(tree, 1, "list_iter"));
1277 if (res && TYPE(CHILD(tree, 0)) == list_for)
1278 res = validate_list_for(CHILD(tree, 0));
1279 else
1280 res = validate_list_if(CHILD(tree, 0));
1282 return res;
1285 /* gen_iter: gen_for | gen_if
1287 static int
1288 validate_gen_iter(node *tree)
1290 int res = (validate_ntype(tree, gen_iter)
1291 && validate_numnodes(tree, 1, "gen_iter"));
1292 if (res && TYPE(CHILD(tree, 0)) == gen_for)
1293 res = validate_gen_for(CHILD(tree, 0));
1294 else
1295 res = validate_gen_if(CHILD(tree, 0));
1297 return res;
1300 /* list_for: 'for' exprlist 'in' testlist [list_iter]
1302 static int
1303 validate_list_for(node *tree)
1305 int nch = NCH(tree);
1306 int res;
1308 if (nch == 5)
1309 res = validate_list_iter(CHILD(tree, 4));
1310 else
1311 res = validate_numnodes(tree, 4, "list_for");
1313 if (res)
1314 res = (validate_name(CHILD(tree, 0), "for")
1315 && validate_exprlist(CHILD(tree, 1))
1316 && validate_name(CHILD(tree, 2), "in")
1317 && validate_testlist_safe(CHILD(tree, 3)));
1319 return res;
1322 /* gen_for: 'for' exprlist 'in' test [gen_iter]
1324 static int
1325 validate_gen_for(node *tree)
1327 int nch = NCH(tree);
1328 int res;
1330 if (nch == 5)
1331 res = validate_gen_iter(CHILD(tree, 4));
1332 else
1333 res = validate_numnodes(tree, 4, "gen_for");
1335 if (res)
1336 res = (validate_name(CHILD(tree, 0), "for")
1337 && validate_exprlist(CHILD(tree, 1))
1338 && validate_name(CHILD(tree, 2), "in")
1339 && validate_or_test(CHILD(tree, 3)));
1341 return res;
1344 /* list_if: 'if' old_test [list_iter]
1346 static int
1347 validate_list_if(node *tree)
1349 int nch = NCH(tree);
1350 int res;
1352 if (nch == 3)
1353 res = validate_list_iter(CHILD(tree, 2));
1354 else
1355 res = validate_numnodes(tree, 2, "list_if");
1357 if (res)
1358 res = (validate_name(CHILD(tree, 0), "if")
1359 && validate_old_test(CHILD(tree, 1)));
1361 return res;
1364 /* gen_if: 'if' old_test [gen_iter]
1366 static int
1367 validate_gen_if(node *tree)
1369 int nch = NCH(tree);
1370 int res;
1372 if (nch == 3)
1373 res = validate_gen_iter(CHILD(tree, 2));
1374 else
1375 res = validate_numnodes(tree, 2, "gen_if");
1377 if (res)
1378 res = (validate_name(CHILD(tree, 0), "if")
1379 && validate_old_test(CHILD(tree, 1)));
1381 return res;
1384 /* validate_fpdef()
1386 * fpdef:
1387 * NAME
1388 * | '(' fplist ')'
1390 static int
1391 validate_fpdef(node *tree)
1393 int nch = NCH(tree);
1394 int res = validate_ntype(tree, fpdef);
1396 if (res) {
1397 if (nch == 1)
1398 res = validate_ntype(CHILD(tree, 0), NAME);
1399 else if (nch == 3)
1400 res = (validate_lparen(CHILD(tree, 0))
1401 && validate_fplist(CHILD(tree, 1))
1402 && validate_rparen(CHILD(tree, 2)));
1403 else
1404 res = validate_numnodes(tree, 1, "fpdef");
1406 return (res);
1410 static int
1411 validate_fplist(node *tree)
1413 return (validate_repeating_list(tree, fplist,
1414 validate_fpdef, "fplist"));
1418 /* simple_stmt | compound_stmt
1421 static int
1422 validate_stmt(node *tree)
1424 int res = (validate_ntype(tree, stmt)
1425 && validate_numnodes(tree, 1, "stmt"));
1427 if (res) {
1428 tree = CHILD(tree, 0);
1430 if (TYPE(tree) == simple_stmt)
1431 res = validate_simple_stmt(tree);
1432 else
1433 res = validate_compound_stmt(tree);
1435 return (res);
1439 /* small_stmt (';' small_stmt)* [';'] NEWLINE
1442 static int
1443 validate_simple_stmt(node *tree)
1445 int nch = NCH(tree);
1446 int res = (validate_ntype(tree, simple_stmt)
1447 && (nch >= 2)
1448 && validate_small_stmt(CHILD(tree, 0))
1449 && validate_newline(CHILD(tree, nch - 1)));
1451 if (nch < 2)
1452 res = validate_numnodes(tree, 2, "simple_stmt");
1453 --nch; /* forget the NEWLINE */
1454 if (res && is_even(nch))
1455 res = validate_semi(CHILD(tree, --nch));
1456 if (res && (nch > 2)) {
1457 int i;
1459 for (i = 1; res && (i < nch); i += 2)
1460 res = (validate_semi(CHILD(tree, i))
1461 && validate_small_stmt(CHILD(tree, i + 1)));
1463 return (res);
1467 static int
1468 validate_small_stmt(node *tree)
1470 int nch = NCH(tree);
1471 int res = validate_numnodes(tree, 1, "small_stmt");
1473 if (res) {
1474 int ntype = TYPE(CHILD(tree, 0));
1476 if ( (ntype == expr_stmt)
1477 || (ntype == print_stmt)
1478 || (ntype == del_stmt)
1479 || (ntype == pass_stmt)
1480 || (ntype == flow_stmt)
1481 || (ntype == import_stmt)
1482 || (ntype == global_stmt)
1483 || (ntype == assert_stmt)
1484 || (ntype == exec_stmt))
1485 res = validate_node(CHILD(tree, 0));
1486 else {
1487 res = 0;
1488 err_string("illegal small_stmt child type");
1491 else if (nch == 1) {
1492 res = 0;
1493 PyErr_Format(parser_error,
1494 "Unrecognized child node of small_stmt: %d.",
1495 TYPE(CHILD(tree, 0)));
1497 return (res);
1501 /* compound_stmt:
1502 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1504 static int
1505 validate_compound_stmt(node *tree)
1507 int res = (validate_ntype(tree, compound_stmt)
1508 && validate_numnodes(tree, 1, "compound_stmt"));
1509 int ntype;
1511 if (!res)
1512 return (0);
1514 tree = CHILD(tree, 0);
1515 ntype = TYPE(tree);
1516 if ( (ntype == if_stmt)
1517 || (ntype == while_stmt)
1518 || (ntype == for_stmt)
1519 || (ntype == try_stmt)
1520 || (ntype == funcdef)
1521 || (ntype == classdef))
1522 res = validate_node(tree);
1523 else {
1524 res = 0;
1525 PyErr_Format(parser_error,
1526 "Illegal compound statement type: %d.", TYPE(tree));
1528 return (res);
1532 static int
1533 validate_yield_or_testlist(node *tree)
1535 if (TYPE(tree) == yield_expr)
1536 return validate_yield_expr(tree);
1537 else
1538 return validate_testlist(tree);
1541 static int
1542 validate_expr_stmt(node *tree)
1544 int j;
1545 int nch = NCH(tree);
1546 int res = (validate_ntype(tree, expr_stmt)
1547 && is_odd(nch)
1548 && validate_testlist(CHILD(tree, 0)));
1550 if (res && nch == 3
1551 && TYPE(CHILD(tree, 1)) == augassign) {
1552 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
1553 && validate_yield_or_testlist(CHILD(tree, 2));
1555 if (res) {
1556 char *s = STR(CHILD(CHILD(tree, 1), 0));
1558 res = (strcmp(s, "+=") == 0
1559 || strcmp(s, "-=") == 0
1560 || strcmp(s, "*=") == 0
1561 || strcmp(s, "/=") == 0
1562 || strcmp(s, "//=") == 0
1563 || strcmp(s, "%=") == 0
1564 || strcmp(s, "&=") == 0
1565 || strcmp(s, "|=") == 0
1566 || strcmp(s, "^=") == 0
1567 || strcmp(s, "<<=") == 0
1568 || strcmp(s, ">>=") == 0
1569 || strcmp(s, "**=") == 0);
1570 if (!res)
1571 err_string("illegal augmmented assignment operator");
1574 else {
1575 for (j = 1; res && (j < nch); j += 2)
1576 res = validate_equal(CHILD(tree, j))
1577 && validate_yield_or_testlist(CHILD(tree, j + 1));
1579 return (res);
1583 /* print_stmt:
1585 * 'print' ( [ test (',' test)* [','] ]
1586 * | '>>' test [ (',' test)+ [','] ] )
1588 static int
1589 validate_print_stmt(node *tree)
1591 int nch = NCH(tree);
1592 int res = (validate_ntype(tree, print_stmt)
1593 && (nch > 0)
1594 && validate_name(CHILD(tree, 0), "print"));
1596 if (res && nch > 1) {
1597 int sym = TYPE(CHILD(tree, 1));
1598 int i = 1;
1599 int allow_trailing_comma = 1;
1601 if (sym == test)
1602 res = validate_test(CHILD(tree, i++));
1603 else {
1604 if (nch < 3)
1605 res = validate_numnodes(tree, 3, "print_stmt");
1606 else {
1607 res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
1608 && validate_test(CHILD(tree, i+1)));
1609 i += 2;
1610 allow_trailing_comma = 0;
1613 if (res) {
1614 /* ... (',' test)* [','] */
1615 while (res && i+2 <= nch) {
1616 res = (validate_comma(CHILD(tree, i))
1617 && validate_test(CHILD(tree, i+1)));
1618 allow_trailing_comma = 1;
1619 i += 2;
1621 if (res && !allow_trailing_comma)
1622 res = validate_numnodes(tree, i, "print_stmt");
1623 else if (res && i < nch)
1624 res = validate_comma(CHILD(tree, i));
1627 return (res);
1631 static int
1632 validate_del_stmt(node *tree)
1634 return (validate_numnodes(tree, 2, "del_stmt")
1635 && validate_name(CHILD(tree, 0), "del")
1636 && validate_exprlist(CHILD(tree, 1)));
1640 static int
1641 validate_return_stmt(node *tree)
1643 int nch = NCH(tree);
1644 int res = (validate_ntype(tree, return_stmt)
1645 && ((nch == 1) || (nch == 2))
1646 && validate_name(CHILD(tree, 0), "return"));
1648 if (res && (nch == 2))
1649 res = validate_testlist(CHILD(tree, 1));
1651 return (res);
1655 static int
1656 validate_raise_stmt(node *tree)
1658 int nch = NCH(tree);
1659 int res = (validate_ntype(tree, raise_stmt)
1660 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
1662 if (res) {
1663 res = validate_name(CHILD(tree, 0), "raise");
1664 if (res && (nch >= 2))
1665 res = validate_test(CHILD(tree, 1));
1666 if (res && nch > 2) {
1667 res = (validate_comma(CHILD(tree, 2))
1668 && validate_test(CHILD(tree, 3)));
1669 if (res && (nch > 4))
1670 res = (validate_comma(CHILD(tree, 4))
1671 && validate_test(CHILD(tree, 5)));
1674 else
1675 (void) validate_numnodes(tree, 2, "raise");
1676 if (res && (nch == 4))
1677 res = (validate_comma(CHILD(tree, 2))
1678 && validate_test(CHILD(tree, 3)));
1680 return (res);
1684 /* yield_expr: 'yield' [testlist]
1686 static int
1687 validate_yield_expr(node *tree)
1689 int nch = NCH(tree);
1690 int res = (validate_ntype(tree, yield_expr)
1691 && ((nch == 1) || (nch == 2))
1692 && validate_name(CHILD(tree, 0), "yield"));
1694 if (res && (nch == 2))
1695 res = validate_testlist(CHILD(tree, 1));
1697 return (res);
1701 /* yield_stmt: yield_expr
1703 static int
1704 validate_yield_stmt(node *tree)
1706 return (validate_ntype(tree, yield_stmt)
1707 && validate_numnodes(tree, 1, "yield_stmt")
1708 && validate_yield_expr(CHILD(tree, 0)));
1712 static int
1713 validate_import_as_name(node *tree)
1715 int nch = NCH(tree);
1716 int ok = validate_ntype(tree, import_as_name);
1718 if (ok) {
1719 if (nch == 1)
1720 ok = validate_name(CHILD(tree, 0), NULL);
1721 else if (nch == 3)
1722 ok = (validate_name(CHILD(tree, 0), NULL)
1723 && validate_name(CHILD(tree, 1), "as")
1724 && validate_name(CHILD(tree, 2), NULL));
1725 else
1726 ok = validate_numnodes(tree, 3, "import_as_name");
1728 return ok;
1732 /* dotted_name: NAME ("." NAME)*
1734 static int
1735 validate_dotted_name(node *tree)
1737 int nch = NCH(tree);
1738 int res = (validate_ntype(tree, dotted_name)
1739 && is_odd(nch)
1740 && validate_name(CHILD(tree, 0), NULL));
1741 int i;
1743 for (i = 1; res && (i < nch); i += 2) {
1744 res = (validate_dot(CHILD(tree, i))
1745 && validate_name(CHILD(tree, i+1), NULL));
1747 return res;
1751 /* dotted_as_name: dotted_name [NAME NAME]
1753 static int
1754 validate_dotted_as_name(node *tree)
1756 int nch = NCH(tree);
1757 int res = validate_ntype(tree, dotted_as_name);
1759 if (res) {
1760 if (nch == 1)
1761 res = validate_dotted_name(CHILD(tree, 0));
1762 else if (nch == 3)
1763 res = (validate_dotted_name(CHILD(tree, 0))
1764 && validate_name(CHILD(tree, 1), "as")
1765 && validate_name(CHILD(tree, 2), NULL));
1766 else {
1767 res = 0;
1768 err_string("illegal number of children for dotted_as_name");
1771 return res;
1775 /* dotted_as_name (',' dotted_as_name)* */
1776 static int
1777 validate_dotted_as_names(node *tree)
1779 int nch = NCH(tree);
1780 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
1781 int i;
1783 for (i = 1; res && (i < nch); i += 2)
1784 res = (validate_comma(CHILD(tree, i))
1785 && validate_dotted_as_name(CHILD(tree, i + 1)));
1786 return (res);
1790 /* import_as_name (',' import_as_name)* [','] */
1791 static int
1792 validate_import_as_names(node *tree)
1794 int nch = NCH(tree);
1795 int res = validate_import_as_name(CHILD(tree, 0));
1796 int i;
1798 for (i = 1; res && (i + 1 < nch); i += 2)
1799 res = (validate_comma(CHILD(tree, i))
1800 && validate_import_as_name(CHILD(tree, i + 1)));
1801 return (res);
1805 /* 'import' dotted_as_names */
1806 static int
1807 validate_import_name(node *tree)
1809 return (validate_ntype(tree, import_name)
1810 && validate_numnodes(tree, 2, "import_name")
1811 && validate_name(CHILD(tree, 0), "import")
1812 && validate_dotted_as_names(CHILD(tree, 1)));
1815 /* Helper function to count the number of leading dots in
1816 * 'from ...module import name'
1818 static int
1819 count_from_dots(node *tree)
1821 int i;
1822 for (i = 0; i < NCH(tree); i++)
1823 if (TYPE(CHILD(tree, i)) != DOT)
1824 break;
1825 return i;
1828 /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
1829 * import_as_names
1831 static int
1832 validate_import_from(node *tree)
1834 int nch = NCH(tree);
1835 int ndots = count_from_dots(tree);
1836 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
1837 int offset = ndots + havename;
1838 int res = validate_ntype(tree, import_from)
1839 && (nch >= 4 + ndots)
1840 && validate_name(CHILD(tree, 0), "from")
1841 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
1842 && validate_name(CHILD(tree, offset + 1), "import");
1844 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
1845 res = ((nch == offset + 5)
1846 && validate_lparen(CHILD(tree, offset + 2))
1847 && validate_import_as_names(CHILD(tree, offset + 3))
1848 && validate_rparen(CHILD(tree, offset + 4)));
1849 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
1850 res = validate_import_as_names(CHILD(tree, offset + 2));
1851 return (res);
1855 /* import_stmt: import_name | import_from */
1856 static int
1857 validate_import_stmt(node *tree)
1859 int nch = NCH(tree);
1860 int res = validate_numnodes(tree, 1, "import_stmt");
1862 if (res) {
1863 int ntype = TYPE(CHILD(tree, 0));
1865 if (ntype == import_name || ntype == import_from)
1866 res = validate_node(CHILD(tree, 0));
1867 else {
1868 res = 0;
1869 err_string("illegal import_stmt child type");
1872 else if (nch == 1) {
1873 res = 0;
1874 PyErr_Format(parser_error,
1875 "Unrecognized child node of import_stmt: %d.",
1876 TYPE(CHILD(tree, 0)));
1878 return (res);
1884 static int
1885 validate_global_stmt(node *tree)
1887 int j;
1888 int nch = NCH(tree);
1889 int res = (validate_ntype(tree, global_stmt)
1890 && is_even(nch) && (nch >= 2));
1892 if (!res && !PyErr_Occurred())
1893 err_string("illegal global statement");
1895 if (res)
1896 res = (validate_name(CHILD(tree, 0), "global")
1897 && validate_ntype(CHILD(tree, 1), NAME));
1898 for (j = 2; res && (j < nch); j += 2)
1899 res = (validate_comma(CHILD(tree, j))
1900 && validate_ntype(CHILD(tree, j + 1), NAME));
1902 return (res);
1906 /* exec_stmt:
1908 * 'exec' expr ['in' test [',' test]]
1910 static int
1911 validate_exec_stmt(node *tree)
1913 int nch = NCH(tree);
1914 int res = (validate_ntype(tree, exec_stmt)
1915 && ((nch == 2) || (nch == 4) || (nch == 6))
1916 && validate_name(CHILD(tree, 0), "exec")
1917 && validate_expr(CHILD(tree, 1)));
1919 if (!res && !PyErr_Occurred())
1920 err_string("illegal exec statement");
1921 if (res && (nch > 2))
1922 res = (validate_name(CHILD(tree, 2), "in")
1923 && validate_test(CHILD(tree, 3)));
1924 if (res && (nch == 6))
1925 res = (validate_comma(CHILD(tree, 4))
1926 && validate_test(CHILD(tree, 5)));
1928 return (res);
1932 /* assert_stmt:
1934 * 'assert' test [',' test]
1936 static int
1937 validate_assert_stmt(node *tree)
1939 int nch = NCH(tree);
1940 int res = (validate_ntype(tree, assert_stmt)
1941 && ((nch == 2) || (nch == 4))
1942 && (validate_name(CHILD(tree, 0), "assert"))
1943 && validate_test(CHILD(tree, 1)));
1945 if (!res && !PyErr_Occurred())
1946 err_string("illegal assert statement");
1947 if (res && (nch > 2))
1948 res = (validate_comma(CHILD(tree, 2))
1949 && validate_test(CHILD(tree, 3)));
1951 return (res);
1955 static int
1956 validate_while(node *tree)
1958 int nch = NCH(tree);
1959 int res = (validate_ntype(tree, while_stmt)
1960 && ((nch == 4) || (nch == 7))
1961 && validate_name(CHILD(tree, 0), "while")
1962 && validate_test(CHILD(tree, 1))
1963 && validate_colon(CHILD(tree, 2))
1964 && validate_suite(CHILD(tree, 3)));
1966 if (res && (nch == 7))
1967 res = (validate_name(CHILD(tree, 4), "else")
1968 && validate_colon(CHILD(tree, 5))
1969 && validate_suite(CHILD(tree, 6)));
1971 return (res);
1975 static int
1976 validate_for(node *tree)
1978 int nch = NCH(tree);
1979 int res = (validate_ntype(tree, for_stmt)
1980 && ((nch == 6) || (nch == 9))
1981 && validate_name(CHILD(tree, 0), "for")
1982 && validate_exprlist(CHILD(tree, 1))
1983 && validate_name(CHILD(tree, 2), "in")
1984 && validate_testlist(CHILD(tree, 3))
1985 && validate_colon(CHILD(tree, 4))
1986 && validate_suite(CHILD(tree, 5)));
1988 if (res && (nch == 9))
1989 res = (validate_name(CHILD(tree, 6), "else")
1990 && validate_colon(CHILD(tree, 7))
1991 && validate_suite(CHILD(tree, 8)));
1993 return (res);
1997 /* try_stmt:
1998 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1999 * | 'try' ':' suite 'finally' ':' suite
2002 static int
2003 validate_try(node *tree)
2005 int nch = NCH(tree);
2006 int pos = 3;
2007 int res = (validate_ntype(tree, try_stmt)
2008 && (nch >= 6) && ((nch % 3) == 0));
2010 if (res)
2011 res = (validate_name(CHILD(tree, 0), "try")
2012 && validate_colon(CHILD(tree, 1))
2013 && validate_suite(CHILD(tree, 2))
2014 && validate_colon(CHILD(tree, nch - 2))
2015 && validate_suite(CHILD(tree, nch - 1)));
2016 else if (!PyErr_Occurred()) {
2017 const char* name = "except";
2018 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
2019 name = STR(CHILD(tree, nch - 3));
2021 PyErr_Format(parser_error,
2022 "Illegal number of children for try/%s node.", name);
2024 /* Skip past except_clause sections: */
2025 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
2026 res = (validate_except_clause(CHILD(tree, pos))
2027 && validate_colon(CHILD(tree, pos + 1))
2028 && validate_suite(CHILD(tree, pos + 2)));
2029 pos += 3;
2031 if (res && (pos < nch)) {
2032 res = validate_ntype(CHILD(tree, pos), NAME);
2033 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
2034 res = (validate_numnodes(tree, 6, "try/finally")
2035 && validate_colon(CHILD(tree, 4))
2036 && validate_suite(CHILD(tree, 5)));
2037 else if (res) {
2038 if (nch == (pos + 3)) {
2039 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
2040 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
2041 if (!res)
2042 err_string("illegal trailing triple in try statement");
2044 else if (nch == (pos + 6)) {
2045 res = (validate_name(CHILD(tree, pos), "except")
2046 && validate_colon(CHILD(tree, pos + 1))
2047 && validate_suite(CHILD(tree, pos + 2))
2048 && validate_name(CHILD(tree, pos + 3), "else"));
2050 else
2051 res = validate_numnodes(tree, pos + 3, "try/except");
2054 return (res);
2058 static int
2059 validate_except_clause(node *tree)
2061 int nch = NCH(tree);
2062 int res = (validate_ntype(tree, except_clause)
2063 && ((nch == 1) || (nch == 2) || (nch == 4))
2064 && validate_name(CHILD(tree, 0), "except"));
2066 if (res && (nch > 1))
2067 res = validate_test(CHILD(tree, 1));
2068 if (res && (nch == 4))
2069 res = (validate_comma(CHILD(tree, 2))
2070 && validate_test(CHILD(tree, 3)));
2072 return (res);
2076 static int
2077 validate_test(node *tree)
2079 int nch = NCH(tree);
2080 int res = validate_ntype(tree, test) && is_odd(nch);
2082 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
2083 res = ((nch == 1)
2084 && validate_lambdef(CHILD(tree, 0)));
2085 else if (res) {
2086 res = validate_or_test(CHILD(tree, 0));
2087 res = (res && (nch == 1 || (nch == 5 &&
2088 validate_name(CHILD(tree, 1), "if") &&
2089 validate_or_test(CHILD(tree, 2)) &&
2090 validate_name(CHILD(tree, 3), "else") &&
2091 validate_test(CHILD(tree, 4)))));
2093 return (res);
2096 static int
2097 validate_old_test(node *tree)
2099 int nch = NCH(tree);
2100 int res = validate_ntype(tree, old_test) && (nch == 1);
2102 if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
2103 res = (validate_old_lambdef(CHILD(tree, 0)));
2104 else if (res) {
2105 res = (validate_or_test(CHILD(tree, 0)));
2107 return (res);
2110 static int
2111 validate_or_test(node *tree)
2113 int nch = NCH(tree);
2114 int res = validate_ntype(tree, or_test) && is_odd(nch);
2116 if (res) {
2117 int pos;
2118 res = validate_and_test(CHILD(tree, 0));
2119 for (pos = 1; res && (pos < nch); pos += 2)
2120 res = (validate_name(CHILD(tree, pos), "or")
2121 && validate_and_test(CHILD(tree, pos + 1)));
2123 return (res);
2127 static int
2128 validate_and_test(node *tree)
2130 int pos;
2131 int nch = NCH(tree);
2132 int res = (validate_ntype(tree, and_test)
2133 && is_odd(nch)
2134 && validate_not_test(CHILD(tree, 0)));
2136 for (pos = 1; res && (pos < nch); pos += 2)
2137 res = (validate_name(CHILD(tree, pos), "and")
2138 && validate_not_test(CHILD(tree, 0)));
2140 return (res);
2144 static int
2145 validate_not_test(node *tree)
2147 int nch = NCH(tree);
2148 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
2150 if (res) {
2151 if (nch == 2)
2152 res = (validate_name(CHILD(tree, 0), "not")
2153 && validate_not_test(CHILD(tree, 1)));
2154 else if (nch == 1)
2155 res = validate_comparison(CHILD(tree, 0));
2157 return (res);
2161 static int
2162 validate_comparison(node *tree)
2164 int pos;
2165 int nch = NCH(tree);
2166 int res = (validate_ntype(tree, comparison)
2167 && is_odd(nch)
2168 && validate_expr(CHILD(tree, 0)));
2170 for (pos = 1; res && (pos < nch); pos += 2)
2171 res = (validate_comp_op(CHILD(tree, pos))
2172 && validate_expr(CHILD(tree, pos + 1)));
2174 return (res);
2178 static int
2179 validate_comp_op(node *tree)
2181 int res = 0;
2182 int nch = NCH(tree);
2184 if (!validate_ntype(tree, comp_op))
2185 return (0);
2186 if (nch == 1) {
2188 * Only child will be a terminal with a well-defined symbolic name
2189 * or a NAME with a string of either 'is' or 'in'
2191 tree = CHILD(tree, 0);
2192 switch (TYPE(tree)) {
2193 case LESS:
2194 case GREATER:
2195 case EQEQUAL:
2196 case EQUAL:
2197 case LESSEQUAL:
2198 case GREATEREQUAL:
2199 case NOTEQUAL:
2200 res = 1;
2201 break;
2202 case NAME:
2203 res = ((strcmp(STR(tree), "in") == 0)
2204 || (strcmp(STR(tree), "is") == 0));
2205 if (!res) {
2206 PyErr_Format(parser_error,
2207 "illegal operator '%s'", STR(tree));
2209 break;
2210 default:
2211 err_string("illegal comparison operator type");
2212 break;
2215 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
2216 res = (validate_ntype(CHILD(tree, 0), NAME)
2217 && validate_ntype(CHILD(tree, 1), NAME)
2218 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
2219 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
2220 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
2221 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
2222 if (!res && !PyErr_Occurred())
2223 err_string("unknown comparison operator");
2225 return (res);
2229 static int
2230 validate_expr(node *tree)
2232 int j;
2233 int nch = NCH(tree);
2234 int res = (validate_ntype(tree, expr)
2235 && is_odd(nch)
2236 && validate_xor_expr(CHILD(tree, 0)));
2238 for (j = 2; res && (j < nch); j += 2)
2239 res = (validate_xor_expr(CHILD(tree, j))
2240 && validate_vbar(CHILD(tree, j - 1)));
2242 return (res);
2246 static int
2247 validate_xor_expr(node *tree)
2249 int j;
2250 int nch = NCH(tree);
2251 int res = (validate_ntype(tree, xor_expr)
2252 && is_odd(nch)
2253 && validate_and_expr(CHILD(tree, 0)));
2255 for (j = 2; res && (j < nch); j += 2)
2256 res = (validate_circumflex(CHILD(tree, j - 1))
2257 && validate_and_expr(CHILD(tree, j)));
2259 return (res);
2263 static int
2264 validate_and_expr(node *tree)
2266 int pos;
2267 int nch = NCH(tree);
2268 int res = (validate_ntype(tree, and_expr)
2269 && is_odd(nch)
2270 && validate_shift_expr(CHILD(tree, 0)));
2272 for (pos = 1; res && (pos < nch); pos += 2)
2273 res = (validate_ampersand(CHILD(tree, pos))
2274 && validate_shift_expr(CHILD(tree, pos + 1)));
2276 return (res);
2280 static int
2281 validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
2283 int pos = 1;
2284 int nch = NCH(tree);
2285 int res = (is_odd(nch)
2286 && (*termvalid)(CHILD(tree, 0)));
2288 for ( ; res && (pos < nch); pos += 2) {
2289 if (TYPE(CHILD(tree, pos)) != op1)
2290 res = validate_ntype(CHILD(tree, pos), op2);
2291 if (res)
2292 res = (*termvalid)(CHILD(tree, pos + 1));
2294 return (res);
2298 static int
2299 validate_shift_expr(node *tree)
2301 return (validate_ntype(tree, shift_expr)
2302 && validate_chain_two_ops(tree, validate_arith_expr,
2303 LEFTSHIFT, RIGHTSHIFT));
2307 static int
2308 validate_arith_expr(node *tree)
2310 return (validate_ntype(tree, arith_expr)
2311 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
2315 static int
2316 validate_term(node *tree)
2318 int pos = 1;
2319 int nch = NCH(tree);
2320 int res = (validate_ntype(tree, term)
2321 && is_odd(nch)
2322 && validate_factor(CHILD(tree, 0)));
2324 for ( ; res && (pos < nch); pos += 2)
2325 res = (((TYPE(CHILD(tree, pos)) == STAR)
2326 || (TYPE(CHILD(tree, pos)) == SLASH)
2327 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
2328 || (TYPE(CHILD(tree, pos)) == PERCENT))
2329 && validate_factor(CHILD(tree, pos + 1)));
2331 return (res);
2335 /* factor:
2337 * factor: ('+'|'-'|'~') factor | power
2339 static int
2340 validate_factor(node *tree)
2342 int nch = NCH(tree);
2343 int res = (validate_ntype(tree, factor)
2344 && (((nch == 2)
2345 && ((TYPE(CHILD(tree, 0)) == PLUS)
2346 || (TYPE(CHILD(tree, 0)) == MINUS)
2347 || (TYPE(CHILD(tree, 0)) == TILDE))
2348 && validate_factor(CHILD(tree, 1)))
2349 || ((nch == 1)
2350 && validate_power(CHILD(tree, 0)))));
2351 return (res);
2355 /* power:
2357 * power: atom trailer* ('**' factor)*
2359 static int
2360 validate_power(node *tree)
2362 int pos = 1;
2363 int nch = NCH(tree);
2364 int res = (validate_ntype(tree, power) && (nch >= 1)
2365 && validate_atom(CHILD(tree, 0)));
2367 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
2368 res = validate_trailer(CHILD(tree, pos++));
2369 if (res && (pos < nch)) {
2370 if (!is_even(nch - pos)) {
2371 err_string("illegal number of nodes for 'power'");
2372 return (0);
2374 for ( ; res && (pos < (nch - 1)); pos += 2)
2375 res = (validate_doublestar(CHILD(tree, pos))
2376 && validate_factor(CHILD(tree, pos + 1)));
2378 return (res);
2382 static int
2383 validate_atom(node *tree)
2385 int pos;
2386 int nch = NCH(tree);
2387 int res = validate_ntype(tree, atom);
2389 if (res && nch < 1)
2390 res = validate_numnodes(tree, nch+1, "atom");
2391 if (res) {
2392 switch (TYPE(CHILD(tree, 0))) {
2393 case LPAR:
2394 res = ((nch <= 3)
2395 && (validate_rparen(CHILD(tree, nch - 1))));
2397 if (res && (nch == 3)) {
2398 if (TYPE(CHILD(tree, 1))==yield_expr)
2399 res = validate_yield_expr(CHILD(tree, 1));
2400 else
2401 res = validate_testlist_gexp(CHILD(tree, 1));
2403 break;
2404 case LSQB:
2405 if (nch == 2)
2406 res = validate_ntype(CHILD(tree, 1), RSQB);
2407 else if (nch == 3)
2408 res = (validate_listmaker(CHILD(tree, 1))
2409 && validate_ntype(CHILD(tree, 2), RSQB));
2410 else {
2411 res = 0;
2412 err_string("illegal list display atom");
2414 break;
2415 case LBRACE:
2416 res = ((nch <= 3)
2417 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
2419 if (res && (nch == 3))
2420 res = validate_dictmaker(CHILD(tree, 1));
2421 break;
2422 case BACKQUOTE:
2423 res = ((nch == 3)
2424 && validate_testlist1(CHILD(tree, 1))
2425 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
2426 break;
2427 case NAME:
2428 case NUMBER:
2429 res = (nch == 1);
2430 break;
2431 case STRING:
2432 for (pos = 1; res && (pos < nch); ++pos)
2433 res = validate_ntype(CHILD(tree, pos), STRING);
2434 break;
2435 default:
2436 res = 0;
2437 break;
2440 return (res);
2444 /* listmaker:
2445 * test ( list_for | (',' test)* [','] )
2447 static int
2448 validate_listmaker(node *tree)
2450 int nch = NCH(tree);
2451 int ok = nch;
2453 if (nch == 0)
2454 err_string("missing child nodes of listmaker");
2455 else
2456 ok = validate_test(CHILD(tree, 0));
2459 * list_for | (',' test)* [',']
2461 if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
2462 ok = validate_list_for(CHILD(tree, 1));
2463 else {
2464 /* (',' test)* [','] */
2465 int i = 1;
2466 while (ok && nch - i >= 2) {
2467 ok = (validate_comma(CHILD(tree, i))
2468 && validate_test(CHILD(tree, i+1)));
2469 i += 2;
2471 if (ok && i == nch-1)
2472 ok = validate_comma(CHILD(tree, i));
2473 else if (i != nch) {
2474 ok = 0;
2475 err_string("illegal trailing nodes for listmaker");
2478 return ok;
2481 /* testlist_gexp:
2482 * test ( gen_for | (',' test)* [','] )
2484 static int
2485 validate_testlist_gexp(node *tree)
2487 int nch = NCH(tree);
2488 int ok = nch;
2490 if (nch == 0)
2491 err_string("missing child nodes of testlist_gexp");
2492 else {
2493 ok = validate_test(CHILD(tree, 0));
2497 * gen_for | (',' test)* [',']
2499 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
2500 ok = validate_gen_for(CHILD(tree, 1));
2501 else {
2502 /* (',' test)* [','] */
2503 int i = 1;
2504 while (ok && nch - i >= 2) {
2505 ok = (validate_comma(CHILD(tree, i))
2506 && validate_test(CHILD(tree, i+1)));
2507 i += 2;
2509 if (ok && i == nch-1)
2510 ok = validate_comma(CHILD(tree, i));
2511 else if (i != nch) {
2512 ok = 0;
2513 err_string("illegal trailing nodes for testlist_gexp");
2516 return ok;
2519 /* decorator:
2520 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2522 static int
2523 validate_decorator(node *tree)
2525 int ok;
2526 int nch = NCH(tree);
2527 ok = (validate_ntype(tree, decorator) &&
2528 (nch == 3 || nch == 5 || nch == 6) &&
2529 validate_at(CHILD(tree, 0)) &&
2530 validate_dotted_name(CHILD(tree, 1)) &&
2531 validate_newline(RCHILD(tree, -1)));
2533 if (ok && nch != 3) {
2534 ok = (validate_lparen(CHILD(tree, 2)) &&
2535 validate_rparen(RCHILD(tree, -2)));
2537 if (ok && nch == 6)
2538 ok = validate_arglist(CHILD(tree, 3));
2541 return ok;
2544 /* decorators:
2545 * decorator+
2547 static int
2548 validate_decorators(node *tree)
2550 int i, nch, ok;
2551 nch = NCH(tree);
2552 ok = validate_ntype(tree, decorators) && nch >= 1;
2554 for (i = 0; ok && i < nch; ++i)
2555 ok = validate_decorator(CHILD(tree, i));
2557 return ok;
2560 /* funcdef:
2562 * -6 -5 -4 -3 -2 -1
2563 * [decorators] 'def' NAME parameters ':' suite
2565 static int
2566 validate_funcdef(node *tree)
2568 int nch = NCH(tree);
2569 int ok = (validate_ntype(tree, funcdef)
2570 && ((nch == 5) || (nch == 6))
2571 && validate_name(RCHILD(tree, -5), "def")
2572 && validate_ntype(RCHILD(tree, -4), NAME)
2573 && validate_colon(RCHILD(tree, -2))
2574 && validate_parameters(RCHILD(tree, -3))
2575 && validate_suite(RCHILD(tree, -1)));
2577 if (ok && (nch == 6))
2578 ok = validate_decorators(CHILD(tree, 0));
2580 return ok;
2584 static int
2585 validate_lambdef(node *tree)
2587 int nch = NCH(tree);
2588 int res = (validate_ntype(tree, lambdef)
2589 && ((nch == 3) || (nch == 4))
2590 && validate_name(CHILD(tree, 0), "lambda")
2591 && validate_colon(CHILD(tree, nch - 2))
2592 && validate_test(CHILD(tree, nch - 1)));
2594 if (res && (nch == 4))
2595 res = validate_varargslist(CHILD(tree, 1));
2596 else if (!res && !PyErr_Occurred())
2597 (void) validate_numnodes(tree, 3, "lambdef");
2599 return (res);
2603 static int
2604 validate_old_lambdef(node *tree)
2606 int nch = NCH(tree);
2607 int res = (validate_ntype(tree, old_lambdef)
2608 && ((nch == 3) || (nch == 4))
2609 && validate_name(CHILD(tree, 0), "lambda")
2610 && validate_colon(CHILD(tree, nch - 2))
2611 && validate_test(CHILD(tree, nch - 1)));
2613 if (res && (nch == 4))
2614 res = validate_varargslist(CHILD(tree, 1));
2615 else if (!res && !PyErr_Occurred())
2616 (void) validate_numnodes(tree, 3, "old_lambdef");
2618 return (res);
2622 /* arglist:
2624 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2626 static int
2627 validate_arglist(node *tree)
2629 int nch = NCH(tree);
2630 int i = 0;
2631 int ok = 1;
2633 if (nch <= 0)
2634 /* raise the right error from having an invalid number of children */
2635 return validate_numnodes(tree, nch + 1, "arglist");
2637 if (nch > 1) {
2638 for (i=0; i<nch; i++) {
2639 if (TYPE(CHILD(tree, i)) == argument) {
2640 node *ch = CHILD(tree, i);
2641 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
2642 err_string("need '(', ')' for generator expression");
2643 return 0;
2649 while (ok && nch-i >= 2) {
2650 /* skip leading (argument ',') */
2651 ok = (validate_argument(CHILD(tree, i))
2652 && validate_comma(CHILD(tree, i+1)));
2653 if (ok)
2654 i += 2;
2655 else
2656 PyErr_Clear();
2658 ok = 1;
2659 if (nch-i > 0) {
2661 * argument | '*' test [',' '**' test] | '**' test
2663 int sym = TYPE(CHILD(tree, i));
2665 if (sym == argument) {
2666 ok = validate_argument(CHILD(tree, i));
2667 if (ok && i+1 != nch) {
2668 err_string("illegal arglist specification"
2669 " (extra stuff on end)");
2670 ok = 0;
2673 else if (sym == STAR) {
2674 ok = validate_star(CHILD(tree, i));
2675 if (ok && (nch-i == 2))
2676 ok = validate_test(CHILD(tree, i+1));
2677 else if (ok && (nch-i == 5))
2678 ok = (validate_test(CHILD(tree, i+1))
2679 && validate_comma(CHILD(tree, i+2))
2680 && validate_doublestar(CHILD(tree, i+3))
2681 && validate_test(CHILD(tree, i+4)));
2682 else {
2683 err_string("illegal use of '*' in arglist");
2684 ok = 0;
2687 else if (sym == DOUBLESTAR) {
2688 if (nch-i == 2)
2689 ok = (validate_doublestar(CHILD(tree, i))
2690 && validate_test(CHILD(tree, i+1)));
2691 else {
2692 err_string("illegal use of '**' in arglist");
2693 ok = 0;
2696 else {
2697 err_string("illegal arglist specification");
2698 ok = 0;
2701 return (ok);
2706 /* argument:
2708 * [test '='] test [gen_for]
2710 static int
2711 validate_argument(node *tree)
2713 int nch = NCH(tree);
2714 int res = (validate_ntype(tree, argument)
2715 && ((nch == 1) || (nch == 2) || (nch == 3))
2716 && validate_test(CHILD(tree, 0)));
2718 if (res && (nch == 2))
2719 res = validate_gen_for(CHILD(tree, 1));
2720 else if (res && (nch == 3))
2721 res = (validate_equal(CHILD(tree, 1))
2722 && validate_test(CHILD(tree, 2)));
2724 return (res);
2729 /* trailer:
2731 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2733 static int
2734 validate_trailer(node *tree)
2736 int nch = NCH(tree);
2737 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
2739 if (res) {
2740 switch (TYPE(CHILD(tree, 0))) {
2741 case LPAR:
2742 res = validate_rparen(CHILD(tree, nch - 1));
2743 if (res && (nch == 3))
2744 res = validate_arglist(CHILD(tree, 1));
2745 break;
2746 case LSQB:
2747 res = (validate_numnodes(tree, 3, "trailer")
2748 && validate_subscriptlist(CHILD(tree, 1))
2749 && validate_ntype(CHILD(tree, 2), RSQB));
2750 break;
2751 case DOT:
2752 res = (validate_numnodes(tree, 2, "trailer")
2753 && validate_ntype(CHILD(tree, 1), NAME));
2754 break;
2755 default:
2756 res = 0;
2757 break;
2760 else {
2761 (void) validate_numnodes(tree, 2, "trailer");
2763 return (res);
2767 /* subscriptlist:
2769 * subscript (',' subscript)* [',']
2771 static int
2772 validate_subscriptlist(node *tree)
2774 return (validate_repeating_list(tree, subscriptlist,
2775 validate_subscript, "subscriptlist"));
2779 /* subscript:
2781 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
2783 static int
2784 validate_subscript(node *tree)
2786 int offset = 0;
2787 int nch = NCH(tree);
2788 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
2790 if (!res) {
2791 if (!PyErr_Occurred())
2792 err_string("invalid number of arguments for subscript node");
2793 return (0);
2795 if (TYPE(CHILD(tree, 0)) == DOT)
2796 /* take care of ('.' '.' '.') possibility */
2797 return (validate_numnodes(tree, 3, "subscript")
2798 && validate_dot(CHILD(tree, 0))
2799 && validate_dot(CHILD(tree, 1))
2800 && validate_dot(CHILD(tree, 2)));
2801 if (nch == 1) {
2802 if (TYPE(CHILD(tree, 0)) == test)
2803 res = validate_test(CHILD(tree, 0));
2804 else
2805 res = validate_colon(CHILD(tree, 0));
2806 return (res);
2808 /* Must be [test] ':' [test] [sliceop],
2809 * but at least one of the optional components will
2810 * be present, but we don't know which yet.
2812 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
2813 res = validate_test(CHILD(tree, 0));
2814 offset = 1;
2816 if (res)
2817 res = validate_colon(CHILD(tree, offset));
2818 if (res) {
2819 int rem = nch - ++offset;
2820 if (rem) {
2821 if (TYPE(CHILD(tree, offset)) == test) {
2822 res = validate_test(CHILD(tree, offset));
2823 ++offset;
2824 --rem;
2826 if (res && rem)
2827 res = validate_sliceop(CHILD(tree, offset));
2830 return (res);
2834 static int
2835 validate_sliceop(node *tree)
2837 int nch = NCH(tree);
2838 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
2839 && validate_ntype(tree, sliceop);
2840 if (!res && !PyErr_Occurred()) {
2841 res = validate_numnodes(tree, 1, "sliceop");
2843 if (res)
2844 res = validate_colon(CHILD(tree, 0));
2845 if (res && (nch == 2))
2846 res = validate_test(CHILD(tree, 1));
2848 return (res);
2852 static int
2853 validate_exprlist(node *tree)
2855 return (validate_repeating_list(tree, exprlist,
2856 validate_expr, "exprlist"));
2860 static int
2861 validate_dictmaker(node *tree)
2863 int nch = NCH(tree);
2864 int res = (validate_ntype(tree, dictmaker)
2865 && (nch >= 3)
2866 && validate_test(CHILD(tree, 0))
2867 && validate_colon(CHILD(tree, 1))
2868 && validate_test(CHILD(tree, 2)));
2870 if (res && ((nch % 4) == 0))
2871 res = validate_comma(CHILD(tree, --nch));
2872 else if (res)
2873 res = ((nch % 4) == 3);
2875 if (res && (nch > 3)) {
2876 int pos = 3;
2877 /* ( ',' test ':' test )* */
2878 while (res && (pos < nch)) {
2879 res = (validate_comma(CHILD(tree, pos))
2880 && validate_test(CHILD(tree, pos + 1))
2881 && validate_colon(CHILD(tree, pos + 2))
2882 && validate_test(CHILD(tree, pos + 3)));
2883 pos += 4;
2886 return (res);
2890 static int
2891 validate_eval_input(node *tree)
2893 int pos;
2894 int nch = NCH(tree);
2895 int res = (validate_ntype(tree, eval_input)
2896 && (nch >= 2)
2897 && validate_testlist(CHILD(tree, 0))
2898 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
2900 for (pos = 1; res && (pos < (nch - 1)); ++pos)
2901 res = validate_ntype(CHILD(tree, pos), NEWLINE);
2903 return (res);
2907 static int
2908 validate_node(node *tree)
2910 int nch = 0; /* num. children on current node */
2911 int res = 1; /* result value */
2912 node* next = 0; /* node to process after this one */
2914 while (res && (tree != 0)) {
2915 nch = NCH(tree);
2916 next = 0;
2917 switch (TYPE(tree)) {
2919 * Definition nodes.
2921 case funcdef:
2922 res = validate_funcdef(tree);
2923 break;
2924 case classdef:
2925 res = validate_class(tree);
2926 break;
2928 * "Trivial" parse tree nodes.
2929 * (Why did I call these trivial?)
2931 case stmt:
2932 res = validate_stmt(tree);
2933 break;
2934 case small_stmt:
2936 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2937 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2939 res = validate_small_stmt(tree);
2940 break;
2941 case flow_stmt:
2942 res = (validate_numnodes(tree, 1, "flow_stmt")
2943 && ((TYPE(CHILD(tree, 0)) == break_stmt)
2944 || (TYPE(CHILD(tree, 0)) == continue_stmt)
2945 || (TYPE(CHILD(tree, 0)) == yield_stmt)
2946 || (TYPE(CHILD(tree, 0)) == return_stmt)
2947 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
2948 if (res)
2949 next = CHILD(tree, 0);
2950 else if (nch == 1)
2951 err_string("illegal flow_stmt type");
2952 break;
2953 case yield_stmt:
2954 res = validate_yield_stmt(tree);
2955 break;
2957 * Compound statements.
2959 case simple_stmt:
2960 res = validate_simple_stmt(tree);
2961 break;
2962 case compound_stmt:
2963 res = validate_compound_stmt(tree);
2964 break;
2966 * Fundamental statements.
2968 case expr_stmt:
2969 res = validate_expr_stmt(tree);
2970 break;
2971 case print_stmt:
2972 res = validate_print_stmt(tree);
2973 break;
2974 case del_stmt:
2975 res = validate_del_stmt(tree);
2976 break;
2977 case pass_stmt:
2978 res = (validate_numnodes(tree, 1, "pass")
2979 && validate_name(CHILD(tree, 0), "pass"));
2980 break;
2981 case break_stmt:
2982 res = (validate_numnodes(tree, 1, "break")
2983 && validate_name(CHILD(tree, 0), "break"));
2984 break;
2985 case continue_stmt:
2986 res = (validate_numnodes(tree, 1, "continue")
2987 && validate_name(CHILD(tree, 0), "continue"));
2988 break;
2989 case return_stmt:
2990 res = validate_return_stmt(tree);
2991 break;
2992 case raise_stmt:
2993 res = validate_raise_stmt(tree);
2994 break;
2995 case import_stmt:
2996 res = validate_import_stmt(tree);
2997 break;
2998 case import_name:
2999 res = validate_import_name(tree);
3000 break;
3001 case import_from:
3002 res = validate_import_from(tree);
3003 break;
3004 case global_stmt:
3005 res = validate_global_stmt(tree);
3006 break;
3007 case exec_stmt:
3008 res = validate_exec_stmt(tree);
3009 break;
3010 case assert_stmt:
3011 res = validate_assert_stmt(tree);
3012 break;
3013 case if_stmt:
3014 res = validate_if(tree);
3015 break;
3016 case while_stmt:
3017 res = validate_while(tree);
3018 break;
3019 case for_stmt:
3020 res = validate_for(tree);
3021 break;
3022 case try_stmt:
3023 res = validate_try(tree);
3024 break;
3025 case suite:
3026 res = validate_suite(tree);
3027 break;
3029 * Expression nodes.
3031 case testlist:
3032 res = validate_testlist(tree);
3033 break;
3034 case yield_expr:
3035 res = validate_yield_expr(tree);
3036 break;
3037 case testlist1:
3038 res = validate_testlist1(tree);
3039 break;
3040 case test:
3041 res = validate_test(tree);
3042 break;
3043 case and_test:
3044 res = validate_and_test(tree);
3045 break;
3046 case not_test:
3047 res = validate_not_test(tree);
3048 break;
3049 case comparison:
3050 res = validate_comparison(tree);
3051 break;
3052 case exprlist:
3053 res = validate_exprlist(tree);
3054 break;
3055 case comp_op:
3056 res = validate_comp_op(tree);
3057 break;
3058 case expr:
3059 res = validate_expr(tree);
3060 break;
3061 case xor_expr:
3062 res = validate_xor_expr(tree);
3063 break;
3064 case and_expr:
3065 res = validate_and_expr(tree);
3066 break;
3067 case shift_expr:
3068 res = validate_shift_expr(tree);
3069 break;
3070 case arith_expr:
3071 res = validate_arith_expr(tree);
3072 break;
3073 case term:
3074 res = validate_term(tree);
3075 break;
3076 case factor:
3077 res = validate_factor(tree);
3078 break;
3079 case power:
3080 res = validate_power(tree);
3081 break;
3082 case atom:
3083 res = validate_atom(tree);
3084 break;
3086 default:
3087 /* Hopefully never reached! */
3088 err_string("unrecognized node type");
3089 res = 0;
3090 break;
3092 tree = next;
3094 return (res);
3098 static int
3099 validate_expr_tree(node *tree)
3101 int res = validate_eval_input(tree);
3103 if (!res && !PyErr_Occurred())
3104 err_string("could not validate expression tuple");
3106 return (res);
3110 /* file_input:
3111 * (NEWLINE | stmt)* ENDMARKER
3113 static int
3114 validate_file_input(node *tree)
3116 int j;
3117 int nch = NCH(tree) - 1;
3118 int res = ((nch >= 0)
3119 && validate_ntype(CHILD(tree, nch), ENDMARKER));
3121 for (j = 0; res && (j < nch); ++j) {
3122 if (TYPE(CHILD(tree, j)) == stmt)
3123 res = validate_stmt(CHILD(tree, j));
3124 else
3125 res = validate_newline(CHILD(tree, j));
3127 /* This stays in to prevent any internal failures from getting to the
3128 * user. Hopefully, this won't be needed. If a user reports getting
3129 * this, we have some debugging to do.
3131 if (!res && !PyErr_Occurred())
3132 err_string("VALIDATION FAILURE: report this to the maintainer!");
3134 return (res);
3137 static int
3138 validate_encoding_decl(node *tree)
3140 int nch = NCH(tree);
3141 int res = ((nch == 1)
3142 && validate_file_input(CHILD(tree, 0)));
3144 if (!res && !PyErr_Occurred())
3145 err_string("Error Parsing encoding_decl");
3147 return res;
3150 static PyObject*
3151 pickle_constructor = NULL;
3154 static PyObject*
3155 parser__pickler(PyObject *self, PyObject *args)
3157 NOTE(ARGUNUSED(self))
3158 PyObject *result = NULL;
3159 PyObject *st = NULL;
3160 PyObject *empty_dict = NULL;
3162 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
3163 PyObject *newargs;
3164 PyObject *tuple;
3166 if ((empty_dict = PyDict_New()) == NULL)
3167 goto finally;
3168 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
3169 goto finally;
3170 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
3171 if (tuple != NULL) {
3172 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
3173 Py_DECREF(tuple);
3175 Py_DECREF(empty_dict);
3176 Py_DECREF(newargs);
3178 finally:
3179 Py_XDECREF(empty_dict);
3181 return (result);
3185 /* Functions exported by this module. Most of this should probably
3186 * be converted into an ST object with methods, but that is better
3187 * done directly in Python, allowing subclasses to be created directly.
3188 * We'd really have to write a wrapper around it all anyway to allow
3189 * inheritance.
3191 static PyMethodDef parser_functions[] = {
3192 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
3193 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3194 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
3195 PyDoc_STR("Creates a list-tree representation of an ST.")},
3196 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
3197 PyDoc_STR("Compiles an ST object into a code object.")},
3198 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
3199 PyDoc_STR("Compiles an ST object into a code object.")},
3200 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
3201 PyDoc_STR("Creates an ST object from an expression.")},
3202 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
3203 PyDoc_STR("Determines if an ST object was created from an expression.")},
3204 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
3205 PyDoc_STR("Determines if an ST object was created from a suite.")},
3206 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
3207 PyDoc_STR("Creates an ST object from a suite.")},
3208 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
3209 PyDoc_STR("Creates an ST object from a tree representation.")},
3210 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
3211 PyDoc_STR("Creates an ST object from a tree representation.")},
3212 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
3213 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3214 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
3215 PyDoc_STR("Creates a list-tree representation of an ST.")},
3216 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
3217 PyDoc_STR("Creates an ST object from a tree representation.")},
3218 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
3219 PyDoc_STR("Creates an ST object from a tree representation.")},
3221 /* private stuff: support pickle module */
3222 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
3223 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
3225 {NULL, NULL, 0, NULL}
3229 PyMODINIT_FUNC initparser(void); /* supply a prototype */
3231 PyMODINIT_FUNC
3232 initparser(void)
3234 PyObject *module, *copyreg;
3236 PyST_Type.ob_type = &PyType_Type;
3237 module = Py_InitModule("parser", parser_functions);
3238 if (module == NULL)
3239 return;
3241 if (parser_error == 0)
3242 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
3244 if (parser_error == 0)
3245 /* caller will check PyErr_Occurred() */
3246 return;
3247 /* CAUTION: The code next used to skip bumping the refcount on
3248 * parser_error. That's a disaster if initparser() gets called more
3249 * than once. By incref'ing, we ensure that each module dict that
3250 * gets created owns its reference to the shared parser_error object,
3251 * and the file static parser_error vrbl owns a reference too.
3253 Py_INCREF(parser_error);
3254 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
3255 return;
3257 Py_INCREF(&PyST_Type);
3258 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
3259 Py_INCREF(&PyST_Type);
3260 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
3262 PyModule_AddStringConstant(module, "__copyright__",
3263 parser_copyright_string);
3264 PyModule_AddStringConstant(module, "__doc__",
3265 parser_doc_string);
3266 PyModule_AddStringConstant(module, "__version__",
3267 parser_version_string);
3269 /* Register to support pickling.
3270 * If this fails, the import of this module will fail because an
3271 * exception will be raised here; should we clear the exception?
3273 copyreg = PyImport_ImportModule("copy_reg");
3274 if (copyreg != NULL) {
3275 PyObject *func, *pickler;
3277 func = PyObject_GetAttrString(copyreg, "pickle");
3278 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3279 pickler = PyObject_GetAttrString(module, "_pickler");
3280 Py_XINCREF(pickle_constructor);
3281 if ((func != NULL) && (pickle_constructor != NULL)
3282 && (pickler != NULL)) {
3283 PyObject *res;
3285 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
3286 pickle_constructor, NULL);
3287 Py_XDECREF(res);
3289 Py_XDECREF(func);
3290 Py_XDECREF(pickle_constructor);
3291 Py_XDECREF(pickler);
3292 Py_DECREF(copyreg);