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() */
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
,
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.
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? */
84 if (ISNONTERMINAL(TYPE(n
))) {
89 v
= mkseq(1 + NCH(n
) + (TYPE(n
) == encoding_decl
));
92 w
= PyInt_FromLong(TYPE(n
));
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
);
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
)));
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
)));
117 (void) addelem(result
, 2, PyInt_FromLong(n
->n_lineno
));
119 (void) addelem(result
, 3, PyInt_FromLong(n
->n_col_offset
));
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. ;-)
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.
156 PyObject_HEAD
/* standard object header */
157 node
* st_node
; /* the node* returned by the parser */
158 int st_type
; /* EXPR or SUITE ? */
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
);
168 PyTypeObject PyST_Type
= {
169 PyVarObject_HEAD_INIT(NULL
, 0)
170 "parser.st", /* tp_name */
171 (int) sizeof(PyST_Object
), /* tp_basicsize */
173 (destructor
)parser_free
, /* tp_dealloc */
175 parser_getattr
, /* tp_getattr */
177 (cmpfunc
)parser_compare
, /* tp_compare */
179 0, /* tp_as_number */
180 0, /* tp_as_sequence */
181 0, /* tp_as_mapping */
188 /* Functions to access object as input/output buffer */
189 0, /* tp_as_buffer */
191 Py_TPFLAGS_DEFAULT
, /* tp_flags */
194 "Intermediate representation of a Python parse tree."
199 parser_compare_nodes(node
*left
, node
*right
)
203 if (TYPE(left
) < TYPE(right
))
206 if (TYPE(right
) < TYPE(left
))
209 if (ISTERMINAL(TYPE(left
)))
210 return (strcmp(STR(left
), STR(right
)));
212 if (NCH(left
) < NCH(right
))
215 if (NCH(right
) < NCH(left
))
218 for (j
= 0; j
< NCH(left
); ++j
) {
219 int v
= parser_compare_nodes(CHILD(left
, j
), CHILD(right
, j
));
228 /* int parser_compare(PyST_Object* left, PyST_Object* right)
230 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
231 * This really just wraps a call to parser_compare_nodes() with some easy
232 * checks and protection code.
236 parser_compare(PyST_Object
*left
, PyST_Object
*right
)
241 if ((left
== 0) || (right
== 0))
244 return (parser_compare_nodes(left
->st_node
, right
->st_node
));
248 /* parser_newstobject(node* st)
250 * Allocates a new Python object representing an ST. This is simply the
251 * 'wrapper' object that holds a node* and allows it to be passed around in
256 parser_newstobject(node
*st
, int type
)
258 PyST_Object
* o
= PyObject_New(PyST_Object
, &PyST_Type
);
267 return ((PyObject
*)o
);
271 /* void parser_free(PyST_Object* st)
273 * This is called by a del statement that reduces the reference count to 0.
277 parser_free(PyST_Object
*st
)
279 PyNode_Free(st
->st_node
);
284 /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
286 * This provides conversion from a node* to a tuple object that can be
287 * returned to the Python-level caller. The ST object is not modified.
291 parser_st2tuple(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
293 PyObject
*line_option
= 0;
294 PyObject
*col_option
= 0;
298 static char *keywords
[] = {"ast", "line_info", "col_info", NULL
};
301 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|OO:st2tuple", keywords
,
302 &PyST_Type
, &self
, &line_option
,
306 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|OO:totuple", &keywords
[1],
307 &line_option
, &col_option
);
311 if (line_option
!= NULL
) {
312 lineno
= (PyObject_IsTrue(line_option
) != 0) ? 1 : 0;
314 if (col_option
!= NULL
) {
315 col_offset
= (PyObject_IsTrue(col_option
) != 0) ? 1 : 0;
318 * Convert ST into a tuple representation. Use Guido's function,
319 * since it's known to work already.
321 res
= node2tuple(((PyST_Object
*)self
)->st_node
,
322 PyTuple_New
, PyTuple_SetItem
, lineno
, col_offset
);
328 parser_ast2tuple(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
330 if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
332 return parser_st2tuple(self
, args
, kw
);
336 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
338 * This provides conversion from a node* to a list object that can be
339 * returned to the Python-level caller. The ST object is not modified.
343 parser_st2list(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
345 PyObject
*line_option
= 0;
346 PyObject
*col_option
= 0;
350 static char *keywords
[] = {"ast", "line_info", "col_info", NULL
};
353 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|OO:st2list", keywords
,
354 &PyST_Type
, &self
, &line_option
,
357 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|OO:tolist", &keywords
[1],
358 &line_option
, &col_option
);
362 if (line_option
!= 0) {
363 lineno
= PyObject_IsTrue(line_option
) ? 1 : 0;
365 if (col_option
!= NULL
) {
366 col_offset
= (PyObject_IsTrue(col_option
) != 0) ? 1 : 0;
369 * Convert ST into a tuple representation. Use Guido's function,
370 * since it's known to work already.
372 res
= node2tuple(self
->st_node
,
373 PyList_New
, PyList_SetItem
, lineno
, col_offset
);
379 parser_ast2list(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
381 if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
383 return parser_st2list(self
, args
, kw
);
387 /* parser_compilest(PyObject* self, PyObject* args)
389 * This function creates code objects from the parse tree represented by
390 * the passed-in data object. An optional file name is passed in as well.
394 parser_compilest(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
397 char* str
= "<syntax-tree>";
400 static char *keywords
[] = {"ast", "filename", NULL
};
403 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|s:compilest", keywords
,
404 &PyST_Type
, &self
, &str
);
406 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|s:compile", &keywords
[1],
410 res
= (PyObject
*)PyNode_Compile(self
->st_node
, str
);
416 parser_compileast(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
418 if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
420 return parser_compilest(self
, args
, kw
);
424 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
425 * PyObject* parser_issuite(PyObject* self, PyObject* args)
427 * Checks the passed-in ST object to determine if it is an expression or
428 * a statement suite, respectively. The return is a Python truth value.
432 parser_isexpr(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
437 static char *keywords
[] = {"ast", NULL
};
440 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:isexpr", keywords
,
443 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":isexpr", &keywords
[1]);
446 /* Check to see if the ST represents an expression or not. */
447 res
= (self
->st_type
== PyST_EXPR
) ? Py_True
: Py_False
;
455 parser_issuite(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
460 static char *keywords
[] = {"ast", NULL
};
463 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:issuite", keywords
,
466 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":issuite", &keywords
[1]);
469 /* Check to see if the ST represents an expression or not. */
470 res
= (self
->st_type
== PyST_EXPR
) ? Py_False
: Py_True
;
477 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
481 {"compile", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
482 PyDoc_STR("Compile this ST object into a code object.")},
483 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
484 PyDoc_STR("Determines if this ST object was created from an expression.")},
485 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
486 PyDoc_STR("Determines if this ST object was created from a suite.")},
487 {"tolist", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
488 PyDoc_STR("Creates a list-tree representation of this ST.")},
489 {"totuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
490 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
492 {NULL
, NULL
, 0, NULL
}
497 parser_getattr(PyObject
*self
, char *name
)
499 return (Py_FindMethod(parser_methods
, self
, name
));
503 /* err_string(char* message)
505 * Sets the error string for an exception of type ParserError.
509 err_string(char *message
)
511 PyErr_SetString(parser_error
, message
);
515 /* PyObject* parser_do_parse(PyObject* args, int type)
517 * Internal function to actually execute the parse and return the result if
518 * successful or set an exception if not.
522 parser_do_parse(PyObject
*args
, PyObject
*kw
, char *argspec
, int type
)
527 static char *keywords
[] = {"source", NULL
};
529 if (PyArg_ParseTupleAndKeywords(args
, kw
, argspec
, keywords
, &string
)) {
530 node
* n
= PyParser_SimpleParseString(string
,
532 ? eval_input
: file_input
);
535 res
= parser_newstobject(n
, type
);
541 /* PyObject* parser_expr(PyObject* self, PyObject* args)
542 * PyObject* parser_suite(PyObject* self, PyObject* args)
544 * External interfaces to the parser itself. Which is called determines if
545 * the parser attempts to recognize an expression ('eval' form) or statement
546 * suite ('exec' form). The real work is done by parser_do_parse() above.
550 parser_expr(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
552 NOTE(ARGUNUSED(self
))
553 return (parser_do_parse(args
, kw
, "s:expr", PyST_EXPR
));
558 parser_suite(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
560 NOTE(ARGUNUSED(self
))
561 return (parser_do_parse(args
, kw
, "s:suite", PyST_SUITE
));
566 /* This is the messy part of the code. Conversion from a tuple to an ST
567 * object requires that the input tuple be valid without having to rely on
568 * catching an exception from the compiler. This is done to allow the
569 * compiler itself to remain fast, since most of its input will come from
570 * the parser directly, and therefore be known to be syntactically correct.
571 * This validation is done to ensure that we don't core dump the compile
572 * phase, returning an exception instead.
574 * Two aspects can be broken out in this code: creating a node tree from
575 * the tuple passed in, and verifying that it is indeed valid. It may be
576 * advantageous to expand the number of ST types to include funcdefs and
577 * lambdadefs to take advantage of the optimizer, recognizing those STs
578 * here. They are not necessary, and not quite as useful in a raw form.
579 * For now, let's get expressions and suites working reliably.
583 static node
* build_node_tree(PyObject
*tuple
);
584 static int validate_expr_tree(node
*tree
);
585 static int validate_file_input(node
*tree
);
586 static int validate_encoding_decl(node
*tree
);
588 /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
590 * This is the public function, called from the Python code. It receives a
591 * single tuple object from the caller, and creates an ST object if the
592 * tuple can be validated. It does this by checking the first code of the
593 * tuple, and, if acceptable, builds the internal representation. If this
594 * step succeeds, the internal representation is validated as fully as
595 * possible with the various validate_*() routines defined below.
597 * This function must be changed if support is to be added for PyST_FRAGMENT
602 parser_tuple2st(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
604 NOTE(ARGUNUSED(self
))
609 static char *keywords
[] = {"sequence", NULL
};
611 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "O:sequence2st", keywords
,
614 if (!PySequence_Check(tuple
)) {
615 PyErr_SetString(PyExc_ValueError
,
616 "sequence2st() requires a single sequence argument");
620 * Convert the tree to the internal form before checking it.
622 tree
= build_node_tree(tuple
);
624 int start_sym
= TYPE(tree
);
625 if (start_sym
== eval_input
) {
626 /* Might be an eval form. */
627 if (validate_expr_tree(tree
))
628 st
= parser_newstobject(tree
, PyST_EXPR
);
632 else if (start_sym
== file_input
) {
633 /* This looks like an exec form so far. */
634 if (validate_file_input(tree
))
635 st
= parser_newstobject(tree
, PyST_SUITE
);
639 else if (start_sym
== encoding_decl
) {
640 /* This looks like an encoding_decl so far. */
641 if (validate_encoding_decl(tree
))
642 st
= parser_newstobject(tree
, PyST_SUITE
);
647 /* This is a fragment, at best. */
649 err_string("parse tree does not use a valid start symbol");
652 /* Make sure we throw an exception on all errors. We should never
653 * get this, but we'd do well to be sure something is done.
655 if (st
== NULL
&& !PyErr_Occurred())
656 err_string("unspecified ST error occurred");
662 parser_tuple2ast(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
664 if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
666 return parser_tuple2st(self
, args
, kw
);
670 /* node* build_node_children()
672 * Iterate across the children of the current non-terminal node and build
673 * their structures. If successful, return the root of this portion of
674 * the tree, otherwise, 0. Any required exception will be specified already,
675 * and no memory will have been deallocated.
679 build_node_children(PyObject
*tuple
, node
*root
, int *line_num
)
681 Py_ssize_t len
= PyObject_Size(tuple
);
685 for (i
= 1; i
< len
; ++i
) {
686 /* elem must always be a sequence, however simple */
687 PyObject
* elem
= PySequence_GetItem(tuple
, i
);
688 int ok
= elem
!= NULL
;
693 ok
= PySequence_Check(elem
);
695 PyObject
*temp
= PySequence_GetItem(elem
, 0);
699 ok
= PyInt_Check(temp
);
701 type
= PyInt_AS_LONG(temp
);
706 PyObject
*err
= Py_BuildValue("os", elem
,
707 "Illegal node construct.");
708 PyErr_SetObject(parser_error
, err
);
713 if (ISTERMINAL(type
)) {
714 Py_ssize_t len
= PyObject_Size(elem
);
717 if ((len
!= 2) && (len
!= 3)) {
718 err_string("terminal nodes must have 2 or 3 entries");
721 temp
= PySequence_GetItem(elem
, 1);
724 if (!PyString_Check(temp
)) {
725 PyErr_Format(parser_error
,
726 "second item in terminal node must be a string,"
728 Py_TYPE(temp
)->tp_name
);
733 PyObject
*o
= PySequence_GetItem(elem
, 2);
736 *line_num
= PyInt_AS_LONG(o
);
738 PyErr_Format(parser_error
,
739 "third item in terminal node must be an"
740 " integer, found %s",
741 Py_TYPE(temp
)->tp_name
);
749 len
= PyString_GET_SIZE(temp
) + 1;
750 strn
= (char *)PyObject_MALLOC(len
);
752 (void) memcpy(strn
, PyString_AS_STRING(temp
), len
);
755 else if (!ISNONTERMINAL(type
)) {
757 * It has to be one or the other; this is an error.
758 * Throw an exception.
760 PyObject
*err
= Py_BuildValue("os", elem
, "unknown node type.");
761 PyErr_SetObject(parser_error
, err
);
766 err
= PyNode_AddChild(root
, type
, strn
, *line_num
, 0);
767 if (err
== E_NOMEM
) {
769 return (node
*) PyErr_NoMemory();
771 if (err
== E_OVERFLOW
) {
773 PyErr_SetString(PyExc_ValueError
,
774 "unsupported number of child nodes");
778 if (ISNONTERMINAL(type
)) {
779 node
* new_child
= CHILD(root
, i
- 1);
781 if (new_child
!= build_node_children(elem
, new_child
, line_num
)) {
786 else if (type
== NEWLINE
) { /* It's true: we increment the */
787 ++(*line_num
); /* line number *after* the newline! */
796 build_node_tree(PyObject
*tuple
)
799 PyObject
*temp
= PySequence_GetItem(tuple
, 0);
803 num
= PyInt_AsLong(temp
);
805 if (ISTERMINAL(num
)) {
807 * The tuple is simple, but it doesn't start with a start symbol.
808 * Throw an exception now and be done with it.
810 tuple
= Py_BuildValue("os", tuple
,
811 "Illegal syntax-tree; cannot start with terminal symbol.");
812 PyErr_SetObject(parser_error
, tuple
);
815 else if (ISNONTERMINAL(num
)) {
817 * Not efficient, but that can be handled later.
820 PyObject
*encoding
= NULL
;
822 if (num
== encoding_decl
) {
823 encoding
= PySequence_GetItem(tuple
, 2);
824 /* tuple isn't borrowed anymore here, need to DECREF */
825 tuple
= PySequence_GetSlice(tuple
, 0, 2);
827 res
= PyNode_New(num
);
829 if (res
!= build_node_children(tuple
, res
, &line_num
)) {
833 if (res
&& encoding
) {
835 len
= PyString_GET_SIZE(encoding
) + 1;
836 res
->n_str
= (char *)PyObject_MALLOC(len
);
837 if (res
->n_str
!= NULL
)
838 (void) memcpy(res
->n_str
, PyString_AS_STRING(encoding
), len
);
845 /* The tuple is illegal -- if the number is neither TERMINAL nor
846 * NONTERMINAL, we can't use it. Not sure the implementation
847 * allows this condition, but the API doesn't preclude it.
849 PyObject
*err
= Py_BuildValue("os", tuple
,
850 "Illegal component tuple.");
851 PyErr_SetObject(parser_error
, err
);
860 * Validation routines used within the validation section:
862 static int validate_terminal(node
*terminal
, int type
, char *string
);
864 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
865 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
866 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
867 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
868 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
869 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
870 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
871 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
872 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
873 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
874 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
875 #define validate_star(ch) validate_terminal(ch, STAR, "*")
876 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
877 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
878 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
879 #define validate_at(ch) validate_terminal(ch, AT, "@")
880 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
882 #define VALIDATER(n) static int validate_##n(node *tree)
884 VALIDATER(node
); VALIDATER(small_stmt
);
885 VALIDATER(class); VALIDATER(node
);
886 VALIDATER(parameters
); VALIDATER(suite
);
887 VALIDATER(testlist
); VALIDATER(varargslist
);
888 VALIDATER(fpdef
); VALIDATER(fplist
);
889 VALIDATER(stmt
); VALIDATER(simple_stmt
);
890 VALIDATER(expr_stmt
); VALIDATER(power
);
891 VALIDATER(print_stmt
); VALIDATER(del_stmt
);
892 VALIDATER(return_stmt
); VALIDATER(list_iter
);
893 VALIDATER(raise_stmt
); VALIDATER(import_stmt
);
894 VALIDATER(import_name
); VALIDATER(import_from
);
895 VALIDATER(global_stmt
); VALIDATER(list_if
);
896 VALIDATER(assert_stmt
); VALIDATER(list_for
);
897 VALIDATER(exec_stmt
); VALIDATER(compound_stmt
);
898 VALIDATER(while); VALIDATER(for);
899 VALIDATER(try); VALIDATER(except_clause
);
900 VALIDATER(test
); VALIDATER(and_test
);
901 VALIDATER(not_test
); VALIDATER(comparison
);
902 VALIDATER(comp_op
); VALIDATER(expr
);
903 VALIDATER(xor_expr
); VALIDATER(and_expr
);
904 VALIDATER(shift_expr
); VALIDATER(arith_expr
);
905 VALIDATER(term
); VALIDATER(factor
);
906 VALIDATER(atom
); VALIDATER(lambdef
);
907 VALIDATER(trailer
); VALIDATER(subscript
);
908 VALIDATER(subscriptlist
); VALIDATER(sliceop
);
909 VALIDATER(exprlist
); VALIDATER(dictmaker
);
910 VALIDATER(arglist
); VALIDATER(argument
);
911 VALIDATER(listmaker
); VALIDATER(yield_stmt
);
912 VALIDATER(testlist1
); VALIDATER(gen_for
);
913 VALIDATER(gen_iter
); VALIDATER(gen_if
);
914 VALIDATER(testlist_gexp
); VALIDATER(yield_expr
);
915 VALIDATER(yield_or_testlist
); VALIDATER(or_test
);
916 VALIDATER(old_test
); VALIDATER(old_lambdef
);
920 #define is_even(n) (((n) & 1) == 0)
921 #define is_odd(n) (((n) & 1) == 1)
925 validate_ntype(node
*n
, int t
)
928 PyErr_Format(parser_error
, "Expected node type %d, got %d.",
936 /* Verifies that the number of child nodes is exactly 'num', raising
937 * an exception if it isn't. The exception message does not indicate
938 * the exact number of nodes, allowing this to be used to raise the
939 * "right" exception when the wrong number of nodes is present in a
940 * specific variant of a statement's syntax. This is commonly used
944 validate_numnodes(node
*n
, int num
, const char *const name
)
947 PyErr_Format(parser_error
,
948 "Illegal number of children for %s node.", name
);
956 validate_terminal(node
*terminal
, int type
, char *string
)
958 int res
= (validate_ntype(terminal
, type
)
959 && ((string
== 0) || (strcmp(string
, STR(terminal
)) == 0)));
961 if (!res
&& !PyErr_Occurred()) {
962 PyErr_Format(parser_error
,
963 "Illegal terminal: expected \"%s\"", string
);
972 validate_repeating_list(node
*tree
, int ntype
, int (*vfunc
)(node
*),
973 const char *const name
)
976 int res
= (nch
&& validate_ntype(tree
, ntype
)
977 && vfunc(CHILD(tree
, 0)));
979 if (!res
&& !PyErr_Occurred())
980 (void) validate_numnodes(tree
, 1, name
);
983 res
= validate_comma(CHILD(tree
, --nch
));
984 if (res
&& nch
> 1) {
986 for ( ; res
&& pos
< nch
; pos
+= 2)
987 res
= (validate_comma(CHILD(tree
, pos
))
988 && vfunc(CHILD(tree
, pos
+ 1)));
998 * 'class' NAME ['(' testlist ')'] ':' suite
1001 validate_class(node
*tree
)
1003 int nch
= NCH(tree
);
1004 int res
= (validate_ntype(tree
, classdef
) &&
1005 ((nch
== 4) || (nch
== 6) || (nch
== 7)));
1008 res
= (validate_name(CHILD(tree
, 0), "class")
1009 && validate_ntype(CHILD(tree
, 1), NAME
)
1010 && validate_colon(CHILD(tree
, nch
- 2))
1011 && validate_suite(CHILD(tree
, nch
- 1)));
1014 (void) validate_numnodes(tree
, 4, "class");
1019 res
= ((validate_lparen(CHILD(tree
, 2)) &&
1020 validate_testlist(CHILD(tree
, 3)) &&
1021 validate_rparen(CHILD(tree
, 4))));
1023 else if (nch
== 6) {
1024 res
= (validate_lparen(CHILD(tree
,2)) &&
1025 validate_rparen(CHILD(tree
,3)));
1033 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1036 validate_if(node
*tree
)
1038 int nch
= NCH(tree
);
1039 int res
= (validate_ntype(tree
, if_stmt
)
1041 && validate_name(CHILD(tree
, 0), "if")
1042 && validate_test(CHILD(tree
, 1))
1043 && validate_colon(CHILD(tree
, 2))
1044 && validate_suite(CHILD(tree
, 3)));
1046 if (res
&& ((nch
% 4) == 3)) {
1047 /* ... 'else' ':' suite */
1048 res
= (validate_name(CHILD(tree
, nch
- 3), "else")
1049 && validate_colon(CHILD(tree
, nch
- 2))
1050 && validate_suite(CHILD(tree
, nch
- 1)));
1053 else if (!res
&& !PyErr_Occurred())
1054 (void) validate_numnodes(tree
, 4, "if");
1056 /* Will catch the case for nch < 4 */
1057 res
= validate_numnodes(tree
, 0, "if");
1058 else if (res
&& (nch
> 4)) {
1059 /* ... ('elif' test ':' suite)+ ... */
1061 while ((j
< nch
) && res
) {
1062 res
= (validate_name(CHILD(tree
, j
), "elif")
1063 && validate_colon(CHILD(tree
, j
+ 2))
1064 && validate_test(CHILD(tree
, j
+ 1))
1065 && validate_suite(CHILD(tree
, j
+ 3)));
1074 * '(' [varargslist] ')'
1078 validate_parameters(node
*tree
)
1080 int nch
= NCH(tree
);
1081 int res
= validate_ntype(tree
, parameters
) && ((nch
== 2) || (nch
== 3));
1084 res
= (validate_lparen(CHILD(tree
, 0))
1085 && validate_rparen(CHILD(tree
, nch
- 1)));
1086 if (res
&& (nch
== 3))
1087 res
= validate_varargslist(CHILD(tree
, 1));
1090 (void) validate_numnodes(tree
, 2, "parameters");
1100 * | NEWLINE INDENT stmt+ DEDENT
1103 validate_suite(node
*tree
)
1105 int nch
= NCH(tree
);
1106 int res
= (validate_ntype(tree
, suite
) && ((nch
== 1) || (nch
>= 4)));
1108 if (res
&& (nch
== 1))
1109 res
= validate_simple_stmt(CHILD(tree
, 0));
1111 /* NEWLINE INDENT stmt+ DEDENT */
1112 res
= (validate_newline(CHILD(tree
, 0))
1113 && validate_indent(CHILD(tree
, 1))
1114 && validate_stmt(CHILD(tree
, 2))
1115 && validate_dedent(CHILD(tree
, nch
- 1)));
1117 if (res
&& (nch
> 4)) {
1119 --nch
; /* forget the DEDENT */
1120 for ( ; res
&& (i
< nch
); ++i
)
1121 res
= validate_stmt(CHILD(tree
, i
));
1124 res
= validate_numnodes(tree
, 4, "suite");
1131 validate_testlist(node
*tree
)
1133 return (validate_repeating_list(tree
, testlist
,
1134 validate_test
, "testlist"));
1139 validate_testlist1(node
*tree
)
1141 return (validate_repeating_list(tree
, testlist1
,
1142 validate_test
, "testlist1"));
1147 validate_testlist_safe(node
*tree
)
1149 return (validate_repeating_list(tree
, testlist_safe
,
1150 validate_old_test
, "testlist_safe"));
1154 /* '*' NAME [',' '**' NAME] | '**' NAME
1157 validate_varargslist_trailer(node
*tree
, int start
)
1159 int nch
= NCH(tree
);
1164 err_string("expected variable argument trailer for varargslist");
1167 sym
= TYPE(CHILD(tree
, start
));
1170 * ('*' NAME [',' '**' NAME]
1173 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1174 else if (nch
-start
== 5)
1175 res
= (validate_name(CHILD(tree
, start
+1), NULL
)
1176 && validate_comma(CHILD(tree
, start
+2))
1177 && validate_doublestar(CHILD(tree
, start
+3))
1178 && validate_name(CHILD(tree
, start
+4), NULL
));
1180 else if (sym
== DOUBLESTAR
) {
1185 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1188 err_string("illegal variable argument trailer for varargslist");
1193 /* validate_varargslist()
1196 * (fpdef ['=' test] ',')*
1197 * ('*' NAME [',' '**' NAME]
1199 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1203 validate_varargslist(node
*tree
)
1205 int nch
= NCH(tree
);
1206 int res
= validate_ntype(tree
, varargslist
) && (nch
!= 0);
1212 err_string("varargslist missing child nodes");
1215 sym
= TYPE(CHILD(tree
, 0));
1216 if (sym
== STAR
|| sym
== DOUBLESTAR
)
1217 /* whole thing matches:
1218 * '*' NAME [',' '**' NAME] | '**' NAME
1220 res
= validate_varargslist_trailer(tree
, 0);
1221 else if (sym
== fpdef
) {
1224 sym
= TYPE(CHILD(tree
, nch
-1));
1227 * (fpdef ['=' test] ',')+
1228 * ('*' NAME [',' '**' NAME]
1231 /* skip over (fpdef ['=' test] ',')+ */
1232 while (res
&& (i
+2 <= nch
)) {
1233 res
= validate_fpdef(CHILD(tree
, i
));
1235 if (res
&& TYPE(CHILD(tree
, i
)) == EQUAL
&& (i
+2 <= nch
)) {
1236 res
= (validate_equal(CHILD(tree
, i
))
1237 && validate_test(CHILD(tree
, i
+1)));
1241 if (res
&& i
< nch
) {
1242 res
= validate_comma(CHILD(tree
, i
));
1245 && (TYPE(CHILD(tree
, i
)) == DOUBLESTAR
1246 || TYPE(CHILD(tree
, i
)) == STAR
))
1250 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1254 res
= validate_varargslist_trailer(tree
, i
);
1258 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1260 /* strip trailing comma node */
1262 res
= validate_comma(CHILD(tree
, nch
-1));
1268 * fpdef ['=' test] (',' fpdef ['=' test])*
1270 res
= validate_fpdef(CHILD(tree
, 0));
1272 if (res
&& (i
+2 <= nch
) && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1273 res
= (validate_equal(CHILD(tree
, i
))
1274 && validate_test(CHILD(tree
, i
+1)));
1278 * ... (',' fpdef ['=' test])*
1281 while (res
&& (nch
- i
) >= 2) {
1282 res
= (validate_comma(CHILD(tree
, i
))
1283 && validate_fpdef(CHILD(tree
, i
+1)));
1285 if (res
&& (nch
- i
) >= 2 && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1286 res
= (validate_equal(CHILD(tree
, i
))
1287 && validate_test(CHILD(tree
, i
+1)));
1291 if (res
&& nch
- i
!= 0) {
1293 err_string("illegal formation for varargslist");
1301 /* list_iter: list_for | list_if
1304 validate_list_iter(node
*tree
)
1306 int res
= (validate_ntype(tree
, list_iter
)
1307 && validate_numnodes(tree
, 1, "list_iter"));
1308 if (res
&& TYPE(CHILD(tree
, 0)) == list_for
)
1309 res
= validate_list_for(CHILD(tree
, 0));
1311 res
= validate_list_if(CHILD(tree
, 0));
1316 /* gen_iter: gen_for | gen_if
1319 validate_gen_iter(node
*tree
)
1321 int res
= (validate_ntype(tree
, gen_iter
)
1322 && validate_numnodes(tree
, 1, "gen_iter"));
1323 if (res
&& TYPE(CHILD(tree
, 0)) == gen_for
)
1324 res
= validate_gen_for(CHILD(tree
, 0));
1326 res
= validate_gen_if(CHILD(tree
, 0));
1331 /* list_for: 'for' exprlist 'in' testlist [list_iter]
1334 validate_list_for(node
*tree
)
1336 int nch
= NCH(tree
);
1340 res
= validate_list_iter(CHILD(tree
, 4));
1342 res
= validate_numnodes(tree
, 4, "list_for");
1345 res
= (validate_name(CHILD(tree
, 0), "for")
1346 && validate_exprlist(CHILD(tree
, 1))
1347 && validate_name(CHILD(tree
, 2), "in")
1348 && validate_testlist_safe(CHILD(tree
, 3)));
1353 /* gen_for: 'for' exprlist 'in' test [gen_iter]
1356 validate_gen_for(node
*tree
)
1358 int nch
= NCH(tree
);
1362 res
= validate_gen_iter(CHILD(tree
, 4));
1364 res
= validate_numnodes(tree
, 4, "gen_for");
1367 res
= (validate_name(CHILD(tree
, 0), "for")
1368 && validate_exprlist(CHILD(tree
, 1))
1369 && validate_name(CHILD(tree
, 2), "in")
1370 && validate_or_test(CHILD(tree
, 3)));
1375 /* list_if: 'if' old_test [list_iter]
1378 validate_list_if(node
*tree
)
1380 int nch
= NCH(tree
);
1384 res
= validate_list_iter(CHILD(tree
, 2));
1386 res
= validate_numnodes(tree
, 2, "list_if");
1389 res
= (validate_name(CHILD(tree
, 0), "if")
1390 && validate_old_test(CHILD(tree
, 1)));
1395 /* gen_if: 'if' old_test [gen_iter]
1398 validate_gen_if(node
*tree
)
1400 int nch
= NCH(tree
);
1404 res
= validate_gen_iter(CHILD(tree
, 2));
1406 res
= validate_numnodes(tree
, 2, "gen_if");
1409 res
= (validate_name(CHILD(tree
, 0), "if")
1410 && validate_old_test(CHILD(tree
, 1)));
1422 validate_fpdef(node
*tree
)
1424 int nch
= NCH(tree
);
1425 int res
= validate_ntype(tree
, fpdef
);
1429 res
= validate_ntype(CHILD(tree
, 0), NAME
);
1431 res
= (validate_lparen(CHILD(tree
, 0))
1432 && validate_fplist(CHILD(tree
, 1))
1433 && validate_rparen(CHILD(tree
, 2)));
1435 res
= validate_numnodes(tree
, 1, "fpdef");
1442 validate_fplist(node
*tree
)
1444 return (validate_repeating_list(tree
, fplist
,
1445 validate_fpdef
, "fplist"));
1449 /* simple_stmt | compound_stmt
1453 validate_stmt(node
*tree
)
1455 int res
= (validate_ntype(tree
, stmt
)
1456 && validate_numnodes(tree
, 1, "stmt"));
1459 tree
= CHILD(tree
, 0);
1461 if (TYPE(tree
) == simple_stmt
)
1462 res
= validate_simple_stmt(tree
);
1464 res
= validate_compound_stmt(tree
);
1470 /* small_stmt (';' small_stmt)* [';'] NEWLINE
1474 validate_simple_stmt(node
*tree
)
1476 int nch
= NCH(tree
);
1477 int res
= (validate_ntype(tree
, simple_stmt
)
1479 && validate_small_stmt(CHILD(tree
, 0))
1480 && validate_newline(CHILD(tree
, nch
- 1)));
1483 res
= validate_numnodes(tree
, 2, "simple_stmt");
1484 --nch
; /* forget the NEWLINE */
1485 if (res
&& is_even(nch
))
1486 res
= validate_semi(CHILD(tree
, --nch
));
1487 if (res
&& (nch
> 2)) {
1490 for (i
= 1; res
&& (i
< nch
); i
+= 2)
1491 res
= (validate_semi(CHILD(tree
, i
))
1492 && validate_small_stmt(CHILD(tree
, i
+ 1)));
1499 validate_small_stmt(node
*tree
)
1501 int nch
= NCH(tree
);
1502 int res
= validate_numnodes(tree
, 1, "small_stmt");
1505 int ntype
= TYPE(CHILD(tree
, 0));
1507 if ( (ntype
== expr_stmt
)
1508 || (ntype
== print_stmt
)
1509 || (ntype
== del_stmt
)
1510 || (ntype
== pass_stmt
)
1511 || (ntype
== flow_stmt
)
1512 || (ntype
== import_stmt
)
1513 || (ntype
== global_stmt
)
1514 || (ntype
== assert_stmt
)
1515 || (ntype
== exec_stmt
))
1516 res
= validate_node(CHILD(tree
, 0));
1519 err_string("illegal small_stmt child type");
1522 else if (nch
== 1) {
1524 PyErr_Format(parser_error
,
1525 "Unrecognized child node of small_stmt: %d.",
1526 TYPE(CHILD(tree
, 0)));
1533 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated
1536 validate_compound_stmt(node
*tree
)
1538 int res
= (validate_ntype(tree
, compound_stmt
)
1539 && validate_numnodes(tree
, 1, "compound_stmt"));
1545 tree
= CHILD(tree
, 0);
1547 if ( (ntype
== if_stmt
)
1548 || (ntype
== while_stmt
)
1549 || (ntype
== for_stmt
)
1550 || (ntype
== try_stmt
)
1551 || (ntype
== funcdef
)
1552 || (ntype
== classdef
)
1553 || (ntype
== decorated
))
1554 res
= validate_node(tree
);
1557 PyErr_Format(parser_error
,
1558 "Illegal compound statement type: %d.", TYPE(tree
));
1564 validate_yield_or_testlist(node
*tree
)
1566 if (TYPE(tree
) == yield_expr
)
1567 return validate_yield_expr(tree
);
1569 return validate_testlist(tree
);
1573 validate_expr_stmt(node
*tree
)
1576 int nch
= NCH(tree
);
1577 int res
= (validate_ntype(tree
, expr_stmt
)
1579 && validate_testlist(CHILD(tree
, 0)));
1582 && TYPE(CHILD(tree
, 1)) == augassign
) {
1583 res
= validate_numnodes(CHILD(tree
, 1), 1, "augassign")
1584 && validate_yield_or_testlist(CHILD(tree
, 2));
1587 char *s
= STR(CHILD(CHILD(tree
, 1), 0));
1589 res
= (strcmp(s
, "+=") == 0
1590 || strcmp(s
, "-=") == 0
1591 || strcmp(s
, "*=") == 0
1592 || strcmp(s
, "/=") == 0
1593 || strcmp(s
, "//=") == 0
1594 || strcmp(s
, "%=") == 0
1595 || strcmp(s
, "&=") == 0
1596 || strcmp(s
, "|=") == 0
1597 || strcmp(s
, "^=") == 0
1598 || strcmp(s
, "<<=") == 0
1599 || strcmp(s
, ">>=") == 0
1600 || strcmp(s
, "**=") == 0);
1602 err_string("illegal augmmented assignment operator");
1606 for (j
= 1; res
&& (j
< nch
); j
+= 2)
1607 res
= validate_equal(CHILD(tree
, j
))
1608 && validate_yield_or_testlist(CHILD(tree
, j
+ 1));
1616 * 'print' ( [ test (',' test)* [','] ]
1617 * | '>>' test [ (',' test)+ [','] ] )
1620 validate_print_stmt(node
*tree
)
1622 int nch
= NCH(tree
);
1623 int res
= (validate_ntype(tree
, print_stmt
)
1625 && validate_name(CHILD(tree
, 0), "print"));
1627 if (res
&& nch
> 1) {
1628 int sym
= TYPE(CHILD(tree
, 1));
1630 int allow_trailing_comma
= 1;
1633 res
= validate_test(CHILD(tree
, i
++));
1636 res
= validate_numnodes(tree
, 3, "print_stmt");
1638 res
= (validate_ntype(CHILD(tree
, i
), RIGHTSHIFT
)
1639 && validate_test(CHILD(tree
, i
+1)));
1641 allow_trailing_comma
= 0;
1645 /* ... (',' test)* [','] */
1646 while (res
&& i
+2 <= nch
) {
1647 res
= (validate_comma(CHILD(tree
, i
))
1648 && validate_test(CHILD(tree
, i
+1)));
1649 allow_trailing_comma
= 1;
1652 if (res
&& !allow_trailing_comma
)
1653 res
= validate_numnodes(tree
, i
, "print_stmt");
1654 else if (res
&& i
< nch
)
1655 res
= validate_comma(CHILD(tree
, i
));
1663 validate_del_stmt(node
*tree
)
1665 return (validate_numnodes(tree
, 2, "del_stmt")
1666 && validate_name(CHILD(tree
, 0), "del")
1667 && validate_exprlist(CHILD(tree
, 1)));
1672 validate_return_stmt(node
*tree
)
1674 int nch
= NCH(tree
);
1675 int res
= (validate_ntype(tree
, return_stmt
)
1676 && ((nch
== 1) || (nch
== 2))
1677 && validate_name(CHILD(tree
, 0), "return"));
1679 if (res
&& (nch
== 2))
1680 res
= validate_testlist(CHILD(tree
, 1));
1687 validate_raise_stmt(node
*tree
)
1689 int nch
= NCH(tree
);
1690 int res
= (validate_ntype(tree
, raise_stmt
)
1691 && ((nch
== 1) || (nch
== 2) || (nch
== 4) || (nch
== 6)));
1694 res
= validate_name(CHILD(tree
, 0), "raise");
1695 if (res
&& (nch
>= 2))
1696 res
= validate_test(CHILD(tree
, 1));
1697 if (res
&& nch
> 2) {
1698 res
= (validate_comma(CHILD(tree
, 2))
1699 && validate_test(CHILD(tree
, 3)));
1700 if (res
&& (nch
> 4))
1701 res
= (validate_comma(CHILD(tree
, 4))
1702 && validate_test(CHILD(tree
, 5)));
1706 (void) validate_numnodes(tree
, 2, "raise");
1707 if (res
&& (nch
== 4))
1708 res
= (validate_comma(CHILD(tree
, 2))
1709 && validate_test(CHILD(tree
, 3)));
1715 /* yield_expr: 'yield' [testlist]
1718 validate_yield_expr(node
*tree
)
1720 int nch
= NCH(tree
);
1721 int res
= (validate_ntype(tree
, yield_expr
)
1722 && ((nch
== 1) || (nch
== 2))
1723 && validate_name(CHILD(tree
, 0), "yield"));
1725 if (res
&& (nch
== 2))
1726 res
= validate_testlist(CHILD(tree
, 1));
1732 /* yield_stmt: yield_expr
1735 validate_yield_stmt(node
*tree
)
1737 return (validate_ntype(tree
, yield_stmt
)
1738 && validate_numnodes(tree
, 1, "yield_stmt")
1739 && validate_yield_expr(CHILD(tree
, 0)));
1744 validate_import_as_name(node
*tree
)
1746 int nch
= NCH(tree
);
1747 int ok
= validate_ntype(tree
, import_as_name
);
1751 ok
= validate_name(CHILD(tree
, 0), NULL
);
1753 ok
= (validate_name(CHILD(tree
, 0), NULL
)
1754 && validate_name(CHILD(tree
, 1), "as")
1755 && validate_name(CHILD(tree
, 2), NULL
));
1757 ok
= validate_numnodes(tree
, 3, "import_as_name");
1763 /* dotted_name: NAME ("." NAME)*
1766 validate_dotted_name(node
*tree
)
1768 int nch
= NCH(tree
);
1769 int res
= (validate_ntype(tree
, dotted_name
)
1771 && validate_name(CHILD(tree
, 0), NULL
));
1774 for (i
= 1; res
&& (i
< nch
); i
+= 2) {
1775 res
= (validate_dot(CHILD(tree
, i
))
1776 && validate_name(CHILD(tree
, i
+1), NULL
));
1782 /* dotted_as_name: dotted_name [NAME NAME]
1785 validate_dotted_as_name(node
*tree
)
1787 int nch
= NCH(tree
);
1788 int res
= validate_ntype(tree
, dotted_as_name
);
1792 res
= validate_dotted_name(CHILD(tree
, 0));
1794 res
= (validate_dotted_name(CHILD(tree
, 0))
1795 && validate_name(CHILD(tree
, 1), "as")
1796 && validate_name(CHILD(tree
, 2), NULL
));
1799 err_string("illegal number of children for dotted_as_name");
1806 /* dotted_as_name (',' dotted_as_name)* */
1808 validate_dotted_as_names(node
*tree
)
1810 int nch
= NCH(tree
);
1811 int res
= is_odd(nch
) && validate_dotted_as_name(CHILD(tree
, 0));
1814 for (i
= 1; res
&& (i
< nch
); i
+= 2)
1815 res
= (validate_comma(CHILD(tree
, i
))
1816 && validate_dotted_as_name(CHILD(tree
, i
+ 1)));
1821 /* import_as_name (',' import_as_name)* [','] */
1823 validate_import_as_names(node
*tree
)
1825 int nch
= NCH(tree
);
1826 int res
= validate_import_as_name(CHILD(tree
, 0));
1829 for (i
= 1; res
&& (i
+ 1 < nch
); i
+= 2)
1830 res
= (validate_comma(CHILD(tree
, i
))
1831 && validate_import_as_name(CHILD(tree
, i
+ 1)));
1836 /* 'import' dotted_as_names */
1838 validate_import_name(node
*tree
)
1840 return (validate_ntype(tree
, import_name
)
1841 && validate_numnodes(tree
, 2, "import_name")
1842 && validate_name(CHILD(tree
, 0), "import")
1843 && validate_dotted_as_names(CHILD(tree
, 1)));
1846 /* Helper function to count the number of leading dots in
1847 * 'from ...module import name'
1850 count_from_dots(node
*tree
)
1853 for (i
= 0; i
< NCH(tree
); i
++)
1854 if (TYPE(CHILD(tree
, i
)) != DOT
)
1859 /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
1863 validate_import_from(node
*tree
)
1865 int nch
= NCH(tree
);
1866 int ndots
= count_from_dots(tree
);
1867 int havename
= (TYPE(CHILD(tree
, ndots
+ 1)) == dotted_name
);
1868 int offset
= ndots
+ havename
;
1869 int res
= validate_ntype(tree
, import_from
)
1870 && (nch
>= 4 + ndots
)
1871 && validate_name(CHILD(tree
, 0), "from")
1872 && (!havename
|| validate_dotted_name(CHILD(tree
, ndots
+ 1)))
1873 && validate_name(CHILD(tree
, offset
+ 1), "import");
1875 if (res
&& TYPE(CHILD(tree
, offset
+ 2)) == LPAR
)
1876 res
= ((nch
== offset
+ 5)
1877 && validate_lparen(CHILD(tree
, offset
+ 2))
1878 && validate_import_as_names(CHILD(tree
, offset
+ 3))
1879 && validate_rparen(CHILD(tree
, offset
+ 4)));
1880 else if (res
&& TYPE(CHILD(tree
, offset
+ 2)) != STAR
)
1881 res
= validate_import_as_names(CHILD(tree
, offset
+ 2));
1886 /* import_stmt: import_name | import_from */
1888 validate_import_stmt(node
*tree
)
1890 int nch
= NCH(tree
);
1891 int res
= validate_numnodes(tree
, 1, "import_stmt");
1894 int ntype
= TYPE(CHILD(tree
, 0));
1896 if (ntype
== import_name
|| ntype
== import_from
)
1897 res
= validate_node(CHILD(tree
, 0));
1900 err_string("illegal import_stmt child type");
1903 else if (nch
== 1) {
1905 PyErr_Format(parser_error
,
1906 "Unrecognized child node of import_stmt: %d.",
1907 TYPE(CHILD(tree
, 0)));
1916 validate_global_stmt(node
*tree
)
1919 int nch
= NCH(tree
);
1920 int res
= (validate_ntype(tree
, global_stmt
)
1921 && is_even(nch
) && (nch
>= 2));
1923 if (!res
&& !PyErr_Occurred())
1924 err_string("illegal global statement");
1927 res
= (validate_name(CHILD(tree
, 0), "global")
1928 && validate_ntype(CHILD(tree
, 1), NAME
));
1929 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1930 res
= (validate_comma(CHILD(tree
, j
))
1931 && validate_ntype(CHILD(tree
, j
+ 1), NAME
));
1939 * 'exec' expr ['in' test [',' test]]
1942 validate_exec_stmt(node
*tree
)
1944 int nch
= NCH(tree
);
1945 int res
= (validate_ntype(tree
, exec_stmt
)
1946 && ((nch
== 2) || (nch
== 4) || (nch
== 6))
1947 && validate_name(CHILD(tree
, 0), "exec")
1948 && validate_expr(CHILD(tree
, 1)));
1950 if (!res
&& !PyErr_Occurred())
1951 err_string("illegal exec statement");
1952 if (res
&& (nch
> 2))
1953 res
= (validate_name(CHILD(tree
, 2), "in")
1954 && validate_test(CHILD(tree
, 3)));
1955 if (res
&& (nch
== 6))
1956 res
= (validate_comma(CHILD(tree
, 4))
1957 && validate_test(CHILD(tree
, 5)));
1965 * 'assert' test [',' test]
1968 validate_assert_stmt(node
*tree
)
1970 int nch
= NCH(tree
);
1971 int res
= (validate_ntype(tree
, assert_stmt
)
1972 && ((nch
== 2) || (nch
== 4))
1973 && (validate_name(CHILD(tree
, 0), "assert"))
1974 && validate_test(CHILD(tree
, 1)));
1976 if (!res
&& !PyErr_Occurred())
1977 err_string("illegal assert statement");
1978 if (res
&& (nch
> 2))
1979 res
= (validate_comma(CHILD(tree
, 2))
1980 && validate_test(CHILD(tree
, 3)));
1987 validate_while(node
*tree
)
1989 int nch
= NCH(tree
);
1990 int res
= (validate_ntype(tree
, while_stmt
)
1991 && ((nch
== 4) || (nch
== 7))
1992 && validate_name(CHILD(tree
, 0), "while")
1993 && validate_test(CHILD(tree
, 1))
1994 && validate_colon(CHILD(tree
, 2))
1995 && validate_suite(CHILD(tree
, 3)));
1997 if (res
&& (nch
== 7))
1998 res
= (validate_name(CHILD(tree
, 4), "else")
1999 && validate_colon(CHILD(tree
, 5))
2000 && validate_suite(CHILD(tree
, 6)));
2007 validate_for(node
*tree
)
2009 int nch
= NCH(tree
);
2010 int res
= (validate_ntype(tree
, for_stmt
)
2011 && ((nch
== 6) || (nch
== 9))
2012 && validate_name(CHILD(tree
, 0), "for")
2013 && validate_exprlist(CHILD(tree
, 1))
2014 && validate_name(CHILD(tree
, 2), "in")
2015 && validate_testlist(CHILD(tree
, 3))
2016 && validate_colon(CHILD(tree
, 4))
2017 && validate_suite(CHILD(tree
, 5)));
2019 if (res
&& (nch
== 9))
2020 res
= (validate_name(CHILD(tree
, 6), "else")
2021 && validate_colon(CHILD(tree
, 7))
2022 && validate_suite(CHILD(tree
, 8)));
2029 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2030 * | 'try' ':' suite 'finally' ':' suite
2034 validate_try(node
*tree
)
2036 int nch
= NCH(tree
);
2038 int res
= (validate_ntype(tree
, try_stmt
)
2039 && (nch
>= 6) && ((nch
% 3) == 0));
2042 res
= (validate_name(CHILD(tree
, 0), "try")
2043 && validate_colon(CHILD(tree
, 1))
2044 && validate_suite(CHILD(tree
, 2))
2045 && validate_colon(CHILD(tree
, nch
- 2))
2046 && validate_suite(CHILD(tree
, nch
- 1)));
2047 else if (!PyErr_Occurred()) {
2048 const char* name
= "except";
2049 if (TYPE(CHILD(tree
, nch
- 3)) != except_clause
)
2050 name
= STR(CHILD(tree
, nch
- 3));
2052 PyErr_Format(parser_error
,
2053 "Illegal number of children for try/%s node.", name
);
2055 /* Skip past except_clause sections: */
2056 while (res
&& (TYPE(CHILD(tree
, pos
)) == except_clause
)) {
2057 res
= (validate_except_clause(CHILD(tree
, pos
))
2058 && validate_colon(CHILD(tree
, pos
+ 1))
2059 && validate_suite(CHILD(tree
, pos
+ 2)));
2062 if (res
&& (pos
< nch
)) {
2063 res
= validate_ntype(CHILD(tree
, pos
), NAME
);
2064 if (res
&& (strcmp(STR(CHILD(tree
, pos
)), "finally") == 0))
2065 res
= (validate_numnodes(tree
, 6, "try/finally")
2066 && validate_colon(CHILD(tree
, 4))
2067 && validate_suite(CHILD(tree
, 5)));
2069 if (nch
== (pos
+ 3)) {
2070 res
= ((strcmp(STR(CHILD(tree
, pos
)), "except") == 0)
2071 || (strcmp(STR(CHILD(tree
, pos
)), "else") == 0));
2073 err_string("illegal trailing triple in try statement");
2075 else if (nch
== (pos
+ 6)) {
2076 res
= (validate_name(CHILD(tree
, pos
), "except")
2077 && validate_colon(CHILD(tree
, pos
+ 1))
2078 && validate_suite(CHILD(tree
, pos
+ 2))
2079 && validate_name(CHILD(tree
, pos
+ 3), "else"));
2082 res
= validate_numnodes(tree
, pos
+ 3, "try/except");
2090 validate_except_clause(node
*tree
)
2092 int nch
= NCH(tree
);
2093 int res
= (validate_ntype(tree
, except_clause
)
2094 && ((nch
== 1) || (nch
== 2) || (nch
== 4))
2095 && validate_name(CHILD(tree
, 0), "except"));
2097 if (res
&& (nch
> 1))
2098 res
= validate_test(CHILD(tree
, 1));
2099 if (res
&& (nch
== 4))
2100 res
= (validate_comma(CHILD(tree
, 2))
2101 && validate_test(CHILD(tree
, 3)));
2108 validate_test(node
*tree
)
2110 int nch
= NCH(tree
);
2111 int res
= validate_ntype(tree
, test
) && is_odd(nch
);
2113 if (res
&& (TYPE(CHILD(tree
, 0)) == lambdef
))
2115 && validate_lambdef(CHILD(tree
, 0)));
2117 res
= validate_or_test(CHILD(tree
, 0));
2118 res
= (res
&& (nch
== 1 || (nch
== 5 &&
2119 validate_name(CHILD(tree
, 1), "if") &&
2120 validate_or_test(CHILD(tree
, 2)) &&
2121 validate_name(CHILD(tree
, 3), "else") &&
2122 validate_test(CHILD(tree
, 4)))));
2128 validate_old_test(node
*tree
)
2130 int nch
= NCH(tree
);
2131 int res
= validate_ntype(tree
, old_test
) && (nch
== 1);
2133 if (res
&& (TYPE(CHILD(tree
, 0)) == old_lambdef
))
2134 res
= (validate_old_lambdef(CHILD(tree
, 0)));
2136 res
= (validate_or_test(CHILD(tree
, 0)));
2142 validate_or_test(node
*tree
)
2144 int nch
= NCH(tree
);
2145 int res
= validate_ntype(tree
, or_test
) && is_odd(nch
);
2149 res
= validate_and_test(CHILD(tree
, 0));
2150 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2151 res
= (validate_name(CHILD(tree
, pos
), "or")
2152 && validate_and_test(CHILD(tree
, pos
+ 1)));
2159 validate_and_test(node
*tree
)
2162 int nch
= NCH(tree
);
2163 int res
= (validate_ntype(tree
, and_test
)
2165 && validate_not_test(CHILD(tree
, 0)));
2167 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2168 res
= (validate_name(CHILD(tree
, pos
), "and")
2169 && validate_not_test(CHILD(tree
, 0)));
2176 validate_not_test(node
*tree
)
2178 int nch
= NCH(tree
);
2179 int res
= validate_ntype(tree
, not_test
) && ((nch
== 1) || (nch
== 2));
2183 res
= (validate_name(CHILD(tree
, 0), "not")
2184 && validate_not_test(CHILD(tree
, 1)));
2186 res
= validate_comparison(CHILD(tree
, 0));
2193 validate_comparison(node
*tree
)
2196 int nch
= NCH(tree
);
2197 int res
= (validate_ntype(tree
, comparison
)
2199 && validate_expr(CHILD(tree
, 0)));
2201 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2202 res
= (validate_comp_op(CHILD(tree
, pos
))
2203 && validate_expr(CHILD(tree
, pos
+ 1)));
2210 validate_comp_op(node
*tree
)
2213 int nch
= NCH(tree
);
2215 if (!validate_ntype(tree
, comp_op
))
2219 * Only child will be a terminal with a well-defined symbolic name
2220 * or a NAME with a string of either 'is' or 'in'
2222 tree
= CHILD(tree
, 0);
2223 switch (TYPE(tree
)) {
2234 res
= ((strcmp(STR(tree
), "in") == 0)
2235 || (strcmp(STR(tree
), "is") == 0));
2237 PyErr_Format(parser_error
,
2238 "illegal operator '%s'", STR(tree
));
2242 err_string("illegal comparison operator type");
2246 else if ((res
= validate_numnodes(tree
, 2, "comp_op")) != 0) {
2247 res
= (validate_ntype(CHILD(tree
, 0), NAME
)
2248 && validate_ntype(CHILD(tree
, 1), NAME
)
2249 && (((strcmp(STR(CHILD(tree
, 0)), "is") == 0)
2250 && (strcmp(STR(CHILD(tree
, 1)), "not") == 0))
2251 || ((strcmp(STR(CHILD(tree
, 0)), "not") == 0)
2252 && (strcmp(STR(CHILD(tree
, 1)), "in") == 0))));
2253 if (!res
&& !PyErr_Occurred())
2254 err_string("unknown comparison operator");
2261 validate_expr(node
*tree
)
2264 int nch
= NCH(tree
);
2265 int res
= (validate_ntype(tree
, expr
)
2267 && validate_xor_expr(CHILD(tree
, 0)));
2269 for (j
= 2; res
&& (j
< nch
); j
+= 2)
2270 res
= (validate_xor_expr(CHILD(tree
, j
))
2271 && validate_vbar(CHILD(tree
, j
- 1)));
2278 validate_xor_expr(node
*tree
)
2281 int nch
= NCH(tree
);
2282 int res
= (validate_ntype(tree
, xor_expr
)
2284 && validate_and_expr(CHILD(tree
, 0)));
2286 for (j
= 2; res
&& (j
< nch
); j
+= 2)
2287 res
= (validate_circumflex(CHILD(tree
, j
- 1))
2288 && validate_and_expr(CHILD(tree
, j
)));
2295 validate_and_expr(node
*tree
)
2298 int nch
= NCH(tree
);
2299 int res
= (validate_ntype(tree
, and_expr
)
2301 && validate_shift_expr(CHILD(tree
, 0)));
2303 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2304 res
= (validate_ampersand(CHILD(tree
, pos
))
2305 && validate_shift_expr(CHILD(tree
, pos
+ 1)));
2312 validate_chain_two_ops(node
*tree
, int (*termvalid
)(node
*), int op1
, int op2
)
2315 int nch
= NCH(tree
);
2316 int res
= (is_odd(nch
)
2317 && (*termvalid
)(CHILD(tree
, 0)));
2319 for ( ; res
&& (pos
< nch
); pos
+= 2) {
2320 if (TYPE(CHILD(tree
, pos
)) != op1
)
2321 res
= validate_ntype(CHILD(tree
, pos
), op2
);
2323 res
= (*termvalid
)(CHILD(tree
, pos
+ 1));
2330 validate_shift_expr(node
*tree
)
2332 return (validate_ntype(tree
, shift_expr
)
2333 && validate_chain_two_ops(tree
, validate_arith_expr
,
2334 LEFTSHIFT
, RIGHTSHIFT
));
2339 validate_arith_expr(node
*tree
)
2341 return (validate_ntype(tree
, arith_expr
)
2342 && validate_chain_two_ops(tree
, validate_term
, PLUS
, MINUS
));
2347 validate_term(node
*tree
)
2350 int nch
= NCH(tree
);
2351 int res
= (validate_ntype(tree
, term
)
2353 && validate_factor(CHILD(tree
, 0)));
2355 for ( ; res
&& (pos
< nch
); pos
+= 2)
2356 res
= (((TYPE(CHILD(tree
, pos
)) == STAR
)
2357 || (TYPE(CHILD(tree
, pos
)) == SLASH
)
2358 || (TYPE(CHILD(tree
, pos
)) == DOUBLESLASH
)
2359 || (TYPE(CHILD(tree
, pos
)) == PERCENT
))
2360 && validate_factor(CHILD(tree
, pos
+ 1)));
2368 * factor: ('+'|'-'|'~') factor | power
2371 validate_factor(node
*tree
)
2373 int nch
= NCH(tree
);
2374 int res
= (validate_ntype(tree
, factor
)
2376 && ((TYPE(CHILD(tree
, 0)) == PLUS
)
2377 || (TYPE(CHILD(tree
, 0)) == MINUS
)
2378 || (TYPE(CHILD(tree
, 0)) == TILDE
))
2379 && validate_factor(CHILD(tree
, 1)))
2381 && validate_power(CHILD(tree
, 0)))));
2388 * power: atom trailer* ('**' factor)*
2391 validate_power(node
*tree
)
2394 int nch
= NCH(tree
);
2395 int res
= (validate_ntype(tree
, power
) && (nch
>= 1)
2396 && validate_atom(CHILD(tree
, 0)));
2398 while (res
&& (pos
< nch
) && (TYPE(CHILD(tree
, pos
)) == trailer
))
2399 res
= validate_trailer(CHILD(tree
, pos
++));
2400 if (res
&& (pos
< nch
)) {
2401 if (!is_even(nch
- pos
)) {
2402 err_string("illegal number of nodes for 'power'");
2405 for ( ; res
&& (pos
< (nch
- 1)); pos
+= 2)
2406 res
= (validate_doublestar(CHILD(tree
, pos
))
2407 && validate_factor(CHILD(tree
, pos
+ 1)));
2414 validate_atom(node
*tree
)
2417 int nch
= NCH(tree
);
2418 int res
= validate_ntype(tree
, atom
);
2421 res
= validate_numnodes(tree
, nch
+1, "atom");
2423 switch (TYPE(CHILD(tree
, 0))) {
2426 && (validate_rparen(CHILD(tree
, nch
- 1))));
2428 if (res
&& (nch
== 3)) {
2429 if (TYPE(CHILD(tree
, 1))==yield_expr
)
2430 res
= validate_yield_expr(CHILD(tree
, 1));
2432 res
= validate_testlist_gexp(CHILD(tree
, 1));
2437 res
= validate_ntype(CHILD(tree
, 1), RSQB
);
2439 res
= (validate_listmaker(CHILD(tree
, 1))
2440 && validate_ntype(CHILD(tree
, 2), RSQB
));
2443 err_string("illegal list display atom");
2448 && validate_ntype(CHILD(tree
, nch
- 1), RBRACE
));
2450 if (res
&& (nch
== 3))
2451 res
= validate_dictmaker(CHILD(tree
, 1));
2455 && validate_testlist1(CHILD(tree
, 1))
2456 && validate_ntype(CHILD(tree
, 2), BACKQUOTE
));
2463 for (pos
= 1; res
&& (pos
< nch
); ++pos
)
2464 res
= validate_ntype(CHILD(tree
, pos
), STRING
);
2476 * test ( list_for | (',' test)* [','] )
2479 validate_listmaker(node
*tree
)
2481 int nch
= NCH(tree
);
2485 err_string("missing child nodes of listmaker");
2487 ok
= validate_test(CHILD(tree
, 0));
2490 * list_for | (',' test)* [',']
2492 if (nch
== 2 && TYPE(CHILD(tree
, 1)) == list_for
)
2493 ok
= validate_list_for(CHILD(tree
, 1));
2495 /* (',' test)* [','] */
2497 while (ok
&& nch
- i
>= 2) {
2498 ok
= (validate_comma(CHILD(tree
, i
))
2499 && validate_test(CHILD(tree
, i
+1)));
2502 if (ok
&& i
== nch
-1)
2503 ok
= validate_comma(CHILD(tree
, i
));
2504 else if (i
!= nch
) {
2506 err_string("illegal trailing nodes for listmaker");
2513 * test ( gen_for | (',' test)* [','] )
2516 validate_testlist_gexp(node
*tree
)
2518 int nch
= NCH(tree
);
2522 err_string("missing child nodes of testlist_gexp");
2524 ok
= validate_test(CHILD(tree
, 0));
2528 * gen_for | (',' test)* [',']
2530 if (nch
== 2 && TYPE(CHILD(tree
, 1)) == gen_for
)
2531 ok
= validate_gen_for(CHILD(tree
, 1));
2533 /* (',' test)* [','] */
2535 while (ok
&& nch
- i
>= 2) {
2536 ok
= (validate_comma(CHILD(tree
, i
))
2537 && validate_test(CHILD(tree
, i
+1)));
2540 if (ok
&& i
== nch
-1)
2541 ok
= validate_comma(CHILD(tree
, i
));
2542 else if (i
!= nch
) {
2544 err_string("illegal trailing nodes for testlist_gexp");
2551 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2554 validate_decorator(node
*tree
)
2557 int nch
= NCH(tree
);
2558 ok
= (validate_ntype(tree
, decorator
) &&
2559 (nch
== 3 || nch
== 5 || nch
== 6) &&
2560 validate_at(CHILD(tree
, 0)) &&
2561 validate_dotted_name(CHILD(tree
, 1)) &&
2562 validate_newline(RCHILD(tree
, -1)));
2564 if (ok
&& nch
!= 3) {
2565 ok
= (validate_lparen(CHILD(tree
, 2)) &&
2566 validate_rparen(RCHILD(tree
, -2)));
2569 ok
= validate_arglist(CHILD(tree
, 3));
2579 validate_decorators(node
*tree
)
2583 ok
= validate_ntype(tree
, decorators
) && nch
>= 1;
2585 for (i
= 0; ok
&& i
< nch
; ++i
)
2586 ok
= validate_decorator(CHILD(tree
, i
));
2594 * 'def' NAME parameters ':' suite
2597 validate_funcdef(node
*tree
)
2599 int nch
= NCH(tree
);
2600 int ok
= (validate_ntype(tree
, funcdef
)
2602 && validate_name(RCHILD(tree
, -5), "def")
2603 && validate_ntype(RCHILD(tree
, -4), NAME
)
2604 && validate_colon(RCHILD(tree
, -2))
2605 && validate_parameters(RCHILD(tree
, -3))
2606 && validate_suite(RCHILD(tree
, -1)));
2612 * decorators (classdef | funcdef)
2615 validate_decorated(node
*tree
)
2617 int nch
= NCH(tree
);
2618 int ok
= (validate_ntype(tree
, decorated
)
2620 && validate_decorators(RCHILD(tree
, -2))
2621 && (validate_funcdef(RCHILD(tree
, -1))
2622 || validate_class(RCHILD(tree
, -1)))
2628 validate_lambdef(node
*tree
)
2630 int nch
= NCH(tree
);
2631 int res
= (validate_ntype(tree
, lambdef
)
2632 && ((nch
== 3) || (nch
== 4))
2633 && validate_name(CHILD(tree
, 0), "lambda")
2634 && validate_colon(CHILD(tree
, nch
- 2))
2635 && validate_test(CHILD(tree
, nch
- 1)));
2637 if (res
&& (nch
== 4))
2638 res
= validate_varargslist(CHILD(tree
, 1));
2639 else if (!res
&& !PyErr_Occurred())
2640 (void) validate_numnodes(tree
, 3, "lambdef");
2647 validate_old_lambdef(node
*tree
)
2649 int nch
= NCH(tree
);
2650 int res
= (validate_ntype(tree
, old_lambdef
)
2651 && ((nch
== 3) || (nch
== 4))
2652 && validate_name(CHILD(tree
, 0), "lambda")
2653 && validate_colon(CHILD(tree
, nch
- 2))
2654 && validate_test(CHILD(tree
, nch
- 1)));
2656 if (res
&& (nch
== 4))
2657 res
= validate_varargslist(CHILD(tree
, 1));
2658 else if (!res
&& !PyErr_Occurred())
2659 (void) validate_numnodes(tree
, 3, "old_lambdef");
2667 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2670 validate_arglist(node
*tree
)
2672 int nch
= NCH(tree
);
2677 /* raise the right error from having an invalid number of children */
2678 return validate_numnodes(tree
, nch
+ 1, "arglist");
2681 for (i
=0; i
<nch
; i
++) {
2682 if (TYPE(CHILD(tree
, i
)) == argument
) {
2683 node
*ch
= CHILD(tree
, i
);
2684 if (NCH(ch
) == 2 && TYPE(CHILD(ch
, 1)) == gen_for
) {
2685 err_string("need '(', ')' for generator expression");
2692 while (ok
&& nch
-i
>= 2) {
2693 /* skip leading (argument ',') */
2694 ok
= (validate_argument(CHILD(tree
, i
))
2695 && validate_comma(CHILD(tree
, i
+1)));
2704 * argument | '*' test [',' '**' test] | '**' test
2706 int sym
= TYPE(CHILD(tree
, i
));
2708 if (sym
== argument
) {
2709 ok
= validate_argument(CHILD(tree
, i
));
2710 if (ok
&& i
+1 != nch
) {
2711 err_string("illegal arglist specification"
2712 " (extra stuff on end)");
2716 else if (sym
== STAR
) {
2717 ok
= validate_star(CHILD(tree
, i
));
2718 if (ok
&& (nch
-i
== 2))
2719 ok
= validate_test(CHILD(tree
, i
+1));
2720 else if (ok
&& (nch
-i
== 5))
2721 ok
= (validate_test(CHILD(tree
, i
+1))
2722 && validate_comma(CHILD(tree
, i
+2))
2723 && validate_doublestar(CHILD(tree
, i
+3))
2724 && validate_test(CHILD(tree
, i
+4)));
2726 err_string("illegal use of '*' in arglist");
2730 else if (sym
== DOUBLESTAR
) {
2732 ok
= (validate_doublestar(CHILD(tree
, i
))
2733 && validate_test(CHILD(tree
, i
+1)));
2735 err_string("illegal use of '**' in arglist");
2740 err_string("illegal arglist specification");
2751 * [test '='] test [gen_for]
2754 validate_argument(node
*tree
)
2756 int nch
= NCH(tree
);
2757 int res
= (validate_ntype(tree
, argument
)
2758 && ((nch
== 1) || (nch
== 2) || (nch
== 3))
2759 && validate_test(CHILD(tree
, 0)));
2761 if (res
&& (nch
== 2))
2762 res
= validate_gen_for(CHILD(tree
, 1));
2763 else if (res
&& (nch
== 3))
2764 res
= (validate_equal(CHILD(tree
, 1))
2765 && validate_test(CHILD(tree
, 2)));
2774 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2777 validate_trailer(node
*tree
)
2779 int nch
= NCH(tree
);
2780 int res
= validate_ntype(tree
, trailer
) && ((nch
== 2) || (nch
== 3));
2783 switch (TYPE(CHILD(tree
, 0))) {
2785 res
= validate_rparen(CHILD(tree
, nch
- 1));
2786 if (res
&& (nch
== 3))
2787 res
= validate_arglist(CHILD(tree
, 1));
2790 res
= (validate_numnodes(tree
, 3, "trailer")
2791 && validate_subscriptlist(CHILD(tree
, 1))
2792 && validate_ntype(CHILD(tree
, 2), RSQB
));
2795 res
= (validate_numnodes(tree
, 2, "trailer")
2796 && validate_ntype(CHILD(tree
, 1), NAME
));
2804 (void) validate_numnodes(tree
, 2, "trailer");
2812 * subscript (',' subscript)* [',']
2815 validate_subscriptlist(node
*tree
)
2817 return (validate_repeating_list(tree
, subscriptlist
,
2818 validate_subscript
, "subscriptlist"));
2824 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
2827 validate_subscript(node
*tree
)
2830 int nch
= NCH(tree
);
2831 int res
= validate_ntype(tree
, subscript
) && (nch
>= 1) && (nch
<= 4);
2834 if (!PyErr_Occurred())
2835 err_string("invalid number of arguments for subscript node");
2838 if (TYPE(CHILD(tree
, 0)) == DOT
)
2839 /* take care of ('.' '.' '.') possibility */
2840 return (validate_numnodes(tree
, 3, "subscript")
2841 && validate_dot(CHILD(tree
, 0))
2842 && validate_dot(CHILD(tree
, 1))
2843 && validate_dot(CHILD(tree
, 2)));
2845 if (TYPE(CHILD(tree
, 0)) == test
)
2846 res
= validate_test(CHILD(tree
, 0));
2848 res
= validate_colon(CHILD(tree
, 0));
2851 /* Must be [test] ':' [test] [sliceop],
2852 * but at least one of the optional components will
2853 * be present, but we don't know which yet.
2855 if ((TYPE(CHILD(tree
, 0)) != COLON
) || (nch
== 4)) {
2856 res
= validate_test(CHILD(tree
, 0));
2860 res
= validate_colon(CHILD(tree
, offset
));
2862 int rem
= nch
- ++offset
;
2864 if (TYPE(CHILD(tree
, offset
)) == test
) {
2865 res
= validate_test(CHILD(tree
, offset
));
2870 res
= validate_sliceop(CHILD(tree
, offset
));
2878 validate_sliceop(node
*tree
)
2880 int nch
= NCH(tree
);
2881 int res
= ((nch
== 1) || validate_numnodes(tree
, 2, "sliceop"))
2882 && validate_ntype(tree
, sliceop
);
2883 if (!res
&& !PyErr_Occurred()) {
2884 res
= validate_numnodes(tree
, 1, "sliceop");
2887 res
= validate_colon(CHILD(tree
, 0));
2888 if (res
&& (nch
== 2))
2889 res
= validate_test(CHILD(tree
, 1));
2896 validate_exprlist(node
*tree
)
2898 return (validate_repeating_list(tree
, exprlist
,
2899 validate_expr
, "exprlist"));
2904 validate_dictmaker(node
*tree
)
2906 int nch
= NCH(tree
);
2907 int res
= (validate_ntype(tree
, dictmaker
)
2909 && validate_test(CHILD(tree
, 0))
2910 && validate_colon(CHILD(tree
, 1))
2911 && validate_test(CHILD(tree
, 2)));
2913 if (res
&& ((nch
% 4) == 0))
2914 res
= validate_comma(CHILD(tree
, --nch
));
2916 res
= ((nch
% 4) == 3);
2918 if (res
&& (nch
> 3)) {
2920 /* ( ',' test ':' test )* */
2921 while (res
&& (pos
< nch
)) {
2922 res
= (validate_comma(CHILD(tree
, pos
))
2923 && validate_test(CHILD(tree
, pos
+ 1))
2924 && validate_colon(CHILD(tree
, pos
+ 2))
2925 && validate_test(CHILD(tree
, pos
+ 3)));
2934 validate_eval_input(node
*tree
)
2937 int nch
= NCH(tree
);
2938 int res
= (validate_ntype(tree
, eval_input
)
2940 && validate_testlist(CHILD(tree
, 0))
2941 && validate_ntype(CHILD(tree
, nch
- 1), ENDMARKER
));
2943 for (pos
= 1; res
&& (pos
< (nch
- 1)); ++pos
)
2944 res
= validate_ntype(CHILD(tree
, pos
), NEWLINE
);
2951 validate_node(node
*tree
)
2953 int nch
= 0; /* num. children on current node */
2954 int res
= 1; /* result value */
2955 node
* next
= 0; /* node to process after this one */
2957 while (res
&& (tree
!= 0)) {
2960 switch (TYPE(tree
)) {
2965 res
= validate_funcdef(tree
);
2968 res
= validate_class(tree
);
2971 res
= validate_decorated(tree
);
2974 * "Trivial" parse tree nodes.
2975 * (Why did I call these trivial?)
2978 res
= validate_stmt(tree
);
2982 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2983 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2985 res
= validate_small_stmt(tree
);
2988 res
= (validate_numnodes(tree
, 1, "flow_stmt")
2989 && ((TYPE(CHILD(tree
, 0)) == break_stmt
)
2990 || (TYPE(CHILD(tree
, 0)) == continue_stmt
)
2991 || (TYPE(CHILD(tree
, 0)) == yield_stmt
)
2992 || (TYPE(CHILD(tree
, 0)) == return_stmt
)
2993 || (TYPE(CHILD(tree
, 0)) == raise_stmt
)));
2995 next
= CHILD(tree
, 0);
2997 err_string("illegal flow_stmt type");
3000 res
= validate_yield_stmt(tree
);
3003 * Compound statements.
3006 res
= validate_simple_stmt(tree
);
3009 res
= validate_compound_stmt(tree
);
3012 * Fundamental statements.
3015 res
= validate_expr_stmt(tree
);
3018 res
= validate_print_stmt(tree
);
3021 res
= validate_del_stmt(tree
);
3024 res
= (validate_numnodes(tree
, 1, "pass")
3025 && validate_name(CHILD(tree
, 0), "pass"));
3028 res
= (validate_numnodes(tree
, 1, "break")
3029 && validate_name(CHILD(tree
, 0), "break"));
3032 res
= (validate_numnodes(tree
, 1, "continue")
3033 && validate_name(CHILD(tree
, 0), "continue"));
3036 res
= validate_return_stmt(tree
);
3039 res
= validate_raise_stmt(tree
);
3042 res
= validate_import_stmt(tree
);
3045 res
= validate_import_name(tree
);
3048 res
= validate_import_from(tree
);
3051 res
= validate_global_stmt(tree
);
3054 res
= validate_exec_stmt(tree
);
3057 res
= validate_assert_stmt(tree
);
3060 res
= validate_if(tree
);
3063 res
= validate_while(tree
);
3066 res
= validate_for(tree
);
3069 res
= validate_try(tree
);
3072 res
= validate_suite(tree
);
3078 res
= validate_testlist(tree
);
3081 res
= validate_yield_expr(tree
);
3084 res
= validate_testlist1(tree
);
3087 res
= validate_test(tree
);
3090 res
= validate_and_test(tree
);
3093 res
= validate_not_test(tree
);
3096 res
= validate_comparison(tree
);
3099 res
= validate_exprlist(tree
);
3102 res
= validate_comp_op(tree
);
3105 res
= validate_expr(tree
);
3108 res
= validate_xor_expr(tree
);
3111 res
= validate_and_expr(tree
);
3114 res
= validate_shift_expr(tree
);
3117 res
= validate_arith_expr(tree
);
3120 res
= validate_term(tree
);
3123 res
= validate_factor(tree
);
3126 res
= validate_power(tree
);
3129 res
= validate_atom(tree
);
3133 /* Hopefully never reached! */
3134 err_string("unrecognized node type");
3145 validate_expr_tree(node
*tree
)
3147 int res
= validate_eval_input(tree
);
3149 if (!res
&& !PyErr_Occurred())
3150 err_string("could not validate expression tuple");
3157 * (NEWLINE | stmt)* ENDMARKER
3160 validate_file_input(node
*tree
)
3163 int nch
= NCH(tree
) - 1;
3164 int res
= ((nch
>= 0)
3165 && validate_ntype(CHILD(tree
, nch
), ENDMARKER
));
3167 for (j
= 0; res
&& (j
< nch
); ++j
) {
3168 if (TYPE(CHILD(tree
, j
)) == stmt
)
3169 res
= validate_stmt(CHILD(tree
, j
));
3171 res
= validate_newline(CHILD(tree
, j
));
3173 /* This stays in to prevent any internal failures from getting to the
3174 * user. Hopefully, this won't be needed. If a user reports getting
3175 * this, we have some debugging to do.
3177 if (!res
&& !PyErr_Occurred())
3178 err_string("VALIDATION FAILURE: report this to the maintainer!");
3184 validate_encoding_decl(node
*tree
)
3186 int nch
= NCH(tree
);
3187 int res
= ((nch
== 1)
3188 && validate_file_input(CHILD(tree
, 0)));
3190 if (!res
&& !PyErr_Occurred())
3191 err_string("Error Parsing encoding_decl");
3197 pickle_constructor
= NULL
;
3201 parser__pickler(PyObject
*self
, PyObject
*args
)
3203 NOTE(ARGUNUSED(self
))
3204 PyObject
*result
= NULL
;
3205 PyObject
*st
= NULL
;
3206 PyObject
*empty_dict
= NULL
;
3208 if (PyArg_ParseTuple(args
, "O!:_pickler", &PyST_Type
, &st
)) {
3212 if ((empty_dict
= PyDict_New()) == NULL
)
3214 if ((newargs
= Py_BuildValue("Oi", st
, 1)) == NULL
)
3216 tuple
= parser_st2tuple((PyST_Object
*)NULL
, newargs
, empty_dict
);
3217 if (tuple
!= NULL
) {
3218 result
= Py_BuildValue("O(O)", pickle_constructor
, tuple
);
3221 Py_DECREF(empty_dict
);
3225 Py_XDECREF(empty_dict
);
3231 /* Functions exported by this module. Most of this should probably
3232 * be converted into an ST object with methods, but that is better
3233 * done directly in Python, allowing subclasses to be created directly.
3234 * We'd really have to write a wrapper around it all anyway to allow
3237 static PyMethodDef parser_functions
[] = {
3238 {"ast2tuple", (PyCFunction
)parser_ast2tuple
, PUBLIC_METHOD_TYPE
,
3239 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3240 {"ast2list", (PyCFunction
)parser_ast2list
, PUBLIC_METHOD_TYPE
,
3241 PyDoc_STR("Creates a list-tree representation of an ST.")},
3242 {"compileast", (PyCFunction
)parser_compileast
,PUBLIC_METHOD_TYPE
,
3243 PyDoc_STR("Compiles an ST object into a code object.")},
3244 {"compilest", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
3245 PyDoc_STR("Compiles an ST object into a code object.")},
3246 {"expr", (PyCFunction
)parser_expr
, PUBLIC_METHOD_TYPE
,
3247 PyDoc_STR("Creates an ST object from an expression.")},
3248 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
3249 PyDoc_STR("Determines if an ST object was created from an expression.")},
3250 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
3251 PyDoc_STR("Determines if an ST object was created from a suite.")},
3252 {"suite", (PyCFunction
)parser_suite
, PUBLIC_METHOD_TYPE
,
3253 PyDoc_STR("Creates an ST object from a suite.")},
3254 {"sequence2ast", (PyCFunction
)parser_tuple2ast
, PUBLIC_METHOD_TYPE
,
3255 PyDoc_STR("Creates an ST object from a tree representation.")},
3256 {"sequence2st", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
3257 PyDoc_STR("Creates an ST object from a tree representation.")},
3258 {"st2tuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
3259 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3260 {"st2list", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
3261 PyDoc_STR("Creates a list-tree representation of an ST.")},
3262 {"tuple2ast", (PyCFunction
)parser_tuple2ast
, PUBLIC_METHOD_TYPE
,
3263 PyDoc_STR("Creates an ST object from a tree representation.")},
3264 {"tuple2st", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
3265 PyDoc_STR("Creates an ST object from a tree representation.")},
3267 /* private stuff: support pickle module */
3268 {"_pickler", (PyCFunction
)parser__pickler
, METH_VARARGS
,
3269 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
3271 {NULL
, NULL
, 0, NULL
}
3275 PyMODINIT_FUNC
initparser(void); /* supply a prototype */
3280 PyObject
*module
, *copyreg
;
3282 Py_TYPE(&PyST_Type
) = &PyType_Type
;
3283 module
= Py_InitModule("parser", parser_functions
);
3287 if (parser_error
== 0)
3288 parser_error
= PyErr_NewException("parser.ParserError", NULL
, NULL
);
3290 if (parser_error
== 0)
3291 /* caller will check PyErr_Occurred() */
3293 /* CAUTION: The code next used to skip bumping the refcount on
3294 * parser_error. That's a disaster if initparser() gets called more
3295 * than once. By incref'ing, we ensure that each module dict that
3296 * gets created owns its reference to the shared parser_error object,
3297 * and the file static parser_error vrbl owns a reference too.
3299 Py_INCREF(parser_error
);
3300 if (PyModule_AddObject(module
, "ParserError", parser_error
) != 0)
3303 Py_INCREF(&PyST_Type
);
3304 PyModule_AddObject(module
, "ASTType", (PyObject
*)&PyST_Type
);
3305 Py_INCREF(&PyST_Type
);
3306 PyModule_AddObject(module
, "STType", (PyObject
*)&PyST_Type
);
3308 PyModule_AddStringConstant(module
, "__copyright__",
3309 parser_copyright_string
);
3310 PyModule_AddStringConstant(module
, "__doc__",
3312 PyModule_AddStringConstant(module
, "__version__",
3313 parser_version_string
);
3315 /* Register to support pickling.
3316 * If this fails, the import of this module will fail because an
3317 * exception will be raised here; should we clear the exception?
3319 copyreg
= PyImport_ImportModuleNoBlock("copy_reg");
3320 if (copyreg
!= NULL
) {
3321 PyObject
*func
, *pickler
;
3323 func
= PyObject_GetAttrString(copyreg
, "pickle");
3324 pickle_constructor
= PyObject_GetAttrString(module
, "sequence2st");
3325 pickler
= PyObject_GetAttrString(module
, "_pickler");
3326 Py_XINCREF(pickle_constructor
);
3327 if ((func
!= NULL
) && (pickle_constructor
!= NULL
)
3328 && (pickler
!= NULL
)) {
3331 res
= PyObject_CallFunctionObjArgs(func
, &PyST_Type
, pickler
,
3332 pickle_constructor
, NULL
);
3336 Py_XDECREF(pickle_constructor
);
3337 Py_XDECREF(pickler
);