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
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() */
46 /* String constants used to initialize module attributes.
49 static char parser_copyright_string
[] =
50 "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
51 University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52 Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53 Centrum, Amsterdam, The Netherlands.";
56 PyDoc_STRVAR(parser_doc_string
,
57 "This is an interface to Python's internal parser.");
59 static char parser_version_string
[] = "0.5";
62 typedef PyObject
* (*SeqMaker
) (int length
);
63 typedef int (*SeqInserter
) (PyObject
* sequence
,
67 /* The function below is copyrighted by Stichting Mathematisch Centrum. The
68 * original copyright statement is included below, and continues to apply
69 * in full to the function immediately following. All other material is
70 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
71 * Institute and State University. Changes were made to comply with the
72 * new naming conventions. Added arguments to provide support for creating
73 * lists as well as tuples, and optionally including the line numbers.
78 node2tuple(node
*n
, /* node to convert */
79 SeqMaker mkseq
, /* create sequence */
80 SeqInserter addelem
, /* func. to add elem. in seq. */
81 int lineno
) /* include line numbers? */
87 if (ISNONTERMINAL(TYPE(n
))) {
92 v
= mkseq(1 + NCH(n
) + (TYPE(n
) == encoding_decl
));
95 w
= PyInt_FromLong(TYPE(n
));
98 return ((PyObject
*) NULL
);
100 (void) addelem(v
, 0, w
);
101 for (i
= 0; i
< NCH(n
); i
++) {
102 w
= node2tuple(CHILD(n
, i
), mkseq
, addelem
, lineno
);
105 return ((PyObject
*) NULL
);
107 (void) addelem(v
, i
+1, w
);
110 if (TYPE(n
) == encoding_decl
)
111 (void) addelem(v
, i
+1, PyString_FromString(STR(n
)));
114 else if (ISTERMINAL(TYPE(n
))) {
115 PyObject
*result
= mkseq(2 + lineno
);
116 if (result
!= NULL
) {
117 (void) addelem(result
, 0, PyInt_FromLong(TYPE(n
)));
118 (void) addelem(result
, 1, PyString_FromString(STR(n
)));
120 (void) addelem(result
, 2, PyInt_FromLong(n
->n_lineno
));
125 PyErr_SetString(PyExc_SystemError
,
126 "unrecognized parse tree node type");
127 return ((PyObject
*) NULL
);
131 * End of material copyrighted by Stichting Mathematisch Centrum.
136 /* There are two types of intermediate objects we're interested in:
137 * 'eval' and 'exec' types. These constants can be used in the st_type
138 * field of the object type to identify which any given object represents.
139 * These should probably go in an external header to allow other extensions
140 * to use them, but then, we really should be using C++ too. ;-)
147 /* These are the internal objects and definitions required to implement the
148 * ST type. Most of the internal names are more reminiscent of the 'old'
149 * naming style, but the code uses the new naming convention.
157 PyObject_HEAD
/* standard object header */
158 node
* st_node
; /* the node* returned by the parser */
159 int st_type
; /* EXPR or SUITE ? */
163 static void parser_free(PyST_Object
*st
);
164 static int parser_compare(PyST_Object
*left
, PyST_Object
*right
);
165 static PyObject
*parser_getattr(PyObject
*self
, char *name
);
169 PyTypeObject PyST_Type
= {
170 PyObject_HEAD_INIT(NULL
)
172 "parser.st", /* tp_name */
173 (int) sizeof(PyST_Object
), /* tp_basicsize */
175 (destructor
)parser_free
, /* tp_dealloc */
177 parser_getattr
, /* tp_getattr */
179 (cmpfunc
)parser_compare
, /* tp_compare */
181 0, /* tp_as_number */
182 0, /* tp_as_sequence */
183 0, /* tp_as_mapping */
190 /* Functions to access object as input/output buffer */
191 0, /* tp_as_buffer */
193 Py_TPFLAGS_DEFAULT
, /* tp_flags */
196 "Intermediate representation of a Python parse tree."
201 parser_compare_nodes(node
*left
, node
*right
)
205 if (TYPE(left
) < TYPE(right
))
208 if (TYPE(right
) < TYPE(left
))
211 if (ISTERMINAL(TYPE(left
)))
212 return (strcmp(STR(left
), STR(right
)));
214 if (NCH(left
) < NCH(right
))
217 if (NCH(right
) < NCH(left
))
220 for (j
= 0; j
< NCH(left
); ++j
) {
221 int v
= parser_compare_nodes(CHILD(left
, j
), CHILD(right
, j
));
230 /* int parser_compare(PyST_Object* left, PyST_Object* right)
232 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
233 * This really just wraps a call to parser_compare_nodes() with some easy
234 * checks and protection code.
238 parser_compare(PyST_Object
*left
, PyST_Object
*right
)
243 if ((left
== 0) || (right
== 0))
246 return (parser_compare_nodes(left
->st_node
, right
->st_node
));
250 /* parser_newstobject(node* st)
252 * Allocates a new Python object representing an ST. This is simply the
253 * 'wrapper' object that holds a node* and allows it to be passed around in
258 parser_newstobject(node
*st
, int type
)
260 PyST_Object
* o
= PyObject_New(PyST_Object
, &PyST_Type
);
269 return ((PyObject
*)o
);
273 /* void parser_free(PyST_Object* st)
275 * This is called by a del statement that reduces the reference count to 0.
279 parser_free(PyST_Object
*st
)
281 PyNode_Free(st
->st_node
);
286 /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
288 * This provides conversion from a node* to a tuple object that can be
289 * returned to the Python-level caller. The ST object is not modified.
293 parser_st2tuple(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
295 PyObject
*line_option
= 0;
299 static char *keywords
[] = {"ast", "line_info", NULL
};
302 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|O:st2tuple", keywords
,
303 &PyST_Type
, &self
, &line_option
);
306 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|O:totuple", &keywords
[1],
310 if (line_option
!= NULL
) {
311 lineno
= (PyObject_IsTrue(line_option
) != 0) ? 1 : 0;
314 * Convert ST into a tuple representation. Use Guido's function,
315 * since it's known to work already.
317 res
= node2tuple(((PyST_Object
*)self
)->st_node
,
318 PyTuple_New
, PyTuple_SetItem
, lineno
);
324 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
326 * This provides conversion from a node* to a list object that can be
327 * returned to the Python-level caller. The ST object is not modified.
331 parser_st2list(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
333 PyObject
*line_option
= 0;
337 static char *keywords
[] = {"ast", "line_info", NULL
};
340 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|O:st2list", keywords
,
341 &PyST_Type
, &self
, &line_option
);
343 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|O:tolist", &keywords
[1],
347 if (line_option
!= 0) {
348 lineno
= PyObject_IsTrue(line_option
) ? 1 : 0;
351 * Convert ST into a tuple representation. Use Guido's function,
352 * since it's known to work already.
354 res
= node2tuple(self
->st_node
,
355 PyList_New
, PyList_SetItem
, lineno
);
361 /* parser_compilest(PyObject* self, PyObject* args)
363 * This function creates code objects from the parse tree represented by
364 * the passed-in data object. An optional file name is passed in as well.
368 parser_compilest(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
371 char* str
= "<syntax-tree>";
374 static char *keywords
[] = {"ast", "filename", NULL
};
377 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|s:compilest", keywords
,
378 &PyST_Type
, &self
, &str
);
380 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|s:compile", &keywords
[1],
384 res
= (PyObject
*)PyNode_Compile(self
->st_node
, str
);
390 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
391 * PyObject* parser_issuite(PyObject* self, PyObject* args)
393 * Checks the passed-in ST object to determine if it is an expression or
394 * a statement suite, respectively. The return is a Python truth value.
398 parser_isexpr(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
403 static char *keywords
[] = {"ast", NULL
};
406 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:isexpr", keywords
,
409 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":isexpr", &keywords
[1]);
412 /* Check to see if the ST represents an expression or not. */
413 res
= (self
->st_type
== PyST_EXPR
) ? Py_True
: Py_False
;
421 parser_issuite(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
426 static char *keywords
[] = {"ast", NULL
};
429 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:issuite", keywords
,
432 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":issuite", &keywords
[1]);
435 /* Check to see if the ST represents an expression or not. */
436 res
= (self
->st_type
== PyST_EXPR
) ? Py_False
: Py_True
;
443 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
447 {"compile", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
448 PyDoc_STR("Compile this ST object into a code object.")},
449 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
450 PyDoc_STR("Determines if this ST object was created from an expression.")},
451 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
452 PyDoc_STR("Determines if this ST object was created from a suite.")},
453 {"tolist", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
454 PyDoc_STR("Creates a list-tree representation of this ST.")},
455 {"totuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
456 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
458 {NULL
, NULL
, 0, NULL
}
463 parser_getattr(PyObject
*self
, char *name
)
465 return (Py_FindMethod(parser_methods
, self
, name
));
469 /* err_string(char* message)
471 * Sets the error string for an exception of type ParserError.
475 err_string(char *message
)
477 PyErr_SetString(parser_error
, message
);
481 /* PyObject* parser_do_parse(PyObject* args, int type)
483 * Internal function to actually execute the parse and return the result if
484 * successful or set an exception if not.
488 parser_do_parse(PyObject
*args
, PyObject
*kw
, char *argspec
, int type
)
493 static char *keywords
[] = {"source", NULL
};
495 if (PyArg_ParseTupleAndKeywords(args
, kw
, argspec
, keywords
, &string
)) {
496 node
* n
= PyParser_SimpleParseString(string
,
498 ? eval_input
: file_input
);
501 res
= parser_newstobject(n
, type
);
507 /* PyObject* parser_expr(PyObject* self, PyObject* args)
508 * PyObject* parser_suite(PyObject* self, PyObject* args)
510 * External interfaces to the parser itself. Which is called determines if
511 * the parser attempts to recognize an expression ('eval' form) or statement
512 * suite ('exec' form). The real work is done by parser_do_parse() above.
516 parser_expr(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
518 NOTE(ARGUNUSED(self
))
519 return (parser_do_parse(args
, kw
, "s:expr", PyST_EXPR
));
524 parser_suite(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
526 NOTE(ARGUNUSED(self
))
527 return (parser_do_parse(args
, kw
, "s:suite", PyST_SUITE
));
532 /* This is the messy part of the code. Conversion from a tuple to an ST
533 * object requires that the input tuple be valid without having to rely on
534 * catching an exception from the compiler. This is done to allow the
535 * compiler itself to remain fast, since most of its input will come from
536 * the parser directly, and therefore be known to be syntactically correct.
537 * This validation is done to ensure that we don't core dump the compile
538 * phase, returning an exception instead.
540 * Two aspects can be broken out in this code: creating a node tree from
541 * the tuple passed in, and verifying that it is indeed valid. It may be
542 * advantageous to expand the number of ST types to include funcdefs and
543 * lambdadefs to take advantage of the optimizer, recognizing those STs
544 * here. They are not necessary, and not quite as useful in a raw form.
545 * For now, let's get expressions and suites working reliably.
549 static node
* build_node_tree(PyObject
*tuple
);
550 static int validate_expr_tree(node
*tree
);
551 static int validate_file_input(node
*tree
);
552 static int validate_encoding_decl(node
*tree
);
554 /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
556 * This is the public function, called from the Python code. It receives a
557 * single tuple object from the caller, and creates an ST object if the
558 * tuple can be validated. It does this by checking the first code of the
559 * tuple, and, if acceptable, builds the internal representation. If this
560 * step succeeds, the internal representation is validated as fully as
561 * possible with the various validate_*() routines defined below.
563 * This function must be changed if support is to be added for PyST_FRAGMENT
568 parser_tuple2st(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
570 NOTE(ARGUNUSED(self
))
575 static char *keywords
[] = {"sequence", NULL
};
577 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "O:sequence2st", keywords
,
580 if (!PySequence_Check(tuple
)) {
581 PyErr_SetString(PyExc_ValueError
,
582 "sequence2st() requires a single sequence argument");
586 * Convert the tree to the internal form before checking it.
588 tree
= build_node_tree(tuple
);
590 int start_sym
= TYPE(tree
);
591 if (start_sym
== eval_input
) {
592 /* Might be an eval form. */
593 if (validate_expr_tree(tree
))
594 st
= parser_newstobject(tree
, PyST_EXPR
);
598 else if (start_sym
== file_input
) {
599 /* This looks like an exec form so far. */
600 if (validate_file_input(tree
))
601 st
= parser_newstobject(tree
, PyST_SUITE
);
605 else if (start_sym
== encoding_decl
) {
606 /* This looks like an encoding_decl so far. */
607 if (validate_encoding_decl(tree
))
608 st
= parser_newstobject(tree
, PyST_SUITE
);
613 /* This is a fragment, at best. */
615 err_string("parse tree does not use a valid start symbol");
618 /* Make sure we throw an exception on all errors. We should never
619 * get this, but we'd do well to be sure something is done.
621 if (st
== NULL
&& !PyErr_Occurred())
622 err_string("unspecified ST error occurred");
628 /* node* build_node_children()
630 * Iterate across the children of the current non-terminal node and build
631 * their structures. If successful, return the root of this portion of
632 * the tree, otherwise, 0. Any required exception will be specified already,
633 * and no memory will have been deallocated.
637 build_node_children(PyObject
*tuple
, node
*root
, int *line_num
)
639 int len
= PyObject_Size(tuple
);
642 for (i
= 1; i
< len
; ++i
) {
643 /* elem must always be a sequence, however simple */
644 PyObject
* elem
= PySequence_GetItem(tuple
, i
);
645 int ok
= elem
!= NULL
;
650 ok
= PySequence_Check(elem
);
652 PyObject
*temp
= PySequence_GetItem(elem
, 0);
656 ok
= PyInt_Check(temp
);
658 type
= PyInt_AS_LONG(temp
);
663 PyErr_SetObject(parser_error
,
664 Py_BuildValue("os", elem
,
665 "Illegal node construct."));
669 if (ISTERMINAL(type
)) {
670 int len
= PyObject_Size(elem
);
673 if ((len
!= 2) && (len
!= 3)) {
674 err_string("terminal nodes must have 2 or 3 entries");
677 temp
= PySequence_GetItem(elem
, 1);
680 if (!PyString_Check(temp
)) {
681 PyErr_Format(parser_error
,
682 "second item in terminal node must be a string,"
684 ((PyTypeObject
*)PyObject_Type(temp
))->tp_name
);
689 PyObject
*o
= PySequence_GetItem(elem
, 2);
692 *line_num
= PyInt_AS_LONG(o
);
694 PyErr_Format(parser_error
,
695 "third item in terminal node must be an"
696 " integer, found %s",
697 ((PyTypeObject
*)PyObject_Type(temp
))->tp_name
);
705 len
= PyString_GET_SIZE(temp
) + 1;
706 strn
= (char *)PyMem_MALLOC(len
);
708 (void) memcpy(strn
, PyString_AS_STRING(temp
), len
);
711 else if (!ISNONTERMINAL(type
)) {
713 * It has to be one or the other; this is an error.
714 * Throw an exception.
716 PyErr_SetObject(parser_error
,
717 Py_BuildValue("os", elem
, "unknown node type."));
721 err
= PyNode_AddChild(root
, type
, strn
, *line_num
);
722 if (err
== E_NOMEM
) {
724 return (node
*) PyErr_NoMemory();
726 if (err
== E_OVERFLOW
) {
728 PyErr_SetString(PyExc_ValueError
,
729 "unsupported number of child nodes");
733 if (ISNONTERMINAL(type
)) {
734 node
* new_child
= CHILD(root
, i
- 1);
736 if (new_child
!= build_node_children(elem
, new_child
, line_num
)) {
741 else if (type
== NEWLINE
) { /* It's true: we increment the */
742 ++(*line_num
); /* line number *after* the newline! */
751 build_node_tree(PyObject
*tuple
)
754 PyObject
*temp
= PySequence_GetItem(tuple
, 0);
758 num
= PyInt_AsLong(temp
);
760 if (ISTERMINAL(num
)) {
762 * The tuple is simple, but it doesn't start with a start symbol.
763 * Throw an exception now and be done with it.
765 tuple
= Py_BuildValue("os", tuple
,
766 "Illegal syntax-tree; cannot start with terminal symbol.");
767 PyErr_SetObject(parser_error
, tuple
);
769 else if (ISNONTERMINAL(num
)) {
771 * Not efficient, but that can be handled later.
774 PyObject
*encoding
= NULL
;
776 if (num
== encoding_decl
) {
777 encoding
= PySequence_GetItem(tuple
, 2);
778 /* tuple isn't borrowed anymore here, need to DECREF */
779 tuple
= PySequence_GetSlice(tuple
, 0, 2);
781 res
= PyNode_New(num
);
783 if (res
!= build_node_children(tuple
, res
, &line_num
)) {
787 if (res
&& encoding
) {
789 len
= PyString_GET_SIZE(encoding
) + 1;
790 res
->n_str
= (char *)PyMem_MALLOC(len
);
791 if (res
->n_str
!= NULL
)
792 (void) memcpy(res
->n_str
, PyString_AS_STRING(encoding
), len
);
799 /* The tuple is illegal -- if the number is neither TERMINAL nor
800 * NONTERMINAL, we can't use it. Not sure the implementation
801 * allows this condition, but the API doesn't preclude it.
803 PyErr_SetObject(parser_error
,
804 Py_BuildValue("os", tuple
,
805 "Illegal component tuple."));
812 * Validation routines used within the validation section:
814 static int validate_terminal(node
*terminal
, int type
, char *string
);
816 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
817 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
818 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
819 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
820 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
821 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
822 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
823 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
824 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
825 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
826 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
827 #define validate_star(ch) validate_terminal(ch, STAR, "*")
828 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
829 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
830 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
831 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
833 #define VALIDATER(n) static int validate_##n(node *tree)
835 VALIDATER(node
); VALIDATER(small_stmt
);
836 VALIDATER(class); VALIDATER(node
);
837 VALIDATER(parameters
); VALIDATER(suite
);
838 VALIDATER(testlist
); VALIDATER(varargslist
);
839 VALIDATER(fpdef
); VALIDATER(fplist
);
840 VALIDATER(stmt
); VALIDATER(simple_stmt
);
841 VALIDATER(expr_stmt
); VALIDATER(power
);
842 VALIDATER(print_stmt
); VALIDATER(del_stmt
);
843 VALIDATER(return_stmt
); VALIDATER(list_iter
);
844 VALIDATER(raise_stmt
); VALIDATER(import_stmt
);
845 VALIDATER(global_stmt
); VALIDATER(list_if
);
846 VALIDATER(assert_stmt
); VALIDATER(list_for
);
847 VALIDATER(exec_stmt
); VALIDATER(compound_stmt
);
848 VALIDATER(while); VALIDATER(for);
849 VALIDATER(try); VALIDATER(except_clause
);
850 VALIDATER(test
); VALIDATER(and_test
);
851 VALIDATER(not_test
); VALIDATER(comparison
);
852 VALIDATER(comp_op
); VALIDATER(expr
);
853 VALIDATER(xor_expr
); VALIDATER(and_expr
);
854 VALIDATER(shift_expr
); VALIDATER(arith_expr
);
855 VALIDATER(term
); VALIDATER(factor
);
856 VALIDATER(atom
); VALIDATER(lambdef
);
857 VALIDATER(trailer
); VALIDATER(subscript
);
858 VALIDATER(subscriptlist
); VALIDATER(sliceop
);
859 VALIDATER(exprlist
); VALIDATER(dictmaker
);
860 VALIDATER(arglist
); VALIDATER(argument
);
861 VALIDATER(listmaker
); VALIDATER(yield_stmt
);
862 VALIDATER(testlist1
);
866 #define is_even(n) (((n) & 1) == 0)
867 #define is_odd(n) (((n) & 1) == 1)
871 validate_ntype(node
*n
, int t
)
874 PyErr_Format(parser_error
, "Expected node type %d, got %d.",
882 /* Verifies that the number of child nodes is exactly 'num', raising
883 * an exception if it isn't. The exception message does not indicate
884 * the exact number of nodes, allowing this to be used to raise the
885 * "right" exception when the wrong number of nodes is present in a
886 * specific variant of a statement's syntax. This is commonly used
890 validate_numnodes(node
*n
, int num
, const char *const name
)
893 PyErr_Format(parser_error
,
894 "Illegal number of children for %s node.", name
);
902 validate_terminal(node
*terminal
, int type
, char *string
)
904 int res
= (validate_ntype(terminal
, type
)
905 && ((string
== 0) || (strcmp(string
, STR(terminal
)) == 0)));
907 if (!res
&& !PyErr_Occurred()) {
908 PyErr_Format(parser_error
,
909 "Illegal terminal: expected \"%s\"", string
);
918 validate_repeating_list(node
*tree
, int ntype
, int (*vfunc
)(node
*),
919 const char *const name
)
922 int res
= (nch
&& validate_ntype(tree
, ntype
)
923 && vfunc(CHILD(tree
, 0)));
925 if (!res
&& !PyErr_Occurred())
926 (void) validate_numnodes(tree
, 1, name
);
929 res
= validate_comma(CHILD(tree
, --nch
));
930 if (res
&& nch
> 1) {
932 for ( ; res
&& pos
< nch
; pos
+= 2)
933 res
= (validate_comma(CHILD(tree
, pos
))
934 && vfunc(CHILD(tree
, pos
+ 1)));
944 * 'class' NAME ['(' testlist ')'] ':' suite
947 validate_class(node
*tree
)
950 int res
= validate_ntype(tree
, classdef
) && ((nch
== 4) || (nch
== 7));
953 res
= (validate_name(CHILD(tree
, 0), "class")
954 && validate_ntype(CHILD(tree
, 1), NAME
)
955 && validate_colon(CHILD(tree
, nch
- 2))
956 && validate_suite(CHILD(tree
, nch
- 1)));
959 (void) validate_numnodes(tree
, 4, "class");
960 if (res
&& (nch
== 7)) {
961 res
= (validate_lparen(CHILD(tree
, 2))
962 && validate_testlist(CHILD(tree
, 3))
963 && validate_rparen(CHILD(tree
, 4)));
970 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
973 validate_if(node
*tree
)
976 int res
= (validate_ntype(tree
, if_stmt
)
978 && validate_name(CHILD(tree
, 0), "if")
979 && validate_test(CHILD(tree
, 1))
980 && validate_colon(CHILD(tree
, 2))
981 && validate_suite(CHILD(tree
, 3)));
983 if (res
&& ((nch
% 4) == 3)) {
984 /* ... 'else' ':' suite */
985 res
= (validate_name(CHILD(tree
, nch
- 3), "else")
986 && validate_colon(CHILD(tree
, nch
- 2))
987 && validate_suite(CHILD(tree
, nch
- 1)));
990 else if (!res
&& !PyErr_Occurred())
991 (void) validate_numnodes(tree
, 4, "if");
993 /* Will catch the case for nch < 4 */
994 res
= validate_numnodes(tree
, 0, "if");
995 else if (res
&& (nch
> 4)) {
996 /* ... ('elif' test ':' suite)+ ... */
998 while ((j
< nch
) && res
) {
999 res
= (validate_name(CHILD(tree
, j
), "elif")
1000 && validate_colon(CHILD(tree
, j
+ 2))
1001 && validate_test(CHILD(tree
, j
+ 1))
1002 && validate_suite(CHILD(tree
, j
+ 3)));
1011 * '(' [varargslist] ')'
1015 validate_parameters(node
*tree
)
1017 int nch
= NCH(tree
);
1018 int res
= validate_ntype(tree
, parameters
) && ((nch
== 2) || (nch
== 3));
1021 res
= (validate_lparen(CHILD(tree
, 0))
1022 && validate_rparen(CHILD(tree
, nch
- 1)));
1023 if (res
&& (nch
== 3))
1024 res
= validate_varargslist(CHILD(tree
, 1));
1027 (void) validate_numnodes(tree
, 2, "parameters");
1037 * | NEWLINE INDENT stmt+ DEDENT
1040 validate_suite(node
*tree
)
1042 int nch
= NCH(tree
);
1043 int res
= (validate_ntype(tree
, suite
) && ((nch
== 1) || (nch
>= 4)));
1045 if (res
&& (nch
== 1))
1046 res
= validate_simple_stmt(CHILD(tree
, 0));
1048 /* NEWLINE INDENT stmt+ DEDENT */
1049 res
= (validate_newline(CHILD(tree
, 0))
1050 && validate_indent(CHILD(tree
, 1))
1051 && validate_stmt(CHILD(tree
, 2))
1052 && validate_dedent(CHILD(tree
, nch
- 1)));
1054 if (res
&& (nch
> 4)) {
1056 --nch
; /* forget the DEDENT */
1057 for ( ; res
&& (i
< nch
); ++i
)
1058 res
= validate_stmt(CHILD(tree
, i
));
1061 res
= validate_numnodes(tree
, 4, "suite");
1068 validate_testlist(node
*tree
)
1070 return (validate_repeating_list(tree
, testlist
,
1071 validate_test
, "testlist"));
1076 validate_testlist1(node
*tree
)
1078 return (validate_repeating_list(tree
, testlist1
,
1079 validate_test
, "testlist1"));
1084 validate_testlist_safe(node
*tree
)
1086 return (validate_repeating_list(tree
, testlist_safe
,
1087 validate_test
, "testlist_safe"));
1091 /* '*' NAME [',' '**' NAME] | '**' NAME
1094 validate_varargslist_trailer(node
*tree
, int start
)
1096 int nch
= NCH(tree
);
1101 err_string("expected variable argument trailer for varargslist");
1104 sym
= TYPE(CHILD(tree
, start
));
1107 * ('*' NAME [',' '**' NAME]
1110 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1111 else if (nch
-start
== 5)
1112 res
= (validate_name(CHILD(tree
, start
+1), NULL
)
1113 && validate_comma(CHILD(tree
, start
+2))
1114 && validate_doublestar(CHILD(tree
, start
+3))
1115 && validate_name(CHILD(tree
, start
+4), NULL
));
1117 else if (sym
== DOUBLESTAR
) {
1122 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1125 err_string("illegal variable argument trailer for varargslist");
1130 /* validate_varargslist()
1133 * (fpdef ['=' test] ',')*
1134 * ('*' NAME [',' '**' NAME]
1136 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1140 validate_varargslist(node
*tree
)
1142 int nch
= NCH(tree
);
1143 int res
= validate_ntype(tree
, varargslist
) && (nch
!= 0);
1149 err_string("varargslist missing child nodes");
1152 sym
= TYPE(CHILD(tree
, 0));
1153 if (sym
== STAR
|| sym
== DOUBLESTAR
)
1154 /* whole thing matches:
1155 * '*' NAME [',' '**' NAME] | '**' NAME
1157 res
= validate_varargslist_trailer(tree
, 0);
1158 else if (sym
== fpdef
) {
1161 sym
= TYPE(CHILD(tree
, nch
-1));
1164 * (fpdef ['=' test] ',')+
1165 * ('*' NAME [',' '**' NAME]
1168 /* skip over (fpdef ['=' test] ',')+ */
1169 while (res
&& (i
+2 <= nch
)) {
1170 res
= validate_fpdef(CHILD(tree
, i
));
1172 if (res
&& TYPE(CHILD(tree
, i
)) == EQUAL
&& (i
+2 <= nch
)) {
1173 res
= (validate_equal(CHILD(tree
, i
))
1174 && validate_test(CHILD(tree
, i
+1)));
1178 if (res
&& i
< nch
) {
1179 res
= validate_comma(CHILD(tree
, i
));
1182 && (TYPE(CHILD(tree
, i
)) == DOUBLESTAR
1183 || TYPE(CHILD(tree
, i
)) == STAR
))
1187 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1191 res
= validate_varargslist_trailer(tree
, i
);
1195 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1197 /* strip trailing comma node */
1199 res
= validate_comma(CHILD(tree
, nch
-1));
1205 * fpdef ['=' test] (',' fpdef ['=' test])*
1207 res
= validate_fpdef(CHILD(tree
, 0));
1209 if (res
&& (i
+2 <= nch
) && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1210 res
= (validate_equal(CHILD(tree
, i
))
1211 && validate_test(CHILD(tree
, i
+1)));
1215 * ... (',' fpdef ['=' test])*
1218 while (res
&& (nch
- i
) >= 2) {
1219 res
= (validate_comma(CHILD(tree
, i
))
1220 && validate_fpdef(CHILD(tree
, i
+1)));
1222 if (res
&& (nch
- i
) >= 2 && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1223 res
= (validate_equal(CHILD(tree
, i
))
1224 && validate_test(CHILD(tree
, i
+1)));
1228 if (res
&& nch
- i
!= 0) {
1230 err_string("illegal formation for varargslist");
1238 /* list_iter: list_for | list_if
1241 validate_list_iter(node
*tree
)
1243 int res
= (validate_ntype(tree
, list_iter
)
1244 && validate_numnodes(tree
, 1, "list_iter"));
1245 if (res
&& TYPE(CHILD(tree
, 0)) == list_for
)
1246 res
= validate_list_for(CHILD(tree
, 0));
1248 res
= validate_list_if(CHILD(tree
, 0));
1253 /* list_for: 'for' exprlist 'in' testlist [list_iter]
1256 validate_list_for(node
*tree
)
1258 int nch
= NCH(tree
);
1262 res
= validate_list_iter(CHILD(tree
, 4));
1264 res
= validate_numnodes(tree
, 4, "list_for");
1267 res
= (validate_name(CHILD(tree
, 0), "for")
1268 && validate_exprlist(CHILD(tree
, 1))
1269 && validate_name(CHILD(tree
, 2), "in")
1270 && validate_testlist_safe(CHILD(tree
, 3)));
1275 /* list_if: 'if' test [list_iter]
1278 validate_list_if(node
*tree
)
1280 int nch
= NCH(tree
);
1284 res
= validate_list_iter(CHILD(tree
, 2));
1286 res
= validate_numnodes(tree
, 2, "list_if");
1289 res
= (validate_name(CHILD(tree
, 0), "if")
1290 && validate_test(CHILD(tree
, 1)));
1303 validate_fpdef(node
*tree
)
1305 int nch
= NCH(tree
);
1306 int res
= validate_ntype(tree
, fpdef
);
1310 res
= validate_ntype(CHILD(tree
, 0), NAME
);
1312 res
= (validate_lparen(CHILD(tree
, 0))
1313 && validate_fplist(CHILD(tree
, 1))
1314 && validate_rparen(CHILD(tree
, 2)));
1316 res
= validate_numnodes(tree
, 1, "fpdef");
1323 validate_fplist(node
*tree
)
1325 return (validate_repeating_list(tree
, fplist
,
1326 validate_fpdef
, "fplist"));
1330 /* simple_stmt | compound_stmt
1334 validate_stmt(node
*tree
)
1336 int res
= (validate_ntype(tree
, stmt
)
1337 && validate_numnodes(tree
, 1, "stmt"));
1340 tree
= CHILD(tree
, 0);
1342 if (TYPE(tree
) == simple_stmt
)
1343 res
= validate_simple_stmt(tree
);
1345 res
= validate_compound_stmt(tree
);
1351 /* small_stmt (';' small_stmt)* [';'] NEWLINE
1355 validate_simple_stmt(node
*tree
)
1357 int nch
= NCH(tree
);
1358 int res
= (validate_ntype(tree
, simple_stmt
)
1360 && validate_small_stmt(CHILD(tree
, 0))
1361 && validate_newline(CHILD(tree
, nch
- 1)));
1364 res
= validate_numnodes(tree
, 2, "simple_stmt");
1365 --nch
; /* forget the NEWLINE */
1366 if (res
&& is_even(nch
))
1367 res
= validate_semi(CHILD(tree
, --nch
));
1368 if (res
&& (nch
> 2)) {
1371 for (i
= 1; res
&& (i
< nch
); i
+= 2)
1372 res
= (validate_semi(CHILD(tree
, i
))
1373 && validate_small_stmt(CHILD(tree
, i
+ 1)));
1380 validate_small_stmt(node
*tree
)
1382 int nch
= NCH(tree
);
1383 int res
= validate_numnodes(tree
, 1, "small_stmt");
1386 int ntype
= TYPE(CHILD(tree
, 0));
1388 if ( (ntype
== expr_stmt
)
1389 || (ntype
== print_stmt
)
1390 || (ntype
== del_stmt
)
1391 || (ntype
== pass_stmt
)
1392 || (ntype
== flow_stmt
)
1393 || (ntype
== import_stmt
)
1394 || (ntype
== global_stmt
)
1395 || (ntype
== assert_stmt
)
1396 || (ntype
== exec_stmt
))
1397 res
= validate_node(CHILD(tree
, 0));
1400 err_string("illegal small_stmt child type");
1403 else if (nch
== 1) {
1405 PyErr_Format(parser_error
,
1406 "Unrecognized child node of small_stmt: %d.",
1407 TYPE(CHILD(tree
, 0)));
1414 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1417 validate_compound_stmt(node
*tree
)
1419 int res
= (validate_ntype(tree
, compound_stmt
)
1420 && validate_numnodes(tree
, 1, "compound_stmt"));
1426 tree
= CHILD(tree
, 0);
1428 if ( (ntype
== if_stmt
)
1429 || (ntype
== while_stmt
)
1430 || (ntype
== for_stmt
)
1431 || (ntype
== try_stmt
)
1432 || (ntype
== funcdef
)
1433 || (ntype
== classdef
))
1434 res
= validate_node(tree
);
1437 PyErr_Format(parser_error
,
1438 "Illegal compound statement type: %d.", TYPE(tree
));
1445 validate_expr_stmt(node
*tree
)
1448 int nch
= NCH(tree
);
1449 int res
= (validate_ntype(tree
, expr_stmt
)
1451 && validate_testlist(CHILD(tree
, 0)));
1454 && TYPE(CHILD(tree
, 1)) == augassign
) {
1455 res
= (validate_numnodes(CHILD(tree
, 1), 1, "augassign")
1456 && validate_testlist(CHILD(tree
, 2)));
1459 char *s
= STR(CHILD(CHILD(tree
, 1), 0));
1461 res
= (strcmp(s
, "+=") == 0
1462 || strcmp(s
, "-=") == 0
1463 || strcmp(s
, "*=") == 0
1464 || strcmp(s
, "/=") == 0
1465 || strcmp(s
, "//=") == 0
1466 || strcmp(s
, "%=") == 0
1467 || strcmp(s
, "&=") == 0
1468 || strcmp(s
, "|=") == 0
1469 || strcmp(s
, "^=") == 0
1470 || strcmp(s
, "<<=") == 0
1471 || strcmp(s
, ">>=") == 0
1472 || strcmp(s
, "**=") == 0);
1474 err_string("illegal augmmented assignment operator");
1478 for (j
= 1; res
&& (j
< nch
); j
+= 2)
1479 res
= (validate_equal(CHILD(tree
, j
))
1480 && validate_testlist(CHILD(tree
, j
+ 1)));
1488 * 'print' ( [ test (',' test)* [','] ]
1489 * | '>>' test [ (',' test)+ [','] ] )
1492 validate_print_stmt(node
*tree
)
1494 int nch
= NCH(tree
);
1495 int res
= (validate_ntype(tree
, print_stmt
)
1497 && validate_name(CHILD(tree
, 0), "print"));
1499 if (res
&& nch
> 1) {
1500 int sym
= TYPE(CHILD(tree
, 1));
1502 int allow_trailing_comma
= 1;
1505 res
= validate_test(CHILD(tree
, i
++));
1508 res
= validate_numnodes(tree
, 3, "print_stmt");
1510 res
= (validate_ntype(CHILD(tree
, i
), RIGHTSHIFT
)
1511 && validate_test(CHILD(tree
, i
+1)));
1513 allow_trailing_comma
= 0;
1517 /* ... (',' test)* [','] */
1518 while (res
&& i
+2 <= nch
) {
1519 res
= (validate_comma(CHILD(tree
, i
))
1520 && validate_test(CHILD(tree
, i
+1)));
1521 allow_trailing_comma
= 1;
1524 if (res
&& !allow_trailing_comma
)
1525 res
= validate_numnodes(tree
, i
, "print_stmt");
1526 else if (res
&& i
< nch
)
1527 res
= validate_comma(CHILD(tree
, i
));
1535 validate_del_stmt(node
*tree
)
1537 return (validate_numnodes(tree
, 2, "del_stmt")
1538 && validate_name(CHILD(tree
, 0), "del")
1539 && validate_exprlist(CHILD(tree
, 1)));
1544 validate_return_stmt(node
*tree
)
1546 int nch
= NCH(tree
);
1547 int res
= (validate_ntype(tree
, return_stmt
)
1548 && ((nch
== 1) || (nch
== 2))
1549 && validate_name(CHILD(tree
, 0), "return"));
1551 if (res
&& (nch
== 2))
1552 res
= validate_testlist(CHILD(tree
, 1));
1559 validate_raise_stmt(node
*tree
)
1561 int nch
= NCH(tree
);
1562 int res
= (validate_ntype(tree
, raise_stmt
)
1563 && ((nch
== 1) || (nch
== 2) || (nch
== 4) || (nch
== 6)));
1566 res
= validate_name(CHILD(tree
, 0), "raise");
1567 if (res
&& (nch
>= 2))
1568 res
= validate_test(CHILD(tree
, 1));
1569 if (res
&& nch
> 2) {
1570 res
= (validate_comma(CHILD(tree
, 2))
1571 && validate_test(CHILD(tree
, 3)));
1572 if (res
&& (nch
> 4))
1573 res
= (validate_comma(CHILD(tree
, 4))
1574 && validate_test(CHILD(tree
, 5)));
1578 (void) validate_numnodes(tree
, 2, "raise");
1579 if (res
&& (nch
== 4))
1580 res
= (validate_comma(CHILD(tree
, 2))
1581 && validate_test(CHILD(tree
, 3)));
1587 /* yield_stmt: 'yield' testlist
1590 validate_yield_stmt(node
*tree
)
1592 return (validate_ntype(tree
, yield_stmt
)
1593 && validate_numnodes(tree
, 2, "yield_stmt")
1594 && validate_name(CHILD(tree
, 0), "yield")
1595 && validate_testlist(CHILD(tree
, 1)));
1600 validate_import_as_name(node
*tree
)
1602 int nch
= NCH(tree
);
1603 int ok
= validate_ntype(tree
, import_as_name
);
1607 ok
= validate_name(CHILD(tree
, 0), NULL
);
1609 ok
= (validate_name(CHILD(tree
, 0), NULL
)
1610 && validate_name(CHILD(tree
, 1), "as")
1611 && validate_name(CHILD(tree
, 2), NULL
));
1613 ok
= validate_numnodes(tree
, 3, "import_as_name");
1619 /* dotted_name: NAME ("." NAME)*
1622 validate_dotted_name(node
*tree
)
1624 int nch
= NCH(tree
);
1625 int res
= (validate_ntype(tree
, dotted_name
)
1627 && validate_name(CHILD(tree
, 0), NULL
));
1630 for (i
= 1; res
&& (i
< nch
); i
+= 2) {
1631 res
= (validate_dot(CHILD(tree
, i
))
1632 && validate_name(CHILD(tree
, i
+1), NULL
));
1638 /* dotted_as_name: dotted_name [NAME NAME]
1641 validate_dotted_as_name(node
*tree
)
1643 int nch
= NCH(tree
);
1644 int res
= validate_ntype(tree
, dotted_as_name
);
1648 res
= validate_dotted_name(CHILD(tree
, 0));
1650 res
= (validate_dotted_name(CHILD(tree
, 0))
1651 && validate_name(CHILD(tree
, 1), "as")
1652 && validate_name(CHILD(tree
, 2), NULL
));
1655 err_string("illegal number of children for dotted_as_name");
1664 * 'import' dotted_as_name (',' dotted_as_name)*
1665 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
1668 validate_import_stmt(node
*tree
)
1670 int nch
= NCH(tree
);
1671 int res
= (validate_ntype(tree
, import_stmt
)
1672 && (nch
>= 2) && is_even(nch
)
1673 && validate_ntype(CHILD(tree
, 0), NAME
));
1675 if (res
&& (strcmp(STR(CHILD(tree
, 0)), "import") == 0)) {
1678 res
= validate_dotted_as_name(CHILD(tree
, 1));
1679 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1680 res
= (validate_comma(CHILD(tree
, j
))
1681 && validate_dotted_as_name(CHILD(tree
, j
+ 1)));
1683 else if (res
&& (res
= validate_name(CHILD(tree
, 0), "from"))) {
1684 res
= ((nch
>= 4) && is_even(nch
)
1685 && validate_dotted_name(CHILD(tree
, 1))
1686 && validate_name(CHILD(tree
, 2), "import"));
1688 if (TYPE(CHILD(tree
, 3)) == import_as_name
)
1689 res
= validate_import_as_name(CHILD(tree
, 3));
1691 res
= validate_star(CHILD(tree
, 3));
1694 /* 'from' dotted_name 'import' import_as_name
1695 * (',' import_as_name)+
1698 res
= validate_import_as_name(CHILD(tree
, 3));
1699 for (j
= 4; res
&& (j
< nch
); j
+= 2)
1700 res
= (validate_comma(CHILD(tree
, j
))
1701 && validate_import_as_name(CHILD(tree
, j
+ 1)));
1712 validate_global_stmt(node
*tree
)
1715 int nch
= NCH(tree
);
1716 int res
= (validate_ntype(tree
, global_stmt
)
1717 && is_even(nch
) && (nch
>= 2));
1719 if (!res
&& !PyErr_Occurred())
1720 err_string("illegal global statement");
1723 res
= (validate_name(CHILD(tree
, 0), "global")
1724 && validate_ntype(CHILD(tree
, 1), NAME
));
1725 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1726 res
= (validate_comma(CHILD(tree
, j
))
1727 && validate_ntype(CHILD(tree
, j
+ 1), NAME
));
1735 * 'exec' expr ['in' test [',' test]]
1738 validate_exec_stmt(node
*tree
)
1740 int nch
= NCH(tree
);
1741 int res
= (validate_ntype(tree
, exec_stmt
)
1742 && ((nch
== 2) || (nch
== 4) || (nch
== 6))
1743 && validate_name(CHILD(tree
, 0), "exec")
1744 && validate_expr(CHILD(tree
, 1)));
1746 if (!res
&& !PyErr_Occurred())
1747 err_string("illegal exec statement");
1748 if (res
&& (nch
> 2))
1749 res
= (validate_name(CHILD(tree
, 2), "in")
1750 && validate_test(CHILD(tree
, 3)));
1751 if (res
&& (nch
== 6))
1752 res
= (validate_comma(CHILD(tree
, 4))
1753 && validate_test(CHILD(tree
, 5)));
1761 * 'assert' test [',' test]
1764 validate_assert_stmt(node
*tree
)
1766 int nch
= NCH(tree
);
1767 int res
= (validate_ntype(tree
, assert_stmt
)
1768 && ((nch
== 2) || (nch
== 4))
1769 && (validate_name(CHILD(tree
, 0), "assert"))
1770 && validate_test(CHILD(tree
, 1)));
1772 if (!res
&& !PyErr_Occurred())
1773 err_string("illegal assert statement");
1774 if (res
&& (nch
> 2))
1775 res
= (validate_comma(CHILD(tree
, 2))
1776 && validate_test(CHILD(tree
, 3)));
1783 validate_while(node
*tree
)
1785 int nch
= NCH(tree
);
1786 int res
= (validate_ntype(tree
, while_stmt
)
1787 && ((nch
== 4) || (nch
== 7))
1788 && validate_name(CHILD(tree
, 0), "while")
1789 && validate_test(CHILD(tree
, 1))
1790 && validate_colon(CHILD(tree
, 2))
1791 && validate_suite(CHILD(tree
, 3)));
1793 if (res
&& (nch
== 7))
1794 res
= (validate_name(CHILD(tree
, 4), "else")
1795 && validate_colon(CHILD(tree
, 5))
1796 && validate_suite(CHILD(tree
, 6)));
1803 validate_for(node
*tree
)
1805 int nch
= NCH(tree
);
1806 int res
= (validate_ntype(tree
, for_stmt
)
1807 && ((nch
== 6) || (nch
== 9))
1808 && validate_name(CHILD(tree
, 0), "for")
1809 && validate_exprlist(CHILD(tree
, 1))
1810 && validate_name(CHILD(tree
, 2), "in")
1811 && validate_testlist(CHILD(tree
, 3))
1812 && validate_colon(CHILD(tree
, 4))
1813 && validate_suite(CHILD(tree
, 5)));
1815 if (res
&& (nch
== 9))
1816 res
= (validate_name(CHILD(tree
, 6), "else")
1817 && validate_colon(CHILD(tree
, 7))
1818 && validate_suite(CHILD(tree
, 8)));
1825 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1826 * | 'try' ':' suite 'finally' ':' suite
1830 validate_try(node
*tree
)
1832 int nch
= NCH(tree
);
1834 int res
= (validate_ntype(tree
, try_stmt
)
1835 && (nch
>= 6) && ((nch
% 3) == 0));
1838 res
= (validate_name(CHILD(tree
, 0), "try")
1839 && validate_colon(CHILD(tree
, 1))
1840 && validate_suite(CHILD(tree
, 2))
1841 && validate_colon(CHILD(tree
, nch
- 2))
1842 && validate_suite(CHILD(tree
, nch
- 1)));
1843 else if (!PyErr_Occurred()) {
1844 const char* name
= "except";
1845 if (TYPE(CHILD(tree
, nch
- 3)) != except_clause
)
1846 name
= STR(CHILD(tree
, nch
- 3));
1848 PyErr_Format(parser_error
,
1849 "Illegal number of children for try/%s node.", name
);
1851 /* Skip past except_clause sections: */
1852 while (res
&& (TYPE(CHILD(tree
, pos
)) == except_clause
)) {
1853 res
= (validate_except_clause(CHILD(tree
, pos
))
1854 && validate_colon(CHILD(tree
, pos
+ 1))
1855 && validate_suite(CHILD(tree
, pos
+ 2)));
1858 if (res
&& (pos
< nch
)) {
1859 res
= validate_ntype(CHILD(tree
, pos
), NAME
);
1860 if (res
&& (strcmp(STR(CHILD(tree
, pos
)), "finally") == 0))
1861 res
= (validate_numnodes(tree
, 6, "try/finally")
1862 && validate_colon(CHILD(tree
, 4))
1863 && validate_suite(CHILD(tree
, 5)));
1865 if (nch
== (pos
+ 3)) {
1866 res
= ((strcmp(STR(CHILD(tree
, pos
)), "except") == 0)
1867 || (strcmp(STR(CHILD(tree
, pos
)), "else") == 0));
1869 err_string("illegal trailing triple in try statement");
1871 else if (nch
== (pos
+ 6)) {
1872 res
= (validate_name(CHILD(tree
, pos
), "except")
1873 && validate_colon(CHILD(tree
, pos
+ 1))
1874 && validate_suite(CHILD(tree
, pos
+ 2))
1875 && validate_name(CHILD(tree
, pos
+ 3), "else"));
1878 res
= validate_numnodes(tree
, pos
+ 3, "try/except");
1886 validate_except_clause(node
*tree
)
1888 int nch
= NCH(tree
);
1889 int res
= (validate_ntype(tree
, except_clause
)
1890 && ((nch
== 1) || (nch
== 2) || (nch
== 4))
1891 && validate_name(CHILD(tree
, 0), "except"));
1893 if (res
&& (nch
> 1))
1894 res
= validate_test(CHILD(tree
, 1));
1895 if (res
&& (nch
== 4))
1896 res
= (validate_comma(CHILD(tree
, 2))
1897 && validate_test(CHILD(tree
, 3)));
1904 validate_test(node
*tree
)
1906 int nch
= NCH(tree
);
1907 int res
= validate_ntype(tree
, test
) && is_odd(nch
);
1909 if (res
&& (TYPE(CHILD(tree
, 0)) == lambdef
))
1911 && validate_lambdef(CHILD(tree
, 0)));
1914 res
= validate_and_test(CHILD(tree
, 0));
1915 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
1916 res
= (validate_name(CHILD(tree
, pos
), "or")
1917 && validate_and_test(CHILD(tree
, pos
+ 1)));
1924 validate_and_test(node
*tree
)
1927 int nch
= NCH(tree
);
1928 int res
= (validate_ntype(tree
, and_test
)
1930 && validate_not_test(CHILD(tree
, 0)));
1932 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
1933 res
= (validate_name(CHILD(tree
, pos
), "and")
1934 && validate_not_test(CHILD(tree
, 0)));
1941 validate_not_test(node
*tree
)
1943 int nch
= NCH(tree
);
1944 int res
= validate_ntype(tree
, not_test
) && ((nch
== 1) || (nch
== 2));
1948 res
= (validate_name(CHILD(tree
, 0), "not")
1949 && validate_not_test(CHILD(tree
, 1)));
1951 res
= validate_comparison(CHILD(tree
, 0));
1958 validate_comparison(node
*tree
)
1961 int nch
= NCH(tree
);
1962 int res
= (validate_ntype(tree
, comparison
)
1964 && validate_expr(CHILD(tree
, 0)));
1966 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
1967 res
= (validate_comp_op(CHILD(tree
, pos
))
1968 && validate_expr(CHILD(tree
, pos
+ 1)));
1975 validate_comp_op(node
*tree
)
1978 int nch
= NCH(tree
);
1980 if (!validate_ntype(tree
, comp_op
))
1984 * Only child will be a terminal with a well-defined symbolic name
1985 * or a NAME with a string of either 'is' or 'in'
1987 tree
= CHILD(tree
, 0);
1988 switch (TYPE(tree
)) {
1999 res
= ((strcmp(STR(tree
), "in") == 0)
2000 || (strcmp(STR(tree
), "is") == 0));
2002 PyErr_Format(parser_error
,
2003 "illegal operator '%s'", STR(tree
));
2007 err_string("illegal comparison operator type");
2011 else if ((res
= validate_numnodes(tree
, 2, "comp_op")) != 0) {
2012 res
= (validate_ntype(CHILD(tree
, 0), NAME
)
2013 && validate_ntype(CHILD(tree
, 1), NAME
)
2014 && (((strcmp(STR(CHILD(tree
, 0)), "is") == 0)
2015 && (strcmp(STR(CHILD(tree
, 1)), "not") == 0))
2016 || ((strcmp(STR(CHILD(tree
, 0)), "not") == 0)
2017 && (strcmp(STR(CHILD(tree
, 1)), "in") == 0))));
2018 if (!res
&& !PyErr_Occurred())
2019 err_string("unknown comparison operator");
2026 validate_expr(node
*tree
)
2029 int nch
= NCH(tree
);
2030 int res
= (validate_ntype(tree
, expr
)
2032 && validate_xor_expr(CHILD(tree
, 0)));
2034 for (j
= 2; res
&& (j
< nch
); j
+= 2)
2035 res
= (validate_xor_expr(CHILD(tree
, j
))
2036 && validate_vbar(CHILD(tree
, j
- 1)));
2043 validate_xor_expr(node
*tree
)
2046 int nch
= NCH(tree
);
2047 int res
= (validate_ntype(tree
, xor_expr
)
2049 && validate_and_expr(CHILD(tree
, 0)));
2051 for (j
= 2; res
&& (j
< nch
); j
+= 2)
2052 res
= (validate_circumflex(CHILD(tree
, j
- 1))
2053 && validate_and_expr(CHILD(tree
, j
)));
2060 validate_and_expr(node
*tree
)
2063 int nch
= NCH(tree
);
2064 int res
= (validate_ntype(tree
, and_expr
)
2066 && validate_shift_expr(CHILD(tree
, 0)));
2068 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2069 res
= (validate_ampersand(CHILD(tree
, pos
))
2070 && validate_shift_expr(CHILD(tree
, pos
+ 1)));
2077 validate_chain_two_ops(node
*tree
, int (*termvalid
)(node
*), int op1
, int op2
)
2080 int nch
= NCH(tree
);
2081 int res
= (is_odd(nch
)
2082 && (*termvalid
)(CHILD(tree
, 0)));
2084 for ( ; res
&& (pos
< nch
); pos
+= 2) {
2085 if (TYPE(CHILD(tree
, pos
)) != op1
)
2086 res
= validate_ntype(CHILD(tree
, pos
), op2
);
2088 res
= (*termvalid
)(CHILD(tree
, pos
+ 1));
2095 validate_shift_expr(node
*tree
)
2097 return (validate_ntype(tree
, shift_expr
)
2098 && validate_chain_two_ops(tree
, validate_arith_expr
,
2099 LEFTSHIFT
, RIGHTSHIFT
));
2104 validate_arith_expr(node
*tree
)
2106 return (validate_ntype(tree
, arith_expr
)
2107 && validate_chain_two_ops(tree
, validate_term
, PLUS
, MINUS
));
2112 validate_term(node
*tree
)
2115 int nch
= NCH(tree
);
2116 int res
= (validate_ntype(tree
, term
)
2118 && validate_factor(CHILD(tree
, 0)));
2120 for ( ; res
&& (pos
< nch
); pos
+= 2)
2121 res
= (((TYPE(CHILD(tree
, pos
)) == STAR
)
2122 || (TYPE(CHILD(tree
, pos
)) == SLASH
)
2123 || (TYPE(CHILD(tree
, pos
)) == DOUBLESLASH
)
2124 || (TYPE(CHILD(tree
, pos
)) == PERCENT
))
2125 && validate_factor(CHILD(tree
, pos
+ 1)));
2133 * factor: ('+'|'-'|'~') factor | power
2136 validate_factor(node
*tree
)
2138 int nch
= NCH(tree
);
2139 int res
= (validate_ntype(tree
, factor
)
2141 && ((TYPE(CHILD(tree
, 0)) == PLUS
)
2142 || (TYPE(CHILD(tree
, 0)) == MINUS
)
2143 || (TYPE(CHILD(tree
, 0)) == TILDE
))
2144 && validate_factor(CHILD(tree
, 1)))
2146 && validate_power(CHILD(tree
, 0)))));
2153 * power: atom trailer* ('**' factor)*
2156 validate_power(node
*tree
)
2159 int nch
= NCH(tree
);
2160 int res
= (validate_ntype(tree
, power
) && (nch
>= 1)
2161 && validate_atom(CHILD(tree
, 0)));
2163 while (res
&& (pos
< nch
) && (TYPE(CHILD(tree
, pos
)) == trailer
))
2164 res
= validate_trailer(CHILD(tree
, pos
++));
2165 if (res
&& (pos
< nch
)) {
2166 if (!is_even(nch
- pos
)) {
2167 err_string("illegal number of nodes for 'power'");
2170 for ( ; res
&& (pos
< (nch
- 1)); pos
+= 2)
2171 res
= (validate_doublestar(CHILD(tree
, pos
))
2172 && validate_factor(CHILD(tree
, pos
+ 1)));
2179 validate_atom(node
*tree
)
2182 int nch
= NCH(tree
);
2183 int res
= validate_ntype(tree
, atom
);
2186 res
= validate_numnodes(tree
, nch
+1, "atom");
2188 switch (TYPE(CHILD(tree
, 0))) {
2191 && (validate_rparen(CHILD(tree
, nch
- 1))));
2193 if (res
&& (nch
== 3))
2194 res
= validate_testlist(CHILD(tree
, 1));
2198 res
= validate_ntype(CHILD(tree
, 1), RSQB
);
2200 res
= (validate_listmaker(CHILD(tree
, 1))
2201 && validate_ntype(CHILD(tree
, 2), RSQB
));
2204 err_string("illegal list display atom");
2209 && validate_ntype(CHILD(tree
, nch
- 1), RBRACE
));
2211 if (res
&& (nch
== 3))
2212 res
= validate_dictmaker(CHILD(tree
, 1));
2216 && validate_testlist1(CHILD(tree
, 1))
2217 && validate_ntype(CHILD(tree
, 2), BACKQUOTE
));
2224 for (pos
= 1; res
&& (pos
< nch
); ++pos
)
2225 res
= validate_ntype(CHILD(tree
, pos
), STRING
);
2237 * test ( list_for | (',' test)* [','] )
2240 validate_listmaker(node
*tree
)
2242 int nch
= NCH(tree
);
2246 err_string("missing child nodes of listmaker");
2248 ok
= validate_test(CHILD(tree
, 0));
2251 * list_iter | (',' test)* [',']
2253 if (nch
== 2 && TYPE(CHILD(tree
, 1)) == list_for
)
2254 ok
= validate_list_for(CHILD(tree
, 1));
2256 /* (',' test)* [','] */
2258 while (ok
&& nch
- i
>= 2) {
2259 ok
= (validate_comma(CHILD(tree
, i
))
2260 && validate_test(CHILD(tree
, i
+1)));
2263 if (ok
&& i
== nch
-1)
2264 ok
= validate_comma(CHILD(tree
, i
));
2265 else if (i
!= nch
) {
2267 err_string("illegal trailing nodes for listmaker");
2275 * 'def' NAME parameters ':' suite
2279 validate_funcdef(node
*tree
)
2281 return (validate_ntype(tree
, funcdef
)
2282 && validate_numnodes(tree
, 5, "funcdef")
2283 && validate_name(CHILD(tree
, 0), "def")
2284 && validate_ntype(CHILD(tree
, 1), NAME
)
2285 && validate_colon(CHILD(tree
, 3))
2286 && validate_parameters(CHILD(tree
, 2))
2287 && validate_suite(CHILD(tree
, 4)));
2292 validate_lambdef(node
*tree
)
2294 int nch
= NCH(tree
);
2295 int res
= (validate_ntype(tree
, lambdef
)
2296 && ((nch
== 3) || (nch
== 4))
2297 && validate_name(CHILD(tree
, 0), "lambda")
2298 && validate_colon(CHILD(tree
, nch
- 2))
2299 && validate_test(CHILD(tree
, nch
- 1)));
2301 if (res
&& (nch
== 4))
2302 res
= validate_varargslist(CHILD(tree
, 1));
2303 else if (!res
&& !PyErr_Occurred())
2304 (void) validate_numnodes(tree
, 3, "lambdef");
2312 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2315 validate_arglist(node
*tree
)
2317 int nch
= NCH(tree
);
2322 /* raise the right error from having an invalid number of children */
2323 return validate_numnodes(tree
, nch
+ 1, "arglist");
2325 while (ok
&& nch
-i
>= 2) {
2326 /* skip leading (argument ',') */
2327 ok
= (validate_argument(CHILD(tree
, i
))
2328 && validate_comma(CHILD(tree
, i
+1)));
2337 * argument | '*' test [',' '**' test] | '**' test
2339 int sym
= TYPE(CHILD(tree
, i
));
2341 if (sym
== argument
) {
2342 ok
= validate_argument(CHILD(tree
, i
));
2343 if (ok
&& i
+1 != nch
) {
2344 err_string("illegal arglist specification"
2345 " (extra stuff on end)");
2349 else if (sym
== STAR
) {
2350 ok
= validate_star(CHILD(tree
, i
));
2351 if (ok
&& (nch
-i
== 2))
2352 ok
= validate_test(CHILD(tree
, i
+1));
2353 else if (ok
&& (nch
-i
== 5))
2354 ok
= (validate_test(CHILD(tree
, i
+1))
2355 && validate_comma(CHILD(tree
, i
+2))
2356 && validate_doublestar(CHILD(tree
, i
+3))
2357 && validate_test(CHILD(tree
, i
+4)));
2359 err_string("illegal use of '*' in arglist");
2363 else if (sym
== DOUBLESTAR
) {
2365 ok
= (validate_doublestar(CHILD(tree
, i
))
2366 && validate_test(CHILD(tree
, i
+1)));
2368 err_string("illegal use of '**' in arglist");
2373 err_string("illegal arglist specification");
2387 validate_argument(node
*tree
)
2389 int nch
= NCH(tree
);
2390 int res
= (validate_ntype(tree
, argument
)
2391 && ((nch
== 1) || (nch
== 3))
2392 && validate_test(CHILD(tree
, 0)));
2394 if (res
&& (nch
== 3))
2395 res
= (validate_equal(CHILD(tree
, 1))
2396 && validate_test(CHILD(tree
, 2)));
2405 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2408 validate_trailer(node
*tree
)
2410 int nch
= NCH(tree
);
2411 int res
= validate_ntype(tree
, trailer
) && ((nch
== 2) || (nch
== 3));
2414 switch (TYPE(CHILD(tree
, 0))) {
2416 res
= validate_rparen(CHILD(tree
, nch
- 1));
2417 if (res
&& (nch
== 3))
2418 res
= validate_arglist(CHILD(tree
, 1));
2421 res
= (validate_numnodes(tree
, 3, "trailer")
2422 && validate_subscriptlist(CHILD(tree
, 1))
2423 && validate_ntype(CHILD(tree
, 2), RSQB
));
2426 res
= (validate_numnodes(tree
, 2, "trailer")
2427 && validate_ntype(CHILD(tree
, 1), NAME
));
2435 (void) validate_numnodes(tree
, 2, "trailer");
2443 * subscript (',' subscript)* [',']
2446 validate_subscriptlist(node
*tree
)
2448 return (validate_repeating_list(tree
, subscriptlist
,
2449 validate_subscript
, "subscriptlist"));
2455 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
2458 validate_subscript(node
*tree
)
2461 int nch
= NCH(tree
);
2462 int res
= validate_ntype(tree
, subscript
) && (nch
>= 1) && (nch
<= 4);
2465 if (!PyErr_Occurred())
2466 err_string("invalid number of arguments for subscript node");
2469 if (TYPE(CHILD(tree
, 0)) == DOT
)
2470 /* take care of ('.' '.' '.') possibility */
2471 return (validate_numnodes(tree
, 3, "subscript")
2472 && validate_dot(CHILD(tree
, 0))
2473 && validate_dot(CHILD(tree
, 1))
2474 && validate_dot(CHILD(tree
, 2)));
2476 if (TYPE(CHILD(tree
, 0)) == test
)
2477 res
= validate_test(CHILD(tree
, 0));
2479 res
= validate_colon(CHILD(tree
, 0));
2482 /* Must be [test] ':' [test] [sliceop],
2483 * but at least one of the optional components will
2484 * be present, but we don't know which yet.
2486 if ((TYPE(CHILD(tree
, 0)) != COLON
) || (nch
== 4)) {
2487 res
= validate_test(CHILD(tree
, 0));
2491 res
= validate_colon(CHILD(tree
, offset
));
2493 int rem
= nch
- ++offset
;
2495 if (TYPE(CHILD(tree
, offset
)) == test
) {
2496 res
= validate_test(CHILD(tree
, offset
));
2501 res
= validate_sliceop(CHILD(tree
, offset
));
2509 validate_sliceop(node
*tree
)
2511 int nch
= NCH(tree
);
2512 int res
= ((nch
== 1) || validate_numnodes(tree
, 2, "sliceop"))
2513 && validate_ntype(tree
, sliceop
);
2514 if (!res
&& !PyErr_Occurred()) {
2515 res
= validate_numnodes(tree
, 1, "sliceop");
2518 res
= validate_colon(CHILD(tree
, 0));
2519 if (res
&& (nch
== 2))
2520 res
= validate_test(CHILD(tree
, 1));
2527 validate_exprlist(node
*tree
)
2529 return (validate_repeating_list(tree
, exprlist
,
2530 validate_expr
, "exprlist"));
2535 validate_dictmaker(node
*tree
)
2537 int nch
= NCH(tree
);
2538 int res
= (validate_ntype(tree
, dictmaker
)
2540 && validate_test(CHILD(tree
, 0))
2541 && validate_colon(CHILD(tree
, 1))
2542 && validate_test(CHILD(tree
, 2)));
2544 if (res
&& ((nch
% 4) == 0))
2545 res
= validate_comma(CHILD(tree
, --nch
));
2547 res
= ((nch
% 4) == 3);
2549 if (res
&& (nch
> 3)) {
2551 /* ( ',' test ':' test )* */
2552 while (res
&& (pos
< nch
)) {
2553 res
= (validate_comma(CHILD(tree
, pos
))
2554 && validate_test(CHILD(tree
, pos
+ 1))
2555 && validate_colon(CHILD(tree
, pos
+ 2))
2556 && validate_test(CHILD(tree
, pos
+ 3)));
2565 validate_eval_input(node
*tree
)
2568 int nch
= NCH(tree
);
2569 int res
= (validate_ntype(tree
, eval_input
)
2571 && validate_testlist(CHILD(tree
, 0))
2572 && validate_ntype(CHILD(tree
, nch
- 1), ENDMARKER
));
2574 for (pos
= 1; res
&& (pos
< (nch
- 1)); ++pos
)
2575 res
= validate_ntype(CHILD(tree
, pos
), NEWLINE
);
2582 validate_node(node
*tree
)
2584 int nch
= 0; /* num. children on current node */
2585 int res
= 1; /* result value */
2586 node
* next
= 0; /* node to process after this one */
2588 while (res
&& (tree
!= 0)) {
2591 switch (TYPE(tree
)) {
2596 res
= validate_funcdef(tree
);
2599 res
= validate_class(tree
);
2602 * "Trivial" parse tree nodes.
2603 * (Why did I call these trivial?)
2606 res
= validate_stmt(tree
);
2610 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2611 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2613 res
= validate_small_stmt(tree
);
2616 res
= (validate_numnodes(tree
, 1, "flow_stmt")
2617 && ((TYPE(CHILD(tree
, 0)) == break_stmt
)
2618 || (TYPE(CHILD(tree
, 0)) == continue_stmt
)
2619 || (TYPE(CHILD(tree
, 0)) == yield_stmt
)
2620 || (TYPE(CHILD(tree
, 0)) == return_stmt
)
2621 || (TYPE(CHILD(tree
, 0)) == raise_stmt
)));
2623 next
= CHILD(tree
, 0);
2625 err_string("illegal flow_stmt type");
2628 res
= validate_yield_stmt(tree
);
2631 * Compound statements.
2634 res
= validate_simple_stmt(tree
);
2637 res
= validate_compound_stmt(tree
);
2640 * Fundamental statements.
2643 res
= validate_expr_stmt(tree
);
2646 res
= validate_print_stmt(tree
);
2649 res
= validate_del_stmt(tree
);
2652 res
= (validate_numnodes(tree
, 1, "pass")
2653 && validate_name(CHILD(tree
, 0), "pass"));
2656 res
= (validate_numnodes(tree
, 1, "break")
2657 && validate_name(CHILD(tree
, 0), "break"));
2660 res
= (validate_numnodes(tree
, 1, "continue")
2661 && validate_name(CHILD(tree
, 0), "continue"));
2664 res
= validate_return_stmt(tree
);
2667 res
= validate_raise_stmt(tree
);
2670 res
= validate_import_stmt(tree
);
2673 res
= validate_global_stmt(tree
);
2676 res
= validate_exec_stmt(tree
);
2679 res
= validate_assert_stmt(tree
);
2682 res
= validate_if(tree
);
2685 res
= validate_while(tree
);
2688 res
= validate_for(tree
);
2691 res
= validate_try(tree
);
2694 res
= validate_suite(tree
);
2700 res
= validate_testlist(tree
);
2703 res
= validate_testlist1(tree
);
2706 res
= validate_test(tree
);
2709 res
= validate_and_test(tree
);
2712 res
= validate_not_test(tree
);
2715 res
= validate_comparison(tree
);
2718 res
= validate_exprlist(tree
);
2721 res
= validate_comp_op(tree
);
2724 res
= validate_expr(tree
);
2727 res
= validate_xor_expr(tree
);
2730 res
= validate_and_expr(tree
);
2733 res
= validate_shift_expr(tree
);
2736 res
= validate_arith_expr(tree
);
2739 res
= validate_term(tree
);
2742 res
= validate_factor(tree
);
2745 res
= validate_power(tree
);
2748 res
= validate_atom(tree
);
2752 /* Hopefully never reached! */
2753 err_string("unrecognized node type");
2764 validate_expr_tree(node
*tree
)
2766 int res
= validate_eval_input(tree
);
2768 if (!res
&& !PyErr_Occurred())
2769 err_string("could not validate expression tuple");
2776 * (NEWLINE | stmt)* ENDMARKER
2779 validate_file_input(node
*tree
)
2782 int nch
= NCH(tree
) - 1;
2783 int res
= ((nch
>= 0)
2784 && validate_ntype(CHILD(tree
, nch
), ENDMARKER
));
2786 for (j
= 0; res
&& (j
< nch
); ++j
) {
2787 if (TYPE(CHILD(tree
, j
)) == stmt
)
2788 res
= validate_stmt(CHILD(tree
, j
));
2790 res
= validate_newline(CHILD(tree
, j
));
2792 /* This stays in to prevent any internal failures from getting to the
2793 * user. Hopefully, this won't be needed. If a user reports getting
2794 * this, we have some debugging to do.
2796 if (!res
&& !PyErr_Occurred())
2797 err_string("VALIDATION FAILURE: report this to the maintainer!");
2803 validate_encoding_decl(node
*tree
)
2805 int nch
= NCH(tree
);
2806 int res
= ((nch
== 1)
2807 && validate_file_input(CHILD(tree
, 0)));
2809 if (!res
&& !PyErr_Occurred())
2810 err_string("Error Parsing encoding_decl");
2816 pickle_constructor
= NULL
;
2820 parser__pickler(PyObject
*self
, PyObject
*args
)
2822 NOTE(ARGUNUSED(self
))
2823 PyObject
*result
= NULL
;
2824 PyObject
*st
= NULL
;
2825 PyObject
*empty_dict
= NULL
;
2827 if (PyArg_ParseTuple(args
, "O!:_pickler", &PyST_Type
, &st
)) {
2831 if ((empty_dict
= PyDict_New()) == NULL
)
2833 if ((newargs
= Py_BuildValue("Oi", st
, 1)) == NULL
)
2835 tuple
= parser_st2tuple((PyST_Object
*)NULL
, newargs
, empty_dict
);
2836 if (tuple
!= NULL
) {
2837 result
= Py_BuildValue("O(O)", pickle_constructor
, tuple
);
2840 Py_DECREF(empty_dict
);
2844 Py_XDECREF(empty_dict
);
2850 /* Functions exported by this module. Most of this should probably
2851 * be converted into an ST object with methods, but that is better
2852 * done directly in Python, allowing subclasses to be created directly.
2853 * We'd really have to write a wrapper around it all anyway to allow
2856 static PyMethodDef parser_functions
[] = {
2857 {"ast2tuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
2858 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
2859 {"ast2list", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
2860 PyDoc_STR("Creates a list-tree representation of an ST.")},
2861 {"compileast", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
2862 PyDoc_STR("Compiles an ST object into a code object.")},
2863 {"compilest", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
2864 PyDoc_STR("Compiles an ST object into a code object.")},
2865 {"expr", (PyCFunction
)parser_expr
, PUBLIC_METHOD_TYPE
,
2866 PyDoc_STR("Creates an ST object from an expression.")},
2867 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
2868 PyDoc_STR("Determines if an ST object was created from an expression.")},
2869 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
2870 PyDoc_STR("Determines if an ST object was created from a suite.")},
2871 {"suite", (PyCFunction
)parser_suite
, PUBLIC_METHOD_TYPE
,
2872 PyDoc_STR("Creates an ST object from a suite.")},
2873 {"sequence2ast", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
2874 PyDoc_STR("Creates an ST object from a tree representation.")},
2875 {"sequence2st", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
2876 PyDoc_STR("Creates an ST object from a tree representation.")},
2877 {"st2tuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
2878 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
2879 {"st2list", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
2880 PyDoc_STR("Creates a list-tree representation of an ST.")},
2881 {"tuple2ast", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
2882 PyDoc_STR("Creates an ST object from a tree representation.")},
2883 {"tuple2st", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
2884 PyDoc_STR("Creates an ST object from a tree representation.")},
2886 /* private stuff: support pickle module */
2887 {"_pickler", (PyCFunction
)parser__pickler
, METH_VARARGS
,
2888 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
2890 {NULL
, NULL
, 0, NULL
}
2894 PyMODINIT_FUNC
initparser(void); /* supply a prototype */
2899 PyObject
*module
, *copyreg
;
2901 PyST_Type
.ob_type
= &PyType_Type
;
2902 module
= Py_InitModule("parser", parser_functions
);
2904 if (parser_error
== 0)
2905 parser_error
= PyErr_NewException("parser.ParserError", NULL
, NULL
);
2907 if ((parser_error
== 0)
2908 || (PyModule_AddObject(module
, "ParserError", parser_error
) != 0)) {
2909 /* caller will check PyErr_Occurred() */
2912 Py_INCREF(&PyST_Type
);
2913 PyModule_AddObject(module
, "ASTType", (PyObject
*)&PyST_Type
);
2914 Py_INCREF(&PyST_Type
);
2915 PyModule_AddObject(module
, "STType", (PyObject
*)&PyST_Type
);
2917 PyModule_AddStringConstant(module
, "__copyright__",
2918 parser_copyright_string
);
2919 PyModule_AddStringConstant(module
, "__doc__",
2921 PyModule_AddStringConstant(module
, "__version__",
2922 parser_version_string
);
2924 /* Register to support pickling.
2925 * If this fails, the import of this module will fail because an
2926 * exception will be raised here; should we clear the exception?
2928 copyreg
= PyImport_ImportModule("copy_reg");
2929 if (copyreg
!= NULL
) {
2930 PyObject
*func
, *pickler
;
2932 func
= PyObject_GetAttrString(copyreg
, "pickle");
2933 pickle_constructor
= PyObject_GetAttrString(module
, "sequence2st");
2934 pickler
= PyObject_GetAttrString(module
, "_pickler");
2935 Py_XINCREF(pickle_constructor
);
2936 if ((func
!= NULL
) && (pickle_constructor
!= NULL
)
2937 && (pickler
!= NULL
)) {
2940 res
= PyObject_CallFunction(func
, "OOO", &PyST_Type
, pickler
,
2941 pickle_constructor
);
2945 Py_XDECREF(pickle_constructor
);
2946 Py_XDECREF(pickler
);