annotate Modules/parsermodule.c @ 13163:a09e8635534d legacy-trunk

Don't try to guess the name of a .def file -- if one is supplied, use it, otherwise just generate an '/export:' option.
author Greg Ward <gward@python.net>
date Thu, 29 Jun 2000 23:09:20 +0000
parents 1c7c2dd1beb1
children 916fb024ce94
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1 /* parsermodule.c
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
3 * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
4 * Institute and State University, Blacksburg, Virginia, USA.
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
5 * Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
6 * Amsterdam, The Netherlands. Copying is permitted under the terms
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
7 * associated with the main Python distribution, with the additional
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
8 * restriction that this additional notice be included and maintained
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
9 * on all distributed copies.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
10 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
11 * This module serves to replace the original parser module written
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
12 * by Guido. The functionality is not matched precisely, but the
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
13 * original may be implemented on top of this. This is desirable
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
14 * since the source of the text to be parsed is now divorced from
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
15 * this interface.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
16 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
17 * Unlike the prior interface, the ability to give a parse tree
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
18 * produced by Python code as a tuple to the compiler is enabled by
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
19 * this module. See the documentation for more details.
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
20 *
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
21 * I've added some annotations that help with the lint code-checking
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
22 * program, but they're not complete by a long shot. The real errors
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
23 * that lint detects are gone, but there are still warnings with
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
24 * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
25 * look like "NOTE(...)".
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
26 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
27
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
28 #include "Python.h" /* general Python API */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
29 #include "graminit.h" /* symbols defined in the grammar */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
30 #include "node.h" /* internal parser structure */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
31 #include "token.h" /* token definitions */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
32 /* ISTERMINAL() / ISNONTERMINAL() */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
33 #include "compile.h" /* PyNode_Compile() */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
34
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
35 #ifdef lint
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
36 #include <note.h>
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
37 #else
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
38 #define NOTE(x)
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
39 #endif
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
40
8184
142165ed84d8 Add a declaration for strdup() for the Mac.
Guido van Rossum <guido@python.org>
parents: 8112
diff changeset
41 #ifdef macintosh
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
42 char *strdup(char *);
8184
142165ed84d8 Add a declaration for strdup() for the Mac.
Guido van Rossum <guido@python.org>
parents: 8112
diff changeset
43 #endif
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
44
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
45 /* String constants used to initialize module attributes.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
46 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
47 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
48 static char*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
49 parser_copyright_string
4099
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
50 = "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
51 University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
52 Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
53 Centrum, Amsterdam, The Netherlands.";
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
54
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
55
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
56 static char*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
57 parser_doc_string
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
58 = "This is an interface to Python's internal parser.";
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
59
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
60 static char*
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
61 parser_version_string = "0.4";
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
62
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
63
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
64 typedef PyObject* (*SeqMaker) (int length);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
65 typedef int (*SeqInserter) (PyObject* sequence,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
66 int index,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
67 PyObject* element);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
68
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
69 /* The function below is copyrigthed by Stichting Mathematisch Centrum. The
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
70 * original copyright statement is included below, and continues to apply
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
71 * in full to the function immediately following. All other material is
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
73 * Institute and State University. Changes were made to comply with the
4099
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
74 * new naming conventions. Added arguments to provide support for creating
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
75 * lists as well as tuples, and optionally including the line numbers.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
76 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
77
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 /***********************************************************
2061
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2055
diff changeset
79 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2055
diff changeset
80 The Netherlands.
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 All Rights Reserved
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
84 Permission to use, copy, modify, and distribute this software and its
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
85 documentation for any purpose and without fee is hereby granted,
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 provided that the above copyright notice appear in all copies and that
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
87 both that copyright notice and this permission notice appear in
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
89 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
90 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
91 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
92 permission.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
93
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
94 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
95 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
96 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
97
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
98 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
99 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
100 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
101 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
102 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
103 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
104 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4287
diff changeset
105 PERFORMANCE OF THIS SOFTWARE.
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 ******************************************************************/
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
109 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
110 node2tuple(node *n, /* node to convert */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
111 SeqMaker mkseq, /* create sequence */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
112 SeqInserter addelem, /* func. to add elem. in seq. */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
113 int lineno) /* include line numbers? */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
114 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
115 if (n == NULL) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
116 Py_INCREF(Py_None);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
117 return (Py_None);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
118 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
119 if (ISNONTERMINAL(TYPE(n))) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
120 int i;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
121 PyObject *v;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
122 PyObject *w;
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
123
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
124 v = mkseq(1 + NCH(n));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
125 if (v == NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
126 return (v);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
127 w = PyInt_FromLong(TYPE(n));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
128 if (w == NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
129 Py_DECREF(v);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
130 return ((PyObject*) NULL);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
131 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
132 (void) addelem(v, 0, w);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
133 for (i = 0; i < NCH(n); i++) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
134 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
135 if (w == NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
136 Py_DECREF(v);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
137 return ((PyObject*) NULL);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
138 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
139 (void) addelem(v, i+1, w);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
140 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
141 return (v);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
142 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
143 else if (ISTERMINAL(TYPE(n))) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
144 PyObject *result = mkseq(2 + lineno);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
145 if (result != NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
146 (void) addelem(result, 0, PyInt_FromLong(TYPE(n)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
147 (void) addelem(result, 1, PyString_FromString(STR(n)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
148 if (lineno == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
149 (void) addelem(result, 2, PyInt_FromLong(n->n_lineno));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
150 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
151 return (result);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
152 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
153 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
154 PyErr_SetString(PyExc_SystemError,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
155 "unrecognized parse tree node type");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
156 return ((PyObject*) NULL);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
157 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
158 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
159 /*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
160 * End of material copyrighted by Stichting Mathematisch Centrum.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
161 */
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
163
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
164
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
165 /* There are two types of intermediate objects we're interested in:
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
166 * 'eval' and 'exec' types. These constants can be used in the ast_type
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
167 * field of the object type to identify which any given object represents.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
168 * These should probably go in an external header to allow other extensions
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
169 * to use them, but then, we really should be using C++ too. ;-)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
170 *
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
171 * The PyAST_FRAGMENT type is not currently supported. Maybe not useful?
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
172 * Haven't decided yet.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
173 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
174
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
175 #define PyAST_EXPR 1
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
176 #define PyAST_SUITE 2
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
177 #define PyAST_FRAGMENT 3
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
178
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
179
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
180 /* These are the internal objects and definitions required to implement the
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
181 * AST type. Most of the internal names are more reminiscent of the 'old'
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
182 * naming style, but the code uses the new naming convention.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
183 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
184
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
185 static PyObject*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
186 parser_error = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
187
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
188
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
189 typedef struct _PyAST_Object {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
190 PyObject_HEAD /* standard object header */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
191 node* ast_node; /* the node* returned by the parser */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
192 int ast_type; /* EXPR or SUITE ? */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
193 } PyAST_Object;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
194
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
195
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
196 staticforward void
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
197 parser_free(PyAST_Object *ast);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
198
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
199 staticforward int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
200 parser_compare(PyAST_Object *left, PyAST_Object *right);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
201
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
202 staticforward PyObject *
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
203 parser_getattr(PyObject *self, char *name);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
204
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
205
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
206 static
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
207 PyTypeObject PyAST_Type = {
8450
61912d4ad52f Sorry, the initializer for ob_type must really be NULL,
Guido van Rossum <guido@python.org>
parents: 8290
diff changeset
208 PyObject_HEAD_INIT(NULL)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
209 0,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
210 "ast", /* tp_name */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
211 (int) sizeof(PyAST_Object), /* tp_basicsize */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
212 0, /* tp_itemsize */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
213 (destructor)parser_free, /* tp_dealloc */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
214 0, /* tp_print */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
215 parser_getattr, /* tp_getattr */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
216 0, /* tp_setattr */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
217 (cmpfunc)parser_compare, /* tp_compare */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
218 0, /* tp_repr */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
219 0, /* tp_as_number */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
220 0, /* tp_as_sequence */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
221 0, /* tp_as_mapping */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
222 0, /* tp_hash */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
223 0, /* tp_call */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
224 0, /* tp_str */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
225 0, /* tp_getattro */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
226 0, /* tp_setattro */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
227
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
228 /* Functions to access object as input/output buffer */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
229 0, /* tp_as_buffer */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
230
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
231 Py_TPFLAGS_DEFAULT, /* tp_flags */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
232
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
233 /* __doc__ */
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
234 "Intermediate representation of a Python parse tree."
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
235 }; /* PyAST_Type */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
236
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
237
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
238 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
239 parser_compare_nodes(node *left, node *right)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
240 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
241 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
242
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
243 if (TYPE(left) < TYPE(right))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
244 return (-1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
245
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
246 if (TYPE(right) < TYPE(left))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
247 return (1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
248
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
249 if (ISTERMINAL(TYPE(left)))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
250 return (strcmp(STR(left), STR(right)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
251
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
252 if (NCH(left) < NCH(right))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
253 return (-1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
254
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
255 if (NCH(right) < NCH(left))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
256 return (1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
257
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
258 for (j = 0; j < NCH(left); ++j) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
259 int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
260
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
261 if (v != 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
262 return (v);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
263 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
264 return (0);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
265 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
266
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
267
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
268 /* int parser_compare(PyAST_Object* left, PyAST_Object* right)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
269 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
270 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
271 * This really just wraps a call to parser_compare_nodes() with some easy
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
272 * checks and protection code.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
273 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
274 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
275 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
276 parser_compare(PyAST_Object *left, PyAST_Object *right)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
277 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
278 if (left == right)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
279 return (0);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
280
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
281 if ((left == 0) || (right == 0))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
282 return (-1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
283
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
284 return (parser_compare_nodes(left->ast_node, right->ast_node));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
285 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
286
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
287
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
288 /* parser_newastobject(node* ast)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
289 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
290 * Allocates a new Python object representing an AST. This is simply the
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
291 * 'wrapper' object that holds a node* and allows it to be passed around in
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
292 * Python code.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
293 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
294 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
295 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
296 parser_newastobject(node *ast, int type)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
297 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12487
diff changeset
298 PyAST_Object* o = PyObject_New(PyAST_Object, &PyAST_Type);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
299
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
300 if (o != 0) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
301 o->ast_node = ast;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
302 o->ast_type = type;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
303 }
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
304 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
305 PyNode_Free(ast);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
306 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
307 return ((PyObject*)o);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
308 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
309
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
310
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
311 /* void parser_free(PyAST_Object* ast)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
312 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
313 * This is called by a del statement that reduces the reference count to 0.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
314 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
315 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
316 static void
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
317 parser_free(PyAST_Object *ast)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
318 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
319 PyNode_Free(ast->ast_node);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12487
diff changeset
320 PyObject_Del(ast);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
321 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
322
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
323
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
324 /* parser_ast2tuple(PyObject* self, PyObject* args, PyObject* kw)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
325 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
326 * This provides conversion from a node* to a tuple object that can be
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
327 * returned to the Python-level caller. The AST object is not modified.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
328 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
329 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
330 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
331 parser_ast2tuple(PyAST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
332 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
333 PyObject *line_option = 0;
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
334 PyObject *res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
335 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
336
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
337 static char *keywords[] = {"ast", "line_info", NULL};
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
338
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
339 if (self == NULL) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
340 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2tuple", keywords,
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
341 &PyAST_Type, &self, &line_option);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
342 }
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
343 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
344 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:totuple", &keywords[1],
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
345 &line_option);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
346 if (ok != 0) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
347 int lineno = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
348 if (line_option != NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
349 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
350 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
351 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
352 * Convert AST into a tuple representation. Use Guido's function,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
353 * since it's known to work already.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
354 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
355 res = node2tuple(((PyAST_Object*)self)->ast_node,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
356 PyTuple_New, PyTuple_SetItem, lineno);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
357 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
358 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
359 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
360
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
361
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
362 /* parser_ast2list(PyObject* self, PyObject* args, PyObject* kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
363 *
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
364 * This provides conversion from a node* to a list object that can be
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
365 * returned to the Python-level caller. The AST object is not modified.
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
366 *
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
367 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
368 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
369 parser_ast2list(PyAST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
370 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
371 PyObject *line_option = 0;
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
372 PyObject *res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
373 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
374
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
375 static char *keywords[] = {"ast", "line_info", NULL};
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
376
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
377 if (self == NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
378 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O:ast2list", keywords,
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
379 &PyAST_Type, &self, &line_option);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
380 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
381 ok = PyArg_ParseTupleAndKeywords(args, kw, "|O:tolist", &keywords[1],
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
382 &line_option);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
383 if (ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
384 int lineno = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
385 if (line_option != 0) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
386 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
387 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
388 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
389 * Convert AST into a tuple representation. Use Guido's function,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
390 * since it's known to work already.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
391 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
392 res = node2tuple(self->ast_node,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
393 PyList_New, PyList_SetItem, lineno);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
394 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
395 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
396 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
397
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
398
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
399 /* parser_compileast(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
400 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
401 * This function creates code objects from the parse tree represented by
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
402 * the passed-in data object. An optional file name is passed in as well.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
403 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
404 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
405 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
406 parser_compileast(PyAST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
407 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
408 PyObject* res = 0;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
409 char* str = "<ast>";
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
410 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
411
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
412 static char *keywords[] = {"ast", "filename", NULL};
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
413
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
414 if (self == NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
415 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compileast", keywords,
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
416 &PyAST_Type, &self, &str);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
417 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
418 ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
419 &str);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
420
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
421 if (ok)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
422 res = (PyObject *)PyNode_Compile(self->ast_node, str);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
423
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
424 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
425 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
426
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
427
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
428 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
429 * PyObject* parser_issuite(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
430 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
431 * Checks the passed-in AST object to determine if it is an expression or
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
432 * a statement suite, respectively. The return is a Python truth value.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
433 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
434 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
435 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
436 parser_isexpr(PyAST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
437 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
438 PyObject* res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
439 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
440
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
441 static char *keywords[] = {"ast", NULL};
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
442
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
443 if (self == NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
444 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
445 &PyAST_Type, &self);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
446 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
447 ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
448
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
449 if (ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
450 /* Check to see if the AST represents an expression or not. */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
451 res = (self->ast_type == PyAST_EXPR) ? Py_True : Py_False;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
452 Py_INCREF(res);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
453 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
454 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
455 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
456
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
457
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
458 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
459 parser_issuite(PyAST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
460 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
461 PyObject* res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
462 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
463
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
464 static char *keywords[] = {"ast", NULL};
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
465
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
466 if (self == NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
467 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
468 &PyAST_Type, &self);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
469 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
470 ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
471
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
472 if (ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
473 /* Check to see if the AST represents an expression or not. */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
474 res = (self->ast_type == PyAST_EXPR) ? Py_False : Py_True;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
475 Py_INCREF(res);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
476 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
477 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
478 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
479
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
480
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
481 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
482
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
483 static PyMethodDef
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
484 parser_methods[] = {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
485 {"compile", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
486 "Compile this AST object into a code object."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
487 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
488 "Determines if this AST object was created from an expression."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
489 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
490 "Determines if this AST object was created from a suite."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
491 {"tolist", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
492 "Creates a list-tree representation of this AST."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
493 {"totuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
494 "Creates a tuple-tree representation of this AST."},
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
495
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
496 {NULL, NULL, 0, NULL}
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
497 };
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
498
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
499
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
500 static PyObject*
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
501 parser_getattr(self, name)
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
502 PyObject *self;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
503 char *name;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
504 {
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
505 return (Py_FindMethod(parser_methods, self, name));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
506 }
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
507
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
508
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
509 /* err_string(char* message)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
510 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
511 * Sets the error string for an exception of type ParserError.
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
512 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
513 */
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
514 static void
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
515 err_string(message)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
516 char *message;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
517 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
518 PyErr_SetString(parser_error, message);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
519 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
520
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
521
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
522 /* PyObject* parser_do_parse(PyObject* args, int type)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
523 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
524 * Internal function to actually execute the parse and return the result if
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
525 * successful, or set an exception if not.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
526 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
527 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
528 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
529 parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
530 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
531 char* string = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
532 PyObject* res = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
533
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
534 static char *keywords[] = {"source", NULL};
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
535
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
536 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
537 node* n = PyParser_SimpleParseString(string,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
538 (type == PyAST_EXPR)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
539 ? eval_input : file_input);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
540
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
541 if (n != 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
542 res = parser_newastobject(n, type);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
543 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
544 err_string("Could not parse string.");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
545 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
546 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
547 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
548
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
549
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
550 /* PyObject* parser_expr(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
551 * PyObject* parser_suite(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
552 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
553 * External interfaces to the parser itself. Which is called determines if
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
554 * the parser attempts to recognize an expression ('eval' form) or statement
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
555 * suite ('exec' form). The real work is done by parser_do_parse() above.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
556 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
557 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
558 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
559 parser_expr(PyAST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
560 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
561 NOTE(ARGUNUSED(self))
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
562 return (parser_do_parse(args, kw, "s:expr", PyAST_EXPR));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
563 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
564
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
565
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
566 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
567 parser_suite(PyAST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
568 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
569 NOTE(ARGUNUSED(self))
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
570 return (parser_do_parse(args, kw, "s:suite", PyAST_SUITE));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
571 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
572
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
573
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
574
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
575 /* This is the messy part of the code. Conversion from a tuple to an AST
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
576 * object requires that the input tuple be valid without having to rely on
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
577 * catching an exception from the compiler. This is done to allow the
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
578 * compiler itself to remain fast, since most of its input will come from
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
579 * the parser directly, and therefore be known to be syntactically correct.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
580 * This validation is done to ensure that we don't core dump the compile
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
581 * phase, returning an exception instead.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
582 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
583 * Two aspects can be broken out in this code: creating a node tree from
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
584 * the tuple passed in, and verifying that it is indeed valid. It may be
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
585 * advantageous to expand the number of AST types to include funcdefs and
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
586 * lambdadefs to take advantage of the optimizer, recognizing those ASTs
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
587 * here. They are not necessary, and not quite as useful in a raw form.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
588 * For now, let's get expressions and suites working reliably.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
589 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
590
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
591
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
592 staticforward node* build_node_tree(PyObject *tuple);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
593 staticforward int validate_expr_tree(node *tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
594 staticforward int validate_file_input(node *tree);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
595
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
596
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
597 /* PyObject* parser_tuple2ast(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
598 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
599 * This is the public function, called from the Python code. It receives a
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
600 * single tuple object from the caller, and creates an AST object if the
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
601 * tuple can be validated. It does this by checking the first code of the
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
602 * tuple, and, if acceptable, builds the internal representation. If this
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
603 * step succeeds, the internal representation is validated as fully as
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
604 * possible with the various validate_*() routines defined below.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
605 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
606 * This function must be changed if support is to be added for PyAST_FRAGMENT
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
607 * AST objects.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
608 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
609 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
610 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
611 parser_tuple2ast(PyAST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
612 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
613 NOTE(ARGUNUSED(self))
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
614 PyObject *ast = 0;
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
615 PyObject *tuple = 0;
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
616 PyObject *temp = 0;
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
617 int ok;
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
618 int start_sym = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
619
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
620 static char *keywords[] = {"sequence", NULL};
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
621
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
622 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:tuple2ast", keywords,
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
623 &tuple))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
624 return (0);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
625 if (!PySequence_Check(tuple)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
626 PyErr_SetString(PyExc_ValueError,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
627 "tuple2ast() requires a single sequence argument");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
628 return (0);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
629 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
630 /*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
631 * This mess of tests is written this way so we can use the abstract
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
632 * object interface (AOI). Unfortunately, the AOI increments reference
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
633 * counts, which requires that we store a pointer to retrieved object
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
634 * so we can DECREF it after the check. But we really should accept
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
635 * lists as well as tuples at the very least.
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
636 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
637 ok = PyObject_Length(tuple) >= 2;
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
638 if (ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
639 temp = PySequence_GetItem(tuple, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
640 ok = (temp != NULL) && PyInt_Check(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
641 if (ok)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
642 /* this is used after the initial checks: */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
643 start_sym = PyInt_AS_LONG(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
644 Py_XDECREF(temp);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
645 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
646 if (ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
647 temp = PySequence_GetItem(tuple, 1);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
648 ok = (temp != NULL) && PySequence_Check(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
649 Py_XDECREF(temp);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
650 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
651 if (ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
652 temp = PySequence_GetItem(tuple, 1);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
653 ok = (temp != NULL) && PyObject_Length(temp) >= 2;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
654 if (ok) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
655 PyObject *temp2 = PySequence_GetItem(temp, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
656 if (temp2 != NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
657 ok = PyInt_Check(temp2);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
658 Py_DECREF(temp2);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
659 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
660 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
661 Py_XDECREF(temp);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
662 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
663 /* If we've failed at some point, get out of here. */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
664 if (!ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
665 err_string("malformed sequence for tuple2ast()");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
666 return (0);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
667 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
668 /*
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
669 * This might be a valid parse tree, but let's do a quick check
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
670 * before we jump the gun.
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
671 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
672 if (start_sym == eval_input) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
673 /* Might be an eval form. */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
674 node* expression = build_node_tree(tuple);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
675
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
676 if ((expression != 0) && validate_expr_tree(expression))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
677 ast = parser_newastobject(expression, PyAST_EXPR);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
678 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
679 else if (start_sym == file_input) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
680 /* This looks like an exec form so far. */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
681 node* suite_tree = build_node_tree(tuple);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
682
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
683 if ((suite_tree != 0) && validate_file_input(suite_tree))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
684 ast = parser_newastobject(suite_tree, PyAST_SUITE);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
685 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
686 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
687 /* This is a fragment, and is not yet supported. Maybe they
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
688 * will be if I find a use for them.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
689 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
690 err_string("Fragmentary parse trees not supported.");
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
691
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
692 /* Make sure we throw an exception on all errors. We should never
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
693 * get this, but we'd do well to be sure something is done.
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
694 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
695 if ((ast == 0) && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
696 err_string("Unspecified ast error occurred.");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
697
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
698 return (ast);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
699 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
700
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
701
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
702 /* int check_terminal_tuple()
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
703 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
704 * Check a tuple to determine that it is indeed a valid terminal
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
705 * node. The node is known to be required as a terminal, so we throw
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
706 * an exception if there is a failure.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
707 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
708 * The format of an acceptable terminal tuple is "(is[i])": the fact
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
709 * that elem is a tuple and the integer is a valid terminal symbol
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
710 * has been established before this function is called. We must
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
711 * check the length of the tuple and the type of the second element
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
712 * and optional third element. We do *NOT* check the actual text of
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
713 * the string element, which we could do in many cases. This is done
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
714 * by the validate_*() functions which operate on the internal
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
715 * representation.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
716 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
717 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
718 check_terminal_tuple(PyObject *elem)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
719 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
720 int len = PyObject_Length(elem);
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
721 int res = 1;
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
722 char* str = "Illegal terminal symbol; bad node length.";
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
723
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
724 if ((len == 2) || (len == 3)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
725 PyObject *temp = PySequence_GetItem(elem, 1);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
726 res = PyString_Check(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
727 str = "Illegal terminal symbol; expected a string.";
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
728 if (res && (len == 3)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
729 PyObject* third = PySequence_GetItem(elem, 2);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
730 res = PyInt_Check(third);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
731 str = "Invalid third element of terminal node.";
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
732 Py_XDECREF(third);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
733 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
734 Py_XDECREF(temp);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
735 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
736 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
737 res = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
738 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
739 if (!res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
740 elem = Py_BuildValue("(os)", elem, str);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
741 PyErr_SetObject(parser_error, elem);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
742 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
743 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
744 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
745
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
746
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
747 /* node* build_node_children()
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
748 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
749 * Iterate across the children of the current non-terminal node and build
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
750 * their structures. If successful, return the root of this portion of
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
751 * the tree, otherwise, 0. Any required exception will be specified already,
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
752 * and no memory will have been deallocated.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
753 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
754 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
755 static node*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
756 build_node_children(PyObject *tuple, node *root, int *line_num)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
757 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
758 int len = PyObject_Length(tuple);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
759 int i;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
760
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
761 for (i = 1; i < len; ++i) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
762 /* elem must always be a tuple, however simple */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
763 PyObject* elem = PySequence_GetItem(tuple, i);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
764 int ok = elem != NULL;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
765 long type = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
766 char *strn = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
767
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
768 if (ok)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
769 ok = PySequence_Check(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
770 if (ok) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
771 PyObject *temp = PySequence_GetItem(elem, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
772 if (temp == NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
773 ok = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
774 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
775 ok = PyInt_Check(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
776 if (ok)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
777 type = PyInt_AS_LONG(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
778 Py_DECREF(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
779 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
780 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
781 if (!ok) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
782 PyErr_SetObject(parser_error,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
783 Py_BuildValue("(os)", elem,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
784 "Illegal node construct."));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
785 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
786 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
787 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
788 if (ISTERMINAL(type)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
789 if (check_terminal_tuple(elem)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
790 PyObject *temp = PySequence_GetItem(elem, 1);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
791
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
792 /* check_terminal_tuple() already verified it's a string */
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12487
diff changeset
793 strn = (char *)PyMem_MALLOC(PyString_GET_SIZE(temp) + 1);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
794 if (strn != NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
795 (void) strcpy(strn, PyString_AS_STRING(temp));
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12487
diff changeset
796 Py_DECREF(temp);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
797
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
798 if (PyObject_Length(elem) == 3) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
799 PyObject* temp = PySequence_GetItem(elem, 2);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
800 *line_num = PyInt_AsLong(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
801 Py_DECREF(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
802 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
803 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
804 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
805 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
806 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
807 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
808 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
809 else if (!ISNONTERMINAL(type)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
810 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
811 * It has to be one or the other; this is an error.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
812 * Throw an exception.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
813 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
814 PyErr_SetObject(parser_error,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
815 Py_BuildValue("(os)", elem,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
816 "Unknown node type."));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
817 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
818 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
819 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
820 PyNode_AddChild(root, type, strn, *line_num);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
821
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
822 if (ISNONTERMINAL(type)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
823 node* new_child = CHILD(root, i - 1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
824
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
825 if (new_child != build_node_children(elem, new_child, line_num)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
826 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
827 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
828 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
829 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
830 else if (type == NEWLINE) { /* It's true: we increment the */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
831 ++(*line_num); /* line number *after* the newline! */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
832 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
833 Py_XDECREF(elem);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
834 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
835 return (root);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
836 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
837
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
838
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
839 static node*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
840 build_node_tree(PyObject *tuple)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
841 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
842 node* res = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
843 PyObject *temp = PySequence_GetItem(tuple, 0);
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
844 long num = -1;
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
845
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
846 if (temp != NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
847 num = PyInt_AsLong(temp);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
848 Py_XDECREF(temp);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
849 if (ISTERMINAL(num)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
850 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
851 * The tuple is simple, but it doesn't start with a start symbol.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
852 * Throw an exception now and be done with it.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
853 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
854 tuple = Py_BuildValue("(os)", tuple,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
855 "Illegal ast tuple; cannot start with terminal symbol.");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
856 PyErr_SetObject(parser_error, tuple);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
857 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
858 else if (ISNONTERMINAL(num)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
859 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
860 * Not efficient, but that can be handled later.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
861 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
862 int line_num = 0;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
863
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
864 res = PyNode_New(num);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
865 if (res != build_node_children(tuple, res, &line_num)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
866 PyNode_Free(res);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
867 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
868 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
869 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
870 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
871 /* The tuple is illegal -- if the number is neither TERMINAL nor
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
872 * NONTERMINAL, we can't use it.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
873 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
874 PyErr_SetObject(parser_error,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
875 Py_BuildValue("(os)", tuple,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
876 "Illegal component tuple."));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
877
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
878 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
879 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
880
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
881
4092
8592fa4a119b Support for old CPP
Guido van Rossum <guido@python.org>
parents: 4084
diff changeset
882 #ifdef HAVE_OLD_CPP
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
883 #define VALIDATER(n) static int validate_/**/n(node *tree)
4092
8592fa4a119b Support for old CPP
Guido van Rossum <guido@python.org>
parents: 4084
diff changeset
884 #else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
885 #define VALIDATER(n) static int validate_##n(node *tree)
4092
8592fa4a119b Support for old CPP
Guido van Rossum <guido@python.org>
parents: 4084
diff changeset
886 #endif
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
887
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
888
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
889 /*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
890 * Validation routines used within the validation section:
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
891 */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
892 staticforward int validate_terminal(node *terminal, int type, char *string);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
893
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
894 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
895 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
896 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
897 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
898 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
899 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
900 #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
901 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
902 #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
903 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
904 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
905 #define validate_star(ch) validate_terminal(ch, STAR, "*")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
906 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
907 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
908 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
909 #define validate_name(ch, str) validate_terminal(ch, NAME, str)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
910
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
911 VALIDATER(node); VALIDATER(small_stmt);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
912 VALIDATER(class); VALIDATER(node);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
913 VALIDATER(parameters); VALIDATER(suite);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
914 VALIDATER(testlist); VALIDATER(varargslist);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
915 VALIDATER(fpdef); VALIDATER(fplist);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
916 VALIDATER(stmt); VALIDATER(simple_stmt);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
917 VALIDATER(expr_stmt); VALIDATER(power);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
918 VALIDATER(print_stmt); VALIDATER(del_stmt);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
919 VALIDATER(return_stmt);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
920 VALIDATER(raise_stmt); VALIDATER(import_stmt);
4099
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
921 VALIDATER(global_stmt);
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
922 VALIDATER(assert_stmt);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
923 VALIDATER(exec_stmt); VALIDATER(compound_stmt);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
924 VALIDATER(while); VALIDATER(for);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
925 VALIDATER(try); VALIDATER(except_clause);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
926 VALIDATER(test); VALIDATER(and_test);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
927 VALIDATER(not_test); VALIDATER(comparison);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
928 VALIDATER(comp_op); VALIDATER(expr);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
929 VALIDATER(xor_expr); VALIDATER(and_expr);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
930 VALIDATER(shift_expr); VALIDATER(arith_expr);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
931 VALIDATER(term); VALIDATER(factor);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
932 VALIDATER(atom); VALIDATER(lambdef);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
933 VALIDATER(trailer); VALIDATER(subscript);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
934 VALIDATER(subscriptlist); VALIDATER(sliceop);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
935 VALIDATER(exprlist); VALIDATER(dictmaker);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
936 VALIDATER(arglist); VALIDATER(argument);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
937
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
938
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
939 #define is_even(n) (((n) & 1) == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
940 #define is_odd(n) (((n) & 1) == 1)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
941
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
942
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
943 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
944 validate_ntype(node *n, int t)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
945 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
946 int res = (TYPE(n) == t);
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
947
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
948 if (!res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
949 char buffer[128];
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
950 (void) sprintf(buffer, "Expected node type %d, got %d.", t, TYPE(n));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
951 err_string(buffer);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
952 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
953 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
954 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
955
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
956
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
957 /* Verifies that the number of child nodes is exactly 'num', raising
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
958 * an exception if it isn't. The exception message does not indicate
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
959 * the exact number of nodes, allowing this to be used to raise the
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
960 * "right" exception when the wrong number of nodes is present in a
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
961 * specific variant of a statement's syntax. This is commonly used
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
962 * in that fashion.
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
963 */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
964 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
965 validate_numnodes(node *n, int num, const char *const name)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
966 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
967 if (NCH(n) != num) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
968 char buff[60];
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
969 (void) sprintf(buff, "Illegal number of children for %s node.", name);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
970 err_string(buff);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
971 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
972 return (NCH(n) == num);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
973 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
974
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
975
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
976 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
977 validate_terminal(node *terminal, int type, char *string)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
978 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
979 int res = (validate_ntype(terminal, type)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
980 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
981
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
982 if (!res && !PyErr_Occurred()) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
983 char buffer[60];
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
984 (void) sprintf(buffer, "Illegal terminal: expected \"%s\"", string);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
985 err_string(buffer);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
986 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
987 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
988 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
989
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
990
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
991 /* X (',' X) [',']
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
992 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
993 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
994 validate_repeating_list(node *tree, int ntype, int (*vfunc)(),
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
995 const char *const name)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
996 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
997 int nch = NCH(tree);
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
998 int res = (nch && validate_ntype(tree, ntype)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
999 && vfunc(CHILD(tree, 0)));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1000
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1001 if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1002 (void) validate_numnodes(tree, 1, name);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1003 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1004 if (is_even(nch))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1005 res = validate_comma(CHILD(tree, --nch));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1006 if (res && nch > 1) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1007 int pos = 1;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1008 for ( ; res && pos < nch; pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1009 res = (validate_comma(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1010 && vfunc(CHILD(tree, pos + 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1011 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1012 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1013 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1014 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1015
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1016
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1017 /* VALIDATE(class)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1018 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1019 * classdef:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1020 * 'class' NAME ['(' testlist ')'] ':' suite
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1021 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1022 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1023 validate_class(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1024 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1025 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1026 int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1027
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1028 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1029 res = (validate_name(CHILD(tree, 0), "class")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1030 && validate_ntype(CHILD(tree, 1), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1031 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1032 && validate_suite(CHILD(tree, nch - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1033 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1034 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1035 (void) validate_numnodes(tree, 4, "class");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1036 if (res && (nch == 7)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1037 res = (validate_lparen(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1038 && validate_testlist(CHILD(tree, 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1039 && validate_rparen(CHILD(tree, 4)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1040 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1041 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1042 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1043
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1044
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1045 /* if_stmt:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1046 * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1047 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1048 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1049 validate_if(node *tree)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1050 {
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1051 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1052 int res = (validate_ntype(tree, if_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1053 && (nch >= 4)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1054 && validate_name(CHILD(tree, 0), "if")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1055 && validate_test(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1056 && validate_colon(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1057 && validate_suite(CHILD(tree, 3)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1058
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1059 if (res && ((nch % 4) == 3)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1060 /* ... 'else' ':' suite */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1061 res = (validate_name(CHILD(tree, nch - 3), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1062 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1063 && validate_suite(CHILD(tree, nch - 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1064 nch -= 3;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1065 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1066 else if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1067 (void) validate_numnodes(tree, 4, "if");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1068 if ((nch % 4) != 0)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1069 /* Will catch the case for nch < 4 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1070 res = validate_numnodes(tree, 0, "if");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1071 else if (res && (nch > 4)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1072 /* ... ('elif' test ':' suite)+ ... */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1073 int j = 4;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1074 while ((j < nch) && res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1075 res = (validate_name(CHILD(tree, j), "elif")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1076 && validate_colon(CHILD(tree, j + 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1077 && validate_test(CHILD(tree, j + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1078 && validate_suite(CHILD(tree, j + 3)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1079 j += 4;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1080 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1081 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1082 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1083 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1084
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1085
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1086 /* parameters:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1087 * '(' [varargslist] ')'
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1088 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1089 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1090 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1091 validate_parameters(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1092 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1093 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1094 int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1095
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1096 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1097 res = (validate_lparen(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1098 && validate_rparen(CHILD(tree, nch - 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1099 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1100 res = validate_varargslist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1101 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1102 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1103 (void) validate_numnodes(tree, 2, "parameters");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1104 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1105 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1106 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1107
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1108
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1109 /* VALIDATE(suite)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1110 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1111 * suite:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1112 * simple_stmt
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1113 * | NEWLINE INDENT stmt+ DEDENT
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1114 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1115 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1116 validate_suite(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1117 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1118 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1119 int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1120
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1121 if (res && (nch == 1))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1122 res = validate_simple_stmt(CHILD(tree, 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1123 else if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1124 /* NEWLINE INDENT stmt+ DEDENT */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1125 res = (validate_newline(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1126 && validate_indent(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1127 && validate_stmt(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1128 && validate_dedent(CHILD(tree, nch - 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1129
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1130 if (res && (nch > 4)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1131 int i = 3;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1132 --nch; /* forget the DEDENT */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1133 for ( ; res && (i < nch); ++i)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1134 res = validate_stmt(CHILD(tree, i));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1135 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1136 else if (nch < 4)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1137 res = validate_numnodes(tree, 4, "suite");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1138 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1139 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1140 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1141
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1142
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1143 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1144 validate_testlist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1145 {
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1146 return (validate_repeating_list(tree, testlist,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1147 validate_test, "testlist"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1148 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1149
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1150
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1151 /* VALIDATE(varargslist)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1152 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1153 * varargslist:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1154 * (fpdef ['=' test] ',')* ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1155 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1156 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1157 * (fpdef ['=' test] ',')*
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1158 * ('*' NAME [',' ('**'|'*' '*') NAME]
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1159 * | ('**'|'*' '*') NAME)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1160 * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1161 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1162 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1163 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1164 validate_varargslist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1165 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1166 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1167 int res = validate_ntype(tree, varargslist) && (nch != 0);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1168
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1169 if (res && (nch >= 2) && (TYPE(CHILD(tree, nch - 1)) == NAME)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1170 /* (fpdef ['=' test] ',')*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1171 * ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1172 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1173 int pos = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1174 int remaining = nch;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1175
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1176 while (res && (TYPE(CHILD(tree, pos)) == fpdef)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1177 res = validate_fpdef(CHILD(tree, pos));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1178 if (res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1179 if (TYPE(CHILD(tree, pos + 1)) == EQUAL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1180 res = validate_test(CHILD(tree, pos + 2));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1181 pos += 2;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1182 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1183 res = res && validate_comma(CHILD(tree, pos + 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1184 pos += 2;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1185 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1186 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1187 if (res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1188 remaining = nch - pos;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1189 res = ((remaining == 2) || (remaining == 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1190 || (remaining == 5) || (remaining == 6));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1191 if (!res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1192 (void) validate_numnodes(tree, 2, "varargslist");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1193 else if (TYPE(CHILD(tree, pos)) == DOUBLESTAR)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1194 return ((remaining == 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1195 && validate_ntype(CHILD(tree, pos+1), NAME));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1196 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1197 res = validate_star(CHILD(tree, pos++));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1198 --remaining;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1199 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1200 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1201 if (res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1202 if (remaining == 2) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1203 res = (validate_star(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1204 && validate_ntype(CHILD(tree, pos + 1), NAME));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1205 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1206 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1207 res = validate_ntype(CHILD(tree, pos++), NAME);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1208 if (res && (remaining >= 4)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1209 res = validate_comma(CHILD(tree, pos));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1210 if (--remaining == 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1211 res = (validate_star(CHILD(tree, pos + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1212 && validate_star(CHILD(tree, pos + 2)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1213 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1214 res = validate_ntype(CHILD(tree, pos + 1), DOUBLESTAR);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1215 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1216 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1217 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1218 if (!res && !PyErr_Occurred())
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1219 err_string("Incorrect validation of variable arguments list.");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1220 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1221 else if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1222 /* fpdef ['=' test] (',' fpdef ['=' test])* [','] */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1223 if (TYPE(CHILD(tree, nch - 1)) == COMMA)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1224 --nch;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1225
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1226 /* fpdef ['=' test] (',' fpdef ['=' test])* */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1227 res = (is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1228 && validate_fpdef(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1229
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1230 if (res && (nch > 1)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1231 int pos = 1;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1232 if (TYPE(CHILD(tree, 1)) == EQUAL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1233 res = validate_test(CHILD(tree, 2));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1234 pos += 2;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1235 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1236 /* ... (',' fpdef ['=' test])* */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1237 for ( ; res && (pos < nch); pos += 2) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1238 /* ',' fpdef */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1239 res = (validate_comma(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1240 && validate_fpdef(CHILD(tree, pos + 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1241 if (res
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1242 && ((nch - pos) > 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1243 && (TYPE(CHILD(tree, pos + 2)) == EQUAL)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1244 /* ['=' test] */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1245 res = validate_test(CHILD(tree, pos + 3));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1246 pos += 2;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1247 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1248 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1249 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1250 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1251 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1252 err_string("Improperly formed argument list.");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1253 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1254 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1255 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1256
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1257
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1258 /* VALIDATE(fpdef)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1259 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1260 * fpdef:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1261 * NAME
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1262 * | '(' fplist ')'
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1263 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1264 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1265 validate_fpdef(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1266 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1267 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1268 int res = validate_ntype(tree, fpdef);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1269
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1270 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1271 if (nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1272 res = validate_ntype(CHILD(tree, 0), NAME);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1273 else if (nch == 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1274 res = (validate_lparen(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1275 && validate_fplist(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1276 && validate_rparen(CHILD(tree, 2)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1277 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1278 res = validate_numnodes(tree, 1, "fpdef");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1279 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1280 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1281 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1282
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1283
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1284 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1285 validate_fplist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1286 {
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1287 return (validate_repeating_list(tree, fplist,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1288 validate_fpdef, "fplist"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1289 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1290
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1291
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1292 /* simple_stmt | compound_stmt
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1293 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1294 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1295 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1296 validate_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1297 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1298 int res = (validate_ntype(tree, stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1299 && validate_numnodes(tree, 1, "stmt"));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1300
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1301 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1302 tree = CHILD(tree, 0);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1303
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1304 if (TYPE(tree) == simple_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1305 res = validate_simple_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1306 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1307 res = validate_compound_stmt(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1308 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1309 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1310 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1311
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1312
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1313 /* small_stmt (';' small_stmt)* [';'] NEWLINE
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1314 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1315 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1316 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1317 validate_simple_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1318 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1319 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1320 int res = (validate_ntype(tree, simple_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1321 && (nch >= 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1322 && validate_small_stmt(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1323 && validate_newline(CHILD(tree, nch - 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1324
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1325 if (nch < 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1326 res = validate_numnodes(tree, 2, "simple_stmt");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1327 --nch; /* forget the NEWLINE */
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1328 if (res && is_even(nch))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1329 res = validate_semi(CHILD(tree, --nch));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1330 if (res && (nch > 2)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1331 int i;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1332
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1333 for (i = 1; res && (i < nch); i += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1334 res = (validate_semi(CHILD(tree, i))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1335 && validate_small_stmt(CHILD(tree, i + 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1336 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1337 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1338 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1339
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1340
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1341 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1342 validate_small_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1343 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1344 int nch = NCH(tree);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1345 int res = (validate_numnodes(tree, 1, "small_stmt")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1346 && ((TYPE(CHILD(tree, 0)) == expr_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1347 || (TYPE(CHILD(tree, 0)) == print_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1348 || (TYPE(CHILD(tree, 0)) == del_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1349 || (TYPE(CHILD(tree, 0)) == pass_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1350 || (TYPE(CHILD(tree, 0)) == flow_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1351 || (TYPE(CHILD(tree, 0)) == import_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1352 || (TYPE(CHILD(tree, 0)) == global_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1353 || (TYPE(CHILD(tree, 0)) == assert_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1354 || (TYPE(CHILD(tree, 0)) == exec_stmt)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1355
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1356 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1357 res = validate_node(CHILD(tree, 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1358 else if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1359 char buffer[60];
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1360 (void) sprintf(buffer, "Unrecognized child node of small_stmt: %d.",
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1361 TYPE(CHILD(tree, 0)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1362 err_string(buffer);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1363 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1364 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1365 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1366
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1367
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1368 /* compound_stmt:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1369 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1370 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1371 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1372 validate_compound_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1373 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1374 int res = (validate_ntype(tree, compound_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1375 && validate_numnodes(tree, 1, "compound_stmt"));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1376
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1377 if (!res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1378 return (0);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1379
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1380 tree = CHILD(tree, 0);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1381 res = ((TYPE(tree) == if_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1382 || (TYPE(tree) == while_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1383 || (TYPE(tree) == for_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1384 || (TYPE(tree) == try_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1385 || (TYPE(tree) == funcdef)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1386 || (TYPE(tree) == classdef));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1387 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1388 res = validate_node(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1389 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1390 char buffer[60];
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1391 (void) sprintf(buffer, "Illegal compound statement type: %d.",
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1392 TYPE(tree));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1393 err_string(buffer);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1394 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1395 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1396 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1397
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1398
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1399 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1400 validate_expr_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1401 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1402 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1403 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1404 int res = (validate_ntype(tree, expr_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1405 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1406 && validate_testlist(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1407
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1408 for (j = 1; res && (j < nch); j += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1409 res = (validate_equal(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1410 && validate_testlist(CHILD(tree, j + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1411
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1412 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1413 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1414
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1415
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1416 /* print_stmt:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1417 *
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1418 * 'print' (test ',')* [test]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1419 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1420 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1421 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1422 validate_print_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1423 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1424 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1425 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1426 int res = (validate_ntype(tree, print_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1427 && (nch != 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1428 && validate_name(CHILD(tree, 0), "print"));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1429
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1430 if (res && is_even(nch)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1431 res = validate_test(CHILD(tree, nch - 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1432 --nch;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1433 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1434 else if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1435 (void) validate_numnodes(tree, 1, "print_stmt");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1436 for (j = 1; res && (j < nch); j += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1437 res = (validate_test(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1438 && validate_ntype(CHILD(tree, j + 1), COMMA));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1439
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1440 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1441 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1442
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1443
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1444 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1445 validate_del_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1446 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1447 return (validate_numnodes(tree, 2, "del_stmt")
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1448 && validate_name(CHILD(tree, 0), "del")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1449 && validate_exprlist(CHILD(tree, 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1450 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1451
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1452
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1453 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1454 validate_return_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1455 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1456 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1457 int res = (validate_ntype(tree, return_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1458 && ((nch == 1) || (nch == 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1459 && validate_name(CHILD(tree, 0), "return"));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1460
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1461 if (res && (nch == 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1462 res = validate_testlist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1463
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1464 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1465 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1466
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1467
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1468 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1469 validate_raise_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1470 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1471 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1472 int res = (validate_ntype(tree, raise_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1473 && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1474
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1475 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1476 res = validate_name(CHILD(tree, 0), "raise");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1477 if (res && (nch >= 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1478 res = validate_test(CHILD(tree, 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1479 if (res && nch > 2) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1480 res = (validate_comma(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1481 && validate_test(CHILD(tree, 3)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1482 if (res && (nch > 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1483 res = (validate_comma(CHILD(tree, 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1484 && validate_test(CHILD(tree, 5)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1485 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1486 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1487 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1488 (void) validate_numnodes(tree, 2, "raise");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1489 if (res && (nch == 4))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1490 res = (validate_comma(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1491 && validate_test(CHILD(tree, 3)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1492
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1493 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1494 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1495
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1496
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1497 /* import_stmt:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1498 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1499 * 'import' dotted_name (',' dotted_name)*
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1500 * | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1501 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1502 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1503 validate_import_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1504 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1505 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1506 int res = (validate_ntype(tree, import_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1507 && (nch >= 2) && is_even(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1508 && validate_ntype(CHILD(tree, 0), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1509 && validate_ntype(CHILD(tree, 1), dotted_name));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1510
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1511 if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1512 int j;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1513
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1514 for (j = 2; res && (j < nch); j += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1515 res = (validate_comma(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1516 && validate_ntype(CHILD(tree, j + 1), dotted_name));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1517 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1518 else if (res && validate_name(CHILD(tree, 0), "from")) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1519 res = ((nch >= 4) && is_even(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1520 && validate_name(CHILD(tree, 2), "import"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1521 if (nch == 4) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1522 res = ((TYPE(CHILD(tree, 3)) == NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1523 || (TYPE(CHILD(tree, 3)) == STAR));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1524 if (!res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1525 err_string("Illegal import statement.");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1526 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1527 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1528 /* 'from' NAME 'import' NAME (',' NAME)+ */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1529 int j;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1530 res = validate_ntype(CHILD(tree, 3), NAME);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1531 for (j = 4; res && (j < nch); j += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1532 res = (validate_comma(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1533 && validate_ntype(CHILD(tree, j + 1), NAME));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1534 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1535 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1536 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1537 res = 0;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1538
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1539 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1540 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1541
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1542
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1543 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1544 validate_global_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1545 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1546 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1547 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1548 int res = (validate_ntype(tree, global_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1549 && is_even(nch) && (nch >= 2));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1550
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1551 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1552 res = (validate_name(CHILD(tree, 0), "global")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1553 && validate_ntype(CHILD(tree, 1), NAME));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1554 for (j = 2; res && (j < nch); j += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1555 res = (validate_comma(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1556 && validate_ntype(CHILD(tree, j + 1), NAME));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1557
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1558 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1559 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1560
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1561
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1562 /* exec_stmt:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1563 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1564 * 'exec' expr ['in' test [',' test]]
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1565 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1566 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1567 validate_exec_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1568 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1569 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1570 int res = (validate_ntype(tree, exec_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1571 && ((nch == 2) || (nch == 4) || (nch == 6))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1572 && validate_name(CHILD(tree, 0), "exec")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1573 && validate_expr(CHILD(tree, 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1574
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1575 if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1576 err_string("Illegal exec statement.");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1577 if (res && (nch > 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1578 res = (validate_name(CHILD(tree, 2), "in")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1579 && validate_test(CHILD(tree, 3)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1580 if (res && (nch == 6))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1581 res = (validate_comma(CHILD(tree, 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1582 && validate_test(CHILD(tree, 5)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1583
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1584 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1585 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1586
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1587
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1588 /* assert_stmt:
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1589 *
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1590 * 'assert' test [',' test]
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1591 */
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1592 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1593 validate_assert_stmt(node *tree)
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1594 {
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1595 int nch = NCH(tree);
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1596 int res = (validate_ntype(tree, assert_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1597 && ((nch == 2) || (nch == 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1598 && (validate_name(CHILD(tree, 0), "__assert__") ||
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1599 validate_name(CHILD(tree, 0), "assert"))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1600 && validate_test(CHILD(tree, 1)));
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1601
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1602 if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1603 err_string("Illegal assert statement.");
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1604 if (res && (nch > 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1605 res = (validate_comma(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1606 && validate_test(CHILD(tree, 3)));
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1607
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1608 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1609 }
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1610
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1611
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1612 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1613 validate_while(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1614 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1615 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1616 int res = (validate_ntype(tree, while_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1617 && ((nch == 4) || (nch == 7))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1618 && validate_name(CHILD(tree, 0), "while")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1619 && validate_test(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1620 && validate_colon(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1621 && validate_suite(CHILD(tree, 3)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1622
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1623 if (res && (nch == 7))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1624 res = (validate_name(CHILD(tree, 4), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1625 && validate_colon(CHILD(tree, 5))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1626 && validate_suite(CHILD(tree, 6)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1627
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1628 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1629 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1630
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1631
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1632 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1633 validate_for(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1634 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1635 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1636 int res = (validate_ntype(tree, for_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1637 && ((nch == 6) || (nch == 9))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1638 && validate_name(CHILD(tree, 0), "for")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1639 && validate_exprlist(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1640 && validate_name(CHILD(tree, 2), "in")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1641 && validate_testlist(CHILD(tree, 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1642 && validate_colon(CHILD(tree, 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1643 && validate_suite(CHILD(tree, 5)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1644
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1645 if (res && (nch == 9))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1646 res = (validate_name(CHILD(tree, 6), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1647 && validate_colon(CHILD(tree, 7))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1648 && validate_suite(CHILD(tree, 8)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1649
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1650 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1651 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1652
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1653
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1654 /* try_stmt:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1655 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1656 * | 'try' ':' suite 'finally' ':' suite
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1657 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1658 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1659 static int
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1660 validate_try(tree)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1661 node *tree;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1662 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1663 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1664 int pos = 3;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1665 int res = (validate_ntype(tree, try_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1666 && (nch >= 6) && ((nch % 3) == 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1667
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1668 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1669 res = (validate_name(CHILD(tree, 0), "try")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1670 && validate_colon(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1671 && validate_suite(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1672 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1673 && validate_suite(CHILD(tree, nch - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1674 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1675 const char* name = "execpt";
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1676 char buffer[60];
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1677 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1678 name = STR(CHILD(tree, nch - 3));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1679 (void) sprintf(buffer,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1680 "Illegal number of children for try/%s node.", name);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1681 err_string(buffer);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1682 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1683 /* Skip past except_clause sections: */
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1684 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1685 res = (validate_except_clause(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1686 && validate_colon(CHILD(tree, pos + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1687 && validate_suite(CHILD(tree, pos + 2)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1688 pos += 3;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1689 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1690 if (res && (pos < nch)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1691 res = validate_ntype(CHILD(tree, pos), NAME);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1692 if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1693 res = (validate_numnodes(tree, 6, "try/finally")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1694 && validate_colon(CHILD(tree, 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1695 && validate_suite(CHILD(tree, 5)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1696 else if (res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1697 if (nch == (pos + 3)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1698 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1699 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1700 if (!res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1701 err_string("Illegal trailing triple in try statement.");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1702 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1703 else if (nch == (pos + 6)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1704 res = (validate_name(CHILD(tree, pos), "except")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1705 && validate_colon(CHILD(tree, pos + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1706 && validate_suite(CHILD(tree, pos + 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1707 && validate_name(CHILD(tree, pos + 3), "else"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1708 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1709 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1710 res = validate_numnodes(tree, pos + 3, "try/except");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1711 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1712 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1713 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1714 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1715
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1716
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1717 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1718 validate_except_clause(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1719 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1720 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1721 int res = (validate_ntype(tree, except_clause)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1722 && ((nch == 1) || (nch == 2) || (nch == 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1723 && validate_name(CHILD(tree, 0), "except"));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1724
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1725 if (res && (nch > 1))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1726 res = validate_test(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1727 if (res && (nch == 4))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1728 res = (validate_comma(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1729 && validate_test(CHILD(tree, 3)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1730
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1731 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1732 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1733
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1734
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1735 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1736 validate_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1737 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1738 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1739 int res = validate_ntype(tree, test) && is_odd(nch);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1740
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1741 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1742 res = ((nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1743 && validate_lambdef(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1744 else if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1745 int pos;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1746 res = validate_and_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1747 for (pos = 1; res && (pos < nch); pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1748 res = (validate_name(CHILD(tree, pos), "or")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1749 && validate_and_test(CHILD(tree, pos + 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1750 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1751 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1752 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1753
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1754
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1755 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1756 validate_and_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1757 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1758 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1759 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1760 int res = (validate_ntype(tree, and_test)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1761 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1762 && validate_not_test(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1763
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1764 for (pos = 1; res && (pos < nch); pos += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1765 res = (validate_name(CHILD(tree, pos), "and")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1766 && validate_not_test(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1767
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1768 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1769 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1770
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1771
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1772 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1773 validate_not_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1774 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1775 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1776 int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1777
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1778 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1779 if (nch == 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1780 res = (validate_name(CHILD(tree, 0), "not")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1781 && validate_not_test(CHILD(tree, 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1782 else if (nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1783 res = validate_comparison(CHILD(tree, 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1784 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1785 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1786 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1787
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1788
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1789 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1790 validate_comparison(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1791 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1792 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1793 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1794 int res = (validate_ntype(tree, comparison)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1795 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1796 && validate_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1797
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1798 for (pos = 1; res && (pos < nch); pos += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1799 res = (validate_comp_op(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1800 && validate_expr(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1801
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1802 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1803 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1804
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1805
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1806 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1807 validate_comp_op(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1808 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1809 int res = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1810 int nch = NCH(tree);
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1811
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1812 if (!validate_ntype(tree, comp_op))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1813 return (0);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1814 if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1815 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1816 * Only child will be a terminal with a well-defined symbolic name
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1817 * or a NAME with a string of either 'is' or 'in'
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1818 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1819 tree = CHILD(tree, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1820 switch (TYPE(tree)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1821 case LESS:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1822 case GREATER:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1823 case EQEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1824 case EQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1825 case LESSEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1826 case GREATEREQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1827 case NOTEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1828 res = 1;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1829 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1830 case NAME:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1831 res = ((strcmp(STR(tree), "in") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1832 || (strcmp(STR(tree), "is") == 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1833 if (!res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1834 char buff[128];
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1835 (void) sprintf(buff, "Illegal operator: '%s'.", STR(tree));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1836 err_string(buff);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1837 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1838 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1839 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1840 err_string("Illegal comparison operator type.");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1841 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1842 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1843 }
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1844 else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1845 res = (validate_ntype(CHILD(tree, 0), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1846 && validate_ntype(CHILD(tree, 1), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1847 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1848 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1849 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1850 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1851 if (!res && !PyErr_Occurred())
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1852 err_string("Unknown comparison operator.");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1853 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1854 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1855 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1856
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1857
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1858 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1859 validate_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1860 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1861 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1862 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1863 int res = (validate_ntype(tree, expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1864 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1865 && validate_xor_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1866
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1867 for (j = 2; res && (j < nch); j += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1868 res = (validate_xor_expr(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1869 && validate_vbar(CHILD(tree, j - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1870
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1871 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1872 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1873
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1874
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1875 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1876 validate_xor_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1877 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1878 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1879 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1880 int res = (validate_ntype(tree, xor_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1881 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1882 && validate_and_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1883
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1884 for (j = 2; res && (j < nch); j += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1885 res = (validate_circumflex(CHILD(tree, j - 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1886 && validate_and_expr(CHILD(tree, j)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1887
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1888 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1889 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1890
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1891
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1892 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1893 validate_and_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1894 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1895 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1896 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1897 int res = (validate_ntype(tree, and_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1898 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1899 && validate_shift_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1900
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1901 for (pos = 1; res && (pos < nch); pos += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1902 res = (validate_ampersand(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1903 && validate_shift_expr(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1904
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1905 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1906 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1907
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1908
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1909 static int
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1910 validate_chain_two_ops(tree, termvalid, op1, op2)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1911 node *tree;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1912 int (*termvalid)();
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1913 int op1;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1914 int op2;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1915 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1916 int pos = 1;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1917 int nch = NCH(tree);
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1918 int res = (is_odd(nch)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1919 && (*termvalid)(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1920
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1921 for ( ; res && (pos < nch); pos += 2) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1922 if (TYPE(CHILD(tree, pos)) != op1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1923 res = validate_ntype(CHILD(tree, pos), op2);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1924 if (res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1925 res = (*termvalid)(CHILD(tree, pos + 1));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1926 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1927 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1928 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1929
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1930
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1931 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1932 validate_shift_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1933 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1934 return (validate_ntype(tree, shift_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1935 && validate_chain_two_ops(tree, validate_arith_expr,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1936 LEFTSHIFT, RIGHTSHIFT));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1937 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1938
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1939
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1940 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1941 validate_arith_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1942 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1943 return (validate_ntype(tree, arith_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1944 && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1945 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1946
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1947
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1948 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1949 validate_term(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1950 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1951 int pos = 1;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1952 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1953 int res = (validate_ntype(tree, term)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1954 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1955 && validate_factor(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1956
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1957 for ( ; res && (pos < nch); pos += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1958 res = (((TYPE(CHILD(tree, pos)) == STAR)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1959 || (TYPE(CHILD(tree, pos)) == SLASH)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1960 || (TYPE(CHILD(tree, pos)) == PERCENT))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1961 && validate_factor(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1962
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1963 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1964 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1965
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1966
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1967 /* factor:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1968 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1969 * factor: ('+'|'-'|'~') factor | power
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1970 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1971 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1972 validate_factor(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1973 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1974 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1975 int res = (validate_ntype(tree, factor)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1976 && (((nch == 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1977 && ((TYPE(CHILD(tree, 0)) == PLUS)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1978 || (TYPE(CHILD(tree, 0)) == MINUS)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1979 || (TYPE(CHILD(tree, 0)) == TILDE))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1980 && validate_factor(CHILD(tree, 1)))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1981 || ((nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1982 && validate_power(CHILD(tree, 0)))));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1983 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1984 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1985
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1986
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1987 /* power:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1988 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1989 * power: atom trailer* ('**' factor)*
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1990 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1991 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1992 validate_power(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1993 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1994 int pos = 1;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1995 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1996 int res = (validate_ntype(tree, power) && (nch >= 1)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1997 && validate_atom(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1998
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1999 while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2000 res = validate_trailer(CHILD(tree, pos++));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2001 if (res && (pos < nch)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2002 if (!is_even(nch - pos)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2003 err_string("Illegal number of nodes for 'power'.");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2004 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2005 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2006 for ( ; res && (pos < (nch - 1)); pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2007 res = (validate_doublestar(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2008 && validate_factor(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2009 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2010 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2011 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2012
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2013
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2014 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2015 validate_atom(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2016 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2017 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2018 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2019 int res = validate_ntype(tree, atom) && (nch >= 1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2020
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2021 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2022 switch (TYPE(CHILD(tree, 0))) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2023 case LPAR:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2024 res = ((nch <= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2025 && (validate_rparen(CHILD(tree, nch - 1))));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2026
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2027 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2028 res = validate_testlist(CHILD(tree, 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2029 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2030 case LSQB:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2031 res = ((nch <= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2032 && validate_ntype(CHILD(tree, nch - 1), RSQB));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2033
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2034 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2035 res = validate_testlist(CHILD(tree, 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2036 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2037 case LBRACE:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2038 res = ((nch <= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2039 && validate_ntype(CHILD(tree, nch - 1), RBRACE));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2040
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2041 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2042 res = validate_dictmaker(CHILD(tree, 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2043 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2044 case BACKQUOTE:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2045 res = ((nch == 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2046 && validate_testlist(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2047 && validate_ntype(CHILD(tree, 2), BACKQUOTE));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2048 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2049 case NAME:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2050 case NUMBER:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2051 res = (nch == 1);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2052 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2053 case STRING:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2054 for (pos = 1; res && (pos < nch); ++pos)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2055 res = validate_ntype(CHILD(tree, pos), STRING);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2056 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2057 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2058 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2059 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2060 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2061 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2062 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2063 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2064
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2065
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2066 /* funcdef:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2067 * 'def' NAME parameters ':' suite
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2068 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2069 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2070 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2071 validate_funcdef(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2072 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2073 return (validate_ntype(tree, funcdef)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2074 && validate_numnodes(tree, 5, "funcdef")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2075 && validate_name(CHILD(tree, 0), "def")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2076 && validate_ntype(CHILD(tree, 1), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2077 && validate_colon(CHILD(tree, 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2078 && validate_parameters(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2079 && validate_suite(CHILD(tree, 4)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2080 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2081
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2082
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2083 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2084 validate_lambdef(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2085 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2086 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2087 int res = (validate_ntype(tree, lambdef)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2088 && ((nch == 3) || (nch == 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2089 && validate_name(CHILD(tree, 0), "lambda")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2090 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2091 && validate_test(CHILD(tree, nch - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2092
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2093 if (res && (nch == 4))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2094 res = validate_varargslist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2095 else if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2096 (void) validate_numnodes(tree, 3, "lambdef");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2097
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2098 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2099 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2100
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2101
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2102 /* arglist:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2103 *
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2104 * (argument ',')* (argument* [','] | '*' test [',' '**' test] | '**' test)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2105 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2106 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2107 validate_arglist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2108 {
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2109 int nch = NCH(tree);
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2110 int i, ok;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2111 node *last;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2112
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2113 if (nch <= 0)
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2114 /* raise the right error from having an invalid number of children */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2115 return validate_numnodes(tree, nch + 1, "arglist");
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2116
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2117 last = CHILD(tree, nch - 1);
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2118 if (TYPE(last) == test) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2119 /* Extended call syntax introduced in Python 1.6 has been used;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2120 * validate and strip that off and continue;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2121 * adjust nch to perform the cut, and ensure resulting nch is even
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2122 * (validation of the first part doesn't require that).
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2123 */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2124 if (nch < 2) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2125 validate_numnodes(tree, nch + 1, "arglist");
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2126 return 0;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2127 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2128 ok = validate_test(last);
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2129 if (ok) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2130 node *prev = CHILD(tree, nch - 2);
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2131 /* next must be '*' or '**' */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2132 if (validate_doublestar(prev)) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2133 nch -= 2;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2134 if (nch >= 3) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2135 /* may include: '*' test ',' */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2136 last = CHILD(tree, nch - 1);
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2137 prev = CHILD(tree, nch - 2);
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2138 if (TYPE(prev) == test) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2139 ok = validate_comma(last)
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2140 && validate_test(prev)
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2141 && validate_star(CHILD(tree, nch - 3));
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2142 if (ok)
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2143 nch -= 3;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2144 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2145 /* otherwise, nothing special */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2146 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2147 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2148 else {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2149 /* must be only: '*' test */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2150 PyErr_Clear();
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2151 ok = validate_star(prev);
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2152 nch -= 2;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2153 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2154 if (ok && is_odd(nch)) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2155 /* Illegal number of nodes before extended call syntax;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2156 * validation of the "normal" arguments does not require
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2157 * a trailing comma, but requiring an even number of
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2158 * children will effect the same requirement.
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2159 */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2160 return validate_numnodes(tree, nch + 1, "arglist");
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2161 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2162 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2163 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2164 /* what remains must be: (argument ",")* [argument [","]] */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2165 i = 0;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2166 while (ok && nch - i >= 2) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2167 ok = validate_argument(CHILD(tree, i))
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2168 && validate_comma(CHILD(tree, i + 1));
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2169 i += 2;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2170 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2171 if (ok && i < nch) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2172 ok = validate_comma(CHILD(tree, i));
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2173 ++i;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2174 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2175 if (i != nch) {
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2176 /* internal error! */
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2177 ok = 0;
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2178 err_string("arglist: internal error; nch != i");
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2179 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2180 return (ok);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2181 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2182
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2183
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2184
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2185 /* argument:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2186 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2187 * [test '='] test
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2188 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2189 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2190 validate_argument(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2191 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2192 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2193 int res = (validate_ntype(tree, argument)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2194 && ((nch == 1) || (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2195 && validate_test(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2196
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2197 if (res && (nch == 3))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2198 res = (validate_equal(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2199 && validate_test(CHILD(tree, 2)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2200
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2201 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2202 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2203
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2204
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2205
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2206 /* trailer:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2207 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2208 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2209 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2210 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2211 validate_trailer(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2212 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2213 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2214 int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
2215
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2216 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2217 switch (TYPE(CHILD(tree, 0))) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2218 case LPAR:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2219 res = validate_rparen(CHILD(tree, nch - 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2220 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2221 res = validate_arglist(CHILD(tree, 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2222 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2223 case LSQB:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2224 res = (validate_numnodes(tree, 3, "trailer")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2225 && validate_subscriptlist(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2226 && validate_ntype(CHILD(tree, 2), RSQB));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2227 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2228 case DOT:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2229 res = (validate_numnodes(tree, 2, "trailer")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2230 && validate_ntype(CHILD(tree, 1), NAME));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2231 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2232 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2233 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2234 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2235 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2236 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2237 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2238 (void) validate_numnodes(tree, 2, "trailer");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2239 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2240 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2241 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2242
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2243
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2244 /* subscriptlist:
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2245 *
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2246 * subscript (',' subscript)* [',']
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2247 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2248 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2249 validate_subscriptlist(node *tree)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2250 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2251 return (validate_repeating_list(tree, subscriptlist,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2252 validate_subscript, "subscriptlist"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2253 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2254
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2255
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2256 /* subscript:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2257 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2258 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2259 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2260 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2261 validate_subscript(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2262 {
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2263 int offset = 0;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2264 int nch = NCH(tree);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2265 int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2266
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2267 if (!res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2268 if (!PyErr_Occurred())
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2269 err_string("invalid number of arguments for subscript node");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2270 return (0);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2271 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2272 if (TYPE(CHILD(tree, 0)) == DOT)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2273 /* take care of ('.' '.' '.') possibility */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2274 return (validate_numnodes(tree, 3, "subscript")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2275 && validate_dot(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2276 && validate_dot(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2277 && validate_dot(CHILD(tree, 2)));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2278 if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2279 if (TYPE(CHILD(tree, 0)) == test)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2280 res = validate_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2281 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2282 res = validate_colon(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2283 return (res);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2284 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2285 /* Must be [test] ':' [test] [sliceop],
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2286 * but at least one of the optional components will
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2287 * be present, but we don't know which yet.
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2288 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2289 if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2290 res = validate_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2291 offset = 1;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2292 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2293 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2294 res = validate_colon(CHILD(tree, offset));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2295 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2296 int rem = nch - ++offset;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2297 if (rem) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2298 if (TYPE(CHILD(tree, offset)) == test) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2299 res = validate_test(CHILD(tree, offset));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2300 ++offset;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2301 --rem;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2302 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2303 if (res && rem)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2304 res = validate_sliceop(CHILD(tree, offset));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2305 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2306 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2307 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2308 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2309
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2310
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2311 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2312 validate_sliceop(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2313 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2314 int nch = NCH(tree);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2315 int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2316 && validate_ntype(tree, sliceop);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2317 if (!res && !PyErr_Occurred()) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2318 res = validate_numnodes(tree, 1, "sliceop");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2319 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2320 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2321 res = validate_colon(CHILD(tree, 0));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2322 if (res && (nch == 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2323 res = validate_test(CHILD(tree, 1));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2324
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2325 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2326 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2327
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2328
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2329 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2330 validate_exprlist(node *tree)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2331 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2332 return (validate_repeating_list(tree, exprlist,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2333 validate_expr, "exprlist"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2334 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2335
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2336
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2337 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2338 validate_dictmaker(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2339 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2340 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2341 int res = (validate_ntype(tree, dictmaker)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2342 && (nch >= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2343 && validate_test(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2344 && validate_colon(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2345 && validate_test(CHILD(tree, 2)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2346
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2347 if (res && ((nch % 4) == 0))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2348 res = validate_comma(CHILD(tree, --nch));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2349 else if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2350 res = ((nch % 4) == 3);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2351
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2352 if (res && (nch > 3)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2353 int pos = 3;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2354 /* ( ',' test ':' test )* */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2355 while (res && (pos < nch)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2356 res = (validate_comma(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2357 && validate_test(CHILD(tree, pos + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2358 && validate_colon(CHILD(tree, pos + 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2359 && validate_test(CHILD(tree, pos + 3)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2360 pos += 4;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2361 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2362 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2363 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2364 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2365
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2366
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2367 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2368 validate_eval_input(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2369 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2370 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2371 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2372 int res = (validate_ntype(tree, eval_input)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2373 && (nch >= 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2374 && validate_testlist(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2375 && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2376
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2377 for (pos = 1; res && (pos < (nch - 1)); ++pos)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2378 res = validate_ntype(CHILD(tree, pos), NEWLINE);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2379
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2380 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2381 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2382
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2383
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2384 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2385 validate_node(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2386 {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2387 int nch = 0; /* num. children on current node */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2388 int res = 1; /* result value */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2389 node* next = 0; /* node to process after this one */
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2390
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2391 while (res & (tree != 0)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2392 nch = NCH(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2393 next = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2394 switch (TYPE(tree)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2395 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2396 * Definition nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2397 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2398 case funcdef:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2399 res = validate_funcdef(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2400 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2401 case classdef:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2402 res = validate_class(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2403 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2404 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2405 * "Trivial" parse tree nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2406 * (Why did I call these trivial?)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2407 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2408 case stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2409 res = validate_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2410 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2411 case small_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2412 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2413 * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2414 * | import_stmt | global_stmt | exec_stmt | assert_stmt
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2415 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2416 res = validate_small_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2417 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2418 case flow_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2419 res = (validate_numnodes(tree, 1, "flow_stmt")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2420 && ((TYPE(CHILD(tree, 0)) == break_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2421 || (TYPE(CHILD(tree, 0)) == continue_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2422 || (TYPE(CHILD(tree, 0)) == return_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2423 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2424 if (res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2425 next = CHILD(tree, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2426 else if (nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2427 err_string("Illegal flow_stmt type.");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2428 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2429 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2430 * Compound statements.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2431 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2432 case simple_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2433 res = validate_simple_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2434 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2435 case compound_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2436 res = validate_compound_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2437 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2438 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2439 * Fundemental statements.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2440 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2441 case expr_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2442 res = validate_expr_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2443 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2444 case print_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2445 res = validate_print_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2446 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2447 case del_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2448 res = validate_del_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2449 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2450 case pass_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2451 res = (validate_numnodes(tree, 1, "pass")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2452 && validate_name(CHILD(tree, 0), "pass"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2453 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2454 case break_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2455 res = (validate_numnodes(tree, 1, "break")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2456 && validate_name(CHILD(tree, 0), "break"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2457 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2458 case continue_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2459 res = (validate_numnodes(tree, 1, "continue")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2460 && validate_name(CHILD(tree, 0), "continue"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2461 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2462 case return_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2463 res = validate_return_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2464 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2465 case raise_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2466 res = validate_raise_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2467 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2468 case import_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2469 res = validate_import_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2470 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2471 case global_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2472 res = validate_global_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2473 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2474 case exec_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2475 res = validate_exec_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2476 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2477 case assert_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2478 res = validate_assert_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2479 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2480 case if_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2481 res = validate_if(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2482 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2483 case while_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2484 res = validate_while(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2485 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2486 case for_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2487 res = validate_for(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2488 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2489 case try_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2490 res = validate_try(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2491 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2492 case suite:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2493 res = validate_suite(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2494 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2495 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2496 * Expression nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2497 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2498 case testlist:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2499 res = validate_testlist(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2500 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2501 case test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2502 res = validate_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2503 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2504 case and_test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2505 res = validate_and_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2506 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2507 case not_test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2508 res = validate_not_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2509 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2510 case comparison:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2511 res = validate_comparison(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2512 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2513 case exprlist:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2514 res = validate_exprlist(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2515 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2516 case comp_op:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2517 res = validate_comp_op(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2518 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2519 case expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2520 res = validate_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2521 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2522 case xor_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2523 res = validate_xor_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2524 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2525 case and_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2526 res = validate_and_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2527 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2528 case shift_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2529 res = validate_shift_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2530 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2531 case arith_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2532 res = validate_arith_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2533 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2534 case term:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2535 res = validate_term(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2536 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2537 case factor:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2538 res = validate_factor(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2539 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2540 case power:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2541 res = validate_power(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2542 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2543 case atom:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2544 res = validate_atom(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2545 break;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2546
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2547 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2548 /* Hopefully never reached! */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2549 err_string("Unrecogniged node type.");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2550 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2551 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2552 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2553 tree = next;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2554 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2555 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2556 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2557
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2558
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2559 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2560 validate_expr_tree(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2561 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2562 int res = validate_eval_input(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2563
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2564 if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2565 err_string("Could not validate expression tuple.");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2566
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2567 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2568 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2569
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2570
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2571 /* file_input:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2572 * (NEWLINE | stmt)* ENDMARKER
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2573 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2574 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2575 validate_file_input(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2576 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2577 int j = 0;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2578 int nch = NCH(tree) - 1;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2579 int res = ((nch >= 0)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2580 && validate_ntype(CHILD(tree, nch), ENDMARKER));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2581
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2582 for ( ; res && (j < nch); ++j) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2583 if (TYPE(CHILD(tree, j)) == stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2584 res = validate_stmt(CHILD(tree, j));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2585 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2586 res = validate_newline(CHILD(tree, j));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2587 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2588 /* This stays in to prevent any internal failues from getting to the
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2589 * user. Hopefully, this won't be needed. If a user reports getting
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2590 * this, we have some debugging to do.
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2591 */
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2592 if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2593 err_string("VALIDATION FAILURE: report this to the maintainer!.");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2594
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2595 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2596 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2597
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2598
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2599 static PyObject*
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2600 pickle_constructor = NULL;
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2601
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2602
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2603 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2604 parser__pickler(PyObject *self, PyObject *args)
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2605 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
2606 NOTE(ARGUNUSED(self))
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2607 PyObject *result = NULL;
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2608 PyObject *ast = NULL;
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2609 PyObject *empty_dict = NULL;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2610
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2611 if (PyArg_ParseTuple(args, "O!:_pickler", &PyAST_Type, &ast)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2612 PyObject *newargs;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2613 PyObject *tuple;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2614
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2615 if ((empty_dict = PyDict_New()) == NULL)
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2616 goto finally;
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2617 if ((newargs = Py_BuildValue("Oi", ast, 1)) == NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2618 goto finally;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2619 tuple = parser_ast2tuple((PyAST_Object*)NULL, newargs, empty_dict);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2620 if (tuple != NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2621 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2622 Py_DECREF(tuple);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2623 }
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2624 Py_DECREF(empty_dict);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2625 Py_DECREF(newargs);
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2626 }
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2627 finally:
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2628 Py_XDECREF(empty_dict);
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2629
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2630 return (result);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2631 }
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2632
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2633
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2634 /* Functions exported by this module. Most of this should probably
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2635 * be converted into an AST object with methods, but that is better
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2636 * done directly in Python, allowing subclasses to be created directly.
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2637 * We'd really have to write a wrapper around it all anyway to allow
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2638 * inheritance.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2639 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2640 static PyMethodDef parser_functions[] = {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2641 {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2642 "Creates a tuple-tree representation of an AST."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2643 {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2644 "Creates a list-tree representation of an AST."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2645 {"compileast", (PyCFunction)parser_compileast, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2646 "Compiles an AST object into a code object."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2647 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2648 "Creates an AST object from an expression."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2649 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2650 "Determines if an AST object was created from an expression."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2651 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2652 "Determines if an AST object was created from a suite."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2653 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2654 "Creates an AST object from a suite."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2655 {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2656 "Creates an AST object from a tree representation."},
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2657 {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2658 "Creates an AST object from a tree representation."},
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2659
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2660 /* private stuff: support pickle module */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2661 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2662 "Returns the pickle magic to allow ast objects to be pickled."},
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2663
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
2664 {NULL, NULL, 0, NULL}
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2665 };
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2666
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2667
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 8813
diff changeset
2668 DL_EXPORT(void)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2669 initparser()
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2670 {
5039
38032267968c Initialize the ob_type field of PyAST_Type dynamically (in
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
2671 PyObject* module;
38032267968c Initialize the ob_type field of PyAST_Type dynamically (in
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
2672 PyObject* dict;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2673
5039
38032267968c Initialize the ob_type field of PyAST_Type dynamically (in
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
2674 PyAST_Type.ob_type = &PyType_Type;
38032267968c Initialize the ob_type field of PyAST_Type dynamically (in
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
2675 module = Py_InitModule("parser", parser_functions);
38032267968c Initialize the ob_type field of PyAST_Type dynamically (in
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
2676 dict = PyModule_GetDict(module);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2677
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
2678 if (parser_error == 0)
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
2679 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
2680 else
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
2681 puts("parser module initialized more than once!");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2682
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2683 if ((parser_error == 0)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2684 || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2685 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2686 * This is serious.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2687 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2688 Py_FatalError("can't define parser.ParserError");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2689 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2690 /*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2691 * Nice to have, but don't cry if we fail.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2692 */
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2693 Py_INCREF(&PyAST_Type);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2694 PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2695
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2696 PyDict_SetItemString(dict, "__copyright__",
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2697 PyString_FromString(parser_copyright_string));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2698 PyDict_SetItemString(dict, "__doc__",
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2699 PyString_FromString(parser_doc_string));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2700 PyDict_SetItemString(dict, "__version__",
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2701 PyString_FromString(parser_version_string));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2702
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2703 /* register to support pickling */
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2704 module = PyImport_ImportModule("copy_reg");
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2705 if (module != NULL) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2706 PyObject *func, *pickler;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2707
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2708 func = PyObject_GetAttrString(module, "pickle");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2709 pickle_constructor = PyDict_GetItemString(dict, "sequence2ast");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2710 pickler = PyDict_GetItemString(dict, "_pickler");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2711 Py_XINCREF(pickle_constructor);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2712 if ((func != NULL) && (pickle_constructor != NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2713 && (pickler != NULL)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2714 PyObject *res;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2715
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2716 res = PyObject_CallFunction(
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2717 func, "OOO", &PyAST_Type, pickler, pickle_constructor);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2718 Py_XDECREF(res);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2719 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2720 Py_XDECREF(func);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2721 Py_DECREF(module);
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2722 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2723 }