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 PyObject_HEAD_INIT(NULL
)
171 "parser.st", /* tp_name */
172 (int) sizeof(PyST_Object
), /* tp_basicsize */
174 (destructor
)parser_free
, /* tp_dealloc */
176 parser_getattr
, /* tp_getattr */
178 (cmpfunc
)parser_compare
, /* tp_compare */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
189 /* Functions to access object as input/output buffer */
190 0, /* tp_as_buffer */
192 Py_TPFLAGS_DEFAULT
, /* tp_flags */
195 "Intermediate representation of a Python parse tree."
200 parser_compare_nodes(node
*left
, node
*right
)
204 if (TYPE(left
) < TYPE(right
))
207 if (TYPE(right
) < TYPE(left
))
210 if (ISTERMINAL(TYPE(left
)))
211 return (strcmp(STR(left
), STR(right
)));
213 if (NCH(left
) < NCH(right
))
216 if (NCH(right
) < NCH(left
))
219 for (j
= 0; j
< NCH(left
); ++j
) {
220 int v
= parser_compare_nodes(CHILD(left
, j
), CHILD(right
, j
));
229 /* int parser_compare(PyST_Object* left, PyST_Object* right)
231 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
232 * This really just wraps a call to parser_compare_nodes() with some easy
233 * checks and protection code.
237 parser_compare(PyST_Object
*left
, PyST_Object
*right
)
242 if ((left
== 0) || (right
== 0))
245 return (parser_compare_nodes(left
->st_node
, right
->st_node
));
249 /* parser_newstobject(node* st)
251 * Allocates a new Python object representing an ST. This is simply the
252 * 'wrapper' object that holds a node* and allows it to be passed around in
257 parser_newstobject(node
*st
, int type
)
259 PyST_Object
* o
= PyObject_New(PyST_Object
, &PyST_Type
);
268 return ((PyObject
*)o
);
272 /* void parser_free(PyST_Object* st)
274 * This is called by a del statement that reduces the reference count to 0.
278 parser_free(PyST_Object
*st
)
280 PyNode_Free(st
->st_node
);
285 /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
287 * This provides conversion from a node* to a tuple object that can be
288 * returned to the Python-level caller. The ST object is not modified.
292 parser_st2tuple(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
294 PyObject
*line_option
= 0;
295 PyObject
*col_option
= 0;
299 static char *keywords
[] = {"ast", "line_info", "col_info", NULL
};
302 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|OO:st2tuple", keywords
,
303 &PyST_Type
, &self
, &line_option
,
307 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|OO:totuple", &keywords
[1],
308 &line_option
, &col_option
);
312 if (line_option
!= NULL
) {
313 lineno
= (PyObject_IsTrue(line_option
) != 0) ? 1 : 0;
315 if (col_option
!= NULL
) {
316 col_offset
= (PyObject_IsTrue(col_option
) != 0) ? 1 : 0;
319 * Convert ST into a tuple representation. Use Guido's function,
320 * since it's known to work already.
322 res
= node2tuple(((PyST_Object
*)self
)->st_node
,
323 PyTuple_New
, PyTuple_SetItem
, lineno
, col_offset
);
329 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
331 * This provides conversion from a node* to a list object that can be
332 * returned to the Python-level caller. The ST object is not modified.
336 parser_st2list(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
338 PyObject
*line_option
= 0;
339 PyObject
*col_option
= 0;
343 static char *keywords
[] = {"ast", "line_info", "col_info", NULL
};
346 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|OO:st2list", keywords
,
347 &PyST_Type
, &self
, &line_option
,
350 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|OO:tolist", &keywords
[1],
351 &line_option
, &col_option
);
355 if (line_option
!= 0) {
356 lineno
= PyObject_IsTrue(line_option
) ? 1 : 0;
358 if (col_option
!= NULL
) {
359 col_offset
= (PyObject_IsTrue(col_option
) != 0) ? 1 : 0;
362 * Convert ST into a tuple representation. Use Guido's function,
363 * since it's known to work already.
365 res
= node2tuple(self
->st_node
,
366 PyList_New
, PyList_SetItem
, lineno
, col_offset
);
372 /* parser_compilest(PyObject* self, PyObject* args)
374 * This function creates code objects from the parse tree represented by
375 * the passed-in data object. An optional file name is passed in as well.
379 parser_compilest(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
382 char* str
= "<syntax-tree>";
385 static char *keywords
[] = {"ast", "filename", NULL
};
388 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|s:compilest", keywords
,
389 &PyST_Type
, &self
, &str
);
391 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|s:compile", &keywords
[1],
395 res
= (PyObject
*)PyNode_Compile(self
->st_node
, str
);
401 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
402 * PyObject* parser_issuite(PyObject* self, PyObject* args)
404 * Checks the passed-in ST object to determine if it is an expression or
405 * a statement suite, respectively. The return is a Python truth value.
409 parser_isexpr(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
414 static char *keywords
[] = {"ast", NULL
};
417 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:isexpr", keywords
,
420 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":isexpr", &keywords
[1]);
423 /* Check to see if the ST represents an expression or not. */
424 res
= (self
->st_type
== PyST_EXPR
) ? Py_True
: Py_False
;
432 parser_issuite(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
437 static char *keywords
[] = {"ast", NULL
};
440 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:issuite", keywords
,
443 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":issuite", &keywords
[1]);
446 /* Check to see if the ST represents an expression or not. */
447 res
= (self
->st_type
== PyST_EXPR
) ? Py_False
: Py_True
;
454 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
458 {"compile", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
459 PyDoc_STR("Compile this ST object into a code object.")},
460 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
461 PyDoc_STR("Determines if this ST object was created from an expression.")},
462 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
463 PyDoc_STR("Determines if this ST object was created from a suite.")},
464 {"tolist", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
465 PyDoc_STR("Creates a list-tree representation of this ST.")},
466 {"totuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
467 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
469 {NULL
, NULL
, 0, NULL
}
474 parser_getattr(PyObject
*self
, char *name
)
476 return (Py_FindMethod(parser_methods
, self
, name
));
480 /* err_string(char* message)
482 * Sets the error string for an exception of type ParserError.
486 err_string(char *message
)
488 PyErr_SetString(parser_error
, message
);
492 /* PyObject* parser_do_parse(PyObject* args, int type)
494 * Internal function to actually execute the parse and return the result if
495 * successful or set an exception if not.
499 parser_do_parse(PyObject
*args
, PyObject
*kw
, char *argspec
, int type
)
504 static char *keywords
[] = {"source", NULL
};
506 if (PyArg_ParseTupleAndKeywords(args
, kw
, argspec
, keywords
, &string
)) {
507 node
* n
= PyParser_SimpleParseString(string
,
509 ? eval_input
: file_input
);
512 res
= parser_newstobject(n
, type
);
518 /* PyObject* parser_expr(PyObject* self, PyObject* args)
519 * PyObject* parser_suite(PyObject* self, PyObject* args)
521 * External interfaces to the parser itself. Which is called determines if
522 * the parser attempts to recognize an expression ('eval' form) or statement
523 * suite ('exec' form). The real work is done by parser_do_parse() above.
527 parser_expr(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
529 NOTE(ARGUNUSED(self
))
530 return (parser_do_parse(args
, kw
, "s:expr", PyST_EXPR
));
535 parser_suite(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
537 NOTE(ARGUNUSED(self
))
538 return (parser_do_parse(args
, kw
, "s:suite", PyST_SUITE
));
543 /* This is the messy part of the code. Conversion from a tuple to an ST
544 * object requires that the input tuple be valid without having to rely on
545 * catching an exception from the compiler. This is done to allow the
546 * compiler itself to remain fast, since most of its input will come from
547 * the parser directly, and therefore be known to be syntactically correct.
548 * This validation is done to ensure that we don't core dump the compile
549 * phase, returning an exception instead.
551 * Two aspects can be broken out in this code: creating a node tree from
552 * the tuple passed in, and verifying that it is indeed valid. It may be
553 * advantageous to expand the number of ST types to include funcdefs and
554 * lambdadefs to take advantage of the optimizer, recognizing those STs
555 * here. They are not necessary, and not quite as useful in a raw form.
556 * For now, let's get expressions and suites working reliably.
560 static node
* build_node_tree(PyObject
*tuple
);
561 static int validate_expr_tree(node
*tree
);
562 static int validate_file_input(node
*tree
);
563 static int validate_encoding_decl(node
*tree
);
565 /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
567 * This is the public function, called from the Python code. It receives a
568 * single tuple object from the caller, and creates an ST object if the
569 * tuple can be validated. It does this by checking the first code of the
570 * tuple, and, if acceptable, builds the internal representation. If this
571 * step succeeds, the internal representation is validated as fully as
572 * possible with the various validate_*() routines defined below.
574 * This function must be changed if support is to be added for PyST_FRAGMENT
579 parser_tuple2st(PyST_Object
*self
, PyObject
*args
, PyObject
*kw
)
581 NOTE(ARGUNUSED(self
))
586 static char *keywords
[] = {"sequence", NULL
};
588 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "O:sequence2st", keywords
,
591 if (!PySequence_Check(tuple
)) {
592 PyErr_SetString(PyExc_ValueError
,
593 "sequence2st() requires a single sequence argument");
597 * Convert the tree to the internal form before checking it.
599 tree
= build_node_tree(tuple
);
601 int start_sym
= TYPE(tree
);
602 if (start_sym
== eval_input
) {
603 /* Might be an eval form. */
604 if (validate_expr_tree(tree
))
605 st
= parser_newstobject(tree
, PyST_EXPR
);
609 else if (start_sym
== file_input
) {
610 /* This looks like an exec form so far. */
611 if (validate_file_input(tree
))
612 st
= parser_newstobject(tree
, PyST_SUITE
);
616 else if (start_sym
== encoding_decl
) {
617 /* This looks like an encoding_decl so far. */
618 if (validate_encoding_decl(tree
))
619 st
= parser_newstobject(tree
, PyST_SUITE
);
624 /* This is a fragment, at best. */
626 err_string("parse tree does not use a valid start symbol");
629 /* Make sure we throw an exception on all errors. We should never
630 * get this, but we'd do well to be sure something is done.
632 if (st
== NULL
&& !PyErr_Occurred())
633 err_string("unspecified ST error occurred");
639 /* node* build_node_children()
641 * Iterate across the children of the current non-terminal node and build
642 * their structures. If successful, return the root of this portion of
643 * the tree, otherwise, 0. Any required exception will be specified already,
644 * and no memory will have been deallocated.
648 build_node_children(PyObject
*tuple
, node
*root
, int *line_num
)
650 Py_ssize_t len
= PyObject_Size(tuple
);
654 for (i
= 1; i
< len
; ++i
) {
655 /* elem must always be a sequence, however simple */
656 PyObject
* elem
= PySequence_GetItem(tuple
, i
);
657 int ok
= elem
!= NULL
;
662 ok
= PySequence_Check(elem
);
664 PyObject
*temp
= PySequence_GetItem(elem
, 0);
668 ok
= PyInt_Check(temp
);
670 type
= PyInt_AS_LONG(temp
);
675 PyObject
*err
= Py_BuildValue("os", elem
,
676 "Illegal node construct.");
677 PyErr_SetObject(parser_error
, err
);
682 if (ISTERMINAL(type
)) {
683 Py_ssize_t len
= PyObject_Size(elem
);
686 if ((len
!= 2) && (len
!= 3)) {
687 err_string("terminal nodes must have 2 or 3 entries");
690 temp
= PySequence_GetItem(elem
, 1);
693 if (!PyString_Check(temp
)) {
694 PyErr_Format(parser_error
,
695 "second item in terminal node must be a string,"
697 temp
->ob_type
->tp_name
);
702 PyObject
*o
= PySequence_GetItem(elem
, 2);
705 *line_num
= PyInt_AS_LONG(o
);
707 PyErr_Format(parser_error
,
708 "third item in terminal node must be an"
709 " integer, found %s",
710 temp
->ob_type
->tp_name
);
718 len
= PyString_GET_SIZE(temp
) + 1;
719 strn
= (char *)PyObject_MALLOC(len
);
721 (void) memcpy(strn
, PyString_AS_STRING(temp
), len
);
724 else if (!ISNONTERMINAL(type
)) {
726 * It has to be one or the other; this is an error.
727 * Throw an exception.
729 PyObject
*err
= Py_BuildValue("os", elem
, "unknown node type.");
730 PyErr_SetObject(parser_error
, err
);
735 err
= PyNode_AddChild(root
, type
, strn
, *line_num
, 0);
736 if (err
== E_NOMEM
) {
738 return (node
*) PyErr_NoMemory();
740 if (err
== E_OVERFLOW
) {
742 PyErr_SetString(PyExc_ValueError
,
743 "unsupported number of child nodes");
747 if (ISNONTERMINAL(type
)) {
748 node
* new_child
= CHILD(root
, i
- 1);
750 if (new_child
!= build_node_children(elem
, new_child
, line_num
)) {
755 else if (type
== NEWLINE
) { /* It's true: we increment the */
756 ++(*line_num
); /* line number *after* the newline! */
765 build_node_tree(PyObject
*tuple
)
768 PyObject
*temp
= PySequence_GetItem(tuple
, 0);
772 num
= PyInt_AsLong(temp
);
774 if (ISTERMINAL(num
)) {
776 * The tuple is simple, but it doesn't start with a start symbol.
777 * Throw an exception now and be done with it.
779 tuple
= Py_BuildValue("os", tuple
,
780 "Illegal syntax-tree; cannot start with terminal symbol.");
781 PyErr_SetObject(parser_error
, tuple
);
784 else if (ISNONTERMINAL(num
)) {
786 * Not efficient, but that can be handled later.
789 PyObject
*encoding
= NULL
;
791 if (num
== encoding_decl
) {
792 encoding
= PySequence_GetItem(tuple
, 2);
793 /* tuple isn't borrowed anymore here, need to DECREF */
794 tuple
= PySequence_GetSlice(tuple
, 0, 2);
796 res
= PyNode_New(num
);
798 if (res
!= build_node_children(tuple
, res
, &line_num
)) {
802 if (res
&& encoding
) {
804 len
= PyString_GET_SIZE(encoding
) + 1;
805 res
->n_str
= (char *)PyObject_MALLOC(len
);
806 if (res
->n_str
!= NULL
)
807 (void) memcpy(res
->n_str
, PyString_AS_STRING(encoding
), len
);
814 /* The tuple is illegal -- if the number is neither TERMINAL nor
815 * NONTERMINAL, we can't use it. Not sure the implementation
816 * allows this condition, but the API doesn't preclude it.
818 PyObject
*err
= Py_BuildValue("os", tuple
,
819 "Illegal component tuple.");
820 PyErr_SetObject(parser_error
, err
);
829 * Validation routines used within the validation section:
831 static int validate_terminal(node
*terminal
, int type
, char *string
);
833 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
834 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
835 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
836 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
837 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
838 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
839 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
840 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
841 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
842 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
843 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
844 #define validate_star(ch) validate_terminal(ch, STAR, "*")
845 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
846 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
847 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
848 #define validate_at(ch) validate_terminal(ch, AT, "@")
849 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
851 #define VALIDATER(n) static int validate_##n(node *tree)
853 VALIDATER(node
); VALIDATER(small_stmt
);
854 VALIDATER(class); VALIDATER(node
);
855 VALIDATER(parameters
); VALIDATER(suite
);
856 VALIDATER(testlist
); VALIDATER(varargslist
);
857 VALIDATER(fpdef
); VALIDATER(fplist
);
858 VALIDATER(stmt
); VALIDATER(simple_stmt
);
859 VALIDATER(expr_stmt
); VALIDATER(power
);
860 VALIDATER(print_stmt
); VALIDATER(del_stmt
);
861 VALIDATER(return_stmt
); VALIDATER(list_iter
);
862 VALIDATER(raise_stmt
); VALIDATER(import_stmt
);
863 VALIDATER(import_name
); VALIDATER(import_from
);
864 VALIDATER(global_stmt
); VALIDATER(list_if
);
865 VALIDATER(assert_stmt
); VALIDATER(list_for
);
866 VALIDATER(exec_stmt
); VALIDATER(compound_stmt
);
867 VALIDATER(while); VALIDATER(for);
868 VALIDATER(try); VALIDATER(except_clause
);
869 VALIDATER(test
); VALIDATER(and_test
);
870 VALIDATER(not_test
); VALIDATER(comparison
);
871 VALIDATER(comp_op
); VALIDATER(expr
);
872 VALIDATER(xor_expr
); VALIDATER(and_expr
);
873 VALIDATER(shift_expr
); VALIDATER(arith_expr
);
874 VALIDATER(term
); VALIDATER(factor
);
875 VALIDATER(atom
); VALIDATER(lambdef
);
876 VALIDATER(trailer
); VALIDATER(subscript
);
877 VALIDATER(subscriptlist
); VALIDATER(sliceop
);
878 VALIDATER(exprlist
); VALIDATER(dictmaker
);
879 VALIDATER(arglist
); VALIDATER(argument
);
880 VALIDATER(listmaker
); VALIDATER(yield_stmt
);
881 VALIDATER(testlist1
); VALIDATER(gen_for
);
882 VALIDATER(gen_iter
); VALIDATER(gen_if
);
883 VALIDATER(testlist_gexp
); VALIDATER(yield_expr
);
884 VALIDATER(yield_or_testlist
); VALIDATER(or_test
);
885 VALIDATER(old_test
); VALIDATER(old_lambdef
);
889 #define is_even(n) (((n) & 1) == 0)
890 #define is_odd(n) (((n) & 1) == 1)
894 validate_ntype(node
*n
, int t
)
897 PyErr_Format(parser_error
, "Expected node type %d, got %d.",
905 /* Verifies that the number of child nodes is exactly 'num', raising
906 * an exception if it isn't. The exception message does not indicate
907 * the exact number of nodes, allowing this to be used to raise the
908 * "right" exception when the wrong number of nodes is present in a
909 * specific variant of a statement's syntax. This is commonly used
913 validate_numnodes(node
*n
, int num
, const char *const name
)
916 PyErr_Format(parser_error
,
917 "Illegal number of children for %s node.", name
);
925 validate_terminal(node
*terminal
, int type
, char *string
)
927 int res
= (validate_ntype(terminal
, type
)
928 && ((string
== 0) || (strcmp(string
, STR(terminal
)) == 0)));
930 if (!res
&& !PyErr_Occurred()) {
931 PyErr_Format(parser_error
,
932 "Illegal terminal: expected \"%s\"", string
);
941 validate_repeating_list(node
*tree
, int ntype
, int (*vfunc
)(node
*),
942 const char *const name
)
945 int res
= (nch
&& validate_ntype(tree
, ntype
)
946 && vfunc(CHILD(tree
, 0)));
948 if (!res
&& !PyErr_Occurred())
949 (void) validate_numnodes(tree
, 1, name
);
952 res
= validate_comma(CHILD(tree
, --nch
));
953 if (res
&& nch
> 1) {
955 for ( ; res
&& pos
< nch
; pos
+= 2)
956 res
= (validate_comma(CHILD(tree
, pos
))
957 && vfunc(CHILD(tree
, pos
+ 1)));
967 * 'class' NAME ['(' testlist ')'] ':' suite
970 validate_class(node
*tree
)
973 int res
= (validate_ntype(tree
, classdef
) &&
974 ((nch
== 4) || (nch
== 6) || (nch
== 7)));
977 res
= (validate_name(CHILD(tree
, 0), "class")
978 && validate_ntype(CHILD(tree
, 1), NAME
)
979 && validate_colon(CHILD(tree
, nch
- 2))
980 && validate_suite(CHILD(tree
, nch
- 1)));
983 (void) validate_numnodes(tree
, 4, "class");
988 res
= ((validate_lparen(CHILD(tree
, 2)) &&
989 validate_testlist(CHILD(tree
, 3)) &&
990 validate_rparen(CHILD(tree
, 4))));
993 res
= (validate_lparen(CHILD(tree
,2)) &&
994 validate_rparen(CHILD(tree
,3)));
1002 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1005 validate_if(node
*tree
)
1007 int nch
= NCH(tree
);
1008 int res
= (validate_ntype(tree
, if_stmt
)
1010 && validate_name(CHILD(tree
, 0), "if")
1011 && validate_test(CHILD(tree
, 1))
1012 && validate_colon(CHILD(tree
, 2))
1013 && validate_suite(CHILD(tree
, 3)));
1015 if (res
&& ((nch
% 4) == 3)) {
1016 /* ... 'else' ':' suite */
1017 res
= (validate_name(CHILD(tree
, nch
- 3), "else")
1018 && validate_colon(CHILD(tree
, nch
- 2))
1019 && validate_suite(CHILD(tree
, nch
- 1)));
1022 else if (!res
&& !PyErr_Occurred())
1023 (void) validate_numnodes(tree
, 4, "if");
1025 /* Will catch the case for nch < 4 */
1026 res
= validate_numnodes(tree
, 0, "if");
1027 else if (res
&& (nch
> 4)) {
1028 /* ... ('elif' test ':' suite)+ ... */
1030 while ((j
< nch
) && res
) {
1031 res
= (validate_name(CHILD(tree
, j
), "elif")
1032 && validate_colon(CHILD(tree
, j
+ 2))
1033 && validate_test(CHILD(tree
, j
+ 1))
1034 && validate_suite(CHILD(tree
, j
+ 3)));
1043 * '(' [varargslist] ')'
1047 validate_parameters(node
*tree
)
1049 int nch
= NCH(tree
);
1050 int res
= validate_ntype(tree
, parameters
) && ((nch
== 2) || (nch
== 3));
1053 res
= (validate_lparen(CHILD(tree
, 0))
1054 && validate_rparen(CHILD(tree
, nch
- 1)));
1055 if (res
&& (nch
== 3))
1056 res
= validate_varargslist(CHILD(tree
, 1));
1059 (void) validate_numnodes(tree
, 2, "parameters");
1069 * | NEWLINE INDENT stmt+ DEDENT
1072 validate_suite(node
*tree
)
1074 int nch
= NCH(tree
);
1075 int res
= (validate_ntype(tree
, suite
) && ((nch
== 1) || (nch
>= 4)));
1077 if (res
&& (nch
== 1))
1078 res
= validate_simple_stmt(CHILD(tree
, 0));
1080 /* NEWLINE INDENT stmt+ DEDENT */
1081 res
= (validate_newline(CHILD(tree
, 0))
1082 && validate_indent(CHILD(tree
, 1))
1083 && validate_stmt(CHILD(tree
, 2))
1084 && validate_dedent(CHILD(tree
, nch
- 1)));
1086 if (res
&& (nch
> 4)) {
1088 --nch
; /* forget the DEDENT */
1089 for ( ; res
&& (i
< nch
); ++i
)
1090 res
= validate_stmt(CHILD(tree
, i
));
1093 res
= validate_numnodes(tree
, 4, "suite");
1100 validate_testlist(node
*tree
)
1102 return (validate_repeating_list(tree
, testlist
,
1103 validate_test
, "testlist"));
1108 validate_testlist1(node
*tree
)
1110 return (validate_repeating_list(tree
, testlist1
,
1111 validate_test
, "testlist1"));
1116 validate_testlist_safe(node
*tree
)
1118 return (validate_repeating_list(tree
, testlist_safe
,
1119 validate_old_test
, "testlist_safe"));
1123 /* '*' NAME [',' '**' NAME] | '**' NAME
1126 validate_varargslist_trailer(node
*tree
, int start
)
1128 int nch
= NCH(tree
);
1133 err_string("expected variable argument trailer for varargslist");
1136 sym
= TYPE(CHILD(tree
, start
));
1139 * ('*' NAME [',' '**' NAME]
1142 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1143 else if (nch
-start
== 5)
1144 res
= (validate_name(CHILD(tree
, start
+1), NULL
)
1145 && validate_comma(CHILD(tree
, start
+2))
1146 && validate_doublestar(CHILD(tree
, start
+3))
1147 && validate_name(CHILD(tree
, start
+4), NULL
));
1149 else if (sym
== DOUBLESTAR
) {
1154 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1157 err_string("illegal variable argument trailer for varargslist");
1162 /* validate_varargslist()
1165 * (fpdef ['=' test] ',')*
1166 * ('*' NAME [',' '**' NAME]
1168 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1172 validate_varargslist(node
*tree
)
1174 int nch
= NCH(tree
);
1175 int res
= validate_ntype(tree
, varargslist
) && (nch
!= 0);
1181 err_string("varargslist missing child nodes");
1184 sym
= TYPE(CHILD(tree
, 0));
1185 if (sym
== STAR
|| sym
== DOUBLESTAR
)
1186 /* whole thing matches:
1187 * '*' NAME [',' '**' NAME] | '**' NAME
1189 res
= validate_varargslist_trailer(tree
, 0);
1190 else if (sym
== fpdef
) {
1193 sym
= TYPE(CHILD(tree
, nch
-1));
1196 * (fpdef ['=' test] ',')+
1197 * ('*' NAME [',' '**' NAME]
1200 /* skip over (fpdef ['=' test] ',')+ */
1201 while (res
&& (i
+2 <= nch
)) {
1202 res
= validate_fpdef(CHILD(tree
, i
));
1204 if (res
&& TYPE(CHILD(tree
, i
)) == EQUAL
&& (i
+2 <= nch
)) {
1205 res
= (validate_equal(CHILD(tree
, i
))
1206 && validate_test(CHILD(tree
, i
+1)));
1210 if (res
&& i
< nch
) {
1211 res
= validate_comma(CHILD(tree
, i
));
1214 && (TYPE(CHILD(tree
, i
)) == DOUBLESTAR
1215 || TYPE(CHILD(tree
, i
)) == STAR
))
1219 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1223 res
= validate_varargslist_trailer(tree
, i
);
1227 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1229 /* strip trailing comma node */
1231 res
= validate_comma(CHILD(tree
, nch
-1));
1237 * fpdef ['=' test] (',' fpdef ['=' test])*
1239 res
= validate_fpdef(CHILD(tree
, 0));
1241 if (res
&& (i
+2 <= nch
) && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1242 res
= (validate_equal(CHILD(tree
, i
))
1243 && validate_test(CHILD(tree
, i
+1)));
1247 * ... (',' fpdef ['=' test])*
1250 while (res
&& (nch
- i
) >= 2) {
1251 res
= (validate_comma(CHILD(tree
, i
))
1252 && validate_fpdef(CHILD(tree
, i
+1)));
1254 if (res
&& (nch
- i
) >= 2 && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1255 res
= (validate_equal(CHILD(tree
, i
))
1256 && validate_test(CHILD(tree
, i
+1)));
1260 if (res
&& nch
- i
!= 0) {
1262 err_string("illegal formation for varargslist");
1270 /* list_iter: list_for | list_if
1273 validate_list_iter(node
*tree
)
1275 int res
= (validate_ntype(tree
, list_iter
)
1276 && validate_numnodes(tree
, 1, "list_iter"));
1277 if (res
&& TYPE(CHILD(tree
, 0)) == list_for
)
1278 res
= validate_list_for(CHILD(tree
, 0));
1280 res
= validate_list_if(CHILD(tree
, 0));
1285 /* gen_iter: gen_for | gen_if
1288 validate_gen_iter(node
*tree
)
1290 int res
= (validate_ntype(tree
, gen_iter
)
1291 && validate_numnodes(tree
, 1, "gen_iter"));
1292 if (res
&& TYPE(CHILD(tree
, 0)) == gen_for
)
1293 res
= validate_gen_for(CHILD(tree
, 0));
1295 res
= validate_gen_if(CHILD(tree
, 0));
1300 /* list_for: 'for' exprlist 'in' testlist [list_iter]
1303 validate_list_for(node
*tree
)
1305 int nch
= NCH(tree
);
1309 res
= validate_list_iter(CHILD(tree
, 4));
1311 res
= validate_numnodes(tree
, 4, "list_for");
1314 res
= (validate_name(CHILD(tree
, 0), "for")
1315 && validate_exprlist(CHILD(tree
, 1))
1316 && validate_name(CHILD(tree
, 2), "in")
1317 && validate_testlist_safe(CHILD(tree
, 3)));
1322 /* gen_for: 'for' exprlist 'in' test [gen_iter]
1325 validate_gen_for(node
*tree
)
1327 int nch
= NCH(tree
);
1331 res
= validate_gen_iter(CHILD(tree
, 4));
1333 res
= validate_numnodes(tree
, 4, "gen_for");
1336 res
= (validate_name(CHILD(tree
, 0), "for")
1337 && validate_exprlist(CHILD(tree
, 1))
1338 && validate_name(CHILD(tree
, 2), "in")
1339 && validate_or_test(CHILD(tree
, 3)));
1344 /* list_if: 'if' old_test [list_iter]
1347 validate_list_if(node
*tree
)
1349 int nch
= NCH(tree
);
1353 res
= validate_list_iter(CHILD(tree
, 2));
1355 res
= validate_numnodes(tree
, 2, "list_if");
1358 res
= (validate_name(CHILD(tree
, 0), "if")
1359 && validate_old_test(CHILD(tree
, 1)));
1364 /* gen_if: 'if' old_test [gen_iter]
1367 validate_gen_if(node
*tree
)
1369 int nch
= NCH(tree
);
1373 res
= validate_gen_iter(CHILD(tree
, 2));
1375 res
= validate_numnodes(tree
, 2, "gen_if");
1378 res
= (validate_name(CHILD(tree
, 0), "if")
1379 && validate_old_test(CHILD(tree
, 1)));
1391 validate_fpdef(node
*tree
)
1393 int nch
= NCH(tree
);
1394 int res
= validate_ntype(tree
, fpdef
);
1398 res
= validate_ntype(CHILD(tree
, 0), NAME
);
1400 res
= (validate_lparen(CHILD(tree
, 0))
1401 && validate_fplist(CHILD(tree
, 1))
1402 && validate_rparen(CHILD(tree
, 2)));
1404 res
= validate_numnodes(tree
, 1, "fpdef");
1411 validate_fplist(node
*tree
)
1413 return (validate_repeating_list(tree
, fplist
,
1414 validate_fpdef
, "fplist"));
1418 /* simple_stmt | compound_stmt
1422 validate_stmt(node
*tree
)
1424 int res
= (validate_ntype(tree
, stmt
)
1425 && validate_numnodes(tree
, 1, "stmt"));
1428 tree
= CHILD(tree
, 0);
1430 if (TYPE(tree
) == simple_stmt
)
1431 res
= validate_simple_stmt(tree
);
1433 res
= validate_compound_stmt(tree
);
1439 /* small_stmt (';' small_stmt)* [';'] NEWLINE
1443 validate_simple_stmt(node
*tree
)
1445 int nch
= NCH(tree
);
1446 int res
= (validate_ntype(tree
, simple_stmt
)
1448 && validate_small_stmt(CHILD(tree
, 0))
1449 && validate_newline(CHILD(tree
, nch
- 1)));
1452 res
= validate_numnodes(tree
, 2, "simple_stmt");
1453 --nch
; /* forget the NEWLINE */
1454 if (res
&& is_even(nch
))
1455 res
= validate_semi(CHILD(tree
, --nch
));
1456 if (res
&& (nch
> 2)) {
1459 for (i
= 1; res
&& (i
< nch
); i
+= 2)
1460 res
= (validate_semi(CHILD(tree
, i
))
1461 && validate_small_stmt(CHILD(tree
, i
+ 1)));
1468 validate_small_stmt(node
*tree
)
1470 int nch
= NCH(tree
);
1471 int res
= validate_numnodes(tree
, 1, "small_stmt");
1474 int ntype
= TYPE(CHILD(tree
, 0));
1476 if ( (ntype
== expr_stmt
)
1477 || (ntype
== print_stmt
)
1478 || (ntype
== del_stmt
)
1479 || (ntype
== pass_stmt
)
1480 || (ntype
== flow_stmt
)
1481 || (ntype
== import_stmt
)
1482 || (ntype
== global_stmt
)
1483 || (ntype
== assert_stmt
)
1484 || (ntype
== exec_stmt
))
1485 res
= validate_node(CHILD(tree
, 0));
1488 err_string("illegal small_stmt child type");
1491 else if (nch
== 1) {
1493 PyErr_Format(parser_error
,
1494 "Unrecognized child node of small_stmt: %d.",
1495 TYPE(CHILD(tree
, 0)));
1502 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1505 validate_compound_stmt(node
*tree
)
1507 int res
= (validate_ntype(tree
, compound_stmt
)
1508 && validate_numnodes(tree
, 1, "compound_stmt"));
1514 tree
= CHILD(tree
, 0);
1516 if ( (ntype
== if_stmt
)
1517 || (ntype
== while_stmt
)
1518 || (ntype
== for_stmt
)
1519 || (ntype
== try_stmt
)
1520 || (ntype
== funcdef
)
1521 || (ntype
== classdef
))
1522 res
= validate_node(tree
);
1525 PyErr_Format(parser_error
,
1526 "Illegal compound statement type: %d.", TYPE(tree
));
1533 validate_yield_or_testlist(node
*tree
)
1535 if (TYPE(tree
) == yield_expr
)
1536 return validate_yield_expr(tree
);
1538 return validate_testlist(tree
);
1542 validate_expr_stmt(node
*tree
)
1545 int nch
= NCH(tree
);
1546 int res
= (validate_ntype(tree
, expr_stmt
)
1548 && validate_testlist(CHILD(tree
, 0)));
1551 && TYPE(CHILD(tree
, 1)) == augassign
) {
1552 res
= validate_numnodes(CHILD(tree
, 1), 1, "augassign")
1553 && validate_yield_or_testlist(CHILD(tree
, 2));
1556 char *s
= STR(CHILD(CHILD(tree
, 1), 0));
1558 res
= (strcmp(s
, "+=") == 0
1559 || strcmp(s
, "-=") == 0
1560 || strcmp(s
, "*=") == 0
1561 || strcmp(s
, "/=") == 0
1562 || strcmp(s
, "//=") == 0
1563 || strcmp(s
, "%=") == 0
1564 || strcmp(s
, "&=") == 0
1565 || strcmp(s
, "|=") == 0
1566 || strcmp(s
, "^=") == 0
1567 || strcmp(s
, "<<=") == 0
1568 || strcmp(s
, ">>=") == 0
1569 || strcmp(s
, "**=") == 0);
1571 err_string("illegal augmmented assignment operator");
1575 for (j
= 1; res
&& (j
< nch
); j
+= 2)
1576 res
= validate_equal(CHILD(tree
, j
))
1577 && validate_yield_or_testlist(CHILD(tree
, j
+ 1));
1585 * 'print' ( [ test (',' test)* [','] ]
1586 * | '>>' test [ (',' test)+ [','] ] )
1589 validate_print_stmt(node
*tree
)
1591 int nch
= NCH(tree
);
1592 int res
= (validate_ntype(tree
, print_stmt
)
1594 && validate_name(CHILD(tree
, 0), "print"));
1596 if (res
&& nch
> 1) {
1597 int sym
= TYPE(CHILD(tree
, 1));
1599 int allow_trailing_comma
= 1;
1602 res
= validate_test(CHILD(tree
, i
++));
1605 res
= validate_numnodes(tree
, 3, "print_stmt");
1607 res
= (validate_ntype(CHILD(tree
, i
), RIGHTSHIFT
)
1608 && validate_test(CHILD(tree
, i
+1)));
1610 allow_trailing_comma
= 0;
1614 /* ... (',' test)* [','] */
1615 while (res
&& i
+2 <= nch
) {
1616 res
= (validate_comma(CHILD(tree
, i
))
1617 && validate_test(CHILD(tree
, i
+1)));
1618 allow_trailing_comma
= 1;
1621 if (res
&& !allow_trailing_comma
)
1622 res
= validate_numnodes(tree
, i
, "print_stmt");
1623 else if (res
&& i
< nch
)
1624 res
= validate_comma(CHILD(tree
, i
));
1632 validate_del_stmt(node
*tree
)
1634 return (validate_numnodes(tree
, 2, "del_stmt")
1635 && validate_name(CHILD(tree
, 0), "del")
1636 && validate_exprlist(CHILD(tree
, 1)));
1641 validate_return_stmt(node
*tree
)
1643 int nch
= NCH(tree
);
1644 int res
= (validate_ntype(tree
, return_stmt
)
1645 && ((nch
== 1) || (nch
== 2))
1646 && validate_name(CHILD(tree
, 0), "return"));
1648 if (res
&& (nch
== 2))
1649 res
= validate_testlist(CHILD(tree
, 1));
1656 validate_raise_stmt(node
*tree
)
1658 int nch
= NCH(tree
);
1659 int res
= (validate_ntype(tree
, raise_stmt
)
1660 && ((nch
== 1) || (nch
== 2) || (nch
== 4) || (nch
== 6)));
1663 res
= validate_name(CHILD(tree
, 0), "raise");
1664 if (res
&& (nch
>= 2))
1665 res
= validate_test(CHILD(tree
, 1));
1666 if (res
&& nch
> 2) {
1667 res
= (validate_comma(CHILD(tree
, 2))
1668 && validate_test(CHILD(tree
, 3)));
1669 if (res
&& (nch
> 4))
1670 res
= (validate_comma(CHILD(tree
, 4))
1671 && validate_test(CHILD(tree
, 5)));
1675 (void) validate_numnodes(tree
, 2, "raise");
1676 if (res
&& (nch
== 4))
1677 res
= (validate_comma(CHILD(tree
, 2))
1678 && validate_test(CHILD(tree
, 3)));
1684 /* yield_expr: 'yield' [testlist]
1687 validate_yield_expr(node
*tree
)
1689 int nch
= NCH(tree
);
1690 int res
= (validate_ntype(tree
, yield_expr
)
1691 && ((nch
== 1) || (nch
== 2))
1692 && validate_name(CHILD(tree
, 0), "yield"));
1694 if (res
&& (nch
== 2))
1695 res
= validate_testlist(CHILD(tree
, 1));
1701 /* yield_stmt: yield_expr
1704 validate_yield_stmt(node
*tree
)
1706 return (validate_ntype(tree
, yield_stmt
)
1707 && validate_numnodes(tree
, 1, "yield_stmt")
1708 && validate_yield_expr(CHILD(tree
, 0)));
1713 validate_import_as_name(node
*tree
)
1715 int nch
= NCH(tree
);
1716 int ok
= validate_ntype(tree
, import_as_name
);
1720 ok
= validate_name(CHILD(tree
, 0), NULL
);
1722 ok
= (validate_name(CHILD(tree
, 0), NULL
)
1723 && validate_name(CHILD(tree
, 1), "as")
1724 && validate_name(CHILD(tree
, 2), NULL
));
1726 ok
= validate_numnodes(tree
, 3, "import_as_name");
1732 /* dotted_name: NAME ("." NAME)*
1735 validate_dotted_name(node
*tree
)
1737 int nch
= NCH(tree
);
1738 int res
= (validate_ntype(tree
, dotted_name
)
1740 && validate_name(CHILD(tree
, 0), NULL
));
1743 for (i
= 1; res
&& (i
< nch
); i
+= 2) {
1744 res
= (validate_dot(CHILD(tree
, i
))
1745 && validate_name(CHILD(tree
, i
+1), NULL
));
1751 /* dotted_as_name: dotted_name [NAME NAME]
1754 validate_dotted_as_name(node
*tree
)
1756 int nch
= NCH(tree
);
1757 int res
= validate_ntype(tree
, dotted_as_name
);
1761 res
= validate_dotted_name(CHILD(tree
, 0));
1763 res
= (validate_dotted_name(CHILD(tree
, 0))
1764 && validate_name(CHILD(tree
, 1), "as")
1765 && validate_name(CHILD(tree
, 2), NULL
));
1768 err_string("illegal number of children for dotted_as_name");
1775 /* dotted_as_name (',' dotted_as_name)* */
1777 validate_dotted_as_names(node
*tree
)
1779 int nch
= NCH(tree
);
1780 int res
= is_odd(nch
) && validate_dotted_as_name(CHILD(tree
, 0));
1783 for (i
= 1; res
&& (i
< nch
); i
+= 2)
1784 res
= (validate_comma(CHILD(tree
, i
))
1785 && validate_dotted_as_name(CHILD(tree
, i
+ 1)));
1790 /* import_as_name (',' import_as_name)* [','] */
1792 validate_import_as_names(node
*tree
)
1794 int nch
= NCH(tree
);
1795 int res
= validate_import_as_name(CHILD(tree
, 0));
1798 for (i
= 1; res
&& (i
+ 1 < nch
); i
+= 2)
1799 res
= (validate_comma(CHILD(tree
, i
))
1800 && validate_import_as_name(CHILD(tree
, i
+ 1)));
1805 /* 'import' dotted_as_names */
1807 validate_import_name(node
*tree
)
1809 return (validate_ntype(tree
, import_name
)
1810 && validate_numnodes(tree
, 2, "import_name")
1811 && validate_name(CHILD(tree
, 0), "import")
1812 && validate_dotted_as_names(CHILD(tree
, 1)));
1815 /* Helper function to count the number of leading dots in
1816 * 'from ...module import name'
1819 count_from_dots(node
*tree
)
1822 for (i
= 0; i
< NCH(tree
); i
++)
1823 if (TYPE(CHILD(tree
, i
)) != DOT
)
1828 /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
1832 validate_import_from(node
*tree
)
1834 int nch
= NCH(tree
);
1835 int ndots
= count_from_dots(tree
);
1836 int havename
= (TYPE(CHILD(tree
, ndots
+ 1)) == dotted_name
);
1837 int offset
= ndots
+ havename
;
1838 int res
= validate_ntype(tree
, import_from
)
1839 && (nch
>= 4 + ndots
)
1840 && validate_name(CHILD(tree
, 0), "from")
1841 && (!havename
|| validate_dotted_name(CHILD(tree
, ndots
+ 1)))
1842 && validate_name(CHILD(tree
, offset
+ 1), "import");
1844 if (res
&& TYPE(CHILD(tree
, offset
+ 2)) == LPAR
)
1845 res
= ((nch
== offset
+ 5)
1846 && validate_lparen(CHILD(tree
, offset
+ 2))
1847 && validate_import_as_names(CHILD(tree
, offset
+ 3))
1848 && validate_rparen(CHILD(tree
, offset
+ 4)));
1849 else if (res
&& TYPE(CHILD(tree
, offset
+ 2)) != STAR
)
1850 res
= validate_import_as_names(CHILD(tree
, offset
+ 2));
1855 /* import_stmt: import_name | import_from */
1857 validate_import_stmt(node
*tree
)
1859 int nch
= NCH(tree
);
1860 int res
= validate_numnodes(tree
, 1, "import_stmt");
1863 int ntype
= TYPE(CHILD(tree
, 0));
1865 if (ntype
== import_name
|| ntype
== import_from
)
1866 res
= validate_node(CHILD(tree
, 0));
1869 err_string("illegal import_stmt child type");
1872 else if (nch
== 1) {
1874 PyErr_Format(parser_error
,
1875 "Unrecognized child node of import_stmt: %d.",
1876 TYPE(CHILD(tree
, 0)));
1885 validate_global_stmt(node
*tree
)
1888 int nch
= NCH(tree
);
1889 int res
= (validate_ntype(tree
, global_stmt
)
1890 && is_even(nch
) && (nch
>= 2));
1892 if (!res
&& !PyErr_Occurred())
1893 err_string("illegal global statement");
1896 res
= (validate_name(CHILD(tree
, 0), "global")
1897 && validate_ntype(CHILD(tree
, 1), NAME
));
1898 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1899 res
= (validate_comma(CHILD(tree
, j
))
1900 && validate_ntype(CHILD(tree
, j
+ 1), NAME
));
1908 * 'exec' expr ['in' test [',' test]]
1911 validate_exec_stmt(node
*tree
)
1913 int nch
= NCH(tree
);
1914 int res
= (validate_ntype(tree
, exec_stmt
)
1915 && ((nch
== 2) || (nch
== 4) || (nch
== 6))
1916 && validate_name(CHILD(tree
, 0), "exec")
1917 && validate_expr(CHILD(tree
, 1)));
1919 if (!res
&& !PyErr_Occurred())
1920 err_string("illegal exec statement");
1921 if (res
&& (nch
> 2))
1922 res
= (validate_name(CHILD(tree
, 2), "in")
1923 && validate_test(CHILD(tree
, 3)));
1924 if (res
&& (nch
== 6))
1925 res
= (validate_comma(CHILD(tree
, 4))
1926 && validate_test(CHILD(tree
, 5)));
1934 * 'assert' test [',' test]
1937 validate_assert_stmt(node
*tree
)
1939 int nch
= NCH(tree
);
1940 int res
= (validate_ntype(tree
, assert_stmt
)
1941 && ((nch
== 2) || (nch
== 4))
1942 && (validate_name(CHILD(tree
, 0), "assert"))
1943 && validate_test(CHILD(tree
, 1)));
1945 if (!res
&& !PyErr_Occurred())
1946 err_string("illegal assert statement");
1947 if (res
&& (nch
> 2))
1948 res
= (validate_comma(CHILD(tree
, 2))
1949 && validate_test(CHILD(tree
, 3)));
1956 validate_while(node
*tree
)
1958 int nch
= NCH(tree
);
1959 int res
= (validate_ntype(tree
, while_stmt
)
1960 && ((nch
== 4) || (nch
== 7))
1961 && validate_name(CHILD(tree
, 0), "while")
1962 && validate_test(CHILD(tree
, 1))
1963 && validate_colon(CHILD(tree
, 2))
1964 && validate_suite(CHILD(tree
, 3)));
1966 if (res
&& (nch
== 7))
1967 res
= (validate_name(CHILD(tree
, 4), "else")
1968 && validate_colon(CHILD(tree
, 5))
1969 && validate_suite(CHILD(tree
, 6)));
1976 validate_for(node
*tree
)
1978 int nch
= NCH(tree
);
1979 int res
= (validate_ntype(tree
, for_stmt
)
1980 && ((nch
== 6) || (nch
== 9))
1981 && validate_name(CHILD(tree
, 0), "for")
1982 && validate_exprlist(CHILD(tree
, 1))
1983 && validate_name(CHILD(tree
, 2), "in")
1984 && validate_testlist(CHILD(tree
, 3))
1985 && validate_colon(CHILD(tree
, 4))
1986 && validate_suite(CHILD(tree
, 5)));
1988 if (res
&& (nch
== 9))
1989 res
= (validate_name(CHILD(tree
, 6), "else")
1990 && validate_colon(CHILD(tree
, 7))
1991 && validate_suite(CHILD(tree
, 8)));
1998 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1999 * | 'try' ':' suite 'finally' ':' suite
2003 validate_try(node
*tree
)
2005 int nch
= NCH(tree
);
2007 int res
= (validate_ntype(tree
, try_stmt
)
2008 && (nch
>= 6) && ((nch
% 3) == 0));
2011 res
= (validate_name(CHILD(tree
, 0), "try")
2012 && validate_colon(CHILD(tree
, 1))
2013 && validate_suite(CHILD(tree
, 2))
2014 && validate_colon(CHILD(tree
, nch
- 2))
2015 && validate_suite(CHILD(tree
, nch
- 1)));
2016 else if (!PyErr_Occurred()) {
2017 const char* name
= "except";
2018 if (TYPE(CHILD(tree
, nch
- 3)) != except_clause
)
2019 name
= STR(CHILD(tree
, nch
- 3));
2021 PyErr_Format(parser_error
,
2022 "Illegal number of children for try/%s node.", name
);
2024 /* Skip past except_clause sections: */
2025 while (res
&& (TYPE(CHILD(tree
, pos
)) == except_clause
)) {
2026 res
= (validate_except_clause(CHILD(tree
, pos
))
2027 && validate_colon(CHILD(tree
, pos
+ 1))
2028 && validate_suite(CHILD(tree
, pos
+ 2)));
2031 if (res
&& (pos
< nch
)) {
2032 res
= validate_ntype(CHILD(tree
, pos
), NAME
);
2033 if (res
&& (strcmp(STR(CHILD(tree
, pos
)), "finally") == 0))
2034 res
= (validate_numnodes(tree
, 6, "try/finally")
2035 && validate_colon(CHILD(tree
, 4))
2036 && validate_suite(CHILD(tree
, 5)));
2038 if (nch
== (pos
+ 3)) {
2039 res
= ((strcmp(STR(CHILD(tree
, pos
)), "except") == 0)
2040 || (strcmp(STR(CHILD(tree
, pos
)), "else") == 0));
2042 err_string("illegal trailing triple in try statement");
2044 else if (nch
== (pos
+ 6)) {
2045 res
= (validate_name(CHILD(tree
, pos
), "except")
2046 && validate_colon(CHILD(tree
, pos
+ 1))
2047 && validate_suite(CHILD(tree
, pos
+ 2))
2048 && validate_name(CHILD(tree
, pos
+ 3), "else"));
2051 res
= validate_numnodes(tree
, pos
+ 3, "try/except");
2059 validate_except_clause(node
*tree
)
2061 int nch
= NCH(tree
);
2062 int res
= (validate_ntype(tree
, except_clause
)
2063 && ((nch
== 1) || (nch
== 2) || (nch
== 4))
2064 && validate_name(CHILD(tree
, 0), "except"));
2066 if (res
&& (nch
> 1))
2067 res
= validate_test(CHILD(tree
, 1));
2068 if (res
&& (nch
== 4))
2069 res
= (validate_comma(CHILD(tree
, 2))
2070 && validate_test(CHILD(tree
, 3)));
2077 validate_test(node
*tree
)
2079 int nch
= NCH(tree
);
2080 int res
= validate_ntype(tree
, test
) && is_odd(nch
);
2082 if (res
&& (TYPE(CHILD(tree
, 0)) == lambdef
))
2084 && validate_lambdef(CHILD(tree
, 0)));
2086 res
= validate_or_test(CHILD(tree
, 0));
2087 res
= (res
&& (nch
== 1 || (nch
== 5 &&
2088 validate_name(CHILD(tree
, 1), "if") &&
2089 validate_or_test(CHILD(tree
, 2)) &&
2090 validate_name(CHILD(tree
, 3), "else") &&
2091 validate_test(CHILD(tree
, 4)))));
2097 validate_old_test(node
*tree
)
2099 int nch
= NCH(tree
);
2100 int res
= validate_ntype(tree
, old_test
) && (nch
== 1);
2102 if (res
&& (TYPE(CHILD(tree
, 0)) == old_lambdef
))
2103 res
= (validate_old_lambdef(CHILD(tree
, 0)));
2105 res
= (validate_or_test(CHILD(tree
, 0)));
2111 validate_or_test(node
*tree
)
2113 int nch
= NCH(tree
);
2114 int res
= validate_ntype(tree
, or_test
) && is_odd(nch
);
2118 res
= validate_and_test(CHILD(tree
, 0));
2119 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2120 res
= (validate_name(CHILD(tree
, pos
), "or")
2121 && validate_and_test(CHILD(tree
, pos
+ 1)));
2128 validate_and_test(node
*tree
)
2131 int nch
= NCH(tree
);
2132 int res
= (validate_ntype(tree
, and_test
)
2134 && validate_not_test(CHILD(tree
, 0)));
2136 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2137 res
= (validate_name(CHILD(tree
, pos
), "and")
2138 && validate_not_test(CHILD(tree
, 0)));
2145 validate_not_test(node
*tree
)
2147 int nch
= NCH(tree
);
2148 int res
= validate_ntype(tree
, not_test
) && ((nch
== 1) || (nch
== 2));
2152 res
= (validate_name(CHILD(tree
, 0), "not")
2153 && validate_not_test(CHILD(tree
, 1)));
2155 res
= validate_comparison(CHILD(tree
, 0));
2162 validate_comparison(node
*tree
)
2165 int nch
= NCH(tree
);
2166 int res
= (validate_ntype(tree
, comparison
)
2168 && validate_expr(CHILD(tree
, 0)));
2170 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2171 res
= (validate_comp_op(CHILD(tree
, pos
))
2172 && validate_expr(CHILD(tree
, pos
+ 1)));
2179 validate_comp_op(node
*tree
)
2182 int nch
= NCH(tree
);
2184 if (!validate_ntype(tree
, comp_op
))
2188 * Only child will be a terminal with a well-defined symbolic name
2189 * or a NAME with a string of either 'is' or 'in'
2191 tree
= CHILD(tree
, 0);
2192 switch (TYPE(tree
)) {
2203 res
= ((strcmp(STR(tree
), "in") == 0)
2204 || (strcmp(STR(tree
), "is") == 0));
2206 PyErr_Format(parser_error
,
2207 "illegal operator '%s'", STR(tree
));
2211 err_string("illegal comparison operator type");
2215 else if ((res
= validate_numnodes(tree
, 2, "comp_op")) != 0) {
2216 res
= (validate_ntype(CHILD(tree
, 0), NAME
)
2217 && validate_ntype(CHILD(tree
, 1), NAME
)
2218 && (((strcmp(STR(CHILD(tree
, 0)), "is") == 0)
2219 && (strcmp(STR(CHILD(tree
, 1)), "not") == 0))
2220 || ((strcmp(STR(CHILD(tree
, 0)), "not") == 0)
2221 && (strcmp(STR(CHILD(tree
, 1)), "in") == 0))));
2222 if (!res
&& !PyErr_Occurred())
2223 err_string("unknown comparison operator");
2230 validate_expr(node
*tree
)
2233 int nch
= NCH(tree
);
2234 int res
= (validate_ntype(tree
, expr
)
2236 && validate_xor_expr(CHILD(tree
, 0)));
2238 for (j
= 2; res
&& (j
< nch
); j
+= 2)
2239 res
= (validate_xor_expr(CHILD(tree
, j
))
2240 && validate_vbar(CHILD(tree
, j
- 1)));
2247 validate_xor_expr(node
*tree
)
2250 int nch
= NCH(tree
);
2251 int res
= (validate_ntype(tree
, xor_expr
)
2253 && validate_and_expr(CHILD(tree
, 0)));
2255 for (j
= 2; res
&& (j
< nch
); j
+= 2)
2256 res
= (validate_circumflex(CHILD(tree
, j
- 1))
2257 && validate_and_expr(CHILD(tree
, j
)));
2264 validate_and_expr(node
*tree
)
2267 int nch
= NCH(tree
);
2268 int res
= (validate_ntype(tree
, and_expr
)
2270 && validate_shift_expr(CHILD(tree
, 0)));
2272 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
2273 res
= (validate_ampersand(CHILD(tree
, pos
))
2274 && validate_shift_expr(CHILD(tree
, pos
+ 1)));
2281 validate_chain_two_ops(node
*tree
, int (*termvalid
)(node
*), int op1
, int op2
)
2284 int nch
= NCH(tree
);
2285 int res
= (is_odd(nch
)
2286 && (*termvalid
)(CHILD(tree
, 0)));
2288 for ( ; res
&& (pos
< nch
); pos
+= 2) {
2289 if (TYPE(CHILD(tree
, pos
)) != op1
)
2290 res
= validate_ntype(CHILD(tree
, pos
), op2
);
2292 res
= (*termvalid
)(CHILD(tree
, pos
+ 1));
2299 validate_shift_expr(node
*tree
)
2301 return (validate_ntype(tree
, shift_expr
)
2302 && validate_chain_two_ops(tree
, validate_arith_expr
,
2303 LEFTSHIFT
, RIGHTSHIFT
));
2308 validate_arith_expr(node
*tree
)
2310 return (validate_ntype(tree
, arith_expr
)
2311 && validate_chain_two_ops(tree
, validate_term
, PLUS
, MINUS
));
2316 validate_term(node
*tree
)
2319 int nch
= NCH(tree
);
2320 int res
= (validate_ntype(tree
, term
)
2322 && validate_factor(CHILD(tree
, 0)));
2324 for ( ; res
&& (pos
< nch
); pos
+= 2)
2325 res
= (((TYPE(CHILD(tree
, pos
)) == STAR
)
2326 || (TYPE(CHILD(tree
, pos
)) == SLASH
)
2327 || (TYPE(CHILD(tree
, pos
)) == DOUBLESLASH
)
2328 || (TYPE(CHILD(tree
, pos
)) == PERCENT
))
2329 && validate_factor(CHILD(tree
, pos
+ 1)));
2337 * factor: ('+'|'-'|'~') factor | power
2340 validate_factor(node
*tree
)
2342 int nch
= NCH(tree
);
2343 int res
= (validate_ntype(tree
, factor
)
2345 && ((TYPE(CHILD(tree
, 0)) == PLUS
)
2346 || (TYPE(CHILD(tree
, 0)) == MINUS
)
2347 || (TYPE(CHILD(tree
, 0)) == TILDE
))
2348 && validate_factor(CHILD(tree
, 1)))
2350 && validate_power(CHILD(tree
, 0)))));
2357 * power: atom trailer* ('**' factor)*
2360 validate_power(node
*tree
)
2363 int nch
= NCH(tree
);
2364 int res
= (validate_ntype(tree
, power
) && (nch
>= 1)
2365 && validate_atom(CHILD(tree
, 0)));
2367 while (res
&& (pos
< nch
) && (TYPE(CHILD(tree
, pos
)) == trailer
))
2368 res
= validate_trailer(CHILD(tree
, pos
++));
2369 if (res
&& (pos
< nch
)) {
2370 if (!is_even(nch
- pos
)) {
2371 err_string("illegal number of nodes for 'power'");
2374 for ( ; res
&& (pos
< (nch
- 1)); pos
+= 2)
2375 res
= (validate_doublestar(CHILD(tree
, pos
))
2376 && validate_factor(CHILD(tree
, pos
+ 1)));
2383 validate_atom(node
*tree
)
2386 int nch
= NCH(tree
);
2387 int res
= validate_ntype(tree
, atom
);
2390 res
= validate_numnodes(tree
, nch
+1, "atom");
2392 switch (TYPE(CHILD(tree
, 0))) {
2395 && (validate_rparen(CHILD(tree
, nch
- 1))));
2397 if (res
&& (nch
== 3)) {
2398 if (TYPE(CHILD(tree
, 1))==yield_expr
)
2399 res
= validate_yield_expr(CHILD(tree
, 1));
2401 res
= validate_testlist_gexp(CHILD(tree
, 1));
2406 res
= validate_ntype(CHILD(tree
, 1), RSQB
);
2408 res
= (validate_listmaker(CHILD(tree
, 1))
2409 && validate_ntype(CHILD(tree
, 2), RSQB
));
2412 err_string("illegal list display atom");
2417 && validate_ntype(CHILD(tree
, nch
- 1), RBRACE
));
2419 if (res
&& (nch
== 3))
2420 res
= validate_dictmaker(CHILD(tree
, 1));
2424 && validate_testlist1(CHILD(tree
, 1))
2425 && validate_ntype(CHILD(tree
, 2), BACKQUOTE
));
2432 for (pos
= 1; res
&& (pos
< nch
); ++pos
)
2433 res
= validate_ntype(CHILD(tree
, pos
), STRING
);
2445 * test ( list_for | (',' test)* [','] )
2448 validate_listmaker(node
*tree
)
2450 int nch
= NCH(tree
);
2454 err_string("missing child nodes of listmaker");
2456 ok
= validate_test(CHILD(tree
, 0));
2459 * list_for | (',' test)* [',']
2461 if (nch
== 2 && TYPE(CHILD(tree
, 1)) == list_for
)
2462 ok
= validate_list_for(CHILD(tree
, 1));
2464 /* (',' test)* [','] */
2466 while (ok
&& nch
- i
>= 2) {
2467 ok
= (validate_comma(CHILD(tree
, i
))
2468 && validate_test(CHILD(tree
, i
+1)));
2471 if (ok
&& i
== nch
-1)
2472 ok
= validate_comma(CHILD(tree
, i
));
2473 else if (i
!= nch
) {
2475 err_string("illegal trailing nodes for listmaker");
2482 * test ( gen_for | (',' test)* [','] )
2485 validate_testlist_gexp(node
*tree
)
2487 int nch
= NCH(tree
);
2491 err_string("missing child nodes of testlist_gexp");
2493 ok
= validate_test(CHILD(tree
, 0));
2497 * gen_for | (',' test)* [',']
2499 if (nch
== 2 && TYPE(CHILD(tree
, 1)) == gen_for
)
2500 ok
= validate_gen_for(CHILD(tree
, 1));
2502 /* (',' test)* [','] */
2504 while (ok
&& nch
- i
>= 2) {
2505 ok
= (validate_comma(CHILD(tree
, i
))
2506 && validate_test(CHILD(tree
, i
+1)));
2509 if (ok
&& i
== nch
-1)
2510 ok
= validate_comma(CHILD(tree
, i
));
2511 else if (i
!= nch
) {
2513 err_string("illegal trailing nodes for testlist_gexp");
2520 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
2523 validate_decorator(node
*tree
)
2526 int nch
= NCH(tree
);
2527 ok
= (validate_ntype(tree
, decorator
) &&
2528 (nch
== 3 || nch
== 5 || nch
== 6) &&
2529 validate_at(CHILD(tree
, 0)) &&
2530 validate_dotted_name(CHILD(tree
, 1)) &&
2531 validate_newline(RCHILD(tree
, -1)));
2533 if (ok
&& nch
!= 3) {
2534 ok
= (validate_lparen(CHILD(tree
, 2)) &&
2535 validate_rparen(RCHILD(tree
, -2)));
2538 ok
= validate_arglist(CHILD(tree
, 3));
2548 validate_decorators(node
*tree
)
2552 ok
= validate_ntype(tree
, decorators
) && nch
>= 1;
2554 for (i
= 0; ok
&& i
< nch
; ++i
)
2555 ok
= validate_decorator(CHILD(tree
, i
));
2563 * [decorators] 'def' NAME parameters ':' suite
2566 validate_funcdef(node
*tree
)
2568 int nch
= NCH(tree
);
2569 int ok
= (validate_ntype(tree
, funcdef
)
2570 && ((nch
== 5) || (nch
== 6))
2571 && validate_name(RCHILD(tree
, -5), "def")
2572 && validate_ntype(RCHILD(tree
, -4), NAME
)
2573 && validate_colon(RCHILD(tree
, -2))
2574 && validate_parameters(RCHILD(tree
, -3))
2575 && validate_suite(RCHILD(tree
, -1)));
2577 if (ok
&& (nch
== 6))
2578 ok
= validate_decorators(CHILD(tree
, 0));
2585 validate_lambdef(node
*tree
)
2587 int nch
= NCH(tree
);
2588 int res
= (validate_ntype(tree
, lambdef
)
2589 && ((nch
== 3) || (nch
== 4))
2590 && validate_name(CHILD(tree
, 0), "lambda")
2591 && validate_colon(CHILD(tree
, nch
- 2))
2592 && validate_test(CHILD(tree
, nch
- 1)));
2594 if (res
&& (nch
== 4))
2595 res
= validate_varargslist(CHILD(tree
, 1));
2596 else if (!res
&& !PyErr_Occurred())
2597 (void) validate_numnodes(tree
, 3, "lambdef");
2604 validate_old_lambdef(node
*tree
)
2606 int nch
= NCH(tree
);
2607 int res
= (validate_ntype(tree
, old_lambdef
)
2608 && ((nch
== 3) || (nch
== 4))
2609 && validate_name(CHILD(tree
, 0), "lambda")
2610 && validate_colon(CHILD(tree
, nch
- 2))
2611 && validate_test(CHILD(tree
, nch
- 1)));
2613 if (res
&& (nch
== 4))
2614 res
= validate_varargslist(CHILD(tree
, 1));
2615 else if (!res
&& !PyErr_Occurred())
2616 (void) validate_numnodes(tree
, 3, "old_lambdef");
2624 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2627 validate_arglist(node
*tree
)
2629 int nch
= NCH(tree
);
2634 /* raise the right error from having an invalid number of children */
2635 return validate_numnodes(tree
, nch
+ 1, "arglist");
2638 for (i
=0; i
<nch
; i
++) {
2639 if (TYPE(CHILD(tree
, i
)) == argument
) {
2640 node
*ch
= CHILD(tree
, i
);
2641 if (NCH(ch
) == 2 && TYPE(CHILD(ch
, 1)) == gen_for
) {
2642 err_string("need '(', ')' for generator expression");
2649 while (ok
&& nch
-i
>= 2) {
2650 /* skip leading (argument ',') */
2651 ok
= (validate_argument(CHILD(tree
, i
))
2652 && validate_comma(CHILD(tree
, i
+1)));
2661 * argument | '*' test [',' '**' test] | '**' test
2663 int sym
= TYPE(CHILD(tree
, i
));
2665 if (sym
== argument
) {
2666 ok
= validate_argument(CHILD(tree
, i
));
2667 if (ok
&& i
+1 != nch
) {
2668 err_string("illegal arglist specification"
2669 " (extra stuff on end)");
2673 else if (sym
== STAR
) {
2674 ok
= validate_star(CHILD(tree
, i
));
2675 if (ok
&& (nch
-i
== 2))
2676 ok
= validate_test(CHILD(tree
, i
+1));
2677 else if (ok
&& (nch
-i
== 5))
2678 ok
= (validate_test(CHILD(tree
, i
+1))
2679 && validate_comma(CHILD(tree
, i
+2))
2680 && validate_doublestar(CHILD(tree
, i
+3))
2681 && validate_test(CHILD(tree
, i
+4)));
2683 err_string("illegal use of '*' in arglist");
2687 else if (sym
== DOUBLESTAR
) {
2689 ok
= (validate_doublestar(CHILD(tree
, i
))
2690 && validate_test(CHILD(tree
, i
+1)));
2692 err_string("illegal use of '**' in arglist");
2697 err_string("illegal arglist specification");
2708 * [test '='] test [gen_for]
2711 validate_argument(node
*tree
)
2713 int nch
= NCH(tree
);
2714 int res
= (validate_ntype(tree
, argument
)
2715 && ((nch
== 1) || (nch
== 2) || (nch
== 3))
2716 && validate_test(CHILD(tree
, 0)));
2718 if (res
&& (nch
== 2))
2719 res
= validate_gen_for(CHILD(tree
, 1));
2720 else if (res
&& (nch
== 3))
2721 res
= (validate_equal(CHILD(tree
, 1))
2722 && validate_test(CHILD(tree
, 2)));
2731 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2734 validate_trailer(node
*tree
)
2736 int nch
= NCH(tree
);
2737 int res
= validate_ntype(tree
, trailer
) && ((nch
== 2) || (nch
== 3));
2740 switch (TYPE(CHILD(tree
, 0))) {
2742 res
= validate_rparen(CHILD(tree
, nch
- 1));
2743 if (res
&& (nch
== 3))
2744 res
= validate_arglist(CHILD(tree
, 1));
2747 res
= (validate_numnodes(tree
, 3, "trailer")
2748 && validate_subscriptlist(CHILD(tree
, 1))
2749 && validate_ntype(CHILD(tree
, 2), RSQB
));
2752 res
= (validate_numnodes(tree
, 2, "trailer")
2753 && validate_ntype(CHILD(tree
, 1), NAME
));
2761 (void) validate_numnodes(tree
, 2, "trailer");
2769 * subscript (',' subscript)* [',']
2772 validate_subscriptlist(node
*tree
)
2774 return (validate_repeating_list(tree
, subscriptlist
,
2775 validate_subscript
, "subscriptlist"));
2781 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
2784 validate_subscript(node
*tree
)
2787 int nch
= NCH(tree
);
2788 int res
= validate_ntype(tree
, subscript
) && (nch
>= 1) && (nch
<= 4);
2791 if (!PyErr_Occurred())
2792 err_string("invalid number of arguments for subscript node");
2795 if (TYPE(CHILD(tree
, 0)) == DOT
)
2796 /* take care of ('.' '.' '.') possibility */
2797 return (validate_numnodes(tree
, 3, "subscript")
2798 && validate_dot(CHILD(tree
, 0))
2799 && validate_dot(CHILD(tree
, 1))
2800 && validate_dot(CHILD(tree
, 2)));
2802 if (TYPE(CHILD(tree
, 0)) == test
)
2803 res
= validate_test(CHILD(tree
, 0));
2805 res
= validate_colon(CHILD(tree
, 0));
2808 /* Must be [test] ':' [test] [sliceop],
2809 * but at least one of the optional components will
2810 * be present, but we don't know which yet.
2812 if ((TYPE(CHILD(tree
, 0)) != COLON
) || (nch
== 4)) {
2813 res
= validate_test(CHILD(tree
, 0));
2817 res
= validate_colon(CHILD(tree
, offset
));
2819 int rem
= nch
- ++offset
;
2821 if (TYPE(CHILD(tree
, offset
)) == test
) {
2822 res
= validate_test(CHILD(tree
, offset
));
2827 res
= validate_sliceop(CHILD(tree
, offset
));
2835 validate_sliceop(node
*tree
)
2837 int nch
= NCH(tree
);
2838 int res
= ((nch
== 1) || validate_numnodes(tree
, 2, "sliceop"))
2839 && validate_ntype(tree
, sliceop
);
2840 if (!res
&& !PyErr_Occurred()) {
2841 res
= validate_numnodes(tree
, 1, "sliceop");
2844 res
= validate_colon(CHILD(tree
, 0));
2845 if (res
&& (nch
== 2))
2846 res
= validate_test(CHILD(tree
, 1));
2853 validate_exprlist(node
*tree
)
2855 return (validate_repeating_list(tree
, exprlist
,
2856 validate_expr
, "exprlist"));
2861 validate_dictmaker(node
*tree
)
2863 int nch
= NCH(tree
);
2864 int res
= (validate_ntype(tree
, dictmaker
)
2866 && validate_test(CHILD(tree
, 0))
2867 && validate_colon(CHILD(tree
, 1))
2868 && validate_test(CHILD(tree
, 2)));
2870 if (res
&& ((nch
% 4) == 0))
2871 res
= validate_comma(CHILD(tree
, --nch
));
2873 res
= ((nch
% 4) == 3);
2875 if (res
&& (nch
> 3)) {
2877 /* ( ',' test ':' test )* */
2878 while (res
&& (pos
< nch
)) {
2879 res
= (validate_comma(CHILD(tree
, pos
))
2880 && validate_test(CHILD(tree
, pos
+ 1))
2881 && validate_colon(CHILD(tree
, pos
+ 2))
2882 && validate_test(CHILD(tree
, pos
+ 3)));
2891 validate_eval_input(node
*tree
)
2894 int nch
= NCH(tree
);
2895 int res
= (validate_ntype(tree
, eval_input
)
2897 && validate_testlist(CHILD(tree
, 0))
2898 && validate_ntype(CHILD(tree
, nch
- 1), ENDMARKER
));
2900 for (pos
= 1; res
&& (pos
< (nch
- 1)); ++pos
)
2901 res
= validate_ntype(CHILD(tree
, pos
), NEWLINE
);
2908 validate_node(node
*tree
)
2910 int nch
= 0; /* num. children on current node */
2911 int res
= 1; /* result value */
2912 node
* next
= 0; /* node to process after this one */
2914 while (res
&& (tree
!= 0)) {
2917 switch (TYPE(tree
)) {
2922 res
= validate_funcdef(tree
);
2925 res
= validate_class(tree
);
2928 * "Trivial" parse tree nodes.
2929 * (Why did I call these trivial?)
2932 res
= validate_stmt(tree
);
2936 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2937 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2939 res
= validate_small_stmt(tree
);
2942 res
= (validate_numnodes(tree
, 1, "flow_stmt")
2943 && ((TYPE(CHILD(tree
, 0)) == break_stmt
)
2944 || (TYPE(CHILD(tree
, 0)) == continue_stmt
)
2945 || (TYPE(CHILD(tree
, 0)) == yield_stmt
)
2946 || (TYPE(CHILD(tree
, 0)) == return_stmt
)
2947 || (TYPE(CHILD(tree
, 0)) == raise_stmt
)));
2949 next
= CHILD(tree
, 0);
2951 err_string("illegal flow_stmt type");
2954 res
= validate_yield_stmt(tree
);
2957 * Compound statements.
2960 res
= validate_simple_stmt(tree
);
2963 res
= validate_compound_stmt(tree
);
2966 * Fundamental statements.
2969 res
= validate_expr_stmt(tree
);
2972 res
= validate_print_stmt(tree
);
2975 res
= validate_del_stmt(tree
);
2978 res
= (validate_numnodes(tree
, 1, "pass")
2979 && validate_name(CHILD(tree
, 0), "pass"));
2982 res
= (validate_numnodes(tree
, 1, "break")
2983 && validate_name(CHILD(tree
, 0), "break"));
2986 res
= (validate_numnodes(tree
, 1, "continue")
2987 && validate_name(CHILD(tree
, 0), "continue"));
2990 res
= validate_return_stmt(tree
);
2993 res
= validate_raise_stmt(tree
);
2996 res
= validate_import_stmt(tree
);
2999 res
= validate_import_name(tree
);
3002 res
= validate_import_from(tree
);
3005 res
= validate_global_stmt(tree
);
3008 res
= validate_exec_stmt(tree
);
3011 res
= validate_assert_stmt(tree
);
3014 res
= validate_if(tree
);
3017 res
= validate_while(tree
);
3020 res
= validate_for(tree
);
3023 res
= validate_try(tree
);
3026 res
= validate_suite(tree
);
3032 res
= validate_testlist(tree
);
3035 res
= validate_yield_expr(tree
);
3038 res
= validate_testlist1(tree
);
3041 res
= validate_test(tree
);
3044 res
= validate_and_test(tree
);
3047 res
= validate_not_test(tree
);
3050 res
= validate_comparison(tree
);
3053 res
= validate_exprlist(tree
);
3056 res
= validate_comp_op(tree
);
3059 res
= validate_expr(tree
);
3062 res
= validate_xor_expr(tree
);
3065 res
= validate_and_expr(tree
);
3068 res
= validate_shift_expr(tree
);
3071 res
= validate_arith_expr(tree
);
3074 res
= validate_term(tree
);
3077 res
= validate_factor(tree
);
3080 res
= validate_power(tree
);
3083 res
= validate_atom(tree
);
3087 /* Hopefully never reached! */
3088 err_string("unrecognized node type");
3099 validate_expr_tree(node
*tree
)
3101 int res
= validate_eval_input(tree
);
3103 if (!res
&& !PyErr_Occurred())
3104 err_string("could not validate expression tuple");
3111 * (NEWLINE | stmt)* ENDMARKER
3114 validate_file_input(node
*tree
)
3117 int nch
= NCH(tree
) - 1;
3118 int res
= ((nch
>= 0)
3119 && validate_ntype(CHILD(tree
, nch
), ENDMARKER
));
3121 for (j
= 0; res
&& (j
< nch
); ++j
) {
3122 if (TYPE(CHILD(tree
, j
)) == stmt
)
3123 res
= validate_stmt(CHILD(tree
, j
));
3125 res
= validate_newline(CHILD(tree
, j
));
3127 /* This stays in to prevent any internal failures from getting to the
3128 * user. Hopefully, this won't be needed. If a user reports getting
3129 * this, we have some debugging to do.
3131 if (!res
&& !PyErr_Occurred())
3132 err_string("VALIDATION FAILURE: report this to the maintainer!");
3138 validate_encoding_decl(node
*tree
)
3140 int nch
= NCH(tree
);
3141 int res
= ((nch
== 1)
3142 && validate_file_input(CHILD(tree
, 0)));
3144 if (!res
&& !PyErr_Occurred())
3145 err_string("Error Parsing encoding_decl");
3151 pickle_constructor
= NULL
;
3155 parser__pickler(PyObject
*self
, PyObject
*args
)
3157 NOTE(ARGUNUSED(self
))
3158 PyObject
*result
= NULL
;
3159 PyObject
*st
= NULL
;
3160 PyObject
*empty_dict
= NULL
;
3162 if (PyArg_ParseTuple(args
, "O!:_pickler", &PyST_Type
, &st
)) {
3166 if ((empty_dict
= PyDict_New()) == NULL
)
3168 if ((newargs
= Py_BuildValue("Oi", st
, 1)) == NULL
)
3170 tuple
= parser_st2tuple((PyST_Object
*)NULL
, newargs
, empty_dict
);
3171 if (tuple
!= NULL
) {
3172 result
= Py_BuildValue("O(O)", pickle_constructor
, tuple
);
3175 Py_DECREF(empty_dict
);
3179 Py_XDECREF(empty_dict
);
3185 /* Functions exported by this module. Most of this should probably
3186 * be converted into an ST object with methods, but that is better
3187 * done directly in Python, allowing subclasses to be created directly.
3188 * We'd really have to write a wrapper around it all anyway to allow
3191 static PyMethodDef parser_functions
[] = {
3192 {"ast2tuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
3193 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3194 {"ast2list", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
3195 PyDoc_STR("Creates a list-tree representation of an ST.")},
3196 {"compileast", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
3197 PyDoc_STR("Compiles an ST object into a code object.")},
3198 {"compilest", (PyCFunction
)parser_compilest
, PUBLIC_METHOD_TYPE
,
3199 PyDoc_STR("Compiles an ST object into a code object.")},
3200 {"expr", (PyCFunction
)parser_expr
, PUBLIC_METHOD_TYPE
,
3201 PyDoc_STR("Creates an ST object from an expression.")},
3202 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
3203 PyDoc_STR("Determines if an ST object was created from an expression.")},
3204 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
3205 PyDoc_STR("Determines if an ST object was created from a suite.")},
3206 {"suite", (PyCFunction
)parser_suite
, PUBLIC_METHOD_TYPE
,
3207 PyDoc_STR("Creates an ST object from a suite.")},
3208 {"sequence2ast", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
3209 PyDoc_STR("Creates an ST object from a tree representation.")},
3210 {"sequence2st", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
3211 PyDoc_STR("Creates an ST object from a tree representation.")},
3212 {"st2tuple", (PyCFunction
)parser_st2tuple
, PUBLIC_METHOD_TYPE
,
3213 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
3214 {"st2list", (PyCFunction
)parser_st2list
, PUBLIC_METHOD_TYPE
,
3215 PyDoc_STR("Creates a list-tree representation of an ST.")},
3216 {"tuple2ast", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
3217 PyDoc_STR("Creates an ST object from a tree representation.")},
3218 {"tuple2st", (PyCFunction
)parser_tuple2st
, PUBLIC_METHOD_TYPE
,
3219 PyDoc_STR("Creates an ST object from a tree representation.")},
3221 /* private stuff: support pickle module */
3222 {"_pickler", (PyCFunction
)parser__pickler
, METH_VARARGS
,
3223 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
3225 {NULL
, NULL
, 0, NULL
}
3229 PyMODINIT_FUNC
initparser(void); /* supply a prototype */
3234 PyObject
*module
, *copyreg
;
3236 PyST_Type
.ob_type
= &PyType_Type
;
3237 module
= Py_InitModule("parser", parser_functions
);
3241 if (parser_error
== 0)
3242 parser_error
= PyErr_NewException("parser.ParserError", NULL
, NULL
);
3244 if (parser_error
== 0)
3245 /* caller will check PyErr_Occurred() */
3247 /* CAUTION: The code next used to skip bumping the refcount on
3248 * parser_error. That's a disaster if initparser() gets called more
3249 * than once. By incref'ing, we ensure that each module dict that
3250 * gets created owns its reference to the shared parser_error object,
3251 * and the file static parser_error vrbl owns a reference too.
3253 Py_INCREF(parser_error
);
3254 if (PyModule_AddObject(module
, "ParserError", parser_error
) != 0)
3257 Py_INCREF(&PyST_Type
);
3258 PyModule_AddObject(module
, "ASTType", (PyObject
*)&PyST_Type
);
3259 Py_INCREF(&PyST_Type
);
3260 PyModule_AddObject(module
, "STType", (PyObject
*)&PyST_Type
);
3262 PyModule_AddStringConstant(module
, "__copyright__",
3263 parser_copyright_string
);
3264 PyModule_AddStringConstant(module
, "__doc__",
3266 PyModule_AddStringConstant(module
, "__version__",
3267 parser_version_string
);
3269 /* Register to support pickling.
3270 * If this fails, the import of this module will fail because an
3271 * exception will be raised here; should we clear the exception?
3273 copyreg
= PyImport_ImportModule("copy_reg");
3274 if (copyreg
!= NULL
) {
3275 PyObject
*func
, *pickler
;
3277 func
= PyObject_GetAttrString(copyreg
, "pickle");
3278 pickle_constructor
= PyObject_GetAttrString(module
, "sequence2st");
3279 pickler
= PyObject_GetAttrString(module
, "_pickler");
3280 Py_XINCREF(pickle_constructor
);
3281 if ((func
!= NULL
) && (pickle_constructor
!= NULL
)
3282 && (pickler
!= NULL
)) {
3285 res
= PyObject_CallFunctionObjArgs(func
, &PyST_Type
, pickler
,
3286 pickle_constructor
, NULL
);
3290 Py_XDECREF(pickle_constructor
);
3291 Py_XDECREF(pickler
);