annotate Modules/parsermodule.c @ 12357:5b59ea41db4e legacy-trunk

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