annotate Modules/parsermodule.c @ 13667:a6c7bfdd6777 legacy-trunk

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