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 "token.h" /* token definitions */
32 /* ISTERMINAL() / ISNONTERMINAL() */
33 #include "compile.h" /* PyNode_Compile() */
45 /* String constants used to initialize module attributes.
49 parser_copyright_string
50 = "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
51 University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
52 Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
53 Centrum, Amsterdam, The Netherlands.";
58 = "This is an interface to Python's internal parser.";
61 parser_version_string
= "0.5";
64 typedef PyObject
* (*SeqMaker
) (int length
);
65 typedef int (*SeqInserter
) (PyObject
* sequence
,
69 /* The function below is copyrighted by Stichting Mathematisch Centrum. The
70 * original copyright statement is included below, and continues to apply
71 * in full to the function immediately following. All other material is
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
73 * Institute and State University. Changes were made to comply with the
74 * new naming conventions. Added arguments to provide support for creating
75 * lists as well as tuples, and optionally including the line numbers.
80 node2tuple(node
*n
, /* node to convert */
81 SeqMaker mkseq
, /* create sequence */
82 SeqInserter addelem
, /* func. to add elem. in seq. */
83 int lineno
) /* include line numbers? */
89 if (ISNONTERMINAL(TYPE(n
))) {
94 v
= mkseq(1 + NCH(n
));
97 w
= PyInt_FromLong(TYPE(n
));
100 return ((PyObject
*) NULL
);
102 (void) addelem(v
, 0, w
);
103 for (i
= 0; i
< NCH(n
); i
++) {
104 w
= node2tuple(CHILD(n
, i
), mkseq
, addelem
, lineno
);
107 return ((PyObject
*) NULL
);
109 (void) addelem(v
, i
+1, w
);
113 else if (ISTERMINAL(TYPE(n
))) {
114 PyObject
*result
= mkseq(2 + lineno
);
115 if (result
!= NULL
) {
116 (void) addelem(result
, 0, PyInt_FromLong(TYPE(n
)));
117 (void) addelem(result
, 1, PyString_FromString(STR(n
)));
119 (void) addelem(result
, 2, PyInt_FromLong(n
->n_lineno
));
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 ast_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. ;-)
141 * The PyAST_FRAGMENT type is not currently supported. Maybe not useful?
142 * Haven't decided yet.
146 #define PyAST_SUITE 2
147 #define PyAST_FRAGMENT 3
150 /* These are the internal objects and definitions required to implement the
151 * AST type. Most of the internal names are more reminiscent of the 'old'
152 * naming style, but the code uses the new naming convention.
159 typedef struct _PyAST_Object
{
160 PyObject_HEAD
/* standard object header */
161 node
* ast_node
; /* the node* returned by the parser */
162 int ast_type
; /* EXPR or SUITE ? */
167 parser_free(PyAST_Object
*ast
);
170 parser_compare(PyAST_Object
*left
, PyAST_Object
*right
);
172 staticforward PyObject
*
173 parser_getattr(PyObject
*self
, char *name
);
177 PyTypeObject PyAST_Type
= {
178 PyObject_HEAD_INIT(NULL
)
181 (int) sizeof(PyAST_Object
), /* tp_basicsize */
183 (destructor
)parser_free
, /* tp_dealloc */
185 parser_getattr
, /* tp_getattr */
187 (cmpfunc
)parser_compare
, /* tp_compare */
189 0, /* tp_as_number */
190 0, /* tp_as_sequence */
191 0, /* tp_as_mapping */
198 /* Functions to access object as input/output buffer */
199 0, /* tp_as_buffer */
201 Py_TPFLAGS_DEFAULT
, /* tp_flags */
204 "Intermediate representation of a Python parse tree."
209 parser_compare_nodes(node
*left
, node
*right
)
213 if (TYPE(left
) < TYPE(right
))
216 if (TYPE(right
) < TYPE(left
))
219 if (ISTERMINAL(TYPE(left
)))
220 return (strcmp(STR(left
), STR(right
)));
222 if (NCH(left
) < NCH(right
))
225 if (NCH(right
) < NCH(left
))
228 for (j
= 0; j
< NCH(left
); ++j
) {
229 int v
= parser_compare_nodes(CHILD(left
, j
), CHILD(right
, j
));
238 /* int parser_compare(PyAST_Object* left, PyAST_Object* right)
240 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
241 * This really just wraps a call to parser_compare_nodes() with some easy
242 * checks and protection code.
246 parser_compare(PyAST_Object
*left
, PyAST_Object
*right
)
251 if ((left
== 0) || (right
== 0))
254 return (parser_compare_nodes(left
->ast_node
, right
->ast_node
));
258 /* parser_newastobject(node* ast)
260 * Allocates a new Python object representing an AST. This is simply the
261 * 'wrapper' object that holds a node* and allows it to be passed around in
266 parser_newastobject(node
*ast
, int type
)
268 PyAST_Object
* o
= PyObject_New(PyAST_Object
, &PyAST_Type
);
277 return ((PyObject
*)o
);
281 /* void parser_free(PyAST_Object* ast)
283 * This is called by a del statement that reduces the reference count to 0.
287 parser_free(PyAST_Object
*ast
)
289 PyNode_Free(ast
->ast_node
);
294 /* parser_ast2tuple(PyObject* self, PyObject* args, PyObject* kw)
296 * This provides conversion from a node* to a tuple object that can be
297 * returned to the Python-level caller. The AST object is not modified.
301 parser_ast2tuple(PyAST_Object
*self
, PyObject
*args
, PyObject
*kw
)
303 PyObject
*line_option
= 0;
307 static char *keywords
[] = {"ast", "line_info", NULL
};
310 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|O:ast2tuple", keywords
,
311 &PyAST_Type
, &self
, &line_option
);
314 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|O:totuple", &keywords
[1],
318 if (line_option
!= NULL
) {
319 lineno
= (PyObject_IsTrue(line_option
) != 0) ? 1 : 0;
322 * Convert AST into a tuple representation. Use Guido's function,
323 * since it's known to work already.
325 res
= node2tuple(((PyAST_Object
*)self
)->ast_node
,
326 PyTuple_New
, PyTuple_SetItem
, lineno
);
332 /* parser_ast2list(PyObject* self, PyObject* args, PyObject* kw)
334 * This provides conversion from a node* to a list object that can be
335 * returned to the Python-level caller. The AST object is not modified.
339 parser_ast2list(PyAST_Object
*self
, PyObject
*args
, PyObject
*kw
)
341 PyObject
*line_option
= 0;
345 static char *keywords
[] = {"ast", "line_info", NULL
};
348 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|O:ast2list", keywords
,
349 &PyAST_Type
, &self
, &line_option
);
351 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|O:tolist", &keywords
[1],
355 if (line_option
!= 0) {
356 lineno
= PyObject_IsTrue(line_option
) ? 1 : 0;
359 * Convert AST into a tuple representation. Use Guido's function,
360 * since it's known to work already.
362 res
= node2tuple(self
->ast_node
,
363 PyList_New
, PyList_SetItem
, lineno
);
369 /* parser_compileast(PyObject* self, PyObject* args)
371 * This function creates code objects from the parse tree represented by
372 * the passed-in data object. An optional file name is passed in as well.
376 parser_compileast(PyAST_Object
*self
, PyObject
*args
, PyObject
*kw
)
382 static char *keywords
[] = {"ast", "filename", NULL
};
385 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!|s:compileast", keywords
,
386 &PyAST_Type
, &self
, &str
);
388 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "|s:compile", &keywords
[1],
392 res
= (PyObject
*)PyNode_Compile(self
->ast_node
, str
);
398 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
399 * PyObject* parser_issuite(PyObject* self, PyObject* args)
401 * Checks the passed-in AST object to determine if it is an expression or
402 * a statement suite, respectively. The return is a Python truth value.
406 parser_isexpr(PyAST_Object
*self
, PyObject
*args
, PyObject
*kw
)
411 static char *keywords
[] = {"ast", NULL
};
414 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:isexpr", keywords
,
417 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":isexpr", &keywords
[1]);
420 /* Check to see if the AST represents an expression or not. */
421 res
= (self
->ast_type
== PyAST_EXPR
) ? Py_True
: Py_False
;
429 parser_issuite(PyAST_Object
*self
, PyObject
*args
, PyObject
*kw
)
434 static char *keywords
[] = {"ast", NULL
};
437 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, "O!:issuite", keywords
,
440 ok
= PyArg_ParseTupleAndKeywords(args
, kw
, ":issuite", &keywords
[1]);
443 /* Check to see if the AST represents an expression or not. */
444 res
= (self
->ast_type
== PyAST_EXPR
) ? Py_False
: Py_True
;
451 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
455 {"compile", (PyCFunction
)parser_compileast
, PUBLIC_METHOD_TYPE
,
456 "Compile this AST object into a code object."},
457 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
458 "Determines if this AST object was created from an expression."},
459 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
460 "Determines if this AST object was created from a suite."},
461 {"tolist", (PyCFunction
)parser_ast2list
, PUBLIC_METHOD_TYPE
,
462 "Creates a list-tree representation of this AST."},
463 {"totuple", (PyCFunction
)parser_ast2tuple
, PUBLIC_METHOD_TYPE
,
464 "Creates a tuple-tree representation of this AST."},
466 {NULL
, NULL
, 0, NULL
}
471 parser_getattr(PyObject
*self
, char *name
)
473 return (Py_FindMethod(parser_methods
, self
, name
));
477 /* err_string(char* message)
479 * Sets the error string for an exception of type ParserError.
483 err_string(char *message
)
485 PyErr_SetString(parser_error
, message
);
489 /* PyObject* parser_do_parse(PyObject* args, int type)
491 * Internal function to actually execute the parse and return the result if
492 * successful, or set an exception if not.
496 parser_do_parse(PyObject
*args
, PyObject
*kw
, char *argspec
, int type
)
501 static char *keywords
[] = {"source", NULL
};
503 if (PyArg_ParseTupleAndKeywords(args
, kw
, argspec
, keywords
, &string
)) {
504 node
* n
= PyParser_SimpleParseString(string
,
506 ? eval_input
: file_input
);
509 res
= parser_newastobject(n
, type
);
511 err_string("could not parse string");
517 /* PyObject* parser_expr(PyObject* self, PyObject* args)
518 * PyObject* parser_suite(PyObject* self, PyObject* args)
520 * External interfaces to the parser itself. Which is called determines if
521 * the parser attempts to recognize an expression ('eval' form) or statement
522 * suite ('exec' form). The real work is done by parser_do_parse() above.
526 parser_expr(PyAST_Object
*self
, PyObject
*args
, PyObject
*kw
)
528 NOTE(ARGUNUSED(self
))
529 return (parser_do_parse(args
, kw
, "s:expr", PyAST_EXPR
));
534 parser_suite(PyAST_Object
*self
, PyObject
*args
, PyObject
*kw
)
536 NOTE(ARGUNUSED(self
))
537 return (parser_do_parse(args
, kw
, "s:suite", PyAST_SUITE
));
542 /* This is the messy part of the code. Conversion from a tuple to an AST
543 * object requires that the input tuple be valid without having to rely on
544 * catching an exception from the compiler. This is done to allow the
545 * compiler itself to remain fast, since most of its input will come from
546 * the parser directly, and therefore be known to be syntactically correct.
547 * This validation is done to ensure that we don't core dump the compile
548 * phase, returning an exception instead.
550 * Two aspects can be broken out in this code: creating a node tree from
551 * the tuple passed in, and verifying that it is indeed valid. It may be
552 * advantageous to expand the number of AST types to include funcdefs and
553 * lambdadefs to take advantage of the optimizer, recognizing those ASTs
554 * here. They are not necessary, and not quite as useful in a raw form.
555 * For now, let's get expressions and suites working reliably.
559 staticforward node
* build_node_tree(PyObject
*tuple
);
560 staticforward
int validate_expr_tree(node
*tree
);
561 staticforward
int validate_file_input(node
*tree
);
564 /* PyObject* parser_tuple2ast(PyObject* self, PyObject* args)
566 * This is the public function, called from the Python code. It receives a
567 * single tuple object from the caller, and creates an AST object if the
568 * tuple can be validated. It does this by checking the first code of the
569 * tuple, and, if acceptable, builds the internal representation. If this
570 * step succeeds, the internal representation is validated as fully as
571 * possible with the various validate_*() routines defined below.
573 * This function must be changed if support is to be added for PyAST_FRAGMENT
578 parser_tuple2ast(PyAST_Object
*self
, PyObject
*args
, PyObject
*kw
)
580 NOTE(ARGUNUSED(self
))
585 static char *keywords
[] = {"sequence", NULL
};
587 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "O:sequence2ast", keywords
,
590 if (!PySequence_Check(tuple
)) {
591 PyErr_SetString(PyExc_ValueError
,
592 "sequence2ast() requires a single sequence argument");
596 * Convert the tree to the internal form before checking it.
598 tree
= build_node_tree(tuple
);
600 int start_sym
= TYPE(tree
);
601 if (start_sym
== eval_input
) {
602 /* Might be an eval form. */
603 if (validate_expr_tree(tree
))
604 ast
= parser_newastobject(tree
, PyAST_EXPR
);
606 else if (start_sym
== file_input
) {
607 /* This looks like an exec form so far. */
608 if (validate_file_input(tree
))
609 ast
= parser_newastobject(tree
, PyAST_SUITE
);
612 /* This is a fragment, at best. */
614 err_string("parse tree does not use a valid start symbol");
617 /* Make sure we throw an exception on all errors. We should never
618 * get this, but we'd do well to be sure something is done.
620 if ((ast
== 0) && !PyErr_Occurred())
621 err_string("unspecified AST error occurred");
627 /* node* build_node_children()
629 * Iterate across the children of the current non-terminal node and build
630 * their structures. If successful, return the root of this portion of
631 * the tree, otherwise, 0. Any required exception will be specified already,
632 * and no memory will have been deallocated.
636 build_node_children(PyObject
*tuple
, node
*root
, int *line_num
)
638 int len
= PyObject_Size(tuple
);
641 for (i
= 1; i
< len
; ++i
) {
642 /* elem must always be a sequence, however simple */
643 PyObject
* elem
= PySequence_GetItem(tuple
, i
);
644 int ok
= elem
!= NULL
;
649 ok
= PySequence_Check(elem
);
651 PyObject
*temp
= PySequence_GetItem(elem
, 0);
655 ok
= PyInt_Check(temp
);
657 type
= PyInt_AS_LONG(temp
);
662 PyErr_SetObject(parser_error
,
663 Py_BuildValue("os", elem
,
664 "Illegal node construct."));
668 if (ISTERMINAL(type
)) {
669 int len
= PyObject_Size(elem
);
672 if ((len
!= 2) && (len
!= 3)) {
673 err_string("terminal nodes must have 2 or 3 entries");
676 temp
= PySequence_GetItem(elem
, 1);
679 if (!PyString_Check(temp
)) {
680 PyErr_Format(parser_error
,
681 "second item in terminal node must be a string,"
683 ((PyTypeObject
*)PyObject_Type(temp
))->tp_name
);
688 PyObject
*o
= PySequence_GetItem(elem
, 2);
691 *line_num
= PyInt_AS_LONG(o
);
693 PyErr_Format(parser_error
,
694 "third item in terminal node must be an"
695 " integer, found %s",
696 ((PyTypeObject
*)PyObject_Type(temp
))->tp_name
);
704 len
= PyString_GET_SIZE(temp
) + 1;
705 strn
= (char *)PyMem_MALLOC(len
);
707 (void) memcpy(strn
, PyString_AS_STRING(temp
), len
);
710 else if (!ISNONTERMINAL(type
)) {
712 * It has to be one or the other; this is an error.
713 * Throw an exception.
715 PyErr_SetObject(parser_error
,
716 Py_BuildValue("os", elem
, "unknown node type."));
720 PyNode_AddChild(root
, type
, strn
, *line_num
);
722 if (ISNONTERMINAL(type
)) {
723 node
* new_child
= CHILD(root
, i
- 1);
725 if (new_child
!= build_node_children(elem
, new_child
, line_num
)) {
730 else if (type
== NEWLINE
) { /* It's true: we increment the */
731 ++(*line_num
); /* line number *after* the newline! */
740 build_node_tree(PyObject
*tuple
)
743 PyObject
*temp
= PySequence_GetItem(tuple
, 0);
747 num
= PyInt_AsLong(temp
);
749 if (ISTERMINAL(num
)) {
751 * The tuple is simple, but it doesn't start with a start symbol.
752 * Throw an exception now and be done with it.
754 tuple
= Py_BuildValue("os", tuple
,
755 "Illegal ast tuple; cannot start with terminal symbol.");
756 PyErr_SetObject(parser_error
, tuple
);
758 else if (ISNONTERMINAL(num
)) {
760 * Not efficient, but that can be handled later.
764 res
= PyNode_New(num
);
765 if (res
!= build_node_children(tuple
, res
, &line_num
)) {
771 /* The tuple is illegal -- if the number is neither TERMINAL nor
772 * NONTERMINAL, we can't use it. Not sure the implementation
773 * allows this condition, but the API doesn't preclude it.
775 PyErr_SetObject(parser_error
,
776 Py_BuildValue("os", tuple
,
777 "Illegal component tuple."));
784 * Validation routines used within the validation section:
786 staticforward
int validate_terminal(node
*terminal
, int type
, char *string
);
788 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
789 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
790 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
791 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
792 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
793 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
794 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
795 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
796 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
797 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
798 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
799 #define validate_star(ch) validate_terminal(ch, STAR, "*")
800 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
801 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
802 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
803 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
805 #define VALIDATER(n) static int validate_##n(node *tree)
807 VALIDATER(node
); VALIDATER(small_stmt
);
808 VALIDATER(class); VALIDATER(node
);
809 VALIDATER(parameters
); VALIDATER(suite
);
810 VALIDATER(testlist
); VALIDATER(varargslist
);
811 VALIDATER(fpdef
); VALIDATER(fplist
);
812 VALIDATER(stmt
); VALIDATER(simple_stmt
);
813 VALIDATER(expr_stmt
); VALIDATER(power
);
814 VALIDATER(print_stmt
); VALIDATER(del_stmt
);
815 VALIDATER(return_stmt
); VALIDATER(list_iter
);
816 VALIDATER(raise_stmt
); VALIDATER(import_stmt
);
817 VALIDATER(global_stmt
); VALIDATER(list_if
);
818 VALIDATER(assert_stmt
); VALIDATER(list_for
);
819 VALIDATER(exec_stmt
); VALIDATER(compound_stmt
);
820 VALIDATER(while); VALIDATER(for);
821 VALIDATER(try); VALIDATER(except_clause
);
822 VALIDATER(test
); VALIDATER(and_test
);
823 VALIDATER(not_test
); VALIDATER(comparison
);
824 VALIDATER(comp_op
); VALIDATER(expr
);
825 VALIDATER(xor_expr
); VALIDATER(and_expr
);
826 VALIDATER(shift_expr
); VALIDATER(arith_expr
);
827 VALIDATER(term
); VALIDATER(factor
);
828 VALIDATER(atom
); VALIDATER(lambdef
);
829 VALIDATER(trailer
); VALIDATER(subscript
);
830 VALIDATER(subscriptlist
); VALIDATER(sliceop
);
831 VALIDATER(exprlist
); VALIDATER(dictmaker
);
832 VALIDATER(arglist
); VALIDATER(argument
);
833 VALIDATER(listmaker
);
837 #define is_even(n) (((n) & 1) == 0)
838 #define is_odd(n) (((n) & 1) == 1)
842 validate_ntype(node
*n
, int t
)
845 PyErr_Format(parser_error
, "Expected node type %d, got %d.",
853 /* Verifies that the number of child nodes is exactly 'num', raising
854 * an exception if it isn't. The exception message does not indicate
855 * the exact number of nodes, allowing this to be used to raise the
856 * "right" exception when the wrong number of nodes is present in a
857 * specific variant of a statement's syntax. This is commonly used
861 validate_numnodes(node
*n
, int num
, const char *const name
)
864 PyErr_Format(parser_error
,
865 "Illegal number of children for %s node.", name
);
873 validate_terminal(node
*terminal
, int type
, char *string
)
875 int res
= (validate_ntype(terminal
, type
)
876 && ((string
== 0) || (strcmp(string
, STR(terminal
)) == 0)));
878 if (!res
&& !PyErr_Occurred()) {
879 PyErr_Format(parser_error
,
880 "Illegal terminal: expected \"%s\"", string
);
889 validate_repeating_list(node
*tree
, int ntype
, int (*vfunc
)(node
*),
890 const char *const name
)
893 int res
= (nch
&& validate_ntype(tree
, ntype
)
894 && vfunc(CHILD(tree
, 0)));
896 if (!res
&& !PyErr_Occurred())
897 (void) validate_numnodes(tree
, 1, name
);
900 res
= validate_comma(CHILD(tree
, --nch
));
901 if (res
&& nch
> 1) {
903 for ( ; res
&& pos
< nch
; pos
+= 2)
904 res
= (validate_comma(CHILD(tree
, pos
))
905 && vfunc(CHILD(tree
, pos
+ 1)));
915 * 'class' NAME ['(' testlist ')'] ':' suite
918 validate_class(node
*tree
)
921 int res
= validate_ntype(tree
, classdef
) && ((nch
== 4) || (nch
== 7));
924 res
= (validate_name(CHILD(tree
, 0), "class")
925 && validate_ntype(CHILD(tree
, 1), NAME
)
926 && validate_colon(CHILD(tree
, nch
- 2))
927 && validate_suite(CHILD(tree
, nch
- 1)));
930 (void) validate_numnodes(tree
, 4, "class");
931 if (res
&& (nch
== 7)) {
932 res
= (validate_lparen(CHILD(tree
, 2))
933 && validate_testlist(CHILD(tree
, 3))
934 && validate_rparen(CHILD(tree
, 4)));
941 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
944 validate_if(node
*tree
)
947 int res
= (validate_ntype(tree
, if_stmt
)
949 && validate_name(CHILD(tree
, 0), "if")
950 && validate_test(CHILD(tree
, 1))
951 && validate_colon(CHILD(tree
, 2))
952 && validate_suite(CHILD(tree
, 3)));
954 if (res
&& ((nch
% 4) == 3)) {
955 /* ... 'else' ':' suite */
956 res
= (validate_name(CHILD(tree
, nch
- 3), "else")
957 && validate_colon(CHILD(tree
, nch
- 2))
958 && validate_suite(CHILD(tree
, nch
- 1)));
961 else if (!res
&& !PyErr_Occurred())
962 (void) validate_numnodes(tree
, 4, "if");
964 /* Will catch the case for nch < 4 */
965 res
= validate_numnodes(tree
, 0, "if");
966 else if (res
&& (nch
> 4)) {
967 /* ... ('elif' test ':' suite)+ ... */
969 while ((j
< nch
) && res
) {
970 res
= (validate_name(CHILD(tree
, j
), "elif")
971 && validate_colon(CHILD(tree
, j
+ 2))
972 && validate_test(CHILD(tree
, j
+ 1))
973 && validate_suite(CHILD(tree
, j
+ 3)));
982 * '(' [varargslist] ')'
986 validate_parameters(node
*tree
)
989 int res
= validate_ntype(tree
, parameters
) && ((nch
== 2) || (nch
== 3));
992 res
= (validate_lparen(CHILD(tree
, 0))
993 && validate_rparen(CHILD(tree
, nch
- 1)));
994 if (res
&& (nch
== 3))
995 res
= validate_varargslist(CHILD(tree
, 1));
998 (void) validate_numnodes(tree
, 2, "parameters");
1008 * | NEWLINE INDENT stmt+ DEDENT
1011 validate_suite(node
*tree
)
1013 int nch
= NCH(tree
);
1014 int res
= (validate_ntype(tree
, suite
) && ((nch
== 1) || (nch
>= 4)));
1016 if (res
&& (nch
== 1))
1017 res
= validate_simple_stmt(CHILD(tree
, 0));
1019 /* NEWLINE INDENT stmt+ DEDENT */
1020 res
= (validate_newline(CHILD(tree
, 0))
1021 && validate_indent(CHILD(tree
, 1))
1022 && validate_stmt(CHILD(tree
, 2))
1023 && validate_dedent(CHILD(tree
, nch
- 1)));
1025 if (res
&& (nch
> 4)) {
1027 --nch
; /* forget the DEDENT */
1028 for ( ; res
&& (i
< nch
); ++i
)
1029 res
= validate_stmt(CHILD(tree
, i
));
1032 res
= validate_numnodes(tree
, 4, "suite");
1039 validate_testlist(node
*tree
)
1041 return (validate_repeating_list(tree
, testlist
,
1042 validate_test
, "testlist"));
1046 /* '*' NAME [',' '**' NAME] | '**' NAME
1049 validate_varargslist_trailer(node
*tree
, int start
)
1051 int nch
= NCH(tree
);
1056 err_string("expected variable argument trailer for varargslist");
1059 sym
= TYPE(CHILD(tree
, start
));
1062 * ('*' NAME [',' '**' NAME]
1065 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1066 else if (nch
-start
== 5)
1067 res
= (validate_name(CHILD(tree
, start
+1), NULL
)
1068 && validate_comma(CHILD(tree
, start
+2))
1069 && validate_doublestar(CHILD(tree
, start
+3))
1070 && validate_name(CHILD(tree
, start
+4), NULL
));
1072 else if (sym
== DOUBLESTAR
) {
1077 res
= validate_name(CHILD(tree
, start
+1), NULL
);
1080 err_string("illegal variable argument trailer for varargslist");
1085 /* validate_varargslist()
1088 * (fpdef ['=' test] ',')*
1089 * ('*' NAME [',' '**' NAME]
1091 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
1095 validate_varargslist(node
*tree
)
1097 int nch
= NCH(tree
);
1098 int res
= validate_ntype(tree
, varargslist
) && (nch
!= 0);
1104 err_string("varargslist missing child nodes");
1107 sym
= TYPE(CHILD(tree
, 0));
1108 if (sym
== STAR
|| sym
== DOUBLESTAR
)
1109 /* whole thing matches:
1110 * '*' NAME [',' '**' NAME] | '**' NAME
1112 res
= validate_varargslist_trailer(tree
, 0);
1113 else if (sym
== fpdef
) {
1116 sym
= TYPE(CHILD(tree
, nch
-1));
1119 * (fpdef ['=' test] ',')+
1120 * ('*' NAME [',' '**' NAME]
1123 /* skip over (fpdef ['=' test] ',')+ */
1124 while (res
&& (i
+2 <= nch
)) {
1125 res
= validate_fpdef(CHILD(tree
, i
));
1127 if (res
&& TYPE(CHILD(tree
, i
)) == EQUAL
&& (i
+2 <= nch
)) {
1128 res
= (validate_equal(CHILD(tree
, i
))
1129 && validate_test(CHILD(tree
, i
+1)));
1133 if (res
&& i
< nch
) {
1134 res
= validate_comma(CHILD(tree
, i
));
1137 && (TYPE(CHILD(tree
, i
)) == DOUBLESTAR
1138 || TYPE(CHILD(tree
, i
)) == STAR
))
1142 /* ... '*' NAME [',' '**' NAME] | '**' NAME
1146 res
= validate_varargslist_trailer(tree
, i
);
1150 * fpdef ['=' test] (',' fpdef ['=' test])* [',']
1152 /* strip trailing comma node */
1154 res
= validate_comma(CHILD(tree
, nch
-1));
1160 * fpdef ['=' test] (',' fpdef ['=' test])*
1162 res
= validate_fpdef(CHILD(tree
, 0));
1164 if (res
&& (i
+2 <= nch
) && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1165 res
= (validate_equal(CHILD(tree
, i
))
1166 && validate_test(CHILD(tree
, i
+1)));
1170 * ... (',' fpdef ['=' test])*
1173 while (res
&& (nch
- i
) >= 2) {
1174 res
= (validate_comma(CHILD(tree
, i
))
1175 && validate_fpdef(CHILD(tree
, i
+1)));
1177 if (res
&& (nch
- i
) >= 2 && TYPE(CHILD(tree
, i
)) == EQUAL
) {
1178 res
= (validate_equal(CHILD(tree
, i
))
1179 && validate_test(CHILD(tree
, i
+1)));
1183 if (res
&& nch
- i
!= 0) {
1185 err_string("illegal formation for varargslist");
1193 /* list_iter: list_for | list_if
1196 validate_list_iter(node
*tree
)
1198 int res
= (validate_ntype(tree
, list_iter
)
1199 && validate_numnodes(tree
, 1, "list_iter"));
1200 if (res
&& TYPE(CHILD(tree
, 0)) == list_for
)
1201 res
= validate_list_for(CHILD(tree
, 0));
1203 res
= validate_list_if(CHILD(tree
, 0));
1208 /* list_for: 'for' exprlist 'in' testlist [list_iter]
1211 validate_list_for(node
*tree
)
1213 int nch
= NCH(tree
);
1217 res
= validate_list_iter(CHILD(tree
, 4));
1219 res
= validate_numnodes(tree
, 4, "list_for");
1222 res
= (validate_name(CHILD(tree
, 0), "for")
1223 && validate_exprlist(CHILD(tree
, 1))
1224 && validate_name(CHILD(tree
, 2), "in")
1225 && validate_testlist(CHILD(tree
, 3)));
1230 /* list_if: 'if' test [list_iter]
1233 validate_list_if(node
*tree
)
1235 int nch
= NCH(tree
);
1239 res
= validate_list_iter(CHILD(tree
, 2));
1241 res
= validate_numnodes(tree
, 2, "list_if");
1244 res
= (validate_name(CHILD(tree
, 0), "if")
1245 && validate_test(CHILD(tree
, 1)));
1258 validate_fpdef(node
*tree
)
1260 int nch
= NCH(tree
);
1261 int res
= validate_ntype(tree
, fpdef
);
1265 res
= validate_ntype(CHILD(tree
, 0), NAME
);
1267 res
= (validate_lparen(CHILD(tree
, 0))
1268 && validate_fplist(CHILD(tree
, 1))
1269 && validate_rparen(CHILD(tree
, 2)));
1271 res
= validate_numnodes(tree
, 1, "fpdef");
1278 validate_fplist(node
*tree
)
1280 return (validate_repeating_list(tree
, fplist
,
1281 validate_fpdef
, "fplist"));
1285 /* simple_stmt | compound_stmt
1289 validate_stmt(node
*tree
)
1291 int res
= (validate_ntype(tree
, stmt
)
1292 && validate_numnodes(tree
, 1, "stmt"));
1295 tree
= CHILD(tree
, 0);
1297 if (TYPE(tree
) == simple_stmt
)
1298 res
= validate_simple_stmt(tree
);
1300 res
= validate_compound_stmt(tree
);
1306 /* small_stmt (';' small_stmt)* [';'] NEWLINE
1310 validate_simple_stmt(node
*tree
)
1312 int nch
= NCH(tree
);
1313 int res
= (validate_ntype(tree
, simple_stmt
)
1315 && validate_small_stmt(CHILD(tree
, 0))
1316 && validate_newline(CHILD(tree
, nch
- 1)));
1319 res
= validate_numnodes(tree
, 2, "simple_stmt");
1320 --nch
; /* forget the NEWLINE */
1321 if (res
&& is_even(nch
))
1322 res
= validate_semi(CHILD(tree
, --nch
));
1323 if (res
&& (nch
> 2)) {
1326 for (i
= 1; res
&& (i
< nch
); i
+= 2)
1327 res
= (validate_semi(CHILD(tree
, i
))
1328 && validate_small_stmt(CHILD(tree
, i
+ 1)));
1335 validate_small_stmt(node
*tree
)
1337 int nch
= NCH(tree
);
1338 int res
= validate_numnodes(tree
, 1, "small_stmt");
1341 int ntype
= TYPE(CHILD(tree
, 0));
1343 if ( (ntype
== expr_stmt
)
1344 || (ntype
== print_stmt
)
1345 || (ntype
== del_stmt
)
1346 || (ntype
== pass_stmt
)
1347 || (ntype
== flow_stmt
)
1348 || (ntype
== import_stmt
)
1349 || (ntype
== global_stmt
)
1350 || (ntype
== assert_stmt
)
1351 || (ntype
== exec_stmt
))
1352 res
= validate_node(CHILD(tree
, 0));
1355 err_string("illegal small_stmt child type");
1358 else if (nch
== 1) {
1360 PyErr_Format(parser_error
,
1361 "Unrecognized child node of small_stmt: %d.",
1362 TYPE(CHILD(tree
, 0)));
1369 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1372 validate_compound_stmt(node
*tree
)
1374 int res
= (validate_ntype(tree
, compound_stmt
)
1375 && validate_numnodes(tree
, 1, "compound_stmt"));
1381 tree
= CHILD(tree
, 0);
1383 if ( (ntype
== if_stmt
)
1384 || (ntype
== while_stmt
)
1385 || (ntype
== for_stmt
)
1386 || (ntype
== try_stmt
)
1387 || (ntype
== funcdef
)
1388 || (ntype
== classdef
))
1389 res
= validate_node(tree
);
1392 PyErr_Format(parser_error
,
1393 "Illegal compound statement type: %d.", TYPE(tree
));
1400 validate_expr_stmt(node
*tree
)
1403 int nch
= NCH(tree
);
1404 int res
= (validate_ntype(tree
, expr_stmt
)
1406 && validate_testlist(CHILD(tree
, 0)));
1409 && TYPE(CHILD(tree
, 1)) == augassign
) {
1410 res
= (validate_numnodes(CHILD(tree
, 1), 1, "augassign")
1411 && validate_testlist(CHILD(tree
, 2)));
1414 char *s
= STR(CHILD(CHILD(tree
, 1), 0));
1416 res
= (strcmp(s
, "+=") == 0
1417 || strcmp(s
, "-=") == 0
1418 || strcmp(s
, "*=") == 0
1419 || strcmp(s
, "/=") == 0
1420 || strcmp(s
, "%=") == 0
1421 || strcmp(s
, "&=") == 0
1422 || strcmp(s
, "|=") == 0
1423 || strcmp(s
, "^=") == 0
1424 || strcmp(s
, "<<=") == 0
1425 || strcmp(s
, ">>=") == 0
1426 || strcmp(s
, "**=") == 0);
1428 err_string("illegal augmmented assignment operator");
1432 for (j
= 1; res
&& (j
< nch
); j
+= 2)
1433 res
= (validate_equal(CHILD(tree
, j
))
1434 && validate_testlist(CHILD(tree
, j
+ 1)));
1442 * 'print' ( [ test (',' test)* [','] ]
1443 * | '>>' test [ (',' test)+ [','] ] )
1446 validate_print_stmt(node
*tree
)
1448 int nch
= NCH(tree
);
1449 int res
= (validate_ntype(tree
, print_stmt
)
1451 && validate_name(CHILD(tree
, 0), "print"));
1453 if (res
&& nch
> 1) {
1454 int sym
= TYPE(CHILD(tree
, 1));
1456 int allow_trailing_comma
= 1;
1459 res
= validate_test(CHILD(tree
, i
++));
1462 res
= validate_numnodes(tree
, 3, "print_stmt");
1464 res
= (validate_ntype(CHILD(tree
, i
), RIGHTSHIFT
)
1465 && validate_test(CHILD(tree
, i
+1)));
1467 allow_trailing_comma
= 0;
1471 /* ... (',' test)* [','] */
1472 while (res
&& i
+2 <= nch
) {
1473 res
= (validate_comma(CHILD(tree
, i
))
1474 && validate_test(CHILD(tree
, i
+1)));
1475 allow_trailing_comma
= 1;
1478 if (res
&& !allow_trailing_comma
)
1479 res
= validate_numnodes(tree
, i
, "print_stmt");
1480 else if (res
&& i
< nch
)
1481 res
= validate_comma(CHILD(tree
, i
));
1489 validate_del_stmt(node
*tree
)
1491 return (validate_numnodes(tree
, 2, "del_stmt")
1492 && validate_name(CHILD(tree
, 0), "del")
1493 && validate_exprlist(CHILD(tree
, 1)));
1498 validate_return_stmt(node
*tree
)
1500 int nch
= NCH(tree
);
1501 int res
= (validate_ntype(tree
, return_stmt
)
1502 && ((nch
== 1) || (nch
== 2))
1503 && validate_name(CHILD(tree
, 0), "return"));
1505 if (res
&& (nch
== 2))
1506 res
= validate_testlist(CHILD(tree
, 1));
1513 validate_raise_stmt(node
*tree
)
1515 int nch
= NCH(tree
);
1516 int res
= (validate_ntype(tree
, raise_stmt
)
1517 && ((nch
== 1) || (nch
== 2) || (nch
== 4) || (nch
== 6)));
1520 res
= validate_name(CHILD(tree
, 0), "raise");
1521 if (res
&& (nch
>= 2))
1522 res
= validate_test(CHILD(tree
, 1));
1523 if (res
&& nch
> 2) {
1524 res
= (validate_comma(CHILD(tree
, 2))
1525 && validate_test(CHILD(tree
, 3)));
1526 if (res
&& (nch
> 4))
1527 res
= (validate_comma(CHILD(tree
, 4))
1528 && validate_test(CHILD(tree
, 5)));
1532 (void) validate_numnodes(tree
, 2, "raise");
1533 if (res
&& (nch
== 4))
1534 res
= (validate_comma(CHILD(tree
, 2))
1535 && validate_test(CHILD(tree
, 3)));
1542 validate_import_as_name(node
*tree
)
1544 int nch
= NCH(tree
);
1545 int ok
= validate_ntype(tree
, import_as_name
);
1549 ok
= validate_name(CHILD(tree
, 0), NULL
);
1551 ok
= (validate_name(CHILD(tree
, 0), NULL
)
1552 && validate_name(CHILD(tree
, 1), "as")
1553 && validate_name(CHILD(tree
, 2), NULL
));
1555 ok
= validate_numnodes(tree
, 3, "import_as_name");
1561 /* dotted_as_name: dotted_name [NAME NAME]
1564 validate_dotted_as_name(node
*tree
)
1566 int nch
= NCH(tree
);
1567 int res
= validate_ntype(tree
, dotted_as_name
);
1571 res
= validate_ntype(CHILD(tree
, 0), dotted_name
);
1573 res
= (validate_ntype(CHILD(tree
, 0), dotted_name
)
1574 && validate_name(CHILD(tree
, 1), "as")
1575 && validate_name(CHILD(tree
, 2), NULL
));
1578 err_string("illegal number of children for dotted_as_name");
1587 * 'import' dotted_as_name (',' dotted_as_name)*
1588 * | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
1591 validate_import_stmt(node
*tree
)
1593 int nch
= NCH(tree
);
1594 int res
= (validate_ntype(tree
, import_stmt
)
1595 && (nch
>= 2) && is_even(nch
)
1596 && validate_ntype(CHILD(tree
, 0), NAME
));
1598 if (res
&& (strcmp(STR(CHILD(tree
, 0)), "import") == 0)) {
1601 res
= validate_dotted_as_name(CHILD(tree
, 1));
1602 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1603 res
= (validate_comma(CHILD(tree
, j
))
1604 && validate_ntype(CHILD(tree
, j
+ 1), dotted_name
));
1606 else if (res
&& (res
= validate_name(CHILD(tree
, 0), "from"))) {
1607 res
= ((nch
>= 4) && is_even(nch
)
1608 && validate_name(CHILD(tree
, 2), "import")
1609 && validate_dotted_as_name(CHILD(tree
, 1)));
1611 if (TYPE(CHILD(tree
, 3)) == import_as_name
)
1612 res
= validate_import_as_name(CHILD(tree
, 3));
1614 res
= validate_star(CHILD(tree
, 3));
1617 /* 'from' dotted_name 'import' import_as_name
1618 * (',' import_as_name)+
1621 res
= validate_import_as_name(CHILD(tree
, 3));
1622 for (j
= 4; res
&& (j
< nch
); j
+= 2)
1623 res
= (validate_comma(CHILD(tree
, j
))
1624 && validate_import_as_name(CHILD(tree
, j
+ 1)));
1635 validate_global_stmt(node
*tree
)
1638 int nch
= NCH(tree
);
1639 int res
= (validate_ntype(tree
, global_stmt
)
1640 && is_even(nch
) && (nch
>= 2));
1643 res
= (validate_name(CHILD(tree
, 0), "global")
1644 && validate_ntype(CHILD(tree
, 1), NAME
));
1645 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1646 res
= (validate_comma(CHILD(tree
, j
))
1647 && validate_ntype(CHILD(tree
, j
+ 1), NAME
));
1655 * 'exec' expr ['in' test [',' test]]
1658 validate_exec_stmt(node
*tree
)
1660 int nch
= NCH(tree
);
1661 int res
= (validate_ntype(tree
, exec_stmt
)
1662 && ((nch
== 2) || (nch
== 4) || (nch
== 6))
1663 && validate_name(CHILD(tree
, 0), "exec")
1664 && validate_expr(CHILD(tree
, 1)));
1666 if (!res
&& !PyErr_Occurred())
1667 err_string("illegal exec statement");
1668 if (res
&& (nch
> 2))
1669 res
= (validate_name(CHILD(tree
, 2), "in")
1670 && validate_test(CHILD(tree
, 3)));
1671 if (res
&& (nch
== 6))
1672 res
= (validate_comma(CHILD(tree
, 4))
1673 && validate_test(CHILD(tree
, 5)));
1681 * 'assert' test [',' test]
1684 validate_assert_stmt(node
*tree
)
1686 int nch
= NCH(tree
);
1687 int res
= (validate_ntype(tree
, assert_stmt
)
1688 && ((nch
== 2) || (nch
== 4))
1689 && (validate_name(CHILD(tree
, 0), "__assert__") ||
1690 validate_name(CHILD(tree
, 0), "assert"))
1691 && validate_test(CHILD(tree
, 1)));
1693 if (!res
&& !PyErr_Occurred())
1694 err_string("illegal assert statement");
1695 if (res
&& (nch
> 2))
1696 res
= (validate_comma(CHILD(tree
, 2))
1697 && validate_test(CHILD(tree
, 3)));
1704 validate_while(node
*tree
)
1706 int nch
= NCH(tree
);
1707 int res
= (validate_ntype(tree
, while_stmt
)
1708 && ((nch
== 4) || (nch
== 7))
1709 && validate_name(CHILD(tree
, 0), "while")
1710 && validate_test(CHILD(tree
, 1))
1711 && validate_colon(CHILD(tree
, 2))
1712 && validate_suite(CHILD(tree
, 3)));
1714 if (res
&& (nch
== 7))
1715 res
= (validate_name(CHILD(tree
, 4), "else")
1716 && validate_colon(CHILD(tree
, 5))
1717 && validate_suite(CHILD(tree
, 6)));
1724 validate_for(node
*tree
)
1726 int nch
= NCH(tree
);
1727 int res
= (validate_ntype(tree
, for_stmt
)
1728 && ((nch
== 6) || (nch
== 9))
1729 && validate_name(CHILD(tree
, 0), "for")
1730 && validate_exprlist(CHILD(tree
, 1))
1731 && validate_name(CHILD(tree
, 2), "in")
1732 && validate_testlist(CHILD(tree
, 3))
1733 && validate_colon(CHILD(tree
, 4))
1734 && validate_suite(CHILD(tree
, 5)));
1736 if (res
&& (nch
== 9))
1737 res
= (validate_name(CHILD(tree
, 6), "else")
1738 && validate_colon(CHILD(tree
, 7))
1739 && validate_suite(CHILD(tree
, 8)));
1746 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1747 * | 'try' ':' suite 'finally' ':' suite
1751 validate_try(node
*tree
)
1753 int nch
= NCH(tree
);
1755 int res
= (validate_ntype(tree
, try_stmt
)
1756 && (nch
>= 6) && ((nch
% 3) == 0));
1759 res
= (validate_name(CHILD(tree
, 0), "try")
1760 && validate_colon(CHILD(tree
, 1))
1761 && validate_suite(CHILD(tree
, 2))
1762 && validate_colon(CHILD(tree
, nch
- 2))
1763 && validate_suite(CHILD(tree
, nch
- 1)));
1764 else if (!PyErr_Occurred()) {
1765 const char* name
= "except";
1766 if (TYPE(CHILD(tree
, nch
- 3)) != except_clause
)
1767 name
= STR(CHILD(tree
, nch
- 3));
1769 PyErr_Format(parser_error
,
1770 "Illegal number of children for try/%s node.", name
);
1772 /* Skip past except_clause sections: */
1773 while (res
&& (TYPE(CHILD(tree
, pos
)) == except_clause
)) {
1774 res
= (validate_except_clause(CHILD(tree
, pos
))
1775 && validate_colon(CHILD(tree
, pos
+ 1))
1776 && validate_suite(CHILD(tree
, pos
+ 2)));
1779 if (res
&& (pos
< nch
)) {
1780 res
= validate_ntype(CHILD(tree
, pos
), NAME
);
1781 if (res
&& (strcmp(STR(CHILD(tree
, pos
)), "finally") == 0))
1782 res
= (validate_numnodes(tree
, 6, "try/finally")
1783 && validate_colon(CHILD(tree
, 4))
1784 && validate_suite(CHILD(tree
, 5)));
1786 if (nch
== (pos
+ 3)) {
1787 res
= ((strcmp(STR(CHILD(tree
, pos
)), "except") == 0)
1788 || (strcmp(STR(CHILD(tree
, pos
)), "else") == 0));
1790 err_string("illegal trailing triple in try statement");
1792 else if (nch
== (pos
+ 6)) {
1793 res
= (validate_name(CHILD(tree
, pos
), "except")
1794 && validate_colon(CHILD(tree
, pos
+ 1))
1795 && validate_suite(CHILD(tree
, pos
+ 2))
1796 && validate_name(CHILD(tree
, pos
+ 3), "else"));
1799 res
= validate_numnodes(tree
, pos
+ 3, "try/except");
1807 validate_except_clause(node
*tree
)
1809 int nch
= NCH(tree
);
1810 int res
= (validate_ntype(tree
, except_clause
)
1811 && ((nch
== 1) || (nch
== 2) || (nch
== 4))
1812 && validate_name(CHILD(tree
, 0), "except"));
1814 if (res
&& (nch
> 1))
1815 res
= validate_test(CHILD(tree
, 1));
1816 if (res
&& (nch
== 4))
1817 res
= (validate_comma(CHILD(tree
, 2))
1818 && validate_test(CHILD(tree
, 3)));
1825 validate_test(node
*tree
)
1827 int nch
= NCH(tree
);
1828 int res
= validate_ntype(tree
, test
) && is_odd(nch
);
1830 if (res
&& (TYPE(CHILD(tree
, 0)) == lambdef
))
1832 && validate_lambdef(CHILD(tree
, 0)));
1835 res
= validate_and_test(CHILD(tree
, 0));
1836 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
1837 res
= (validate_name(CHILD(tree
, pos
), "or")
1838 && validate_and_test(CHILD(tree
, pos
+ 1)));
1845 validate_and_test(node
*tree
)
1848 int nch
= NCH(tree
);
1849 int res
= (validate_ntype(tree
, and_test
)
1851 && validate_not_test(CHILD(tree
, 0)));
1853 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
1854 res
= (validate_name(CHILD(tree
, pos
), "and")
1855 && validate_not_test(CHILD(tree
, 0)));
1862 validate_not_test(node
*tree
)
1864 int nch
= NCH(tree
);
1865 int res
= validate_ntype(tree
, not_test
) && ((nch
== 1) || (nch
== 2));
1869 res
= (validate_name(CHILD(tree
, 0), "not")
1870 && validate_not_test(CHILD(tree
, 1)));
1872 res
= validate_comparison(CHILD(tree
, 0));
1879 validate_comparison(node
*tree
)
1882 int nch
= NCH(tree
);
1883 int res
= (validate_ntype(tree
, comparison
)
1885 && validate_expr(CHILD(tree
, 0)));
1887 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
1888 res
= (validate_comp_op(CHILD(tree
, pos
))
1889 && validate_expr(CHILD(tree
, pos
+ 1)));
1896 validate_comp_op(node
*tree
)
1899 int nch
= NCH(tree
);
1901 if (!validate_ntype(tree
, comp_op
))
1905 * Only child will be a terminal with a well-defined symbolic name
1906 * or a NAME with a string of either 'is' or 'in'
1908 tree
= CHILD(tree
, 0);
1909 switch (TYPE(tree
)) {
1920 res
= ((strcmp(STR(tree
), "in") == 0)
1921 || (strcmp(STR(tree
), "is") == 0));
1923 PyErr_Format(parser_error
,
1924 "illegal operator '%s'", STR(tree
));
1928 err_string("illegal comparison operator type");
1932 else if ((res
= validate_numnodes(tree
, 2, "comp_op")) != 0) {
1933 res
= (validate_ntype(CHILD(tree
, 0), NAME
)
1934 && validate_ntype(CHILD(tree
, 1), NAME
)
1935 && (((strcmp(STR(CHILD(tree
, 0)), "is") == 0)
1936 && (strcmp(STR(CHILD(tree
, 1)), "not") == 0))
1937 || ((strcmp(STR(CHILD(tree
, 0)), "not") == 0)
1938 && (strcmp(STR(CHILD(tree
, 1)), "in") == 0))));
1939 if (!res
&& !PyErr_Occurred())
1940 err_string("unknown comparison operator");
1947 validate_expr(node
*tree
)
1950 int nch
= NCH(tree
);
1951 int res
= (validate_ntype(tree
, expr
)
1953 && validate_xor_expr(CHILD(tree
, 0)));
1955 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1956 res
= (validate_xor_expr(CHILD(tree
, j
))
1957 && validate_vbar(CHILD(tree
, j
- 1)));
1964 validate_xor_expr(node
*tree
)
1967 int nch
= NCH(tree
);
1968 int res
= (validate_ntype(tree
, xor_expr
)
1970 && validate_and_expr(CHILD(tree
, 0)));
1972 for (j
= 2; res
&& (j
< nch
); j
+= 2)
1973 res
= (validate_circumflex(CHILD(tree
, j
- 1))
1974 && validate_and_expr(CHILD(tree
, j
)));
1981 validate_and_expr(node
*tree
)
1984 int nch
= NCH(tree
);
1985 int res
= (validate_ntype(tree
, and_expr
)
1987 && validate_shift_expr(CHILD(tree
, 0)));
1989 for (pos
= 1; res
&& (pos
< nch
); pos
+= 2)
1990 res
= (validate_ampersand(CHILD(tree
, pos
))
1991 && validate_shift_expr(CHILD(tree
, pos
+ 1)));
1998 validate_chain_two_ops(node
*tree
, int (*termvalid
)(node
*), int op1
, int op2
)
2001 int nch
= NCH(tree
);
2002 int res
= (is_odd(nch
)
2003 && (*termvalid
)(CHILD(tree
, 0)));
2005 for ( ; res
&& (pos
< nch
); pos
+= 2) {
2006 if (TYPE(CHILD(tree
, pos
)) != op1
)
2007 res
= validate_ntype(CHILD(tree
, pos
), op2
);
2009 res
= (*termvalid
)(CHILD(tree
, pos
+ 1));
2016 validate_shift_expr(node
*tree
)
2018 return (validate_ntype(tree
, shift_expr
)
2019 && validate_chain_two_ops(tree
, validate_arith_expr
,
2020 LEFTSHIFT
, RIGHTSHIFT
));
2025 validate_arith_expr(node
*tree
)
2027 return (validate_ntype(tree
, arith_expr
)
2028 && validate_chain_two_ops(tree
, validate_term
, PLUS
, MINUS
));
2033 validate_term(node
*tree
)
2036 int nch
= NCH(tree
);
2037 int res
= (validate_ntype(tree
, term
)
2039 && validate_factor(CHILD(tree
, 0)));
2041 for ( ; res
&& (pos
< nch
); pos
+= 2)
2042 res
= (((TYPE(CHILD(tree
, pos
)) == STAR
)
2043 || (TYPE(CHILD(tree
, pos
)) == SLASH
)
2044 || (TYPE(CHILD(tree
, pos
)) == PERCENT
))
2045 && validate_factor(CHILD(tree
, pos
+ 1)));
2053 * factor: ('+'|'-'|'~') factor | power
2056 validate_factor(node
*tree
)
2058 int nch
= NCH(tree
);
2059 int res
= (validate_ntype(tree
, factor
)
2061 && ((TYPE(CHILD(tree
, 0)) == PLUS
)
2062 || (TYPE(CHILD(tree
, 0)) == MINUS
)
2063 || (TYPE(CHILD(tree
, 0)) == TILDE
))
2064 && validate_factor(CHILD(tree
, 1)))
2066 && validate_power(CHILD(tree
, 0)))));
2073 * power: atom trailer* ('**' factor)*
2076 validate_power(node
*tree
)
2079 int nch
= NCH(tree
);
2080 int res
= (validate_ntype(tree
, power
) && (nch
>= 1)
2081 && validate_atom(CHILD(tree
, 0)));
2083 while (res
&& (pos
< nch
) && (TYPE(CHILD(tree
, pos
)) == trailer
))
2084 res
= validate_trailer(CHILD(tree
, pos
++));
2085 if (res
&& (pos
< nch
)) {
2086 if (!is_even(nch
- pos
)) {
2087 err_string("illegal number of nodes for 'power'");
2090 for ( ; res
&& (pos
< (nch
- 1)); pos
+= 2)
2091 res
= (validate_doublestar(CHILD(tree
, pos
))
2092 && validate_factor(CHILD(tree
, pos
+ 1)));
2099 validate_atom(node
*tree
)
2102 int nch
= NCH(tree
);
2103 int res
= validate_ntype(tree
, atom
);
2106 res
= validate_numnodes(tree
, nch
+1, "atom");
2108 switch (TYPE(CHILD(tree
, 0))) {
2111 && (validate_rparen(CHILD(tree
, nch
- 1))));
2113 if (res
&& (nch
== 3))
2114 res
= validate_testlist(CHILD(tree
, 1));
2118 res
= validate_ntype(CHILD(tree
, 1), RSQB
);
2120 res
= (validate_listmaker(CHILD(tree
, 1))
2121 && validate_ntype(CHILD(tree
, 2), RSQB
));
2124 err_string("illegal list display atom");
2129 && validate_ntype(CHILD(tree
, nch
- 1), RBRACE
));
2131 if (res
&& (nch
== 3))
2132 res
= validate_dictmaker(CHILD(tree
, 1));
2136 && validate_testlist(CHILD(tree
, 1))
2137 && validate_ntype(CHILD(tree
, 2), BACKQUOTE
));
2144 for (pos
= 1; res
&& (pos
< nch
); ++pos
)
2145 res
= validate_ntype(CHILD(tree
, pos
), STRING
);
2157 * test ( list_for | (',' test)* [','] )
2160 validate_listmaker(node
*tree
)
2162 int nch
= NCH(tree
);
2166 err_string("missing child nodes of listmaker");
2168 ok
= validate_test(CHILD(tree
, 0));
2171 * list_iter | (',' test)* [',']
2173 if (nch
== 2 && TYPE(CHILD(tree
, 1)) == list_for
)
2174 ok
= validate_list_for(CHILD(tree
, 1));
2176 /* (',' test)* [','] */
2178 while (ok
&& nch
- i
>= 2) {
2179 ok
= (validate_comma(CHILD(tree
, i
))
2180 && validate_test(CHILD(tree
, i
+1)));
2183 if (ok
&& i
== nch
-1)
2184 ok
= validate_comma(CHILD(tree
, i
));
2185 else if (i
!= nch
) {
2187 err_string("illegal trailing nodes for listmaker");
2195 * 'def' NAME parameters ':' suite
2199 validate_funcdef(node
*tree
)
2201 return (validate_ntype(tree
, funcdef
)
2202 && validate_numnodes(tree
, 5, "funcdef")
2203 && validate_name(CHILD(tree
, 0), "def")
2204 && validate_ntype(CHILD(tree
, 1), NAME
)
2205 && validate_colon(CHILD(tree
, 3))
2206 && validate_parameters(CHILD(tree
, 2))
2207 && validate_suite(CHILD(tree
, 4)));
2212 validate_lambdef(node
*tree
)
2214 int nch
= NCH(tree
);
2215 int res
= (validate_ntype(tree
, lambdef
)
2216 && ((nch
== 3) || (nch
== 4))
2217 && validate_name(CHILD(tree
, 0), "lambda")
2218 && validate_colon(CHILD(tree
, nch
- 2))
2219 && validate_test(CHILD(tree
, nch
- 1)));
2221 if (res
&& (nch
== 4))
2222 res
= validate_varargslist(CHILD(tree
, 1));
2223 else if (!res
&& !PyErr_Occurred())
2224 (void) validate_numnodes(tree
, 3, "lambdef");
2232 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
2235 validate_arglist(node
*tree
)
2237 int nch
= NCH(tree
);
2242 /* raise the right error from having an invalid number of children */
2243 return validate_numnodes(tree
, nch
+ 1, "arglist");
2245 while (ok
&& nch
-i
>= 2) {
2246 /* skip leading (argument ',') */
2247 ok
= (validate_argument(CHILD(tree
, i
))
2248 && validate_comma(CHILD(tree
, i
+1)));
2257 * argument | '*' test [',' '**' test] | '**' test
2259 int sym
= TYPE(CHILD(tree
, i
));
2261 if (sym
== argument
) {
2262 ok
= validate_argument(CHILD(tree
, i
));
2263 if (ok
&& i
+1 != nch
) {
2264 err_string("illegal arglist specification"
2265 " (extra stuff on end)");
2269 else if (sym
== STAR
) {
2270 ok
= validate_star(CHILD(tree
, i
));
2271 if (ok
&& (nch
-i
== 2))
2272 ok
= validate_test(CHILD(tree
, i
+1));
2273 else if (ok
&& (nch
-i
== 5))
2274 ok
= (validate_test(CHILD(tree
, i
+1))
2275 && validate_comma(CHILD(tree
, i
+2))
2276 && validate_doublestar(CHILD(tree
, i
+3))
2277 && validate_test(CHILD(tree
, i
+4)));
2279 err_string("illegal use of '*' in arglist");
2283 else if (sym
== DOUBLESTAR
) {
2285 ok
= (validate_doublestar(CHILD(tree
, i
))
2286 && validate_test(CHILD(tree
, i
+1)));
2288 err_string("illegal use of '**' in arglist");
2293 err_string("illegal arglist specification");
2307 validate_argument(node
*tree
)
2309 int nch
= NCH(tree
);
2310 int res
= (validate_ntype(tree
, argument
)
2311 && ((nch
== 1) || (nch
== 3))
2312 && validate_test(CHILD(tree
, 0)));
2314 if (res
&& (nch
== 3))
2315 res
= (validate_equal(CHILD(tree
, 1))
2316 && validate_test(CHILD(tree
, 2)));
2325 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
2328 validate_trailer(node
*tree
)
2330 int nch
= NCH(tree
);
2331 int res
= validate_ntype(tree
, trailer
) && ((nch
== 2) || (nch
== 3));
2334 switch (TYPE(CHILD(tree
, 0))) {
2336 res
= validate_rparen(CHILD(tree
, nch
- 1));
2337 if (res
&& (nch
== 3))
2338 res
= validate_arglist(CHILD(tree
, 1));
2341 res
= (validate_numnodes(tree
, 3, "trailer")
2342 && validate_subscriptlist(CHILD(tree
, 1))
2343 && validate_ntype(CHILD(tree
, 2), RSQB
));
2346 res
= (validate_numnodes(tree
, 2, "trailer")
2347 && validate_ntype(CHILD(tree
, 1), NAME
));
2355 (void) validate_numnodes(tree
, 2, "trailer");
2363 * subscript (',' subscript)* [',']
2366 validate_subscriptlist(node
*tree
)
2368 return (validate_repeating_list(tree
, subscriptlist
,
2369 validate_subscript
, "subscriptlist"));
2375 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
2378 validate_subscript(node
*tree
)
2381 int nch
= NCH(tree
);
2382 int res
= validate_ntype(tree
, subscript
) && (nch
>= 1) && (nch
<= 4);
2385 if (!PyErr_Occurred())
2386 err_string("invalid number of arguments for subscript node");
2389 if (TYPE(CHILD(tree
, 0)) == DOT
)
2390 /* take care of ('.' '.' '.') possibility */
2391 return (validate_numnodes(tree
, 3, "subscript")
2392 && validate_dot(CHILD(tree
, 0))
2393 && validate_dot(CHILD(tree
, 1))
2394 && validate_dot(CHILD(tree
, 2)));
2396 if (TYPE(CHILD(tree
, 0)) == test
)
2397 res
= validate_test(CHILD(tree
, 0));
2399 res
= validate_colon(CHILD(tree
, 0));
2402 /* Must be [test] ':' [test] [sliceop],
2403 * but at least one of the optional components will
2404 * be present, but we don't know which yet.
2406 if ((TYPE(CHILD(tree
, 0)) != COLON
) || (nch
== 4)) {
2407 res
= validate_test(CHILD(tree
, 0));
2411 res
= validate_colon(CHILD(tree
, offset
));
2413 int rem
= nch
- ++offset
;
2415 if (TYPE(CHILD(tree
, offset
)) == test
) {
2416 res
= validate_test(CHILD(tree
, offset
));
2421 res
= validate_sliceop(CHILD(tree
, offset
));
2429 validate_sliceop(node
*tree
)
2431 int nch
= NCH(tree
);
2432 int res
= ((nch
== 1) || validate_numnodes(tree
, 2, "sliceop"))
2433 && validate_ntype(tree
, sliceop
);
2434 if (!res
&& !PyErr_Occurred()) {
2435 res
= validate_numnodes(tree
, 1, "sliceop");
2438 res
= validate_colon(CHILD(tree
, 0));
2439 if (res
&& (nch
== 2))
2440 res
= validate_test(CHILD(tree
, 1));
2447 validate_exprlist(node
*tree
)
2449 return (validate_repeating_list(tree
, exprlist
,
2450 validate_expr
, "exprlist"));
2455 validate_dictmaker(node
*tree
)
2457 int nch
= NCH(tree
);
2458 int res
= (validate_ntype(tree
, dictmaker
)
2460 && validate_test(CHILD(tree
, 0))
2461 && validate_colon(CHILD(tree
, 1))
2462 && validate_test(CHILD(tree
, 2)));
2464 if (res
&& ((nch
% 4) == 0))
2465 res
= validate_comma(CHILD(tree
, --nch
));
2467 res
= ((nch
% 4) == 3);
2469 if (res
&& (nch
> 3)) {
2471 /* ( ',' test ':' test )* */
2472 while (res
&& (pos
< nch
)) {
2473 res
= (validate_comma(CHILD(tree
, pos
))
2474 && validate_test(CHILD(tree
, pos
+ 1))
2475 && validate_colon(CHILD(tree
, pos
+ 2))
2476 && validate_test(CHILD(tree
, pos
+ 3)));
2485 validate_eval_input(node
*tree
)
2488 int nch
= NCH(tree
);
2489 int res
= (validate_ntype(tree
, eval_input
)
2491 && validate_testlist(CHILD(tree
, 0))
2492 && validate_ntype(CHILD(tree
, nch
- 1), ENDMARKER
));
2494 for (pos
= 1; res
&& (pos
< (nch
- 1)); ++pos
)
2495 res
= validate_ntype(CHILD(tree
, pos
), NEWLINE
);
2502 validate_node(node
*tree
)
2504 int nch
= 0; /* num. children on current node */
2505 int res
= 1; /* result value */
2506 node
* next
= 0; /* node to process after this one */
2508 while (res
& (tree
!= 0)) {
2511 switch (TYPE(tree
)) {
2516 res
= validate_funcdef(tree
);
2519 res
= validate_class(tree
);
2522 * "Trivial" parse tree nodes.
2523 * (Why did I call these trivial?)
2526 res
= validate_stmt(tree
);
2530 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
2531 * | import_stmt | global_stmt | exec_stmt | assert_stmt
2533 res
= validate_small_stmt(tree
);
2536 res
= (validate_numnodes(tree
, 1, "flow_stmt")
2537 && ((TYPE(CHILD(tree
, 0)) == break_stmt
)
2538 || (TYPE(CHILD(tree
, 0)) == continue_stmt
)
2539 || (TYPE(CHILD(tree
, 0)) == return_stmt
)
2540 || (TYPE(CHILD(tree
, 0)) == raise_stmt
)));
2542 next
= CHILD(tree
, 0);
2544 err_string("illegal flow_stmt type");
2547 * Compound statements.
2550 res
= validate_simple_stmt(tree
);
2553 res
= validate_compound_stmt(tree
);
2556 * Fundamental statements.
2559 res
= validate_expr_stmt(tree
);
2562 res
= validate_print_stmt(tree
);
2565 res
= validate_del_stmt(tree
);
2568 res
= (validate_numnodes(tree
, 1, "pass")
2569 && validate_name(CHILD(tree
, 0), "pass"));
2572 res
= (validate_numnodes(tree
, 1, "break")
2573 && validate_name(CHILD(tree
, 0), "break"));
2576 res
= (validate_numnodes(tree
, 1, "continue")
2577 && validate_name(CHILD(tree
, 0), "continue"));
2580 res
= validate_return_stmt(tree
);
2583 res
= validate_raise_stmt(tree
);
2586 res
= validate_import_stmt(tree
);
2589 res
= validate_global_stmt(tree
);
2592 res
= validate_exec_stmt(tree
);
2595 res
= validate_assert_stmt(tree
);
2598 res
= validate_if(tree
);
2601 res
= validate_while(tree
);
2604 res
= validate_for(tree
);
2607 res
= validate_try(tree
);
2610 res
= validate_suite(tree
);
2616 res
= validate_testlist(tree
);
2619 res
= validate_test(tree
);
2622 res
= validate_and_test(tree
);
2625 res
= validate_not_test(tree
);
2628 res
= validate_comparison(tree
);
2631 res
= validate_exprlist(tree
);
2634 res
= validate_comp_op(tree
);
2637 res
= validate_expr(tree
);
2640 res
= validate_xor_expr(tree
);
2643 res
= validate_and_expr(tree
);
2646 res
= validate_shift_expr(tree
);
2649 res
= validate_arith_expr(tree
);
2652 res
= validate_term(tree
);
2655 res
= validate_factor(tree
);
2658 res
= validate_power(tree
);
2661 res
= validate_atom(tree
);
2665 /* Hopefully never reached! */
2666 err_string("unrecognized node type");
2677 validate_expr_tree(node
*tree
)
2679 int res
= validate_eval_input(tree
);
2681 if (!res
&& !PyErr_Occurred())
2682 err_string("could not validate expression tuple");
2689 * (NEWLINE | stmt)* ENDMARKER
2692 validate_file_input(node
*tree
)
2695 int nch
= NCH(tree
) - 1;
2696 int res
= ((nch
>= 0)
2697 && validate_ntype(CHILD(tree
, nch
), ENDMARKER
));
2699 for ( ; res
&& (j
< nch
); ++j
) {
2700 if (TYPE(CHILD(tree
, j
)) == stmt
)
2701 res
= validate_stmt(CHILD(tree
, j
));
2703 res
= validate_newline(CHILD(tree
, j
));
2705 /* This stays in to prevent any internal failures from getting to the
2706 * user. Hopefully, this won't be needed. If a user reports getting
2707 * this, we have some debugging to do.
2709 if (!res
&& !PyErr_Occurred())
2710 err_string("VALIDATION FAILURE: report this to the maintainer!");
2717 pickle_constructor
= NULL
;
2721 parser__pickler(PyObject
*self
, PyObject
*args
)
2723 NOTE(ARGUNUSED(self
))
2724 PyObject
*result
= NULL
;
2725 PyObject
*ast
= NULL
;
2726 PyObject
*empty_dict
= NULL
;
2728 if (PyArg_ParseTuple(args
, "O!:_pickler", &PyAST_Type
, &ast
)) {
2732 if ((empty_dict
= PyDict_New()) == NULL
)
2734 if ((newargs
= Py_BuildValue("Oi", ast
, 1)) == NULL
)
2736 tuple
= parser_ast2tuple((PyAST_Object
*)NULL
, newargs
, empty_dict
);
2737 if (tuple
!= NULL
) {
2738 result
= Py_BuildValue("O(O)", pickle_constructor
, tuple
);
2741 Py_DECREF(empty_dict
);
2745 Py_XDECREF(empty_dict
);
2751 /* Functions exported by this module. Most of this should probably
2752 * be converted into an AST object with methods, but that is better
2753 * done directly in Python, allowing subclasses to be created directly.
2754 * We'd really have to write a wrapper around it all anyway to allow
2757 static PyMethodDef parser_functions
[] = {
2758 {"ast2tuple", (PyCFunction
)parser_ast2tuple
, PUBLIC_METHOD_TYPE
,
2759 "Creates a tuple-tree representation of an AST."},
2760 {"ast2list", (PyCFunction
)parser_ast2list
, PUBLIC_METHOD_TYPE
,
2761 "Creates a list-tree representation of an AST."},
2762 {"compileast", (PyCFunction
)parser_compileast
, PUBLIC_METHOD_TYPE
,
2763 "Compiles an AST object into a code object."},
2764 {"expr", (PyCFunction
)parser_expr
, PUBLIC_METHOD_TYPE
,
2765 "Creates an AST object from an expression."},
2766 {"isexpr", (PyCFunction
)parser_isexpr
, PUBLIC_METHOD_TYPE
,
2767 "Determines if an AST object was created from an expression."},
2768 {"issuite", (PyCFunction
)parser_issuite
, PUBLIC_METHOD_TYPE
,
2769 "Determines if an AST object was created from a suite."},
2770 {"suite", (PyCFunction
)parser_suite
, PUBLIC_METHOD_TYPE
,
2771 "Creates an AST object from a suite."},
2772 {"sequence2ast", (PyCFunction
)parser_tuple2ast
, PUBLIC_METHOD_TYPE
,
2773 "Creates an AST object from a tree representation."},
2774 {"tuple2ast", (PyCFunction
)parser_tuple2ast
, PUBLIC_METHOD_TYPE
,
2775 "Creates an AST object from a tree representation."},
2777 /* private stuff: support pickle module */
2778 {"_pickler", (PyCFunction
)parser__pickler
, METH_VARARGS
,
2779 "Returns the pickle magic to allow ast objects to be pickled."},
2781 {NULL
, NULL
, 0, NULL
}
2785 DL_EXPORT(void) initparser(void); /* supply a prototype */
2793 PyAST_Type
.ob_type
= &PyType_Type
;
2794 module
= Py_InitModule("parser", parser_functions
);
2795 dict
= PyModule_GetDict(module
);
2797 if (parser_error
== 0)
2798 parser_error
= PyErr_NewException("parser.ParserError", NULL
, NULL
);
2800 if ((parser_error
== 0)
2801 || (PyDict_SetItemString(dict
, "ParserError", parser_error
) != 0))
2803 /* caller will check PyErr_Occurred() */
2807 * Nice to have, but don't cry if we fail.
2809 Py_INCREF(&PyAST_Type
);
2810 PyDict_SetItemString(dict
, "ASTType", (PyObject
*)&PyAST_Type
);
2812 PyDict_SetItemString(dict
, "__copyright__",
2813 PyString_FromString(parser_copyright_string
));
2814 PyDict_SetItemString(dict
, "__doc__",
2815 PyString_FromString(parser_doc_string
));
2816 PyDict_SetItemString(dict
, "__version__",
2817 PyString_FromString(parser_version_string
));
2819 /* register to support pickling */
2820 module
= PyImport_ImportModule("copy_reg");
2821 if (module
!= NULL
) {
2822 PyObject
*func
, *pickler
;
2824 func
= PyObject_GetAttrString(module
, "pickle");
2825 pickle_constructor
= PyDict_GetItemString(dict
, "sequence2ast");
2826 pickler
= PyDict_GetItemString(dict
, "_pickler");
2827 Py_XINCREF(pickle_constructor
);
2828 if ((func
!= NULL
) && (pickle_constructor
!= NULL
)
2829 && (pickler
!= NULL
)) {
2832 res
= PyObject_CallFunction(
2833 func
, "OOO", &PyAST_Type
, pickler
, pickle_constructor
);