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 "Python-ast.h" /* mod_ty */
30 #include "graminit.h" /* symbols defined in the grammar */
31 #include "node.h" /* internal parser structure */
32 #include "errcode.h" /* error codes for PyNode_*() */
33 #include "token.h" /* token definitions */
36 /* ISTERMINAL() / ISNONTERMINAL() */
42 extern grammar _PyParser_Grammar
; /* From graminit.c */
50 /* String constants used to initialize module attributes.
53 static char parser_copyright_string
[] =
54 "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
55 University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
56 Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
57 Centrum, Amsterdam, The Netherlands.";
60 PyDoc_STRVAR(parser_doc_string
,
61 "This is an interface to Python's internal parser.");
63 static char parser_version_string
[] = "0.5";
66 typedef PyObject
* (*SeqMaker
) (Py_ssize_t length
);
67 typedef int (*SeqInserter
) (PyObject
* sequence
,
71 /* The function below is copyrighted by Stichting Mathematisch Centrum. The
72 * original copyright statement is included below, and continues to apply
73 * in full to the function immediately following. All other material is
74 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
75 * Institute and State University. Changes were made to comply with the
76 * new naming conventions. Added arguments to provide support for creating
77 * lists as well as tuples, and optionally including the line numbers.
82 node2tuple(node
*n
, /* node to convert */
83 SeqMaker mkseq
, /* create sequence */
84 SeqInserter addelem
, /* func. to add elem. in seq. */
85 int lineno
, /* include line numbers? */
86 int col_offset
) /* include column offsets? */
92 if (ISNONTERMINAL(TYPE(n
))) {
97 v
= mkseq(1 + NCH(n
) + (TYPE(n
) == encoding_decl
));
100 w
= PyInt_FromLong(TYPE(n
));
103 return ((PyObject
*) NULL
);
105 (void) addelem(v
, 0, w
);
106 for (i
= 0; i
< NCH(n
); i
++) {
107 w
= node2tuple(CHILD(n
, i
), mkseq
, addelem
, lineno
, col_offset
);
110 return ((PyObject
*) NULL
);
112 (void) addelem(v
, i
+1, w
);
115 if (TYPE(n
) == encoding_decl
)
116 (void) addelem(v
, i
+1, PyString_FromString(STR(n
)));
119 else if (ISTERMINAL(TYPE(n
))) {
120 PyObject
*result
= mkseq(2 + lineno
+ col_offset
);
121 if (result
!= NULL
) {
122 (void) addelem(result
, 0, PyInt_FromLong(TYPE(n
)));
123 (void) addelem(result
, 1, PyString_FromString(STR(n
)));
125 (void) addelem(result
, 2, PyInt_FromLong(n
->n_lineno
));
127 (void) addelem(result
, 3, PyInt_FromLong(n
->n_col_offset
));
132 PyErr_SetString(PyExc_SystemError
,
133 "unrecognized parse tree node type");
134 return ((PyObject
*) NULL
);
138 * End of material copyrighted by Stichting Mathematisch Centrum.
143 /* There are two types of intermediate objects we're interested in:
144 * 'eval' and 'exec' types. These constants can be used in the st_type
145 * field of the object type to identify which any given object represents.
146 * These should probably go in an external header to allow other extensions
147 * to use them, but then, we really should be using C++ too. ;-)
154 /* These are the internal objects and definitions required to implement the
155 * ST type. Most of the internal names are more reminiscent of the 'old'
156 * naming style, but the code uses the new naming convention.
164 PyObject_HEAD
/* standard object header */
165 node
* st_node
; /* the node* returned by the parser */
166 int st_type
; /* EXPR or SUITE ? */
167 PyCompilerFlags st_flags
; /* Parser and compiler flags */
171 static void parser_free(PyST_Object
*st
);
172 static PyObject
* parser_sizeof(PyST_Object
*, void *);
173 static int parser_compare(PyST_Object
*left
, PyST_Object
*right
);
174 static PyObject
*parser_getattr(PyObject
*self
, char *name
);
175 static PyObject
* parser_compilest(PyST_Object
*, PyObject
*, PyObject
*);
176 static PyObject
* parser_isexpr(PyST_Object
*, PyObject
*, PyObject
*);
177 static PyObject
* parser_issuite(PyST_Object
*, PyObject
*, PyObject
*);
178 static PyObject
* parser_st2list(PyST_Object
*, PyObject
*, PyObject
*);
179 static PyObject
* parser_st2tuple(PyST_Object
*, PyObject
*, PyObject
*);
181 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
185 {"compile", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
186 PyDoc_STR("Compile this ST object into a code object.")},
187 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
188 PyDoc_STR("Determines if this ST object was created from an expression.")},
189 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
190 PyDoc_STR("Determines if this ST object was created from a suite.")},
191 {"tolist", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
192 PyDoc_STR("Creates a list-tree representation of this ST.")},
193 {"totuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
194 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
195 {"__sizeof__", (PyCFunction
)parser_sizeof
, METH_NOARGS
,
196 PyDoc_STR("Returns size in memory, in bytes.")},
197 {NULL
, NULL
, 0, NULL
}
201 PyTypeObject PyST_Type
= {
202 PyVarObject_HEAD_INIT(NULL
, 0)
203 "parser.st", /* tp_name */
204 (int) sizeof(PyST_Object
), /* tp_basicsize */
206 (destructor
)parser_free
, /* tp_dealloc */
208 parser_getattr
, /* tp_getattr */
210 (cmpfunc
)parser_compare
, /* tp_compare */
212 0, /* tp_as_number */
213 0, /* tp_as_sequence */
214 0, /* tp_as_mapping */
221 /* Functions to access object as input/output buffer */
222 0, /* tp_as_buffer */
224 Py_TPFLAGS_DEFAULT
, /* tp_flags */
227 "Intermediate representation of a Python parse tree.",
230 0, /* tp_richcompare */
231 0, /* tp_weaklistoffset */
234 parser_methods
, /* tp_methods */
239 parser_compare_nodes(node
*left
, node
*right
)
243 if (TYPE(left
) < TYPE(right
))
246 if (TYPE(right
) < TYPE(left
))
249 if (ISTERMINAL(TYPE(left
)))
250 return (strcmp(STR(left
), STR(right
)));
252 if (NCH(left
) < NCH(right
))
255 if (NCH(right
) < NCH(left
))
258 for (j
= 0; j
< NCH(left
); ++j
) {
259 int v
= parser_compare_nodes(CHILD(left
, j
), CHILD(right
, j
));
268 /* int parser_compare(PyST_Object* left, PyST_Object* right)
270 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
271 * This really just wraps a call to parser_compare_nodes() with some easy
272 * checks and protection code.
276 parser_compare(PyST_Object
*left
, PyST_Object
*right
)
281 if ((left
== 0) || (right
== 0))
284 return (parser_compare_nodes(left
->st_node
, right
->st_node
));
288 /* parser_newstobject(node* st)
290 * Allocates a new Python object representing an ST. This is simply the
291 * 'wrapper' object that holds a node* and allows it to be passed around in
296 parser_newstobject(node
*st
, int type
)
298 PyST_Object
* o
= PyObject_New(PyST_Object
, &PyST_Type
);
303 o
->st_flags
.cf_flags
= 0;
308 return ((PyObject
*)o
);
312 /* void parser_free(PyST_Object* st)
314 * This is called by a del statement that reduces the reference count to 0.
318 parser_free(PyST_Object
*st
)
320 PyNode_Free(st
->st_node
);
325 /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
327 * This provides conversion from a node* to a tuple object that can be
328 * returned to the Python-level caller. The ST object is not modified.
332 parser_st2tuple(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
334 PyObject
*line_option
= 0;
335 PyObject
*col_option
= 0;
339 static char *keywords
[] = {"ast", "line_info", "col_info", NULL
};
342 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|OO:st2tuple", keywords
,
343 &PyST_Type
, &self
, &line_option
,
347 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|OO:totuple", &keywords
[1],
348 &line_option
, &col_option
);
352 if (line_option
!= NULL
) {
353 lineno
= PyObject_IsTrue(line_option
);
357 if (col_option
!= NULL
) {
358 col_offset
= PyObject_IsTrue(col_option
);
363 * Convert ST into a tuple representation. Use Guido's function,
364 * since it's known to work already.
366 res
= node2tuple(((PyST_Object
*)self
)->st_node
,
367 PyTuple_New
, PyTuple_SetItem
, lineno
, col_offset
);
373 parser_ast2tuple(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
375 if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
377 return parser_st2tuple(self
, args
, kw
);
381 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
383 * This provides conversion from a node* to a list object that can be
384 * returned to the Python-level caller. The ST object is not modified.
388 parser_st2list(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
390 PyObject
*line_option
= 0;
391 PyObject
*col_option
= 0;
395 static char *keywords
[] = {"ast", "line_info", "col_info", NULL
};
398 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|OO:st2list", keywords
,
399 &PyST_Type
, &self
, &line_option
,
402 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|OO:tolist", &keywords
[1],
403 &line_option
, &col_option
);
407 if (line_option
!= 0) {
408 lineno
= PyObject_IsTrue(line_option
);
412 if (col_option
!= 0) {
413 col_offset
= PyObject_IsTrue(col_option
);
418 * Convert ST into a tuple representation. Use Guido's function,
419 * since it's known to work already.
421 res
= node2tuple(self
->st_node
,
422 PyList_New
, PyList_SetItem
, lineno
, col_offset
);
428 parser_ast2list(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
430 if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
432 return parser_st2list(self
, args
, kw
);
436 /* parser_compilest(PyObject* self, PyObject* args)
438 * This function creates code objects from the parse tree represented by
439 * the passed-in data object. An optional file name is passed in as well.
443 parser_compilest(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
448 char* str
= "<syntax-tree>";
451 static char *keywords
[] = {"ast", "filename", NULL
};
454 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|s:compilest", keywords
,
455 &PyST_Type
, &self
, &str
);
457 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|s:compile", &keywords
[1],
461 arena
= PyArena_New();
463 mod
= PyAST_FromNode(self
->st_node
, &(self
->st_flags
), str
, arena
);
465 res
= (PyObject
*)PyAST_Compile(mod
, str
, &(self
->st_flags
), arena
);
475 parser_compileast(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
477 if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
479 return parser_compilest(self
, args
, kw
);
483 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
484 * PyObject* parser_issuite(PyObject* self, PyObject* args)
486 * Checks the passed-in ST object to determine if it is an expression or
487 * a statement suite, respectively. The return is a Python truth value.
491 parser_isexpr(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
496 static char *keywords
[] = {"ast", NULL
};
499 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:isexpr", keywords
,
502 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":isexpr", &keywords
[1]);
505 /* Check to see if the ST represents an expression or not. */
506 res
= (self
->st_type
== PyST_EXPR
) ? Py_True
: Py_False
;
514 parser_issuite(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
519 static char *keywords
[] = {"ast", NULL
};
522 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:issuite", keywords
,
525 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":issuite", &keywords
[1]);
528 /* Check to see if the ST represents an expression or not. */
529 res
= (self
->st_type
== PyST_EXPR
) ? Py_False
: Py_True
;
537 parser_getattr(PyObject
*self
, char *name
)
539 return (Py_FindMethod(parser_methods
, self
, name
));
543 /* err_string(char* message)
545 * Sets the error string for an exception of type ParserError.
549 err_string(char *message
)
551 PyErr_SetString(parser_error
, message
);
555 /* PyObject* parser_do_parse(PyObject* args, int type)
557 * Internal function to actually execute the parse and return the result if
558 * successful or set an exception if not.
562 parser_do_parse(PyObject
*args
, PyObject
*kw
, char *argspec
, int type
)
569 static char *keywords
[] = {"source", NULL
};
571 if (PyArg_ParseTupleAndKeywords(args
, kw
, argspec
, keywords
, &string
)) {
572 node
* n
= PyParser_ParseStringFlagsFilenameEx(string
, NULL
,
575 ? eval_input
: file_input
,
579 res
= parser_newstobject(n
, type
);
581 ((PyST_Object
*)res
)->st_flags
.cf_flags
= flags
& PyCF_MASK
;
584 PyParser_SetError(&err
);
590 /* PyObject* parser_expr(PyObject* self, PyObject* args)
591 * PyObject* parser_suite(PyObject* self, PyObject* args)
593 * External interfaces to the parser itself. Which is called determines if
594 * the parser attempts to recognize an expression ('eval' form) or statement
595 * suite ('exec' form). The real work is done by parser_do_parse() above.
599 parser_expr(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
601 NOTE(ARGUNUSED(self
))
602 return (parser_do_parse(args
, kw
, "s:expr", PyST_EXPR
));
607 parser_suite(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
609 NOTE(ARGUNUSED(self
))
610 return (parser_do_parse(args
, kw
, "s:suite", PyST_SUITE
));
615 /* This is the messy part of the code. Conversion from a tuple to an ST
616 * object requires that the input tuple be valid without having to rely on
617 * catching an exception from the compiler. This is done to allow the
618 * compiler itself to remain fast, since most of its input will come from
619 * the parser directly, and therefore be known to be syntactically correct.
620 * This validation is done to ensure that we don't core dump the compile
621 * phase, returning an exception instead.
623 * Two aspects can be broken out in this code: creating a node tree from
624 * the tuple passed in, and verifying that it is indeed valid. It may be
625 * advantageous to expand the number of ST types to include funcdefs and
626 * lambdadefs to take advantage of the optimizer, recognizing those STs
627 * here. They are not necessary, and not quite as useful in a raw form.
628 * For now, let's get expressions and suites working reliably.
632 static node
* build_node_tree(PyObject
*tuple
);
633 static int validate_expr_tree(node
*tree
);
634 static int validate_file_input(node
*tree
);
635 static int validate_encoding_decl(node
*tree
);
637 /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
639 * This is the public function, called from the Python code. It receives a
640 * single tuple object from the caller, and creates an ST object if the
641 * tuple can be validated. It does this by checking the first code of the
642 * tuple, and, if acceptable, builds the internal representation. If this
643 * step succeeds, the internal representation is validated as fully as
644 * possible with the various validate_*() routines defined below.
646 * This function must be changed if support is to be added for PyST_FRAGMENT
651 parser_tuple2st(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
653 NOTE(ARGUNUSED(self
))
658 static char *keywords
[] = {"sequence", NULL
};
660 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "O:sequence2st", keywords
,
663 if (!PySequence_Check(tuple
)) {
664 PyErr_SetString(PyExc_ValueError
,
665 "sequence2st() requires a single sequence argument");
669 * Convert the tree to the internal form before checking it.
671 tree
= build_node_tree(tuple
);
673 int start_sym
= TYPE(tree
);
674 if (start_sym
== eval_input
) {
675 /* Might be an eval form. */
676 if (validate_expr_tree(tree
))
677 st
= parser_newstobject(tree
, PyST_EXPR
);
681 else if (start_sym
== file_input
) {
682 /* This looks like an exec form so far. */
683 if (validate_file_input(tree
))
684 st
= parser_newstobject(tree
, PyST_SUITE
);
688 else if (start_sym
== encoding_decl
) {
689 /* This looks like an encoding_decl so far. */
690 if (validate_encoding_decl(tree
))
691 st
= parser_newstobject(tree
, PyST_SUITE
);
696 /* This is a fragment, at best. */
698 err_string("parse tree does not use a valid start symbol");
701 /* Make sure we raise an exception on all errors. We should never
702 * get this, but we'd do well to be sure something is done.
704 if (st
== NULL
&& !PyErr_Occurred())
705 err_string("unspecified ST error occurred");
711 parser_tuple2ast(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
713 if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
715 return parser_tuple2st(self
, args
, kw
);
719 parser_sizeof(PyST_Object
*st
, void *unused
)
723 res
= sizeof(PyST_Object
) + _PyNode_SizeOf(st
->st_node
);
724 return PyLong_FromSsize_t(res
);
728 /* node* build_node_children()
730 * Iterate across the children of the current non-terminal node and build
731 * their structures. If successful, return the root of this portion of
732 * the tree, otherwise, 0. Any required exception will be specified already,
733 * and no memory will have been deallocated.
737 build_node_children(PyObject
*tuple
, node
*root
, int *line_num
)
739 Py_ssize_t len
= PyObject_Size(tuple
);
743 for (i
= 1; i
< len
; ++i
) {
744 /* elem must always be a sequence, however simple */
745 PyObject
* elem
= PySequence_GetItem(tuple
, i
);
746 int ok
= elem
!= NULL
;
751 ok
= PySequence_Check(elem
);
753 PyObject
*temp
= PySequence_GetItem(elem
, 0);
757 ok
= PyInt_Check(temp
);
759 type
= PyInt_AS_LONG(temp
);
764 PyObject
*err
= Py_BuildValue("os", elem
,
765 "Illegal node construct.");
766 PyErr_SetObject(parser_error
, err
);
771 if (ISTERMINAL(type
)) {
772 Py_ssize_t len
= PyObject_Size(elem
);
775 if ((len
!= 2) && (len
!= 3)) {
776 err_string("terminal nodes must have 2 or 3 entries");
779 temp
= PySequence_GetItem(elem
, 1);
782 if (!PyString_Check(temp
)) {
783 PyErr_Format(parser_error
,
784 "second item in terminal node must be a string,"
786 Py_TYPE(temp
)->tp_name
);
791 PyObject
*o
= PySequence_GetItem(elem
, 2);
794 *line_num
= PyInt_AS_LONG(o
);
796 PyErr_Format(parser_error
,
797 "third item in terminal node must be an"
798 " integer, found %s",
799 Py_TYPE(temp
)->tp_name
);
807 len
= PyString_GET_SIZE(temp
) + 1;
808 strn
= (char *)PyObject_MALLOC(len
);
810 (void) memcpy(strn
, PyString_AS_STRING(temp
), len
);
813 else if (!ISNONTERMINAL(type
)) {
815 * It has to be one or the other; this is an error.
816 * Raise an exception.
818 PyObject
*err
= Py_BuildValue("os", elem
, "unknown node type.");
819 PyErr_SetObject(parser_error
, err
);
824 err
= PyNode_AddChild(root
, type
, strn
, *line_num
, 0);
825 if (err
== E_NOMEM
) {
827 return (node
*) PyErr_NoMemory();
829 if (err
== E_OVERFLOW
) {
831 PyErr_SetString(PyExc_ValueError
,
832 "unsupported number of child nodes");
836 if (ISNONTERMINAL(type
)) {
837 node
* new_child
= CHILD(root
, i
- 1);
839 if (new_child
!= build_node_children(elem
, new_child
, line_num
)) {
844 else if (type
== NEWLINE
) { /* It's true: we increment the */
845 ++(*line_num
); /* line number *after* the newline! */
854 build_node_tree(PyObject
*tuple
)
857 PyObject
*temp
= PySequence_GetItem(tuple
, 0);
861 num
= PyInt_AsLong(temp
);
863 if (ISTERMINAL(num
)) {
865 * The tuple is simple, but it doesn't start with a start symbol.
866 * Raise an exception now and be done with it.
868 tuple
= Py_BuildValue("os", tuple
,
869 "Illegal syntax-tree; cannot start with terminal symbol.");
870 PyErr_SetObject(parser_error
, tuple
);
873 else if (ISNONTERMINAL(num
)) {
875 * Not efficient, but that can be handled later.
878 PyObject
*encoding
= NULL
;
880 if (num
== encoding_decl
) {
881 encoding
= PySequence_GetItem(tuple
, 2);
882 /* tuple isn't borrowed anymore here, need to DECREF */
883 tuple
= PySequence_GetSlice(tuple
, 0, 2);
885 res
= PyNode_New(num
);
887 if (res
!= build_node_children(tuple
, res
, &line_num
)) {
891 if (res
&& encoding
) {
893 len
= PyString_GET_SIZE(encoding
) + 1;
894 res
->n_str
= (char *)PyObject_MALLOC(len
);
895 if (res
->n_str
!= NULL
)
896 (void) memcpy(res
->n_str
, PyString_AS_STRING(encoding
), len
);
903 /* The tuple is illegal -- if the number is neither TERMINAL nor
904 * NONTERMINAL, we can't use it. Not sure the implementation
905 * allows this condition, but the API doesn't preclude it.
907 PyObject
*err
= Py_BuildValue("os", tuple
,
908 "Illegal component tuple.");
909 PyErr_SetObject(parser_error
, err
);
918 * Validation routines used within the validation section:
920 static int validate_terminal(node
*terminal
, int type
, char *string
);
922 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
923 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
924 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
925 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
926 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
927 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
928 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
929 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
930 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
931 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
932 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
933 #define validate_star(ch) validate_terminal(ch, STAR, "*")
934 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
935 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
936 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
937 #define validate_at(ch) validate_terminal(ch, AT, "@")
938 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
940 #define VALIDATER(n) static int validate_##n(node *tree)
942 VALIDATER(node
); VALIDATER(small_stmt
);
943 VALIDATER(class); VALIDATER(node
);
944 VALIDATER(parameters
); VALIDATER(suite
);
945 VALIDATER(testlist
); VALIDATER(varargslist
);
946 VALIDATER(fpdef
); VALIDATER(fplist
);
947 VALIDATER(stmt
); VALIDATER(simple_stmt
);
948 VALIDATER(expr_stmt
); VALIDATER(power
);
949 VALIDATER(print_stmt
); VALIDATER(del_stmt
);
950 VALIDATER(return_stmt
); VALIDATER(list_iter
);
951 VALIDATER(raise_stmt
); VALIDATER(import_stmt
);
952 VALIDATER(import_name
); VALIDATER(import_from
);
953 VALIDATER(global_stmt
); VALIDATER(list_if
);
954 VALIDATER(assert_stmt
); VALIDATER(list_for
);
955 VALIDATER(exec_stmt
); VALIDATER(compound_stmt
);
956 VALIDATER(while); VALIDATER(for);
957 VALIDATER(try); VALIDATER(except_clause
);
958 VALIDATER(test
); VALIDATER(and_test
);
959 VALIDATER(not_test
); VALIDATER(comparison
);
960 VALIDATER(comp_op
); VALIDATER(expr
);
961 VALIDATER(xor_expr
); VALIDATER(and_expr
);
962 VALIDATER(shift_expr
); VALIDATER(arith_expr
);
963 VALIDATER(term
); VALIDATER(factor
);
964 VALIDATER(atom
); VALIDATER(lambdef
);
965 VALIDATER(trailer
); VALIDATER(subscript
);
966 VALIDATER(subscriptlist
); VALIDATER(sliceop
);
967 VALIDATER(exprlist
); VALIDATER(dictorsetmaker
);
968 VALIDATER(arglist
); VALIDATER(argument
);
969 VALIDATER(listmaker
); VALIDATER(yield_stmt
);
970 VALIDATER(testlist1
); VALIDATER(comp_for
);
971 VALIDATER(comp_iter
); VALIDATER(comp_if
);
972 VALIDATER(testlist_comp
); VALIDATER(yield_expr
);
973 VALIDATER(yield_or_testlist
); VALIDATER(or_test
);
974 VALIDATER(old_test
); VALIDATER(old_lambdef
);
978 #define is_even(n) (((n) & 1) == 0)
979 #define is_odd(n) (((n) & 1) == 1)
983 validate_ntype(node
*n
, int t
)
986 PyErr_Format(parser_error
, "Expected node type %d, got %d.",
994 /* Verifies that the number of child nodes is exactly 'num', raising
995 * an exception if it isn't. The exception message does not indicate
996 * the exact number of nodes, allowing this to be used to raise the
997 * "right" exception when the wrong number of nodes is present in a
998 * specific variant of a statement's syntax. This is commonly used
1002 validate_numnodes(node
*n
, int num
, const char *const name
)
1004 if (NCH(n
) != num
) {
1005 PyErr_Format(parser_error
,
1006 "Illegal number of children for %s node.", name
);
1014 validate_terminal(node
*terminal
, int type
, char *string
)
1016 int res
= (validate_ntype(terminal
, type
)
1017 && ((string
== 0) || (strcmp(string
, STR(terminal
)) == 0)));
1019 if (!res
&& !PyErr_Occurred()) {
1020 PyErr_Format(parser_error
,
1021 "Illegal terminal: expected \"%s\"", string
);
1030 validate_repeating_list(node
*tree
, int ntype
, int (*vfunc
)(node
*),
1031 const char *const name
)
1033 int nch
= NCH(tree
);
1034 int res
= (nch
&& validate_ntype(tree
, ntype
)
1035 && vfunc(CHILD(tree
, 0)));
1037 if (!res
&& !PyErr_Occurred())
1038 (void) validate_numnodes(tree
, 1, name
);
1041 res
= validate_comma(CHILD(tree
, --nch
));
1042 if (res
&& nch
> 1) {
1044 for ( ; res
&& pos
< nch
; pos
+= 2)
1045 res
= (validate_comma(CHILD(tree
, pos
))
1046 && vfunc(CHILD(tree
, pos
+ 1)));
1056 * 'class' NAME ['(' testlist ')'] ':' suite
1059 validate_class(node
*tree
)
1061 int nch
= NCH(tree
);
1062 int res
= (validate_ntype(tree
, classdef
) &&
1063 ((nch
== 4) || (nch
== 6) || (nch
== 7)));
1066 res
= (validate_name(CHILD(tree
, 0), "class")
1067 && validate_ntype(CHILD(tree
, 1), NAME
)
1068 && validate_colon(CHILD(tree
, nch
- 2))
1069 && validate_suite(CHILD(tree
, nch
- 1)));
1072 (void) validate_numnodes(tree
, 4, "class");
1077 res
= ((validate_lparen(CHILD(tree
, 2)) &&
1078 validate_testlist(CHILD(tree
, 3)) &&
1079 validate_rparen(CHILD(tree
, 4))));
1081 else if (nch
== 6) {
1082 res
= (validate_lparen(CHILD(tree
,2)) &&
1083 validate_rparen(CHILD(tree
,3)));
1091 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1094 validate_if(node
*tree
)
1096 int nch
= NCH(tree
);
1097 int res
= (validate_ntype(tree
, if_stmt
)
1099 && validate_name(CHILD(tree
, 0), "if")
1100 && validate_test(CHILD(tree
, 1))
1101 && validate_colon(CHILD(tree
, 2))
1102 && validate_suite(CHILD(tree
, 3)));
1104 if (res
&& ((nch
% 4) == 3)) {
1105 /* ... 'else' ':' suite */
1106 res
= (validate_name(CHILD(tree
, nch
- 3), "else")
1107 && validate_colon(CHILD(tree
, nch
- 2))
1108 && validate_suite(CHILD(tree
, nch
- 1)));
1111 else if (!res
&& !PyErr_Occurred())
1112 (void) validate_numnodes(tree
, 4, "if");
1114 /* Will catch the case for nch < 4 */
1115 res
= validate_numnodes(tree
, 0, "if");
1116 else if (res
&& (nch
> 4)) {
1117 /* ... ('elif' test ':' suite)+ ... */
1119 while ((j
< nch
) && res
) {
1120 res
= (validate_name(CHILD(tree
, j
), "elif")
1121 && validate_colon(CHILD(tree
, j
+ 2))
1122 && validate_test(CHILD(tree
, j
+ 1))
1123 && validate_suite(CHILD(tree
, j
+ 3)));
1132 * '(' [varargslist] ')'
1136 validate_parameters(node
*tree
)
1138 int nch
= NCH(tree
);
1139 int res
= validate_ntype(tree
, parameters
) && ((nch
== 2) || (nch
== 3));
1142 res
= (validate_lparen(CHILD(tree
, 0))
1143 && validate_rparen(CHILD(tree
, nch
- 1)));
1144 if (res
&& (nch
== 3))
1145 res
= validate_varargslist(CHILD(tree
, 1));
1148 (void) validate_numnodes(tree
, 2, "parameters");
1158 * | NEWLINE INDENT stmt+ DEDENT
1161 validate_suite(node
*tree
)
1163 int nch
= NCH(tree
);
1164 int res
= (validate_ntype(tree
, suite
) && ((nch
== 1) || (nch
>= 4)));
1166 if (res
&& (nch
== 1))
1167 res
= validate_simple_stmt(CHILD(tree
, 0));
1169 /* NEWLINE INDENT stmt+ DEDENT */
1170 res
= (validate_newline(CHILD(tree
, 0))
1171 && validate_indent(CHILD(tree
, 1))
1172 && validate_stmt(CHILD(tree
, 2))
1173 && validate_dedent(CHILD(tree
, nch
- 1)));
1175 if (res
&& (nch
> 4)) {
1177 --nch
; /* forget the DEDENT */
1178 for ( ; res
&& (i
< nch
); ++i
)
1179 res
= validate_stmt(CHILD(tree
, i
));
1182 res
= validate_numnodes(tree
, 4, "suite");
1189 validate_testlist(node
*tree
)
1191 return (validate_repeating_list(tree
, testlist
,
1192 validate_test
, "testlist"));
1197 validate_testlist1(node
*tree
)
1199 return (validate_repeating_list(tree
, testlist1
,
1200 validate_test
, "testlist1"));
1205 validate_testlist_safe(node
*tree
)
1207 return (validate_repeating_list(tree
, testlist_safe
,
1208 validate_old_test
, "testlist_safe"));
1212 /* '*' NAME [',' '**' NAME] | '**' NAME
1215 validate_varargslist_trailer(node
*tree
, int start
)
1217 int nch
= NCH(tree
);
1222 err_string("expected variable argument trailer for varargslist");
1225 sym
= TYPE(CHILD(tree
, start
));
1228 * ('*' NAME [',' '**' NAME]
1231 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1232 else if (nch
-start
== 5)
1233 res
= (validate_name(CHILD(tree
, start
+1), NULL
)
1234 && validate_comma(CHILD(tree
, start
+2))
1235 && validate_doublestar(CHILD(tree
, start
+3))
1236 && validate_name(CHILD(tree
, start
+4), NULL
));
1238 else if (sym
== DOUBLESTAR
) {
1243 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1246 err_string("illegal variable argument trailer for varargslist");
1251 /* validate_varargslist()
1254 * (fpdef ['=' test] ',')*
1255 * ('*' NAME [',' '**' NAME]
1257 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1261 validate_varargslist(node
*tree
)
1263 int nch
= NCH(tree
);
1264 int res
= validate_ntype(tree
, varargslist
) && (nch
!= 0);
1270 err_string("varargslist missing child nodes");
1273 sym
= TYPE(CHILD(tree
, 0));
1274 if (sym
== STAR
|| sym
== DOUBLESTAR
)
1275 /* whole thing matches:
1276 * '*' NAME [',' '**' NAME] | '**' NAME
1278 res
= validate_varargslist_trailer(tree
, 0);
1279 else if (sym
== fpdef
) {
1282 sym
= TYPE(CHILD(tree
, nch
-1));
1285 * (fpdef ['=' test] ',')+
1286 * ('*' NAME [',' '**' NAME]
1289 /* skip over (fpdef ['=' test] ',')+ */
1290 while (res
&& (i
+2 <= nch
)) {
1291 res
= validate_fpdef(CHILD(tree
, i
));
1293 if (res
&& TYPE(CHILD(tree
, i
)) == EQUAL
&& (i
+2 <= nch
)) {
1294 res
= (validate_equal(CHILD(tree
, i
))
1295 && validate_test(CHILD(tree
, i
+1)));
1299 if (res
&& i
< nch
) {
1300 res
= validate_comma(CHILD(tree
, i
));
1303 && (TYPE(CHILD(tree
, i
)) == DOUBLESTAR
1304 || TYPE(CHILD(tree
, i
)) == STAR
))
1308 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1312 res
= validate_varargslist_trailer(tree
, i
);
1316 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1318 /* strip trailing comma node */
1320 res
= validate_comma(CHILD(tree
, nch
-1));
1326 * fpdef ['=' test] (',' fpdef ['=' test])*
1328 res
= validate_fpdef(CHILD(tree
, 0));
1330 if (res
&& (i
+2 <= nch
) && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1331 res
= (validate_equal(CHILD(tree
, i
))
1332 && validate_test(CHILD(tree
, i
+1)));
1336 * ... (',' fpdef ['=' test])*
1339 while (res
&& (nch
- i
) >= 2) {
1340 res
= (validate_comma(CHILD(tree
, i
))
1341 && validate_fpdef(CHILD(tree
, i
+1)));
1343 if (res
&& (nch
- i
) >= 2 && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1344 res
= (validate_equal(CHILD(tree
, i
))
1345 && validate_test(CHILD(tree
, i
+1)));
1349 if (res
&& nch
- i
!= 0) {
1351 err_string("illegal formation for varargslist");
1359 /* list_iter: list_for | list_if
1362 validate_list_iter(node
*tree
)
1364 int res
= (validate_ntype(tree
, list_iter
)
1365 && validate_numnodes(tree
, 1, "list_iter"));
1366 if (res
&& TYPE(CHILD(tree
, 0)) == list_for
)
1367 res
= validate_list_for(CHILD(tree
, 0));
1369 res
= validate_list_if(CHILD(tree
, 0));
1374 /* comp_iter: comp_for | comp_if
1377 validate_comp_iter(node
*tree
)
1379 int res
= (validate_ntype(tree
, comp_iter
)
1380 && validate_numnodes(tree
, 1, "comp_iter"));
1381 if (res
&& TYPE(CHILD(tree
, 0)) == comp_for
)
1382 res
= validate_comp_for(CHILD(tree
, 0));
1384 res
= validate_comp_if(CHILD(tree
, 0));
1389 /* list_for: 'for' exprlist 'in' testlist [list_iter]
1392 validate_list_for(node
*tree
)
1394 int nch
= NCH(tree
);
1398 res
= validate_list_iter(CHILD(tree
, 4));
1400 res
= validate_numnodes(tree
, 4, "list_for");
1403 res
= (validate_name(CHILD(tree
, 0), "for")
1404 && validate_exprlist(CHILD(tree
, 1))
1405 && validate_name(CHILD(tree
, 2), "in")
1406 && validate_testlist_safe(CHILD(tree
, 3)));
1411 /* comp_for: 'for' exprlist 'in' test [comp_iter]
1414 validate_comp_for(node
*tree
)
1416 int nch
= NCH(tree
);
1420 res
= validate_comp_iter(CHILD(tree
, 4));
1422 res
= validate_numnodes(tree
, 4, "comp_for");
1425 res
= (validate_name(CHILD(tree
, 0), "for")
1426 && validate_exprlist(CHILD(tree
, 1))
1427 && validate_name(CHILD(tree
, 2), "in")
1428 && validate_or_test(CHILD(tree
, 3)));
1433 /* list_if: 'if' old_test [list_iter]
1436 validate_list_if(node
*tree
)
1438 int nch
= NCH(tree
);
1442 res
= validate_list_iter(CHILD(tree
, 2));
1444 res
= validate_numnodes(tree
, 2, "list_if");
1447 res
= (validate_name(CHILD(tree
, 0), "if")
1448 && validate_old_test(CHILD(tree
, 1)));
1453 /* comp_if: 'if' old_test [comp_iter]
1456 validate_comp_if(node
*tree
)
1458 int nch
= NCH(tree
);
1462 res
= validate_comp_iter(CHILD(tree
, 2));
1464 res
= validate_numnodes(tree
, 2, "comp_if");
1467 res
= (validate_name(CHILD(tree
, 0), "if")
1468 && validate_old_test(CHILD(tree
, 1)));
1480 validate_fpdef(node
*tree
)
1482 int nch
= NCH(tree
);
1483 int res
= validate_ntype(tree
, fpdef
);
1487 res
= validate_ntype(CHILD(tree
, 0), NAME
);
1489 res
= (validate_lparen(CHILD(tree
, 0))
1490 && validate_fplist(CHILD(tree
, 1))
1491 && validate_rparen(CHILD(tree
, 2)));
1493 res
= validate_numnodes(tree
, 1, "fpdef");
1500 validate_fplist(node
*tree
)
1502 return (validate_repeating_list(tree
, fplist
,
1503 validate_fpdef
, "fplist"));
1507 /* simple_stmt | compound_stmt
1511 validate_stmt(node
*tree
)
1513 int res
= (validate_ntype(tree
, stmt
)
1514 && validate_numnodes(tree
, 1, "stmt"));
1517 tree
= CHILD(tree
, 0);
1519 if (TYPE(tree
) == simple_stmt
)
1520 res
= validate_simple_stmt(tree
);
1522 res
= validate_compound_stmt(tree
);
1528 /* small_stmt (';' small_stmt)* [';'] NEWLINE
1532 validate_simple_stmt(node
*tree
)
1534 int nch
= NCH(tree
);
1535 int res
= (validate_ntype(tree
, simple_stmt
)
1537 && validate_small_stmt(CHILD(tree
, 0))
1538 && validate_newline(CHILD(tree
, nch
- 1)));
1541 res
= validate_numnodes(tree
, 2, "simple_stmt");
1542 --nch
; /* forget the NEWLINE */
1543 if (res
&& is_even(nch
))
1544 res
= validate_semi(CHILD(tree
, --nch
));
1545 if (res
&& (nch
> 2)) {
1548 for (i
= 1; res
&& (i
< nch
); i
+= 2)
1549 res
= (validate_semi(CHILD(tree
, i
))
1550 && validate_small_stmt(CHILD(tree
, i
+ 1)));
1557 validate_small_stmt(node
*tree
)
1559 int nch
= NCH(tree
);
1560 int res
= validate_numnodes(tree
, 1, "small_stmt");
1563 int ntype
= TYPE(CHILD(tree
, 0));
1565 if ( (ntype
== expr_stmt
)
1566 || (ntype
== print_stmt
)
1567 || (ntype
== del_stmt
)
1568 || (ntype
== pass_stmt
)
1569 || (ntype
== flow_stmt
)
1570 || (ntype
== import_stmt
)
1571 || (ntype
== global_stmt
)
1572 || (ntype
== assert_stmt
)
1573 || (ntype
== exec_stmt
))
1574 res
= validate_node(CHILD(tree
, 0));
1577 err_string("illegal small_stmt child type");
1580 else if (nch
== 1) {
1582 PyErr_Format(parser_error
,
1583 "Unrecognized child node of small_stmt: %d.",
1584 TYPE(CHILD(tree
, 0)));
1591 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
1594 validate_compound_stmt(node
*tree
)
1596 int res
= (validate_ntype(tree
, compound_stmt
)
1597 && validate_numnodes(tree
, 1, "compound_stmt"));
1603 tree
= CHILD(tree
, 0);
1605 if ( (ntype
== if_stmt
)
1606 || (ntype
== while_stmt
)
1607 || (ntype
== for_stmt
)
1608 || (ntype
== try_stmt
)
1609 || (ntype
== with_stmt
)
1610 || (ntype
== funcdef
)
1611 || (ntype
== classdef
)
1612 || (ntype
== decorated
))
1613 res
= validate_node(tree
);
1616 PyErr_Format(parser_error
,
1617 "Illegal compound statement type: %d.", TYPE(tree
));
1623 validate_yield_or_testlist(node
*tree
)
1625 if (TYPE(tree
) == yield_expr
)
1626 return validate_yield_expr(tree
);
1628 return validate_testlist(tree
);
1632 validate_expr_stmt(node
*tree
)
1635 int nch
= NCH(tree
);
1636 int res
= (validate_ntype(tree
, expr_stmt
)
1638 && validate_testlist(CHILD(tree
, 0)));
1641 && TYPE(CHILD(tree
, 1)) == augassign
) {
1642 res
= validate_numnodes(CHILD(tree
, 1), 1, "augassign")
1643 && validate_yield_or_testlist(CHILD(tree
, 2));
1646 char *s
= STR(CHILD(CHILD(tree
, 1), 0));
1648 res
= (strcmp(s
, "+=") == 0
1649 || strcmp(s
, "-=") == 0
1650 || strcmp(s
, "*=") == 0
1651 || strcmp(s
, "/=") == 0
1652 || strcmp(s
, "//=") == 0
1653 || strcmp(s
, "%=") == 0
1654 || strcmp(s
, "&=") == 0
1655 || strcmp(s
, "|=") == 0
1656 || strcmp(s
, "^=") == 0
1657 || strcmp(s
, "<<=") == 0
1658 || strcmp(s
, ">>=") == 0
1659 || strcmp(s
, "**=") == 0);
1661 err_string("illegal augmented assignment operator");
1665 for (j
= 1; res
&& (j
< nch
); j
+= 2)
1666 res
= validate_equal(CHILD(tree
, j
))
1667 && validate_yield_or_testlist(CHILD(tree
, j
+ 1));
1675 * 'print' ( [ test (',' test)* [','] ]
1676 * | '>>' test [ (',' test)+ [','] ] )
1679 validate_print_stmt(node
*tree
)
1681 int nch
= NCH(tree
);
1682 int res
= (validate_ntype(tree
, print_stmt
)
1684 && validate_name(CHILD(tree
, 0), "print"));
1686 if (res
&& nch
> 1) {
1687 int sym
= TYPE(CHILD(tree
, 1));
1689 int allow_trailing_comma
= 1;
1692 res
= validate_test(CHILD(tree
, i
++));
1695 res
= validate_numnodes(tree
, 3, "print_stmt");
1697 res
= (validate_ntype(CHILD(tree
, i
), RIGHTSHIFT
)
1698 && validate_test(CHILD(tree
, i
+1)));
1700 allow_trailing_comma
= 0;
1704 /* ... (',' test)* [','] */
1705 while (res
&& i
+2 <= nch
) {
1706 res
= (validate_comma(CHILD(tree
, i
))
1707 && validate_test(CHILD(tree
, i
+1)));
1708 allow_trailing_comma
= 1;
1711 if (res
&& !allow_trailing_comma
)
1712 res
= validate_numnodes(tree
, i
, "print_stmt");
1713 else if (res
&& i
< nch
)
1714 res
= validate_comma(CHILD(tree
, i
));
1722 validate_del_stmt(node
*tree
)
1724 return (validate_numnodes(tree
, 2, "del_stmt")
1725 && validate_name(CHILD(tree
, 0), "del")
1726 && validate_exprlist(CHILD(tree
, 1)));
1731 validate_return_stmt(node
*tree
)
1733 int nch
= NCH(tree
);
1734 int res
= (validate_ntype(tree
, return_stmt
)
1735 && ((nch
== 1) || (nch
== 2))
1736 && validate_name(CHILD(tree
, 0), "return"));
1738 if (res
&& (nch
== 2))
1739 res
= validate_testlist(CHILD(tree
, 1));
1746 validate_raise_stmt(node
*tree
)
1748 int nch
= NCH(tree
);
1749 int res
= (validate_ntype(tree
, raise_stmt
)
1750 && ((nch
== 1) || (nch
== 2) || (nch
== 4) || (nch
== 6)));
1753 res
= validate_name(CHILD(tree
, 0), "raise");
1754 if (res
&& (nch
>= 2))
1755 res
= validate_test(CHILD(tree
, 1));
1756 if (res
&& nch
> 2) {
1757 res
= (validate_comma(CHILD(tree
, 2))
1758 && validate_test(CHILD(tree
, 3)));
1759 if (res
&& (nch
> 4))
1760 res
= (validate_comma(CHILD(tree
, 4))
1761 && validate_test(CHILD(tree
, 5)));
1765 (void) validate_numnodes(tree
, 2, "raise");
1766 if (res
&& (nch
== 4))
1767 res
= (validate_comma(CHILD(tree
, 2))
1768 && validate_test(CHILD(tree
, 3)));
1774 /* yield_expr: 'yield' [testlist]
1777 validate_yield_expr(node
*tree
)
1779 int nch
= NCH(tree
);
1780 int res
= (validate_ntype(tree
, yield_expr
)
1781 && ((nch
== 1) || (nch
== 2))
1782 && validate_name(CHILD(tree
, 0), "yield"));
1784 if (res
&& (nch
== 2))
1785 res
= validate_testlist(CHILD(tree
, 1));
1791 /* yield_stmt: yield_expr
1794 validate_yield_stmt(node
*tree
)
1796 return (validate_ntype(tree
, yield_stmt
)
1797 && validate_numnodes(tree
, 1, "yield_stmt")
1798 && validate_yield_expr(CHILD(tree
, 0)));
1803 validate_import_as_name(node
*tree
)
1805 int nch
= NCH(tree
);
1806 int ok
= validate_ntype(tree
, import_as_name
);
1810 ok
= validate_name(CHILD(tree
, 0), NULL
);
1812 ok
= (validate_name(CHILD(tree
, 0), NULL
)
1813 && validate_name(CHILD(tree
, 1), "as")
1814 && validate_name(CHILD(tree
, 2), NULL
));
1816 ok
= validate_numnodes(tree
, 3, "import_as_name");
1822 /* dotted_name: NAME ("." NAME)*
1825 validate_dotted_name(node
*tree
)
1827 int nch
= NCH(tree
);
1828 int res
= (validate_ntype(tree
, dotted_name
)
1830 && validate_name(CHILD(tree
, 0), NULL
));
1833 for (i
= 1; res
&& (i
< nch
); i
+= 2) {
1834 res
= (validate_dot(CHILD(tree
, i
))
1835 && validate_name(CHILD(tree
, i
+1), NULL
));
1841 /* dotted_as_name: dotted_name [NAME NAME]
1844 validate_dotted_as_name(node
*tree
)
1846 int nch
= NCH(tree
);
1847 int res
= validate_ntype(tree
, dotted_as_name
);
1851 res
= validate_dotted_name(CHILD(tree
, 0));
1853 res
= (validate_dotted_name(CHILD(tree
, 0))
1854 && validate_name(CHILD(tree
, 1), "as")
1855 && validate_name(CHILD(tree
, 2), NULL
));
1858 err_string("illegal number of children for dotted_as_name");
1865 /* dotted_as_name (',' dotted_as_name)* */
1867 validate_dotted_as_names(node
*tree
)
1869 int nch
= NCH(tree
);
1870 int res
= is_odd(nch
) && validate_dotted_as_name(CHILD(tree
, 0));
1873 for (i
= 1; res
&& (i
< nch
); i
+= 2)
1874 res
= (validate_comma(CHILD(tree
, i
))
1875 && validate_dotted_as_name(CHILD(tree
, i
+ 1)));
1880 /* import_as_name (',' import_as_name)* [','] */
1882 validate_import_as_names(node
*tree
)
1884 int nch
= NCH(tree
);
1885 int res
= validate_import_as_name(CHILD(tree
, 0));
1888 for (i
= 1; res
&& (i
+ 1 < nch
); i
+= 2)
1889 res
= (validate_comma(CHILD(tree
, i
))
1890 && validate_import_as_name(CHILD(tree
, i
+ 1)));
1895 /* 'import' dotted_as_names */
1897 validate_import_name(node
*tree
)
1899 return (validate_ntype(tree
, import_name
)
1900 && validate_numnodes(tree
, 2, "import_name")
1901 && validate_name(CHILD(tree
, 0), "import")
1902 && validate_dotted_as_names(CHILD(tree
, 1)));
1905 /* Helper function to count the number of leading dots in
1906 * 'from ...module import name'
1909 count_from_dots(node
*tree
)
1912 for (i
= 1; i
< NCH(tree
); i
++)
1913 if (TYPE(CHILD(tree
, i
)) != DOT
)
1918 /* import_from: ('from' ('.'* dotted_name | '.'+)
1919 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
1922 validate_import_from(node
*tree
)
1924 int nch
= NCH(tree
);
1925 int ndots
= count_from_dots(tree
);
1926 int havename
= (TYPE(CHILD(tree
, ndots
+ 1)) == dotted_name
);
1927 int offset
= ndots
+ havename
;
1928 int res
= validate_ntype(tree
, import_from
)
1930 && (nch
>= 3 + offset
)
1931 && validate_name(CHILD(tree
, 0), "from")
1932 && (!havename
|| validate_dotted_name(CHILD(tree
, ndots
+ 1)))
1933 && validate_name(CHILD(tree
, offset
+ 1), "import");
1935 if (res
&& TYPE(CHILD(tree
, offset
+ 2)) == LPAR
)
1936 res
= ((nch
== offset
+ 5)
1937 && validate_lparen(CHILD(tree
, offset
+ 2))
1938 && validate_import_as_names(CHILD(tree
, offset
+ 3))
1939 && validate_rparen(CHILD(tree
, offset
+ 4)));
1940 else if (res
&& TYPE(CHILD(tree
, offset
+ 2)) != STAR
)
1941 res
= validate_import_as_names(CHILD(tree
, offset
+ 2));
1946 /* import_stmt: import_name | import_from */
1948 validate_import_stmt(node
*tree
)
1950 int nch
= NCH(tree
);
1951 int res
= validate_numnodes(tree
, 1, "import_stmt");
1954 int ntype
= TYPE(CHILD(tree
, 0));
1956 if (ntype
== import_name
|| ntype
== import_from
)
1957 res
= validate_node(CHILD(tree
, 0));
1960 err_string("illegal import_stmt child type");
1963 else if (nch
== 1) {
1965 PyErr_Format(parser_error
,
1966 "Unrecognized child node of import_stmt: %d.",
1967 TYPE(CHILD(tree
, 0)));
1976 validate_global_stmt(node
*tree
)
1979 int nch
= NCH(tree
);
1980 int res
= (validate_ntype(tree
, global_stmt
)
1981 && is_even(nch
) && (nch
>= 2));
1983 if (!res
&& !PyErr_Occurred())
1984 err_string("illegal global statement");
1987 res
= (validate_name(CHILD(tree
, 0), "global")
1988 && validate_ntype(CHILD(tree
, 1), NAME
));
1989 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1990 res
= (validate_comma(CHILD(tree
, j
))
1991 && validate_ntype(CHILD(tree
, j
+ 1), NAME
));
1999 * 'exec' expr ['in' test [',' test]]
2002 validate_exec_stmt(node
*tree
)
2004 int nch
= NCH(tree
);
2005 int res
= (validate_ntype(tree
, exec_stmt
)
2006 && ((nch
== 2) || (nch
== 4) || (nch
== 6))
2007 && validate_name(CHILD(tree
, 0), "exec")
2008 && validate_expr(CHILD(tree
, 1)));
2010 if (!res
&& !PyErr_Occurred())
2011 err_string("illegal exec statement");
2012 if (res
&& (nch
> 2))
2013 res
= (validate_name(CHILD(tree
, 2), "in")
2014 && validate_test(CHILD(tree
, 3)));
2015 if (res
&& (nch
== 6))
2016 res
= (validate_comma(CHILD(tree
, 4))
2017 && validate_test(CHILD(tree
, 5)));
2025 * 'assert' test [',' test]
2028 validate_assert_stmt(node
*tree
)
2030 int nch
= NCH(tree
);
2031 int res
= (validate_ntype(tree
, assert_stmt
)
2032 && ((nch
== 2) || (nch
== 4))
2033 && (validate_name(CHILD(tree
, 0), "assert"))
2034 && validate_test(CHILD(tree
, 1)));
2036 if (!res
&& !PyErr_Occurred())
2037 err_string("illegal assert statement");
2038 if (res
&& (nch
> 2))
2039 res
= (validate_comma(CHILD(tree
, 2))
2040 && validate_test(CHILD(tree
, 3)));
2047 validate_while(node
*tree
)
2049 int nch
= NCH(tree
);
2050 int res
= (validate_ntype(tree
, while_stmt
)
2051 && ((nch
== 4) || (nch
== 7))
2052 && validate_name(CHILD(tree
, 0), "while")
2053 && validate_test(CHILD(tree
, 1))
2054 && validate_colon(CHILD(tree
, 2))
2055 && validate_suite(CHILD(tree
, 3)));
2057 if (res
&& (nch
== 7))
2058 res
= (validate_name(CHILD(tree
, 4), "else")
2059 && validate_colon(CHILD(tree
, 5))
2060 && validate_suite(CHILD(tree
, 6)));
2067 validate_for(node
*tree
)
2069 int nch
= NCH(tree
);
2070 int res
= (validate_ntype(tree
, for_stmt
)
2071 && ((nch
== 6) || (nch
== 9))
2072 && validate_name(CHILD(tree
, 0), "for")
2073 && validate_exprlist(CHILD(tree
, 1))
2074 && validate_name(CHILD(tree
, 2), "in")
2075 && validate_testlist(CHILD(tree
, 3))
2076 && validate_colon(CHILD(tree
, 4))
2077 && validate_suite(CHILD(tree
, 5)));
2079 if (res
&& (nch
== 9))
2080 res
= (validate_name(CHILD(tree
, 6), "else")
2081 && validate_colon(CHILD(tree
, 7))
2082 && validate_suite(CHILD(tree
, 8)));
2089 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2090 ['finally' ':' suite]
2091 * | 'try' ':' suite 'finally' ':' suite
2095 validate_try(node
*tree
)
2097 int nch
= NCH(tree
);
2099 int res
= (validate_ntype(tree
, try_stmt
)
2100 && (nch
>= 6) && ((nch
% 3) == 0));
2103 res
= (validate_name(CHILD(tree
, 0), "try")
2104 && validate_colon(CHILD(tree
, 1))
2105 && validate_suite(CHILD(tree
, 2))
2106 && validate_colon(CHILD(tree
, nch
- 2))
2107 && validate_suite(CHILD(tree
, nch
- 1)));
2108 else if (!PyErr_Occurred()) {
2109 const char* name
= "except";
2110 if (TYPE(CHILD(tree
, nch
- 3)) != except_clause
)
2111 name
= STR(CHILD(tree
, nch
- 3));
2113 PyErr_Format(parser_error
,
2114 "Illegal number of children for try/%s node.", name
);
2116 /* Handle try/finally statement */
2117 if (res
&& (TYPE(CHILD(tree
, pos
)) == NAME
) &&
2118 (strcmp(STR(CHILD(tree
, pos
)), "finally") == 0)) {
2119 res
= (validate_numnodes(tree
, 6, "try/finally")
2120 && validate_colon(CHILD(tree
, 4))
2121 && validate_suite(CHILD(tree
, 5)));
2124 /* try/except statement: skip past except_clause sections */
2125 while (res
&& pos
< nch
&& (TYPE(CHILD(tree
, pos
)) == except_clause
)) {
2126 res
= (validate_except_clause(CHILD(tree
, pos
))
2127 && validate_colon(CHILD(tree
, pos
+ 1))
2128 && validate_suite(CHILD(tree
, pos
+ 2)));
2131 /* skip else clause */
2132 if (res
&& pos
< nch
&& (TYPE(CHILD(tree
, pos
)) == NAME
) &&
2133 (strcmp(STR(CHILD(tree
, pos
)), "else") == 0)) {
2134 res
= (validate_colon(CHILD(tree
, pos
+ 1))
2135 && validate_suite(CHILD(tree
, pos
+ 2)));
2138 if (res
&& pos
< nch
) {
2139 /* last clause must be a finally */
2140 res
= (validate_name(CHILD(tree
, pos
), "finally")
2141 && validate_numnodes(tree
, pos
+ 3, "try/except/finally")
2142 && validate_colon(CHILD(tree
, pos
+ 1))
2143 && validate_suite(CHILD(tree
, pos
+ 2)));
2150 validate_except_clause(node
*tree
)
2152 int nch
= NCH(tree
);
2153 int res
= (validate_ntype(tree
, except_clause
)
2154 && ((nch
== 1) || (nch
== 2) || (nch
== 4))
2155 && validate_name(CHILD(tree
, 0), "except"));
2157 if (res
&& (nch
> 1))
2158 res
= validate_test(CHILD(tree
, 1));
2159 if (res
&& (nch
== 4)) {
2160 if (TYPE(CHILD(tree
, 2)) == NAME
)
2161 res
= validate_name(CHILD(tree
, 2), "as");
2163 res
= validate_comma(CHILD(tree
, 2));
2164 res
= res
&& validate_test(CHILD(tree
, 3));
2171 validate_test(node
*tree
)
2173 int nch
= NCH(tree
);
2174 int res
= validate_ntype(tree
, test
) && is_odd(nch
);
2176 if (res
&& (TYPE(CHILD(tree
, 0)) == lambdef
))
2178 && validate_lambdef(CHILD(tree
, 0)));
2180 res
= validate_or_test(CHILD(tree
, 0));
2181 res
= (res
&& (nch
== 1 || (nch
== 5 &&
2182 validate_name(CHILD(tree
, 1), "if") &&
2183 validate_or_test(CHILD(tree
, 2)) &&
2184 validate_name(CHILD(tree
, 3), "else") &&
2185 validate_test(CHILD(tree
, 4)))));
2191 validate_old_test(node
*tree
)
2193 int nch
= NCH(tree
);
2194 int res
= validate_ntype(tree
, old_test
) && (nch
== 1);
2196 if (res
&& (TYPE(CHILD(tree
, 0)) == old_lambdef
))
2197 res
= (validate_old_lambdef(CHILD(tree
, 0)));
2199 res
= (validate_or_test(CHILD(tree
, 0)));
2205 validate_or_test(node
*tree
)
2207 int nch
= NCH(tree
);
2208 int res
= validate_ntype(tree
, or_test
) && is_odd(nch
);
2212 res
= validate_and_test(CHILD(tree
, 0));
2213 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2214 res
= (validate_name(CHILD(tree
, pos
), "or")
2215 && validate_and_test(CHILD(tree
, pos
+ 1)));
2222 validate_and_test(node
*tree
)
2225 int nch
= NCH(tree
);
2226 int res
= (validate_ntype(tree
, and_test
)
2228 && validate_not_test(CHILD(tree
, 0)));
2230 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2231 res
= (validate_name(CHILD(tree
, pos
), "and")
2232 && validate_not_test(CHILD(tree
, 0)));
2239 validate_not_test(node
*tree
)
2241 int nch
= NCH(tree
);
2242 int res
= validate_ntype(tree
, not_test
) && ((nch
== 1) || (nch
== 2));
2246 res
= (validate_name(CHILD(tree
, 0), "not")
2247 && validate_not_test(CHILD(tree
, 1)));
2249 res
= validate_comparison(CHILD(tree
, 0));
2256 validate_comparison(node
*tree
)
2259 int nch
= NCH(tree
);
2260 int res
= (validate_ntype(tree
, comparison
)
2262 && validate_expr(CHILD(tree
, 0)));
2264 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2265 res
= (validate_comp_op(CHILD(tree
, pos
))
2266 && validate_expr(CHILD(tree
, pos
+ 1)));
2273 validate_comp_op(node
*tree
)
2276 int nch
= NCH(tree
);
2278 if (!validate_ntype(tree
, comp_op
))
2282 * Only child will be a terminal with a well-defined symbolic name
2283 * or a NAME with a string of either 'is' or 'in'
2285 tree
= CHILD(tree
, 0);
2286 switch (TYPE(tree
)) {
2297 res
= ((strcmp(STR(tree
), "in") == 0)
2298 || (strcmp(STR(tree
), "is") == 0));
2300 PyErr_Format(parser_error
,
2301 "illegal operator '%s'", STR(tree
));
2305 err_string("illegal comparison operator type");
2309 else if ((res
= validate_numnodes(tree
, 2, "comp_op")) != 0) {
2310 res
= (validate_ntype(CHILD(tree
, 0), NAME
)
2311 && validate_ntype(CHILD(tree
, 1), NAME
)
2312 && (((strcmp(STR(CHILD(tree
, 0)), "is") == 0)
2313 && (strcmp(STR(CHILD(tree
, 1)), "not") == 0))
2314 || ((strcmp(STR(CHILD(tree
, 0)), "not") == 0)
2315 && (strcmp(STR(CHILD(tree
, 1)), "in") == 0))));
2316 if (!res
&& !PyErr_Occurred())
2317 err_string("unknown comparison operator");
2324 validate_expr(node
*tree
)
2327 int nch
= NCH(tree
);
2328 int res
= (validate_ntype(tree
, expr
)
2330 && validate_xor_expr(CHILD(tree
, 0)));
2332 for (j
= 2; res
&& (j
< nch
); j
+= 2)
2333 res
= (validate_xor_expr(CHILD(tree
, j
))
2334 && validate_vbar(CHILD(tree
, j
- 1)));
2341 validate_xor_expr(node
*tree
)
2344 int nch
= NCH(tree
);
2345 int res
= (validate_ntype(tree
, xor_expr
)
2347 && validate_and_expr(CHILD(tree
, 0)));
2349 for (j
= 2; res
&& (j
< nch
); j
+= 2)
2350 res
= (validate_circumflex(CHILD(tree
, j
- 1))
2351 && validate_and_expr(CHILD(tree
, j
)));
2358 validate_and_expr(node
*tree
)
2361 int nch
= NCH(tree
);
2362 int res
= (validate_ntype(tree
, and_expr
)
2364 && validate_shift_expr(CHILD(tree
, 0)));
2366 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2367 res
= (validate_ampersand(CHILD(tree
, pos
))
2368 && validate_shift_expr(CHILD(tree
, pos
+ 1)));
2375 validate_chain_two_ops(node
*tree
, int (*termvalid
)(node
*), int op1
, int op2
)
2378 int nch
= NCH(tree
);
2379 int res
= (is_odd(nch
)
2380 && (*termvalid
)(CHILD(tree
, 0)));
2382 for ( ; res
&& (pos
< nch
); pos
+= 2) {
2383 if (TYPE(CHILD(tree
, pos
)) != op1
)
2384 res
= validate_ntype(CHILD(tree
, pos
), op2
);
2386 res
= (*termvalid
)(CHILD(tree
, pos
+ 1));
2393 validate_shift_expr(node
*tree
)
2395 return (validate_ntype(tree
, shift_expr
)
2396 && validate_chain_two_ops(tree
, validate_arith_expr
,
2397 LEFTSHIFT
, RIGHTSHIFT
));
2402 validate_arith_expr(node
*tree
)
2404 return (validate_ntype(tree
, arith_expr
)
2405 && validate_chain_two_ops(tree
, validate_term
, PLUS
, MINUS
));
2410 validate_term(node
*tree
)
2413 int nch
= NCH(tree
);
2414 int res
= (validate_ntype(tree
, term
)
2416 && validate_factor(CHILD(tree
, 0)));
2418 for ( ; res
&& (pos
< nch
); pos
+= 2)
2419 res
= (((TYPE(CHILD(tree
, pos
)) == STAR
)
2420 || (TYPE(CHILD(tree
, pos
)) == SLASH
)
2421 || (TYPE(CHILD(tree
, pos
)) == DOUBLESLASH
)
2422 || (TYPE(CHILD(tree
, pos
)) == PERCENT
))
2423 && validate_factor(CHILD(tree
, pos
+ 1)));
2431 * factor: ('+'|'-'|'~') factor | power
2434 validate_factor(node
*tree
)
2436 int nch
= NCH(tree
);
2437 int res
= (validate_ntype(tree
, factor
)
2439 && ((TYPE(CHILD(tree
, 0)) == PLUS
)
2440 || (TYPE(CHILD(tree
, 0)) == MINUS
)
2441 || (TYPE(CHILD(tree
, 0)) == TILDE
))
2442 && validate_factor(CHILD(tree
, 1)))
2444 && validate_power(CHILD(tree
, 0)))));
2451 * power: atom trailer* ('**' factor)*
2454 validate_power(node
*tree
)
2457 int nch
= NCH(tree
);
2458 int res
= (validate_ntype(tree
, power
) && (nch
>= 1)
2459 && validate_atom(CHILD(tree
, 0)));
2461 while (res
&& (pos
< nch
) && (TYPE(CHILD(tree
, pos
)) == trailer
))
2462 res
= validate_trailer(CHILD(tree
, pos
++));
2463 if (res
&& (pos
< nch
)) {
2464 if (!is_even(nch
- pos
)) {
2465 err_string("illegal number of nodes for 'power'");
2468 for ( ; res
&& (pos
< (nch
- 1)); pos
+= 2)
2469 res
= (validate_doublestar(CHILD(tree
, pos
))
2470 && validate_factor(CHILD(tree
, pos
+ 1)));
2477 validate_atom(node
*tree
)
2480 int nch
= NCH(tree
);
2481 int res
= validate_ntype(tree
, atom
);
2484 res
= validate_numnodes(tree
, nch
+1, "atom");
2486 switch (TYPE(CHILD(tree
, 0))) {
2489 && (validate_rparen(CHILD(tree
, nch
- 1))));
2491 if (res
&& (nch
== 3)) {
2492 if (TYPE(CHILD(tree
, 1))==yield_expr
)
2493 res
= validate_yield_expr(CHILD(tree
, 1));
2495 res
= validate_testlist_comp(CHILD(tree
, 1));
2500 res
= validate_ntype(CHILD(tree
, 1), RSQB
);
2502 res
= (validate_listmaker(CHILD(tree
, 1))
2503 && validate_ntype(CHILD(tree
, 2), RSQB
));
2506 err_string("illegal list display atom");
2511 && validate_ntype(CHILD(tree
, nch
- 1), RBRACE
));
2513 if (res
&& (nch
== 3))
2514 res
= validate_dictorsetmaker(CHILD(tree
, 1));
2518 && validate_testlist1(CHILD(tree
, 1))
2519 && validate_ntype(CHILD(tree
, 2), BACKQUOTE
));
2526 for (pos
= 1; res
&& (pos
< nch
); ++pos
)
2527 res
= validate_ntype(CHILD(tree
, pos
), STRING
);
2539 * test ( list_for | (',' test)* [','] )
2542 validate_listmaker(node
*tree
)
2544 int nch
= NCH(tree
);
2548 err_string("missing child nodes of listmaker");
2550 ok
= validate_test(CHILD(tree
, 0));
2553 * list_for | (',' test)* [',']
2555 if (nch
== 2 && TYPE(CHILD(tree
, 1)) == list_for
)
2556 ok
= validate_list_for(CHILD(tree
, 1));
2558 /* (',' test)* [','] */
2560 while (ok
&& nch
- i
>= 2) {
2561 ok
= (validate_comma(CHILD(tree
, i
))
2562 && validate_test(CHILD(tree
, i
+1)));
2565 if (ok
&& i
== nch
-1)
2566 ok
= validate_comma(CHILD(tree
, i
));
2567 else if (i
!= nch
) {
2569 err_string("illegal trailing nodes for listmaker");
2576 * test ( comp_for | (',' test)* [','] )
2579 validate_testlist_comp(node
*tree
)
2581 int nch
= NCH(tree
);
2585 err_string("missing child nodes of testlist_comp");
2587 ok
= validate_test(CHILD(tree
, 0));
2591 * comp_for | (',' test)* [',']
2593 if (nch
== 2 && TYPE(CHILD(tree
, 1)) == comp_for
)
2594 ok
= validate_comp_for(CHILD(tree
, 1));
2596 /* (',' test)* [','] */
2598 while (ok
&& nch
- i
>= 2) {
2599 ok
= (validate_comma(CHILD(tree
, i
))
2600 && validate_test(CHILD(tree
, i
+1)));
2603 if (ok
&& i
== nch
-1)
2604 ok
= validate_comma(CHILD(tree
, i
));
2605 else if (i
!= nch
) {
2607 err_string("illegal trailing nodes for testlist_comp");
2614 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2617 validate_decorator(node
*tree
)
2620 int nch
= NCH(tree
);
2621 ok
= (validate_ntype(tree
, decorator
) &&
2622 (nch
== 3 || nch
== 5 || nch
== 6) &&
2623 validate_at(CHILD(tree
, 0)) &&
2624 validate_dotted_name(CHILD(tree
, 1)) &&
2625 validate_newline(RCHILD(tree
, -1)));
2627 if (ok
&& nch
!= 3) {
2628 ok
= (validate_lparen(CHILD(tree
, 2)) &&
2629 validate_rparen(RCHILD(tree
, -2)));
2632 ok
= validate_arglist(CHILD(tree
, 3));
2642 validate_decorators(node
*tree
)
2646 ok
= validate_ntype(tree
, decorators
) && nch
>= 1;
2648 for (i
= 0; ok
&& i
< nch
; ++i
)
2649 ok
= validate_decorator(CHILD(tree
, i
));
2658 validate_with_item(node
*tree
)
2660 int nch
= NCH(tree
);
2661 int ok
= (validate_ntype(tree
, with_item
)
2662 && (nch
== 1 || nch
== 3)
2663 && validate_test(CHILD(tree
, 0)));
2665 ok
= (validate_name(CHILD(tree
, 1), "as")
2666 && validate_expr(CHILD(tree
, 2)));
2672 * 'with' with_item (',' with_item)* ':' suite
2675 validate_with_stmt(node
*tree
)
2678 int nch
= NCH(tree
);
2679 int ok
= (validate_ntype(tree
, with_stmt
)
2681 && validate_name(CHILD(tree
, 0), "with")
2682 && validate_colon(RCHILD(tree
, -2))
2683 && validate_suite(RCHILD(tree
, -1)));
2684 for (i
= 1; ok
&& i
< nch
- 2; i
+= 2)
2685 ok
= validate_with_item(CHILD(tree
, i
));
2692 * 'def' NAME parameters ':' suite
2695 validate_funcdef(node
*tree
)
2697 int nch
= NCH(tree
);
2698 int ok
= (validate_ntype(tree
, funcdef
)
2700 && validate_name(RCHILD(tree
, -5), "def")
2701 && validate_ntype(RCHILD(tree
, -4), NAME
)
2702 && validate_colon(RCHILD(tree
, -2))
2703 && validate_parameters(RCHILD(tree
, -3))
2704 && validate_suite(RCHILD(tree
, -1)));
2710 * decorators (classdef | funcdef)
2713 validate_decorated(node
*tree
)
2715 int nch
= NCH(tree
);
2716 int ok
= (validate_ntype(tree
, decorated
)
2718 && validate_decorators(RCHILD(tree
, -2)));
2719 if (TYPE(RCHILD(tree
, -1)) == funcdef
)
2720 ok
= ok
&& validate_funcdef(RCHILD(tree
, -1));
2722 ok
= ok
&& validate_class(RCHILD(tree
, -1));
2727 validate_lambdef(node
*tree
)
2729 int nch
= NCH(tree
);
2730 int res
= (validate_ntype(tree
, lambdef
)
2731 && ((nch
== 3) || (nch
== 4))
2732 && validate_name(CHILD(tree
, 0), "lambda")
2733 && validate_colon(CHILD(tree
, nch
- 2))
2734 && validate_test(CHILD(tree
, nch
- 1)));
2736 if (res
&& (nch
== 4))
2737 res
= validate_varargslist(CHILD(tree
, 1));
2738 else if (!res
&& !PyErr_Occurred())
2739 (void) validate_numnodes(tree
, 3, "lambdef");
2746 validate_old_lambdef(node
*tree
)
2748 int nch
= NCH(tree
);
2749 int res
= (validate_ntype(tree
, old_lambdef
)
2750 && ((nch
== 3) || (nch
== 4))
2751 && validate_name(CHILD(tree
, 0), "lambda")
2752 && validate_colon(CHILD(tree
, nch
- 2))
2753 && validate_test(CHILD(tree
, nch
- 1)));
2755 if (res
&& (nch
== 4))
2756 res
= validate_varargslist(CHILD(tree
, 1));
2757 else if (!res
&& !PyErr_Occurred())
2758 (void) validate_numnodes(tree
, 3, "old_lambdef");
2766 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2769 validate_arglist(node
*tree
)
2771 int nch
= NCH(tree
);
2776 /* raise the right error from having an invalid number of children */
2777 return validate_numnodes(tree
, nch
+ 1, "arglist");
2780 for (i
=0; i
<nch
; i
++) {
2781 if (TYPE(CHILD(tree
, i
)) == argument
) {
2782 node
*ch
= CHILD(tree
, i
);
2783 if (NCH(ch
) == 2 && TYPE(CHILD(ch
, 1)) == comp_for
) {
2784 err_string("need '(', ')' for generator expression");
2791 while (ok
&& nch
-i
>= 2) {
2792 /* skip leading (argument ',') */
2793 ok
= (validate_argument(CHILD(tree
, i
))
2794 && validate_comma(CHILD(tree
, i
+1)));
2803 * argument | '*' test [',' '**' test] | '**' test
2805 int sym
= TYPE(CHILD(tree
, i
));
2807 if (sym
== argument
) {
2808 ok
= validate_argument(CHILD(tree
, i
));
2809 if (ok
&& i
+1 != nch
) {
2810 err_string("illegal arglist specification"
2811 " (extra stuff on end)");
2815 else if (sym
== STAR
) {
2816 ok
= validate_star(CHILD(tree
, i
));
2817 if (ok
&& (nch
-i
== 2))
2818 ok
= validate_test(CHILD(tree
, i
+1));
2819 else if (ok
&& (nch
-i
== 5))
2820 ok
= (validate_test(CHILD(tree
, i
+1))
2821 && validate_comma(CHILD(tree
, i
+2))
2822 && validate_doublestar(CHILD(tree
, i
+3))
2823 && validate_test(CHILD(tree
, i
+4)));
2825 err_string("illegal use of '*' in arglist");
2829 else if (sym
== DOUBLESTAR
) {
2831 ok
= (validate_doublestar(CHILD(tree
, i
))
2832 && validate_test(CHILD(tree
, i
+1)));
2834 err_string("illegal use of '**' in arglist");
2839 err_string("illegal arglist specification");
2850 * [test '='] test [comp_for]
2853 validate_argument(node
*tree
)
2855 int nch
= NCH(tree
);
2856 int res
= (validate_ntype(tree
, argument
)
2857 && ((nch
== 1) || (nch
== 2) || (nch
== 3))
2858 && validate_test(CHILD(tree
, 0)));
2860 if (res
&& (nch
== 2))
2861 res
= validate_comp_for(CHILD(tree
, 1));
2862 else if (res
&& (nch
== 3))
2863 res
= (validate_equal(CHILD(tree
, 1))
2864 && validate_test(CHILD(tree
, 2)));
2873 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2876 validate_trailer(node
*tree
)
2878 int nch
= NCH(tree
);
2879 int res
= validate_ntype(tree
, trailer
) && ((nch
== 2) || (nch
== 3));
2882 switch (TYPE(CHILD(tree
, 0))) {
2884 res
= validate_rparen(CHILD(tree
, nch
- 1));
2885 if (res
&& (nch
== 3))
2886 res
= validate_arglist(CHILD(tree
, 1));
2889 res
= (validate_numnodes(tree
, 3, "trailer")
2890 && validate_subscriptlist(CHILD(tree
, 1))
2891 && validate_ntype(CHILD(tree
, 2), RSQB
));
2894 res
= (validate_numnodes(tree
, 2, "trailer")
2895 && validate_ntype(CHILD(tree
, 1), NAME
));
2903 (void) validate_numnodes(tree
, 2, "trailer");
2911 * subscript (',' subscript)* [',']
2914 validate_subscriptlist(node
*tree
)
2916 return (validate_repeating_list(tree
, subscriptlist
,
2917 validate_subscript
, "subscriptlist"));
2923 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
2926 validate_subscript(node
*tree
)
2929 int nch
= NCH(tree
);
2930 int res
= validate_ntype(tree
, subscript
) && (nch
>= 1) && (nch
<= 4);
2933 if (!PyErr_Occurred())
2934 err_string("invalid number of arguments for subscript node");
2937 if (TYPE(CHILD(tree
, 0)) == DOT
)
2938 /* take care of ('.' '.' '.') possibility */
2939 return (validate_numnodes(tree
, 3, "subscript")
2940 && validate_dot(CHILD(tree
, 0))
2941 && validate_dot(CHILD(tree
, 1))
2942 && validate_dot(CHILD(tree
, 2)));
2944 if (TYPE(CHILD(tree
, 0)) == test
)
2945 res
= validate_test(CHILD(tree
, 0));
2947 res
= validate_colon(CHILD(tree
, 0));
2950 /* Must be [test] ':' [test] [sliceop],
2951 * but at least one of the optional components will
2952 * be present, but we don't know which yet.
2954 if ((TYPE(CHILD(tree
, 0)) != COLON
) || (nch
== 4)) {
2955 res
= validate_test(CHILD(tree
, 0));
2959 res
= validate_colon(CHILD(tree
, offset
));
2961 int rem
= nch
- ++offset
;
2963 if (TYPE(CHILD(tree
, offset
)) == test
) {
2964 res
= validate_test(CHILD(tree
, offset
));
2969 res
= validate_sliceop(CHILD(tree
, offset
));
2977 validate_sliceop(node
*tree
)
2979 int nch
= NCH(tree
);
2980 int res
= ((nch
== 1) || validate_numnodes(tree
, 2, "sliceop"))
2981 && validate_ntype(tree
, sliceop
);
2982 if (!res
&& !PyErr_Occurred()) {
2983 res
= validate_numnodes(tree
, 1, "sliceop");
2986 res
= validate_colon(CHILD(tree
, 0));
2987 if (res
&& (nch
== 2))
2988 res
= validate_test(CHILD(tree
, 1));
2995 validate_exprlist(node
*tree
)
2997 return (validate_repeating_list(tree
, exprlist
,
2998 validate_expr
, "exprlist"));
3005 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
3006 * (test (comp_for | (',' test)* [',']))
3009 validate_dictorsetmaker(node
*tree
)
3011 int nch
= NCH(tree
);
3012 int ok
= validate_ntype(tree
, dictorsetmaker
);
3014 int check_trailing_comma
= 0;
3018 if (ok
&& (nch
== 1 || TYPE(CHILD(tree
, 1)) == COMMA
)) {
3020 * test (',' test)* [',']
3022 ok
= validate_test(CHILD(tree
, i
++));
3023 while (ok
&& nch
- i
>= 2) {
3024 ok
= (validate_comma(CHILD(tree
, i
))
3025 && validate_test(CHILD(tree
, i
+1)));
3028 check_trailing_comma
= 1;
3030 else if (ok
&& TYPE(CHILD(tree
, 1)) == comp_for
) {
3031 /* We got a set comprehension:
3034 ok
= (validate_test(CHILD(tree
, 0))
3035 && validate_comp_for(CHILD(tree
, 1)));
3037 else if (ok
&& NCH(tree
) > 3 && TYPE(CHILD(tree
, 3)) == comp_for
) {
3038 /* We got a dict comprehension:
3039 * test ':' test comp_for
3041 ok
= (validate_test(CHILD(tree
, 0))
3042 && validate_colon(CHILD(tree
, 1))
3043 && validate_test(CHILD(tree
, 2))
3044 && validate_comp_for(CHILD(tree
, 3)));
3048 * test ':' test (',' test ':' test)* [',']
3051 ok
= (validate_test(CHILD(tree
, i
))
3052 && validate_colon(CHILD(tree
, i
+1))
3053 && validate_test(CHILD(tree
, i
+2)));
3058 err_string("illegal number of nodes for dictorsetmaker");
3061 while (ok
&& nch
- i
>= 4) {
3062 ok
= (validate_comma(CHILD(tree
, i
))
3063 && validate_test(CHILD(tree
, i
+1))
3064 && validate_colon(CHILD(tree
, i
+2))
3065 && validate_test(CHILD(tree
, i
+3)));
3068 check_trailing_comma
= 1;
3070 if (ok
&& check_trailing_comma
) {
3072 ok
= validate_comma(CHILD(tree
, i
));
3073 else if (i
!= nch
) {
3075 err_string("illegal trailing nodes for dictorsetmaker");
3084 validate_eval_input(node
*tree
)
3087 int nch
= NCH(tree
);
3088 int res
= (validate_ntype(tree
, eval_input
)
3090 && validate_testlist(CHILD(tree
, 0))
3091 && validate_ntype(CHILD(tree
, nch
- 1), ENDMARKER
));
3093 for (pos
= 1; res
&& (pos
< (nch
- 1)); ++pos
)
3094 res
= validate_ntype(CHILD(tree
, pos
), NEWLINE
);
3101 validate_node(node
*tree
)
3103 int nch
= 0; /* num. children on current node */
3104 int res
= 1; /* result value */
3105 node
* next
= 0; /* node to process after this one */
3107 while (res
&& (tree
!= 0)) {
3110 switch (TYPE(tree
)) {
3115 res
= validate_funcdef(tree
);
3118 res
= validate_with_stmt(tree
);
3121 res
= validate_class(tree
);
3124 res
= validate_decorated(tree
);
3127 * "Trivial" parse tree nodes.
3128 * (Why did I call these trivial?)
3131 res
= validate_stmt(tree
);
3135 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
3136 * | import_stmt | global_stmt | exec_stmt | assert_stmt
3138 res
= validate_small_stmt(tree
);
3141 res
= (validate_numnodes(tree
, 1, "flow_stmt")
3142 && ((TYPE(CHILD(tree
, 0)) == break_stmt
)
3143 || (TYPE(CHILD(tree
, 0)) == continue_stmt
)
3144 || (TYPE(CHILD(tree
, 0)) == yield_stmt
)
3145 || (TYPE(CHILD(tree
, 0)) == return_stmt
)
3146 || (TYPE(CHILD(tree
, 0)) == raise_stmt
)));
3148 next
= CHILD(tree
, 0);
3150 err_string("illegal flow_stmt type");
3153 res
= validate_yield_stmt(tree
);
3156 * Compound statements.
3159 res
= validate_simple_stmt(tree
);
3162 res
= validate_compound_stmt(tree
);
3165 * Fundamental statements.
3168 res
= validate_expr_stmt(tree
);
3171 res
= validate_print_stmt(tree
);
3174 res
= validate_del_stmt(tree
);
3177 res
= (validate_numnodes(tree
, 1, "pass")
3178 && validate_name(CHILD(tree
, 0), "pass"));
3181 res
= (validate_numnodes(tree
, 1, "break")
3182 && validate_name(CHILD(tree
, 0), "break"));
3185 res
= (validate_numnodes(tree
, 1, "continue")
3186 && validate_name(CHILD(tree
, 0), "continue"));
3189 res
= validate_return_stmt(tree
);
3192 res
= validate_raise_stmt(tree
);
3195 res
= validate_import_stmt(tree
);
3198 res
= validate_import_name(tree
);
3201 res
= validate_import_from(tree
);
3204 res
= validate_global_stmt(tree
);
3207 res
= validate_exec_stmt(tree
);
3210 res
= validate_assert_stmt(tree
);
3213 res
= validate_if(tree
);
3216 res
= validate_while(tree
);
3219 res
= validate_for(tree
);
3222 res
= validate_try(tree
);
3225 res
= validate_suite(tree
);
3231 res
= validate_testlist(tree
);
3234 res
= validate_yield_expr(tree
);
3237 res
= validate_testlist1(tree
);
3240 res
= validate_test(tree
);
3243 res
= validate_and_test(tree
);
3246 res
= validate_not_test(tree
);
3249 res
= validate_comparison(tree
);
3252 res
= validate_exprlist(tree
);
3255 res
= validate_comp_op(tree
);
3258 res
= validate_expr(tree
);
3261 res
= validate_xor_expr(tree
);
3264 res
= validate_and_expr(tree
);
3267 res
= validate_shift_expr(tree
);
3270 res
= validate_arith_expr(tree
);
3273 res
= validate_term(tree
);
3276 res
= validate_factor(tree
);
3279 res
= validate_power(tree
);
3282 res
= validate_atom(tree
);
3286 /* Hopefully never reached! */
3287 err_string("unrecognized node type");
3298 validate_expr_tree(node
*tree
)
3300 int res
= validate_eval_input(tree
);
3302 if (!res
&& !PyErr_Occurred())
3303 err_string("could not validate expression tuple");
3310 * (NEWLINE | stmt)* ENDMARKER
3313 validate_file_input(node
*tree
)
3316 int nch
= NCH(tree
) - 1;
3317 int res
= ((nch
>= 0)
3318 && validate_ntype(CHILD(tree
, nch
), ENDMARKER
));
3320 for (j
= 0; res
&& (j
< nch
); ++j
) {
3321 if (TYPE(CHILD(tree
, j
)) == stmt
)
3322 res
= validate_stmt(CHILD(tree
, j
));
3324 res
= validate_newline(CHILD(tree
, j
));
3326 /* This stays in to prevent any internal failures from getting to the
3327 * user. Hopefully, this won't be needed. If a user reports getting
3328 * this, we have some debugging to do.
3330 if (!res
&& !PyErr_Occurred())
3331 err_string("VALIDATION FAILURE: report this to the maintainer!");
3337 validate_encoding_decl(node
*tree
)
3339 int nch
= NCH(tree
);
3340 int res
= ((nch
== 1)
3341 && validate_file_input(CHILD(tree
, 0)));
3343 if (!res
&& !PyErr_Occurred())
3344 err_string("Error Parsing encoding_decl");
3350 pickle_constructor
= NULL
;
3354 parser__pickler(PyObject
*self
, PyObject
*args
)
3356 NOTE(ARGUNUSED(self
))
3357 PyObject
*result
= NULL
;
3358 PyObject
*st
= NULL
;
3359 PyObject
*empty_dict
= NULL
;
3361 if (PyArg_ParseTuple(args
, "O!:_pickler", &PyST_Type
, &st
)) {
3365 if ((empty_dict
= PyDict_New()) == NULL
)
3367 if ((newargs
= Py_BuildValue("Oi", st
, 1)) == NULL
)
3369 tuple
= parser_st2tuple((PyST_Object
*)NULL
, newargs
, empty_dict
);
3370 if (tuple
!= NULL
) {
3371 result
= Py_BuildValue("O(O)", pickle_constructor
, tuple
);
3374 Py_DECREF(empty_dict
);
3378 Py_XDECREF(empty_dict
);
3384 /* Functions exported by this module. Most of this should probably
3385 * be converted into an ST object with methods, but that is better
3386 * done directly in Python, allowing subclasses to be created directly.
3387 * We'd really have to write a wrapper around it all anyway to allow
3390 static PyMethodDef parser_functions
[] = {
3391 {"ast2tuple", (PyCFunction
)parser_ast2tuple
, PUBLIC_METHOD_TYPE
,
3392 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3393 {"ast2list", (PyCFunction
)parser_ast2list
, PUBLIC_METHOD_TYPE
,
3394 PyDoc_STR("Creates a list-tree representation of an ST.")},
3395 {"compileast", (PyCFunction
)parser_compileast
,PUBLIC_METHOD_TYPE
,
3396 PyDoc_STR("Compiles an ST object into a code object.")},
3397 {"compilest", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
3398 PyDoc_STR("Compiles an ST object into a code object.")},
3399 {"expr", (PyCFunction
)parser_expr
, PUBLIC_METHOD_TYPE
,
3400 PyDoc_STR("Creates an ST object from an expression.")},
3401 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
3402 PyDoc_STR("Determines if an ST object was created from an expression.")},
3403 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
3404 PyDoc_STR("Determines if an ST object was created from a suite.")},
3405 {"suite", (PyCFunction
)parser_suite
, PUBLIC_METHOD_TYPE
,
3406 PyDoc_STR("Creates an ST object from a suite.")},
3407 {"sequence2ast", (PyCFunction
)parser_tuple2ast
, PUBLIC_METHOD_TYPE
,
3408 PyDoc_STR("Creates an ST object from a tree representation.")},
3409 {"sequence2st", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
3410 PyDoc_STR("Creates an ST object from a tree representation.")},
3411 {"st2tuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
3412 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3413 {"st2list", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
3414 PyDoc_STR("Creates a list-tree representation of an ST.")},
3415 {"tuple2ast", (PyCFunction
)parser_tuple2ast
, PUBLIC_METHOD_TYPE
,
3416 PyDoc_STR("Creates an ST object from a tree representation.")},
3417 {"tuple2st", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
3418 PyDoc_STR("Creates an ST object from a tree representation.")},
3420 /* private stuff: support pickle module */
3421 {"_pickler", (PyCFunction
)parser__pickler
, METH_VARARGS
,
3422 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
3424 {NULL
, NULL
, 0, NULL
}
3428 PyMODINIT_FUNC
initparser(void); /* supply a prototype */
3433 PyObject
*module
, *copyreg
;
3435 Py_TYPE(&PyST_Type
) = &PyType_Type
;
3436 module
= Py_InitModule("parser", parser_functions
);
3440 if (parser_error
== 0)
3441 parser_error
= PyErr_NewException("parser.ParserError", NULL
, NULL
);
3443 if (parser_error
== 0)
3444 /* caller will check PyErr_Occurred() */
3446 /* CAUTION: The code next used to skip bumping the refcount on
3447 * parser_error. That's a disaster if initparser() gets called more
3448 * than once. By incref'ing, we ensure that each module dict that
3449 * gets created owns its reference to the shared parser_error object,
3450 * and the file static parser_error vrbl owns a reference too.
3452 Py_INCREF(parser_error
);
3453 if (PyModule_AddObject(module
, "ParserError", parser_error
) != 0)
3456 Py_INCREF(&PyST_Type
);
3457 PyModule_AddObject(module
, "ASTType", (PyObject
*)&PyST_Type
);
3458 Py_INCREF(&PyST_Type
);
3459 PyModule_AddObject(module
, "STType", (PyObject
*)&PyST_Type
);
3461 PyModule_AddStringConstant(module
, "__copyright__",
3462 parser_copyright_string
);
3463 PyModule_AddStringConstant(module
, "__doc__",
3465 PyModule_AddStringConstant(module
, "__version__",
3466 parser_version_string
);
3468 /* Register to support pickling.
3469 * If this fails, the import of this module will fail because an
3470 * exception will be raised here; should we clear the exception?
3472 copyreg
= PyImport_ImportModuleNoBlock("copy_reg");
3473 if (copyreg
!= NULL
) {
3474 PyObject
*func
, *pickler
;
3476 func
= PyObject_GetAttrString(copyreg
, "pickle");
3477 pickle_constructor
= PyObject_GetAttrString(module
, "sequence2st");
3478 pickler
= PyObject_GetAttrString(module
, "_pickler");
3479 Py_XINCREF(pickle_constructor
);
3480 if ((func
!= NULL
) && (pickle_constructor
!= NULL
)
3481 && (pickler
!= NULL
)) {
3484 res
= PyObject_CallFunctionObjArgs(func
, &PyST_Type
, pickler
,
3485 pickle_constructor
, NULL
);
3489 Py_XDECREF(pickle_constructor
);
3490 Py_XDECREF(pickler
);