annotate Modules/parsermodule.c @ 46644:ea594306ea85

Issue #1950: Fixed misusage of PyUnicode_AsString().
author Alexandre Vassalotti <alexandre@peadrop.com>
date Sat, 03 May 2008 18:24:43 +0000
parents b91ca87e6f32
children dd5513b472dc
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 */
21237
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
31 #include "errcode.h" /* error codes for PyNode_*() */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
32 #include "token.h" /* token definitions */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
33 /* ISTERMINAL() / ISNONTERMINAL() */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
34 #include "compile.h" /* PyNode_Compile() */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
35
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
36 #ifdef lint
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
37 #include <note.h>
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
38 #else
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
39 #define NOTE(x)
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
40 #endif
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
41
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
42 /* String constants used to initialize module attributes.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
43 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
44 */
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
45 static char parser_copyright_string[] =
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
46 "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
4099
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
47 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
48 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
49 Centrum, Amsterdam, The Netherlands.";
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
50
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
51
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
52 PyDoc_STRVAR(parser_doc_string,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
53 "This is an interface to Python's internal parser.");
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
54
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
55 static char parser_version_string[] = "0.5";
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
56
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
57
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
58 typedef PyObject* (*SeqMaker) (Py_ssize_t length);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
59 typedef int (*SeqInserter) (PyObject* sequence,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
60 Py_ssize_t index,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
61 PyObject* element);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
62
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
63 /* 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
64 * 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
65 * 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
66 * 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
67 * 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
68 * 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
69 * 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
70 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
71
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
73 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
74 node2tuple(node *n, /* node to convert */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
75 SeqMaker mkseq, /* create sequence */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
76 SeqInserter addelem, /* func. to add elem. in seq. */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
77 int lineno, /* include line numbers? */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
78 int col_offset) /* include column offsets? */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
79 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
80 if (n == NULL) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
81 Py_INCREF(Py_None);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
82 return (Py_None);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
83 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
84 if (ISNONTERMINAL(TYPE(n))) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
85 int i;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
86 PyObject *v;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
87 PyObject *w;
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
88
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24796
diff changeset
89 v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
90 if (v == NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
91 return (v);
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
92 w = PyLong_FromLong(TYPE(n));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
93 if (w == NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
94 Py_DECREF(v);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
95 return ((PyObject*) NULL);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
96 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
97 (void) addelem(v, 0, w);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
98 for (i = 0; i < NCH(n); i++) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
99 w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
100 if (w == NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
101 Py_DECREF(v);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
102 return ((PyObject*) NULL);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
103 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
104 (void) addelem(v, i+1, w);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
105 }
29952
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
106
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24796
diff changeset
107 if (TYPE(n) == encoding_decl)
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42535
diff changeset
108 (void) addelem(v, i+1, PyUnicode_FromString(STR(n)));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
109 return (v);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
110 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
111 else if (ISTERMINAL(TYPE(n))) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
112 PyObject *result = mkseq(2 + lineno + col_offset);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
113 if (result != NULL) {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
114 (void) addelem(result, 0, PyLong_FromLong(TYPE(n)));
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42535
diff changeset
115 (void) addelem(result, 1, PyUnicode_FromString(STR(n)));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
116 if (lineno == 1)
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
117 (void) addelem(result, 2, PyLong_FromLong(n->n_lineno));
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
118 if (col_offset == 1)
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
119 (void) addelem(result, 3, PyLong_FromLong(n->n_col_offset));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
120 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
121 return (result);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
122 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
123 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
124 PyErr_SetString(PyExc_SystemError,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
125 "unrecognized parse tree node type");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
126 return ((PyObject*) NULL);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
127 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
128 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
129 /*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
130 * 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
131 */
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
133
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
134
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
135 /* There are two types of intermediate objects we're interested in:
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
136 * 'eval' and 'exec' types. These constants can be used in the st_type
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
137 * 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
138 * 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
139 * 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
140 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
141
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
142 #define PyST_EXPR 1
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
143 #define PyST_SUITE 2
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
144
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
145
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
146 /* These are the internal objects and definitions required to implement the
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
147 * ST type. Most of the internal names are more reminiscent of the 'old'
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
148 * 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
149 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
150
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
151 static PyObject*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
152 parser_error = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
153
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
154
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
155 typedef struct {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
156 PyObject_HEAD /* standard object header */
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
157 node* st_node; /* the node* returned by the parser */
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
158 int st_type; /* EXPR or SUITE ? */
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
159 } PyST_Object;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
160
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
161
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
162 static void parser_free(PyST_Object *st);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
163 static int parser_compare(PyST_Object *left, PyST_Object *right);
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
164 static PyObject *parser_getattr(PyObject *self, char *name);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
165
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
166
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
167 static
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
168 PyTypeObject PyST_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41824
diff changeset
169 PyVarObject_HEAD_INIT(NULL, 0)
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 21237
diff changeset
170 "parser.st", /* tp_name */
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
171 (int) sizeof(PyST_Object), /* tp_basicsize */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
172 0, /* tp_itemsize */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
173 (destructor)parser_free, /* tp_dealloc */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
174 0, /* tp_print */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
175 parser_getattr, /* tp_getattr */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
176 0, /* tp_setattr */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
177 (cmpfunc)parser_compare, /* tp_compare */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
178 0, /* tp_repr */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
179 0, /* tp_as_number */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
180 0, /* tp_as_sequence */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
181 0, /* tp_as_mapping */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
182 0, /* tp_hash */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
183 0, /* tp_call */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
184 0, /* tp_str */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
185 0, /* tp_getattro */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
186 0, /* tp_setattro */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
187
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
188 /* 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
189 0, /* tp_as_buffer */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
190
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
191 Py_TPFLAGS_DEFAULT, /* tp_flags */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
192
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
193 /* __doc__ */
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
194 "Intermediate representation of a Python parse tree."
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
195 }; /* PyST_Type */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
196
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
197
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
198 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
199 parser_compare_nodes(node *left, node *right)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
200 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
201 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
202
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
203 if (TYPE(left) < TYPE(right))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
204 return (-1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
205
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
206 if (TYPE(right) < TYPE(left))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
207 return (1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
208
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
209 if (ISTERMINAL(TYPE(left)))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
210 return (strcmp(STR(left), STR(right)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
211
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
212 if (NCH(left) < NCH(right))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
213 return (-1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
214
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
215 if (NCH(right) < NCH(left))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
216 return (1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
217
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
218 for (j = 0; j < NCH(left); ++j) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
219 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
220
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
221 if (v != 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
222 return (v);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
223 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
224 return (0);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
225 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
226
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
227
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
228 /* int parser_compare(PyST_Object* left, PyST_Object* right)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
229 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
230 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
231 * 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
232 * checks and protection code.
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 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
235 static int
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
236 parser_compare(PyST_Object *left, PyST_Object *right)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
237 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
238 if (left == right)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
239 return (0);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
240
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
241 if ((left == 0) || (right == 0))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
242 return (-1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
243
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
244 return (parser_compare_nodes(left->st_node, right->st_node));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
245 }
3200
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
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
248 /* parser_newstobject(node* st)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
249 *
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
250 * Allocates a new Python object representing an ST. This is simply the
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
251 * '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
252 * Python code.
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 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
255 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
256 parser_newstobject(node *st, int type)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
257 {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
258 PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
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 (o != 0) {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
261 o->st_node = st;
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
262 o->st_type = type;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
263 }
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
264 else {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
265 PyNode_Free(st);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
266 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
267 return ((PyObject*)o);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
268 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
269
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
270
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
271 /* void parser_free(PyST_Object* st)
3200
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 * 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
274 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
275 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
276 static void
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
277 parser_free(PyST_Object *st)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
278 {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
279 PyNode_Free(st->st_node);
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
280 PyObject_Del(st);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
281 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
282
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
283
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
284 /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
285 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
286 * This provides conversion from a node* to a tuple object that can be
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
287 * returned to the Python-level caller. The ST object is not modified.
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 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
291 parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
292 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
293 PyObject *line_option = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
294 PyObject *col_option = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
295 PyObject *res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
296 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
297
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
298 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
299
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
300 if (self == NULL) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
301 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
302 &PyST_Type, &self, &line_option,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
303 &col_option);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
304 }
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
305 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
306 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
307 &line_option, &col_option);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
308 if (ok != 0) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
309 int lineno = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
310 int col_offset = 0;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
311 if (line_option != NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
312 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
313 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
314 if (col_option != NULL) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
315 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
316 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
317 /*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
318 * Convert ST into a tuple representation. Use Guido's function,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
319 * since it's known to work already.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
320 */
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
321 res = node2tuple(((PyST_Object*)self)->st_node,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
322 PyTuple_New, PyTuple_SetItem, lineno, col_offset);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
323 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
324 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
325 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
326
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
327
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
328 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
329 *
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
330 * This provides conversion from a node* to a list object that can be
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
331 * returned to the Python-level caller. The ST object is not modified.
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
332 *
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
333 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
334 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
335 parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
336 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
337 PyObject *line_option = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
338 PyObject *col_option = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
339 PyObject *res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
340 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
341
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
342 static char *keywords[] = {"ast", "line_info", "col_info", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
343
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
344 if (self == NULL)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
345 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
346 &PyST_Type, &self, &line_option,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
347 &col_option);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
348 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
349 ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
350 &line_option, &col_option);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
351 if (ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
352 int lineno = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
353 int col_offset = 0;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
354 if (line_option != 0) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
355 lineno = PyObject_IsTrue(line_option) ? 1 : 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
356 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
357 if (col_option != NULL) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
358 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
359 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
360 /*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
361 * Convert ST into a tuple representation. Use Guido's function,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
362 * since it's known to work already.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
363 */
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
364 res = node2tuple(self->st_node,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
365 PyList_New, PyList_SetItem, lineno, col_offset);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
366 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
367 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
368 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
369
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
370
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
371 /* parser_compilest(PyObject* self, PyObject* args)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
372 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
373 * 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
374 * 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
375 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
376 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
377 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
378 parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
379 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
380 PyObject* res = 0;
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
381 char* str = "<syntax-tree>";
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
382 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
383
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
384 static char *keywords[] = {"ast", "filename", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
385
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
386 if (self == NULL)
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
387 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
388 &PyST_Type, &self, &str);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
389 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
390 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
391 &str);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
392
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
393 if (ok)
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
394 res = (PyObject *)PyNode_Compile(self->st_node, str);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
395
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
396 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
397 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
398
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
399
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
400 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
401 * PyObject* parser_issuite(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
402 *
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
403 * Checks the passed-in ST object to determine if it is an expression or
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
404 * 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
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 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
408 parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
409 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
410 PyObject* res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
411 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
412
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
413 static char *keywords[] = {"ast", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
414
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
415 if (self == NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
416 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
417 &PyST_Type, &self);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
418 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
419 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
420
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
421 if (ok) {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
422 /* Check to see if the ST represents an expression or not. */
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
423 res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
424 Py_INCREF(res);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
425 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
426 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
427 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
428
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
429
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
430 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
431 parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
432 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
433 PyObject* res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
434 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
435
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
436 static char *keywords[] = {"ast", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
437
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
438 if (self == NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
439 ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
440 &PyST_Type, &self);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
441 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
442 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
443
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
444 if (ok) {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
445 /* Check to see if the ST represents an expression or not. */
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
446 res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
447 Py_INCREF(res);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
448 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
449 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
450 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
451
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
452
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
453 #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
454
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
455 static PyMethodDef
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
456 parser_methods[] = {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
457 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
458 PyDoc_STR("Compile this ST object into a code object.")},
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
459 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
460 PyDoc_STR("Determines if this ST object was created from an expression.")},
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
461 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
462 PyDoc_STR("Determines if this ST object was created from a suite.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
463 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
464 PyDoc_STR("Creates a list-tree representation of this ST.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
465 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
466 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
467
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
468 {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
469 };
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
470
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
471
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
472 static PyObject*
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
473 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
474 {
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
475 return (Py_FindMethod(parser_methods, self, name));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
476 }
8039
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
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
479 /* err_string(char* message)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
480 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
481 * 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
482 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
483 */
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
484 static void
13503
6484098f40fc ANSI-fying
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13466
diff changeset
485 err_string(char *message)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
486 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
487 PyErr_SetString(parser_error, message);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
488 }
3774
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
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
491 /* 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
492 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
493 * Internal function to actually execute the parse and return the result if
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24796
diff changeset
494 * successful or set an exception if not.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
495 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
496 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
497 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
498 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
499 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
500 char* string = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
501 PyObject* res = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
502
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
503 static char *keywords[] = {"source", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
504
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
505 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
506 node* n = PyParser_SimpleParseString(string,
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
507 (type == PyST_EXPR)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
508 ? eval_input : file_input);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
509
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24796
diff changeset
510 if (n)
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24796
diff changeset
511 res = parser_newstobject(n, type);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
512 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
513 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
514 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
515
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
516
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
517 /* PyObject* parser_expr(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
518 * PyObject* parser_suite(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
519 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
520 * 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
521 * 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
522 * 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
523 *
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 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
526 parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
527 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
528 NOTE(ARGUNUSED(self))
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
529 return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
530 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
531
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 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
534 parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
535 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
536 NOTE(ARGUNUSED(self))
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
537 return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
538 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
539
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
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
542 /* This is the messy part of the code. Conversion from a tuple to an ST
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
543 * 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
544 * 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
545 * 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
546 * 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
547 * 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
548 * phase, returning an exception instead.
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 * 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
551 * the tuple passed in, and verifying that it is indeed valid. It may be
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
552 * advantageous to expand the number of ST types to include funcdefs and
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
553 * lambdadefs to take advantage of the optimizer, recognizing those STs
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
554 * 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
555 * 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
556 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
557
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
558
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
559 static node* build_node_tree(PyObject *tuple);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
560 static int validate_expr_tree(node *tree);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
561 static int validate_file_input(node *tree);
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
562 static int validate_encoding_decl(node *tree);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
563
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
564 /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
3200
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 * This is the public function, called from the Python code. It receives a
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
567 * single tuple object from the caller, and creates an ST object if the
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
568 * 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
569 * 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
570 * 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
571 * 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
572 *
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
573 * This function must be changed if support is to be added for PyST_FRAGMENT
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
574 * ST objects.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
575 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
576 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
577 static PyObject*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
578 parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
579 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
580 NOTE(ARGUNUSED(self))
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
581 PyObject *st = 0;
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
582 PyObject *tuple;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
583 node *tree;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
584
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
585 static char *keywords[] = {"sequence", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
586
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
587 if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
588 &tuple))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
589 return (0);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
590 if (!PySequence_Check(tuple)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
591 PyErr_SetString(PyExc_ValueError,
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
592 "sequence2st() requires a single sequence argument");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
593 return (0);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
594 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
595 /*
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
596 * Convert the tree to the internal form before checking it.
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
597 */
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
598 tree = build_node_tree(tuple);
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
599 if (tree != 0) {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
600 int start_sym = TYPE(tree);
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
601 if (start_sym == eval_input) {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
602 /* Might be an eval form. */
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
603 if (validate_expr_tree(tree))
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
604 st = parser_newstobject(tree, PyST_EXPR);
21237
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
605 else
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
606 PyNode_Free(tree);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
607 }
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
608 else if (start_sym == file_input) {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
609 /* This looks like an exec form so far. */
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
610 if (validate_file_input(tree))
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
611 st = parser_newstobject(tree, PyST_SUITE);
21237
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
612 else
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
613 PyNode_Free(tree);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
614 }
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
615 else if (start_sym == encoding_decl) {
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
616 /* This looks like an encoding_decl so far. */
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
617 if (validate_encoding_decl(tree))
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
618 st = parser_newstobject(tree, PyST_SUITE);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
619 else
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
620 PyNode_Free(tree);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
621 }
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
622 else {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
623 /* This is a fragment, at best. */
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
624 PyNode_Free(tree);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
625 err_string("parse tree does not use a valid start symbol");
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
626 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
627 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
628 /* 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
629 * 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
630 */
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
631 if (st == NULL && !PyErr_Occurred())
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
632 err_string("unspecified ST error occurred");
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
633
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
634 return st;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
635 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
636
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
637
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
638 /* node* build_node_children()
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
639 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
640 * 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
641 * 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
642 * 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
643 * and no memory will have been deallocated.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
644 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
645 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
646 static node*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
647 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
648 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
649 Py_ssize_t len = PyObject_Size(tuple);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
650 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
651 int err;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
652
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
653 for (i = 1; i < len; ++i) {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
654 /* elem must always be a sequence, however simple */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
655 PyObject* elem = PySequence_GetItem(tuple, i);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
656 int ok = elem != NULL;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
657 long type = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
658 char *strn = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
659
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
660 if (ok)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
661 ok = PySequence_Check(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
662 if (ok) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
663 PyObject *temp = PySequence_GetItem(elem, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
664 if (temp == NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
665 ok = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
666 else {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
667 ok = PyLong_Check(temp);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
668 if (ok)
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
669 type = PyLong_AS_LONG(temp);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
670 Py_DECREF(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
671 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
672 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
673 if (!ok) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
674 PyObject *err = Py_BuildValue("os", elem,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
675 "Illegal node construct.");
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
676 PyErr_SetObject(parser_error, err);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
677 Py_XDECREF(err);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
678 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
679 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
680 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
681 if (ISTERMINAL(type)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
682 Py_ssize_t len = PyObject_Size(elem);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
683 PyObject *temp;
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42535
diff changeset
684 const char *temp_str;
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
685
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
686 if ((len != 2) && (len != 3)) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
687 err_string("terminal nodes must have 2 or 3 entries");
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
688 return 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
689 }
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
690 temp = PySequence_GetItem(elem, 1);
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
691 if (temp == NULL)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
692 return 0;
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42535
diff changeset
693 if (!PyUnicode_Check(temp)) {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
694 PyErr_Format(parser_error,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
695 "second item in terminal node must be a string,"
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
696 " found %s",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
697 Py_TYPE(temp)->tp_name);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12487
diff changeset
698 Py_DECREF(temp);
42535
8d6867cb8a8d Fix refleaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
699 Py_DECREF(elem);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
700 return 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
701 }
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
702 if (len == 3) {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
703 PyObject *o = PySequence_GetItem(elem, 2);
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
704 if (o != NULL) {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
705 if (PyLong_Check(o))
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
706 *line_num = PyLong_AS_LONG(o);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
707 else {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
708 PyErr_Format(parser_error,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
709 "third item in terminal node must be an"
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
710 " integer, found %s",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
711 Py_TYPE(temp)->tp_name);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
712 Py_DECREF(o);
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
713 Py_DECREF(temp);
42535
8d6867cb8a8d Fix refleaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
714 Py_DECREF(elem);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
715 return 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
716 }
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
717 Py_DECREF(o);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
718 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
719 }
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
720 temp_str = PyUnicode_AsStringAndSize(temp, &len);
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
721 strn = (char *)PyObject_MALLOC(len + 1);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
722 if (strn != NULL)
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
723 (void) memcpy(strn, temp_str, len + 1);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
724 Py_DECREF(temp);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
725 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
726 else if (!ISNONTERMINAL(type)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
727 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
728 * 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
729 * Throw an exception.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
730 */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
731 PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
732 PyErr_SetObject(parser_error, err);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
733 Py_XDECREF(err);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
734 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
735 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
736 }
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36712
diff changeset
737 err = PyNode_AddChild(root, type, strn, *line_num, 0);
21237
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
738 if (err == E_NOMEM) {
42535
8d6867cb8a8d Fix refleaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
739 Py_XDECREF(elem);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
740 PyObject_FREE(strn);
21237
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
741 return (node *) PyErr_NoMemory();
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
742 }
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
743 if (err == E_OVERFLOW) {
42535
8d6867cb8a8d Fix refleaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
744 Py_XDECREF(elem);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
745 PyObject_FREE(strn);
21237
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
746 PyErr_SetString(PyExc_ValueError,
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
747 "unsupported number of child nodes");
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
748 return NULL;
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
749 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
750
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
751 if (ISNONTERMINAL(type)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
752 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
753
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
754 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
755 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
756 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
757 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
758 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
759 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
760 ++(*line_num); /* line number *after* the newline! */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
761 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
762 Py_XDECREF(elem);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
763 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
764 return root;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
765 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
766
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
767
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
768 static node*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
769 build_node_tree(PyObject *tuple)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
770 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
771 node* res = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
772 PyObject *temp = PySequence_GetItem(tuple, 0);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
773 long num = -1;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
774
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
775 if (temp != NULL)
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43862
diff changeset
776 num = PyLong_AsLong(temp);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
777 Py_XDECREF(temp);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
778 if (ISTERMINAL(num)) {
12416
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 * 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
781 * 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
782 */
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
783 tuple = Py_BuildValue("os", tuple,
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
784 "Illegal syntax-tree; cannot start with terminal symbol.");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
785 PyErr_SetObject(parser_error, tuple);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
786 Py_XDECREF(tuple);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
787 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
788 else if (ISNONTERMINAL(num)) {
12416
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 * Not efficient, but that can be handled later.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
791 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
792 int line_num = 0;
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
793 PyObject *encoding = NULL;
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
794
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
795 if (num == encoding_decl) {
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
796 encoding = PySequence_GetItem(tuple, 2);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
797 /* tuple isn't borrowed anymore here, need to DECREF */
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
798 tuple = PySequence_GetSlice(tuple, 0, 2);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
799 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
800 res = PyNode_New(num);
21237
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
801 if (res != NULL) {
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
802 if (res != build_node_children(tuple, res, &line_num)) {
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
803 PyNode_Free(res);
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
804 res = NULL;
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
805 }
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
806 if (res && encoding) {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
807 Py_ssize_t len;
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42535
diff changeset
808 const char *temp;
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
809 temp = PyUnicode_AsStringAndSize(encoding, &len);
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
810 res->n_str = (char *)PyObject_MALLOC(len + 1);
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42535
diff changeset
811 if (res->n_str != NULL && temp != NULL)
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
812 (void) memcpy(res->n_str, temp, len + 1);
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
813 Py_DECREF(encoding);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
814 Py_DECREF(tuple);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
815 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
816 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
817 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
818 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
819 /* The tuple is illegal -- if the number is neither TERMINAL nor
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
820 * NONTERMINAL, we can't use it. Not sure the implementation
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
821 * allows this condition, but the API doesn't preclude it.
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
822 */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
823 PyObject *err = Py_BuildValue("os", tuple,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
824 "Illegal component tuple.");
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
825 PyErr_SetObject(parser_error, err);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
826 Py_XDECREF(err);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36786
diff changeset
827 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
828
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
829 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
830 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
831
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
832
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 * Validation routines used within the validation section:
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
835 */
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
836 static 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
837
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
838 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
839 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
840 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
841 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
842 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
843 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
844 #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
845 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
846 #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
847 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
848 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
849 #define validate_star(ch) validate_terminal(ch, STAR, "*")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
850 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
851 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
852 #define validate_dot(ch) validate_terminal(ch, DOT, ".")
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
853 #define validate_at(ch) validate_terminal(ch, AT, "@")
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
854 #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
855
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
856 #define VALIDATER(n) static int validate_##n(node *tree)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
857
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
858 VALIDATER(node); VALIDATER(small_stmt);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
859 VALIDATER(class); VALIDATER(node);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
860 VALIDATER(parameters); VALIDATER(suite);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
861 VALIDATER(testlist); VALIDATER(varargslist);
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
862 VALIDATER(vfpdef);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
863 VALIDATER(stmt); VALIDATER(simple_stmt);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
864 VALIDATER(expr_stmt); VALIDATER(power);
40774
793e0323d4d6 Essential changes for print function changes.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
865 VALIDATER(del_stmt);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
866 VALIDATER(return_stmt); VALIDATER(raise_stmt);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
867 VALIDATER(import_stmt); VALIDATER(import_stmt);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
868 VALIDATER(import_name); VALIDATER(yield_stmt);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
869 VALIDATER(global_stmt); VALIDATER(assert_stmt);
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
870 VALIDATER(compound_stmt);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
871 VALIDATER(while); VALIDATER(for);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
872 VALIDATER(try); VALIDATER(except_clause);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
873 VALIDATER(test); VALIDATER(and_test);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
874 VALIDATER(not_test); VALIDATER(comparison);
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
875 VALIDATER(comp_op);
41699
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
876 VALIDATER(star_expr); VALIDATER(expr);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
877 VALIDATER(xor_expr); VALIDATER(and_expr);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
878 VALIDATER(shift_expr); VALIDATER(arith_expr);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
879 VALIDATER(term); VALIDATER(factor);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
880 VALIDATER(atom); VALIDATER(lambdef);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
881 VALIDATER(trailer); VALIDATER(subscript);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
882 VALIDATER(subscriptlist); VALIDATER(sliceop);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
883 VALIDATER(exprlist); VALIDATER(dictorsetmaker);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
884 VALIDATER(arglist); VALIDATER(argument);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
885 VALIDATER(testlist1); VALIDATER(comp_for);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
886 VALIDATER(comp_iter); VALIDATER(comp_if);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
887 VALIDATER(testlist_comp); VALIDATER(yield_expr);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
888 VALIDATER(yield_or_testlist); VALIDATER(or_test);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
889 VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
890
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
891 #undef VALIDATER
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
892
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
893 #define is_even(n) (((n) & 1) == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
894 #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
895
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
896
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
897 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
898 validate_ntype(node *n, int t)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
899 {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
900 if (TYPE(n) != t) {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
901 PyErr_Format(parser_error, "Expected node type %d, got %d.",
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
902 t, TYPE(n));
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
903 return 0;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
904 }
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
905 return 1;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
906 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
907
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
908
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
909 /* 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
910 * 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
911 * 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
912 * "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
913 * 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
914 * in that fashion.
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
915 */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
916 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
917 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
918 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
919 if (NCH(n) != num) {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
920 PyErr_Format(parser_error,
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
921 "Illegal number of children for %s node.", name);
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
922 return 0;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
923 }
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
924 return 1;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
925 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
926
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
927
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
928 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
929 validate_terminal(node *terminal, int type, char *string)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
930 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
931 int res = (validate_ntype(terminal, type)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
932 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
933
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
934 if (!res && !PyErr_Occurred()) {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
935 PyErr_Format(parser_error,
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
936 "Illegal terminal: expected \"%s\"", string);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
937 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
938 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
939 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
940
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
941
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
942 /* X (',' X) [',']
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
943 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
944 static int
13667
a6c7bfdd6777 Even more ANSIfication: fix as many function pointers and declarations as
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
945 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
946 const char *const name)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
947 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
948 int nch = NCH(tree);
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
949 int res = (nch && validate_ntype(tree, ntype)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
950 && vfunc(CHILD(tree, 0)));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
951
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
952 if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
953 (void) validate_numnodes(tree, 1, name);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
954 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
955 if (is_even(nch))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
956 res = validate_comma(CHILD(tree, --nch));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
957 if (res && nch > 1) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
958 int pos = 1;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
959 for ( ; res && pos < nch; pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
960 res = (validate_comma(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
961 && vfunc(CHILD(tree, pos + 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
962 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
963 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
964 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
965 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
966
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
967
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
968 /* validate_class()
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
969 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
970 * classdef:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
971 * 'class' NAME ['(' testlist ')'] ':' suite
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
972 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
973 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
974 validate_class(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
975 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
976 int nch = NCH(tree);
34887
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
977 int res = (validate_ntype(tree, classdef) &&
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
978 ((nch == 4) || (nch == 6) || (nch == 7)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
979
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
980 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
981 res = (validate_name(CHILD(tree, 0), "class")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
982 && validate_ntype(CHILD(tree, 1), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
983 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
984 && validate_suite(CHILD(tree, nch - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
985 }
34887
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
986 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
987 (void) validate_numnodes(tree, 4, "class");
34887
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
988 }
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
989
34887
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
990 if (res) {
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
991 if (nch == 7) {
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
992 res = ((validate_lparen(CHILD(tree, 2)) &&
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
993 validate_arglist(CHILD(tree, 3)) &&
34887
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
994 validate_rparen(CHILD(tree, 4))));
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
995 }
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
996 else if (nch == 6) {
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
997 res = (validate_lparen(CHILD(tree,2)) &&
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
998 validate_rparen(CHILD(tree,3)));
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
999 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1000 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1001 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1002 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1003
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1004
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1005 /* if_stmt:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1006 * '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
1007 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1008 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1009 validate_if(node *tree)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1010 {
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1011 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1012 int res = (validate_ntype(tree, if_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1013 && (nch >= 4)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1014 && validate_name(CHILD(tree, 0), "if")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1015 && validate_test(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1016 && validate_colon(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1017 && validate_suite(CHILD(tree, 3)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1018
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1019 if (res && ((nch % 4) == 3)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1020 /* ... 'else' ':' suite */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1021 res = (validate_name(CHILD(tree, nch - 3), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1022 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1023 && validate_suite(CHILD(tree, nch - 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1024 nch -= 3;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1025 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1026 else if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1027 (void) validate_numnodes(tree, 4, "if");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1028 if ((nch % 4) != 0)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1029 /* Will catch the case for nch < 4 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1030 res = validate_numnodes(tree, 0, "if");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1031 else if (res && (nch > 4)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1032 /* ... ('elif' test ':' suite)+ ... */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1033 int j = 4;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1034 while ((j < nch) && res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1035 res = (validate_name(CHILD(tree, j), "elif")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1036 && validate_colon(CHILD(tree, j + 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1037 && validate_test(CHILD(tree, j + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1038 && validate_suite(CHILD(tree, j + 3)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1039 j += 4;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1040 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1041 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1042 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1043 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1044
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1045
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1046 /* parameters:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1047 * '(' [varargslist] ')'
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1048 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1049 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1050 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1051 validate_parameters(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1052 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1053 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1054 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
1055
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1056 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1057 res = (validate_lparen(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1058 && validate_rparen(CHILD(tree, nch - 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1059 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1060 res = validate_varargslist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1061 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1062 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1063 (void) validate_numnodes(tree, 2, "parameters");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1064 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1065 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1066 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1067
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1068
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1069 /* validate_suite()
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1070 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1071 * suite:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1072 * simple_stmt
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1073 * | NEWLINE INDENT stmt+ DEDENT
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1074 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1075 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1076 validate_suite(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1077 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1078 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1079 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
1080
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1081 if (res && (nch == 1))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1082 res = validate_simple_stmt(CHILD(tree, 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1083 else if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1084 /* NEWLINE INDENT stmt+ DEDENT */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1085 res = (validate_newline(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1086 && validate_indent(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1087 && validate_stmt(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1088 && validate_dedent(CHILD(tree, nch - 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1089
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1090 if (res && (nch > 4)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1091 int i = 3;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1092 --nch; /* forget the DEDENT */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1093 for ( ; res && (i < nch); ++i)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1094 res = validate_stmt(CHILD(tree, i));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1095 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1096 else if (nch < 4)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1097 res = validate_numnodes(tree, 4, "suite");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1098 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1099 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1100 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1101
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1102
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1103 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1104 validate_testlist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1105 {
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1106 return (validate_repeating_list(tree, testlist,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1107 validate_test, "testlist"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1108 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1109
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1110
20432
56141b86a7f2 Very subtle syntax change: in a list comprehension, the testlist in
Guido van Rossum <guido@python.org>
parents: 19274
diff changeset
1111 static int
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1112 validate_testlist1(node *tree)
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1113 {
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1114 return (validate_repeating_list(tree, testlist1,
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1115 validate_test, "testlist1"));
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1116 }
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1117
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1118
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1119 /* validate either vfpdef or tfpdef.
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1120 * vfpdef: NAME
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1121 * tfpdef: NAME [':' test]
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1122 */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1123 static int
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1124 validate_vfpdef(node *tree)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1125 {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1126 int nch = NCH(tree);
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1127 if (TYPE(tree) == vfpdef) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1128 return nch == 1 && validate_name(CHILD(tree, 0), NULL);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1129 }
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1130 else if (TYPE(tree) == tfpdef) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1131 if (nch == 1) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1132 return validate_name(CHILD(tree, 0), NULL);
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1133 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1134 else if (nch == 3) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1135 return validate_name(CHILD(tree, 0), NULL) &&
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1136 validate_colon(CHILD(tree, 1)) &&
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1137 validate_test(CHILD(tree, 2));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1138 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1139 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1140 return 0;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1141 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1142
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1143 /* '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1144 * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1145 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1146 static int
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1147 validate_varargslist_trailer(node *tree, int start)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1148 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1149 int nch = NCH(tree);
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1150 int res = 0, i;
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1151 int sym;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1152
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1153 if (nch <= start) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1154 err_string("expected variable argument trailer for varargslist");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1155 return 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1156 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1157 sym = TYPE(CHILD(tree, start));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1158 if (sym == STAR) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1159 /*
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1160 * '*' vfpdef (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1161 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1162 if (nch-start == 2)
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1163 res = validate_vfpdef(CHILD(tree, start+1));
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1164 else if (nch-start == 5 && TYPE(CHILD(tree, start+2)) == COMMA)
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1165 res = (validate_vfpdef(CHILD(tree, start+1))
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1166 && validate_comma(CHILD(tree, start+2))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1167 && validate_doublestar(CHILD(tree, start+3))
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1168 && validate_vfpdef(CHILD(tree, start+4)));
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1169 else {
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1170 /* skip over vfpdef (',' vfpdef ['=' test])* */
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1171 i = start + 1;
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1172 if (TYPE(CHILD(tree, i)) == vfpdef ||
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1173 TYPE(CHILD(tree, i)) == tfpdef) { /* skip over vfpdef or tfpdef */
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1174 i += 1;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1175 }
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1176 while (res && i+1 < nch) { /* validate (',' vfpdef ['=' test])* */
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1177 res = validate_comma(CHILD(tree, i));
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
1178 if (TYPE(CHILD(tree, i+1)) == DOUBLESTAR)
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1179 break;
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1180 res = res && validate_vfpdef(CHILD(tree, i+1));
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1181 if (res && i+2 < nch && TYPE(CHILD(tree, i+2)) == EQUAL) {
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
1182 res = res && (i+3 < nch)
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1183 && validate_test(CHILD(tree, i+3));
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1184 i += 4;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1185 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1186 else {
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1187 i += 2;
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1188 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1189 }
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1190 /* [',' '**' vfpdef] */
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1191 if (res && i+1 < nch && TYPE(CHILD(tree, i+1)) == DOUBLESTAR) {
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1192 res = validate_vfpdef(CHILD(tree, i+2));
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1193 }
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1194 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1195 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1196 else if (sym == DOUBLESTAR) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1197 /*
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1198 * '**' NAME
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1199 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1200 if (nch-start == 2)
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1201 res = validate_vfpdef(CHILD(tree, start+1));
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1202 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1203 if (!res)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1204 err_string("illegal variable argument trailer for varargslist");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1205 return res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1206 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1207
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1208
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1209 /* validate_varargslist()
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1210 *
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1211 * Validate typedargslist or varargslist.
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1212 *
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1213 * typedargslist: ((tfpdef ['=' test] ',')*
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1214 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1215 * '**' tfpdef)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1216 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1217 * tfpdef: NAME [':' test]
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1218 * varargslist: ((vfpdef ['=' test] ',')*
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1219 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1220 * '**' vfpdef)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1221 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1222 * vfpdef: NAME
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1223 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1224 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1225 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1226 validate_varargslist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1227 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1228 int nch = NCH(tree);
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1229 int res = (TYPE(tree) == varargslist ||
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1230 TYPE(tree) == typedargslist) &&
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1231 (nch != 0);
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1232 int sym;
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1233 node *ch;
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1234 int i = 0;
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
1235
15506
ac830c9e9a83 validate_varargslist(): Fix two bugs in this function, one that affected
Fred Drake <fdrake@acm.org>
parents: 15318
diff changeset
1236 if (!res)
ac830c9e9a83 validate_varargslist(): Fix two bugs in this function, one that affected
Fred Drake <fdrake@acm.org>
parents: 15318
diff changeset
1237 return 0;
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1238 if (nch < 1) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1239 err_string("varargslist missing child nodes");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1240 return 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1241 }
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1242 while (i < nch) {
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
1243 ch = CHILD(tree, i);
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1244 sym = TYPE(ch);
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1245 if (sym == vfpdef || sym == tfpdef) {
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1246 /* validate (vfpdef ['=' test] ',')+ */
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1247 res = validate_vfpdef(ch);
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1248 ++i;
15506
ac830c9e9a83 validate_varargslist(): Fix two bugs in this function, one that affected
Fred Drake <fdrake@acm.org>
parents: 15318
diff changeset
1249 if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
ac830c9e9a83 validate_varargslist(): Fix two bugs in this function, one that affected
Fred Drake <fdrake@acm.org>
parents: 15318
diff changeset
1250 res = (validate_equal(CHILD(tree, i))
ac830c9e9a83 validate_varargslist(): Fix two bugs in this function, one that affected
Fred Drake <fdrake@acm.org>
parents: 15318
diff changeset
1251 && validate_test(CHILD(tree, i+1)));
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1252 if (res)
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1253 i += 2;
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1254 }
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1255 if (res && i < nch) {
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1256 res = validate_comma(CHILD(tree, i));
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1257 ++i;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1258 }
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1259 } else if (sym == DOUBLESTAR || sym == STAR) {
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1260 res = validate_varargslist_trailer(tree, i);
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1261 break;
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1262 } else {
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1263 res = 0;
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1264 err_string("illegal formation for varargslist");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1265 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1266 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1267 return res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1268 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1269
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1270
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1271 /* comp_iter: comp_for | comp_if
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1272 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1273 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1274 validate_comp_iter(node *tree)
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1275 {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1276 int res = (validate_ntype(tree, comp_iter)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1277 && validate_numnodes(tree, 1, "comp_iter"));
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1278 if (res && TYPE(CHILD(tree, 0)) == comp_for)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1279 res = validate_comp_for(CHILD(tree, 0));
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1280 else
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1281 res = validate_comp_if(CHILD(tree, 0));
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1282
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1283 return res;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1284 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1285
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1286 /* comp_for: 'for' exprlist 'in' test [comp_iter]
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1287 */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1288 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1289 validate_comp_for(node *tree)
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1290 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1291 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1292 int res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1293
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1294 if (nch == 5)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1295 res = validate_comp_iter(CHILD(tree, 4));
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1296 else
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1297 res = validate_numnodes(tree, 4, "comp_for");
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1298
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1299 if (res)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1300 res = (validate_name(CHILD(tree, 0), "for")
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1301 && validate_exprlist(CHILD(tree, 1))
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1302 && validate_name(CHILD(tree, 2), "in")
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1303 && validate_or_test(CHILD(tree, 3)));
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1304
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1305 return res;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1306 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1307
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1308 /* comp_if: 'if' test_nocond [comp_iter]
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1309 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1310 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1311 validate_comp_if(node *tree)
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1312 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1313 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1314 int res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1315
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1316 if (nch == 3)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1317 res = validate_comp_iter(CHILD(tree, 2));
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1318 else
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1319 res = validate_numnodes(tree, 2, "comp_if");
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1320
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1321 if (res)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1322 res = (validate_name(CHILD(tree, 0), "if")
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1323 && validate_test_nocond(CHILD(tree, 1)));
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1324
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1325 return res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1326 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1327
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1328
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1329 /* simple_stmt | compound_stmt
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1330 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1331 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1332 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1333 validate_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1334 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1335 int res = (validate_ntype(tree, stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1336 && validate_numnodes(tree, 1, "stmt"));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1337
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1338 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1339 tree = CHILD(tree, 0);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1340
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1341 if (TYPE(tree) == simple_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1342 res = validate_simple_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1343 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1344 res = validate_compound_stmt(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 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1347 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1348
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1349
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1350 /* small_stmt (';' small_stmt)* [';'] NEWLINE
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 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1353 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1354 validate_simple_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1355 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1356 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1357 int res = (validate_ntype(tree, simple_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1358 && (nch >= 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1359 && validate_small_stmt(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1360 && validate_newline(CHILD(tree, nch - 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1361
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1362 if (nch < 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1363 res = validate_numnodes(tree, 2, "simple_stmt");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1364 --nch; /* forget the NEWLINE */
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1365 if (res && is_even(nch))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1366 res = validate_semi(CHILD(tree, --nch));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1367 if (res && (nch > 2)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1368 int i;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1369
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1370 for (i = 1; res && (i < nch); i += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1371 res = (validate_semi(CHILD(tree, i))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1372 && 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
1373 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1374 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1375 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1376
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1377
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1378 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1379 validate_small_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1380 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1381 int nch = NCH(tree);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1382 int res = validate_numnodes(tree, 1, "small_stmt");
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1383
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1384 if (res) {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1385 int ntype = TYPE(CHILD(tree, 0));
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1386
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1387 if ( (ntype == expr_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1388 || (ntype == del_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1389 || (ntype == pass_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1390 || (ntype == flow_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1391 || (ntype == import_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1392 || (ntype == global_stmt)
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39701
diff changeset
1393 || (ntype == assert_stmt))
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1394 res = validate_node(CHILD(tree, 0));
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1395 else {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1396 res = 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1397 err_string("illegal small_stmt child type");
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1398 }
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1399 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1400 else if (nch == 1) {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1401 res = 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1402 PyErr_Format(parser_error,
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1403 "Unrecognized child node of small_stmt: %d.",
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1404 TYPE(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1405 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1406 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1407 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1408
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1409
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1410 /* compound_stmt:
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
1411 * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1412 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1413 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1414 validate_compound_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1415 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1416 int res = (validate_ntype(tree, compound_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1417 && validate_numnodes(tree, 1, "compound_stmt"));
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1418 int ntype;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1419
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1420 if (!res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1421 return (0);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1422
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1423 tree = CHILD(tree, 0);
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1424 ntype = TYPE(tree);
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1425 if ( (ntype == if_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1426 || (ntype == while_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1427 || (ntype == for_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1428 || (ntype == try_stmt)
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1429 || (ntype == funcdef)
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
1430 || (ntype == classdef)
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
1431 || (ntype == decorated))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1432 res = validate_node(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1433 else {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1434 res = 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1435 PyErr_Format(parser_error,
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1436 "Illegal compound statement type: %d.", TYPE(tree));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1437 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1438 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1439 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1440
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1441 static int
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1442 validate_yield_or_testlist(node *tree)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1443 {
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
1444 if (TYPE(tree) == yield_expr)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1445 return validate_yield_expr(tree);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1446 else
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1447 return validate_testlist(tree);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1448 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1449
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1450 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1451 validate_expr_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1452 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1453 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1454 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1455 int res = (validate_ntype(tree, expr_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1456 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1457 && validate_testlist(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1458
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1459 if (res && nch == 3
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1460 && TYPE(CHILD(tree, 1)) == augassign) {
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1461 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1462 && validate_yield_or_testlist(CHILD(tree, 2));
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1463
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1464 if (res) {
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1465 char *s = STR(CHILD(CHILD(tree, 1), 0));
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1466
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1467 res = (strcmp(s, "+=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1468 || strcmp(s, "-=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1469 || strcmp(s, "*=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1470 || strcmp(s, "/=") == 0
27517
51abb5e86226 Teach the parsermodule about floor division. Fixes
Michael W. Hudson <mwh@python.net>
parents: 26979
diff changeset
1471 || strcmp(s, "//=") == 0
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1472 || strcmp(s, "%=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1473 || strcmp(s, "&=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1474 || strcmp(s, "|=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1475 || strcmp(s, "^=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1476 || strcmp(s, "<<=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1477 || strcmp(s, ">>=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1478 || strcmp(s, "**=") == 0);
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1479 if (!res)
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1480 err_string("illegal augmmented assignment operator");
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1481 }
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1482 }
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1483 else {
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1484 for (j = 1; res && (j < nch); j += 2)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1485 res = validate_equal(CHILD(tree, j))
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1486 && validate_yield_or_testlist(CHILD(tree, j + 1));
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1487 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1488 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1489 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1490
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1491
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1492 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1493 validate_del_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1494 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1495 return (validate_numnodes(tree, 2, "del_stmt")
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1496 && validate_name(CHILD(tree, 0), "del")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1497 && validate_exprlist(CHILD(tree, 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1498 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1499
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1500
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1501 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1502 validate_return_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1503 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1504 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1505 int res = (validate_ntype(tree, return_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1506 && ((nch == 1) || (nch == 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1507 && validate_name(CHILD(tree, 0), "return"));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1508
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1509 if (res && (nch == 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1510 res = validate_testlist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1511
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1512 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1513 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1514
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1515
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1516 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1517 validate_raise_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1518 {
3200
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, raise_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1521 && ((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
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), "raise");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1525 if (res && (nch >= 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1526 res = validate_test(CHILD(tree, 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1527 if (res && nch > 2) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1528 res = (validate_comma(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1529 && validate_test(CHILD(tree, 3)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1530 if (res && (nch > 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1531 res = (validate_comma(CHILD(tree, 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1532 && validate_test(CHILD(tree, 5)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1533 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1534 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1535 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1536 (void) validate_numnodes(tree, 2, "raise");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1537 if (res && (nch == 4))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1538 res = (validate_comma(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1539 && validate_test(CHILD(tree, 3)));
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 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1542 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1543
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1544
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1545 /* yield_expr: 'yield' [testlist]
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1546 */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1547 static int
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1548 validate_yield_expr(node *tree)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1549 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1550 int nch = NCH(tree);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1551 int res = (validate_ntype(tree, yield_expr)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1552 && ((nch == 1) || (nch == 2))
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1553 && validate_name(CHILD(tree, 0), "yield"));
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1554
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1555 if (res && (nch == 2))
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1556 res = validate_testlist(CHILD(tree, 1));
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1557
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1558 return (res);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1559 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1560
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1561
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1562 /* yield_stmt: yield_expr
18646
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1563 */
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1564 static int
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1565 validate_yield_stmt(node *tree)
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1566 {
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1567 return (validate_ntype(tree, yield_stmt)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1568 && validate_numnodes(tree, 1, "yield_stmt")
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1569 && validate_yield_expr(CHILD(tree, 0)));
18646
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1570 }
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1571
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1572
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1573 static int
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1574 validate_import_as_name(node *tree)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1575 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1576 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1577 int ok = validate_ntype(tree, import_as_name);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1578
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1579 if (ok) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1580 if (nch == 1)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1581 ok = validate_name(CHILD(tree, 0), NULL);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1582 else if (nch == 3)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1583 ok = (validate_name(CHILD(tree, 0), NULL)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1584 && validate_name(CHILD(tree, 1), "as")
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1585 && validate_name(CHILD(tree, 2), NULL));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1586 else
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1587 ok = validate_numnodes(tree, 3, "import_as_name");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1588 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1589 return ok;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1590 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1591
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1592
15751
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1593 /* dotted_name: NAME ("." NAME)*
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1594 */
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1595 static int
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1596 validate_dotted_name(node *tree)
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1597 {
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1598 int nch = NCH(tree);
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1599 int res = (validate_ntype(tree, dotted_name)
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1600 && is_odd(nch)
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1601 && validate_name(CHILD(tree, 0), NULL));
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1602 int i;
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1603
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1604 for (i = 1; res && (i < nch); i += 2) {
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1605 res = (validate_dot(CHILD(tree, i))
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1606 && validate_name(CHILD(tree, i+1), NULL));
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1607 }
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1608 return res;
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1609 }
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1610
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1611
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1612 /* dotted_as_name: dotted_name [NAME NAME]
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1613 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1614 static int
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1615 validate_dotted_as_name(node *tree)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1616 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1617 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1618 int res = validate_ntype(tree, dotted_as_name);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1619
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1620 if (res) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1621 if (nch == 1)
15751
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1622 res = validate_dotted_name(CHILD(tree, 0));
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1623 else if (nch == 3)
15751
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1624 res = (validate_dotted_name(CHILD(tree, 0))
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1625 && validate_name(CHILD(tree, 1), "as")
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1626 && validate_name(CHILD(tree, 2), NULL));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1627 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1628 res = 0;
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
1629 err_string("illegal number of children for dotted_as_name");
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1630 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1631 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1632 return res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1633 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1634
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1635
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1636 /* dotted_as_name (',' dotted_as_name)* */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1637 static int
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1638 validate_dotted_as_names(node *tree)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1639 {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1640 int nch = NCH(tree);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1641 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1642 int i;
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1643
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1644 for (i = 1; res && (i < nch); i += 2)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1645 res = (validate_comma(CHILD(tree, i))
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1646 && validate_dotted_as_name(CHILD(tree, i + 1)));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1647 return (res);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1648 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1649
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1650
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1651 /* import_as_name (',' import_as_name)* [','] */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1652 static int
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1653 validate_import_as_names(node *tree)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1654 {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1655 int nch = NCH(tree);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1656 int res = validate_import_as_name(CHILD(tree, 0));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1657 int i;
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1658
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1659 for (i = 1; res && (i + 1 < nch); i += 2)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1660 res = (validate_comma(CHILD(tree, i))
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1661 && validate_import_as_name(CHILD(tree, i + 1)));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1662 return (res);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1663 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1664
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1665
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1666 /* 'import' dotted_as_names */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1667 static int
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1668 validate_import_name(node *tree)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1669 {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1670 return (validate_ntype(tree, import_name)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1671 && validate_numnodes(tree, 2, "import_name")
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1672 && validate_name(CHILD(tree, 0), "import")
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1673 && validate_dotted_as_names(CHILD(tree, 1)));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1674 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1675
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
1676 /* Helper function to count the number of leading dots in
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1677 * 'from ...module import name'
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1678 */
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1679 static int
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1680 count_from_dots(node *tree)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1681 {
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1682 int i;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1683 for (i = 0; i < NCH(tree); i++)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1684 if (TYPE(CHILD(tree, i)) != DOT)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1685 break;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1686 return i;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1687 }
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1688
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1689 /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1690 * import_as_names
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1691 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1692 static int
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1693 validate_import_from(node *tree)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1694 {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1695 int nch = NCH(tree);
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1696 int ndots = count_from_dots(tree);
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1697 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1698 int offset = ndots + havename;
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1699 int res = validate_ntype(tree, import_from)
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1700 && (nch >= 4 + ndots)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1701 && validate_name(CHILD(tree, 0), "from")
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1702 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1703 && validate_name(CHILD(tree, offset + 1), "import");
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1704
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1705 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1706 res = ((nch == offset + 5)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1707 && validate_lparen(CHILD(tree, offset + 2))
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1708 && validate_import_as_names(CHILD(tree, offset + 3))
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1709 && validate_rparen(CHILD(tree, offset + 4)));
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1710 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1711 res = validate_import_as_names(CHILD(tree, offset + 2));
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1712 return (res);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1713 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1714
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1715
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1716 /* import_stmt: import_name | import_from */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1717 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1718 validate_import_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1719 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1720 int nch = NCH(tree);
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1721 int res = validate_numnodes(tree, 1, "import_stmt");
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1722
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1723 if (res) {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1724 int ntype = TYPE(CHILD(tree, 0));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1725
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1726 if (ntype == import_name || ntype == import_from)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1727 res = validate_node(CHILD(tree, 0));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1728 else {
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1729 res = 0;
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1730 err_string("illegal import_stmt child type");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1731 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1732 }
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1733 else if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1734 res = 0;
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1735 PyErr_Format(parser_error,
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1736 "Unrecognized child node of import_stmt: %d.",
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1737 TYPE(CHILD(tree, 0)));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
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
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1743
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1744
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1745 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1746 validate_global_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1747 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1748 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1749 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1750 int res = (validate_ntype(tree, global_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1751 && is_even(nch) && (nch >= 2));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1752
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
1753 if (!res && !PyErr_Occurred())
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
1754 err_string("illegal global statement");
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
1755
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1756 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1757 res = (validate_name(CHILD(tree, 0), "global")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1758 && validate_ntype(CHILD(tree, 1), NAME));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1759 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
1760 res = (validate_comma(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1761 && validate_ntype(CHILD(tree, j + 1), NAME));
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 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1764 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1765
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1766
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1767 /* assert_stmt:
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1768 *
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1769 * 'assert' test [',' test]
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1770 */
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1771 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1772 validate_assert_stmt(node *tree)
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1773 {
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1774 int nch = NCH(tree);
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1775 int res = (validate_ntype(tree, assert_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1776 && ((nch == 2) || (nch == 4))
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
1777 && (validate_name(CHILD(tree, 0), "assert"))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1778 && validate_test(CHILD(tree, 1)));
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1779
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1780 if (!res && !PyErr_Occurred())
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
1781 err_string("illegal assert statement");
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1782 if (res && (nch > 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1783 res = (validate_comma(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1784 && validate_test(CHILD(tree, 3)));
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1785
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1786 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1787 }
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1788
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1789
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1790 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1791 validate_while(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1792 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1793 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1794 int res = (validate_ntype(tree, while_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1795 && ((nch == 4) || (nch == 7))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1796 && validate_name(CHILD(tree, 0), "while")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1797 && validate_test(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1798 && validate_colon(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1799 && validate_suite(CHILD(tree, 3)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1800
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1801 if (res && (nch == 7))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1802 res = (validate_name(CHILD(tree, 4), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1803 && validate_colon(CHILD(tree, 5))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1804 && validate_suite(CHILD(tree, 6)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1805
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1806 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1807 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1808
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1809
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1810 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1811 validate_for(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1812 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1813 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1814 int res = (validate_ntype(tree, for_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1815 && ((nch == 6) || (nch == 9))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1816 && validate_name(CHILD(tree, 0), "for")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1817 && validate_exprlist(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1818 && validate_name(CHILD(tree, 2), "in")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1819 && validate_testlist(CHILD(tree, 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1820 && validate_colon(CHILD(tree, 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1821 && validate_suite(CHILD(tree, 5)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1822
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1823 if (res && (nch == 9))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1824 res = (validate_name(CHILD(tree, 6), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1825 && validate_colon(CHILD(tree, 7))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1826 && validate_suite(CHILD(tree, 8)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1827
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1828 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1829 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1830
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1831
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1832 /* try_stmt:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1833 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1834 * | 'try' ':' suite 'finally' ':' suite
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1835 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1836 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1837 static int
13503
6484098f40fc ANSI-fying
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13466
diff changeset
1838 validate_try(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1839 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1840 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1841 int pos = 3;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1842 int res = (validate_ntype(tree, try_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1843 && (nch >= 6) && ((nch % 3) == 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1844
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1845 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1846 res = (validate_name(CHILD(tree, 0), "try")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1847 && validate_colon(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1848 && validate_suite(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1849 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1850 && validate_suite(CHILD(tree, nch - 1)));
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1851 else if (!PyErr_Occurred()) {
13325
7bac10a9bb61 Andrew Kuchling <akuchlin@mems-exchange.org>:
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
1852 const char* name = "except";
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1853 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1854 name = STR(CHILD(tree, nch - 3));
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1855
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1856 PyErr_Format(parser_error,
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1857 "Illegal number of children for try/%s node.", name);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1858 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1859 /* Skip past except_clause sections: */
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1860 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
1861 res = (validate_except_clause(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1862 && validate_colon(CHILD(tree, pos + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1863 && validate_suite(CHILD(tree, pos + 2)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1864 pos += 3;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1865 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1866 if (res && (pos < nch)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1867 res = validate_ntype(CHILD(tree, pos), NAME);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1868 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
1869 res = (validate_numnodes(tree, 6, "try/finally")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1870 && validate_colon(CHILD(tree, 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1871 && validate_suite(CHILD(tree, 5)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1872 else if (res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1873 if (nch == (pos + 3)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1874 res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1875 || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1876 if (!res)
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
1877 err_string("illegal trailing triple in try statement");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1878 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1879 else if (nch == (pos + 6)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1880 res = (validate_name(CHILD(tree, pos), "except")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1881 && validate_colon(CHILD(tree, pos + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1882 && validate_suite(CHILD(tree, pos + 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1883 && validate_name(CHILD(tree, pos + 3), "else"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1884 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1885 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1886 res = validate_numnodes(tree, pos + 3, "try/except");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1887 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1888 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1889 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1890 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1891
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1892
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1893 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1894 validate_except_clause(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1895 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1896 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1897 int res = (validate_ntype(tree, except_clause)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1898 && ((nch == 1) || (nch == 2) || (nch == 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1899 && validate_name(CHILD(tree, 0), "except"));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1900
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1901 if (res && (nch > 1))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1902 res = validate_test(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1903 if (res && (nch == 4))
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40529
diff changeset
1904 res = (validate_name(CHILD(tree, 2), "as")
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40529
diff changeset
1905 && validate_ntype(CHILD(tree, 3), NAME));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1906
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1907 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1908 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1909
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1910
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1911 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1912 validate_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1913 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1914 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1915 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
1916
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1917 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1918 res = ((nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1919 && validate_lambdef(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1920 else if (res) {
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1921 res = validate_or_test(CHILD(tree, 0));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1922 res = (res && (nch == 1 || (nch == 5 &&
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1923 validate_name(CHILD(tree, 1), "if") &&
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1924 validate_or_test(CHILD(tree, 2)) &&
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1925 validate_name(CHILD(tree, 3), "else") &&
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1926 validate_test(CHILD(tree, 4)))));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1927 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1928 return (res);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1929 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1930
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1931 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1932 validate_test_nocond(node *tree)
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1933 {
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1934 int nch = NCH(tree);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1935 int res = validate_ntype(tree, test_nocond) && (nch == 1);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1936
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1937 if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1938 res = (validate_lambdef_nocond(CHILD(tree, 0)));
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1939 else if (res) {
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1940 res = (validate_or_test(CHILD(tree, 0)));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1941 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1942 return (res);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1943 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1944
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1945 static int
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1946 validate_or_test(node *tree)
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1947 {
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1948 int nch = NCH(tree);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1949 int res = validate_ntype(tree, or_test) && is_odd(nch);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1950
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1951 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1952 int pos;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1953 res = validate_and_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1954 for (pos = 1; res && (pos < nch); pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1955 res = (validate_name(CHILD(tree, pos), "or")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1956 && 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
1957 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1958 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1959 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1960
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1961
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1962 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1963 validate_and_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1964 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1965 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1966 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1967 int res = (validate_ntype(tree, and_test)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1968 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1969 && validate_not_test(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1970
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1971 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
1972 res = (validate_name(CHILD(tree, pos), "and")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1973 && validate_not_test(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1974
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1975 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1976 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1977
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1978
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1979 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1980 validate_not_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1981 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1982 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1983 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
1984
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1985 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1986 if (nch == 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1987 res = (validate_name(CHILD(tree, 0), "not")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1988 && validate_not_test(CHILD(tree, 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1989 else if (nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1990 res = validate_comparison(CHILD(tree, 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1991 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1992 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1993 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1994
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1995
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1996 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1997 validate_comparison(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1998 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1999 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2000 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2001 int res = (validate_ntype(tree, comparison)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2002 && is_odd(nch)
41699
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2003 && validate_star_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2004
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2005 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
2006 res = (validate_comp_op(CHILD(tree, pos))
41699
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2007 && validate_star_expr(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2008
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2009 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2010 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2011
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2012
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2013 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2014 validate_comp_op(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2015 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2016 int res = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2017 int nch = NCH(tree);
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2018
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2019 if (!validate_ntype(tree, comp_op))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2020 return (0);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2021 if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2022 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2023 * 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
2024 * 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
2025 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2026 tree = CHILD(tree, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2027 switch (TYPE(tree)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2028 case LESS:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2029 case GREATER:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2030 case EQEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2031 case EQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2032 case LESSEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2033 case GREATEREQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2034 case NOTEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2035 res = 1;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2036 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2037 case NAME:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2038 res = ((strcmp(STR(tree), "in") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2039 || (strcmp(STR(tree), "is") == 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2040 if (!res) {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
2041 PyErr_Format(parser_error,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2042 "illegal operator '%s'", STR(tree));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2043 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2044 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2045 default:
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2046 err_string("illegal comparison operator type");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2047 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2048 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2049 }
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
2050 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
2051 res = (validate_ntype(CHILD(tree, 0), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2052 && validate_ntype(CHILD(tree, 1), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2053 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2054 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2055 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2056 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2057 if (!res && !PyErr_Occurred())
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2058 err_string("unknown comparison operator");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2059 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2060 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2061 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2062
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2063
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2064 static int
41699
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2065 validate_star_expr(node *tree)
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2066 {
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2067 int res = validate_ntype(tree, star_expr);
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2068 if (!res) return res;
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2069 if (NCH(tree) == 2) {
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2070 return validate_ntype(CHILD(tree, 0), STAR) && \
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2071 validate_expr(CHILD(tree, 1));
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2072 } else {
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2073 return validate_expr(CHILD(tree, 0));
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2074 }
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2075 }
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2076
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2077
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2078 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2079 validate_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2080 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2081 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2082 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2083 int res = (validate_ntype(tree, expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2084 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2085 && validate_xor_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2086
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2087 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
2088 res = (validate_xor_expr(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2089 && validate_vbar(CHILD(tree, j - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2090
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2091 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2092 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2093
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2094
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2095 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2096 validate_xor_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2097 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2098 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2099 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2100 int res = (validate_ntype(tree, xor_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2101 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2102 && validate_and_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2103
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2104 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
2105 res = (validate_circumflex(CHILD(tree, j - 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2106 && validate_and_expr(CHILD(tree, j)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2107
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2108 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2109 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2110
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2111
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2112 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2113 validate_and_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2114 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2115 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2116 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2117 int res = (validate_ntype(tree, and_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2118 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2119 && validate_shift_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2120
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2121 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
2122 res = (validate_ampersand(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2123 && validate_shift_expr(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2124
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2125 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2126 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2127
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2128
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2129 static int
13503
6484098f40fc ANSI-fying
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13466
diff changeset
2130 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
2131 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2132 int pos = 1;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2133 int nch = NCH(tree);
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2134 int res = (is_odd(nch)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2135 && (*termvalid)(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2136
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2137 for ( ; res && (pos < nch); pos += 2) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2138 if (TYPE(CHILD(tree, pos)) != op1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2139 res = validate_ntype(CHILD(tree, pos), op2);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2140 if (res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2141 res = (*termvalid)(CHILD(tree, pos + 1));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2142 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2143 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2144 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2145
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2146
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2147 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2148 validate_shift_expr(node *tree)
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 return (validate_ntype(tree, shift_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2151 && validate_chain_two_ops(tree, validate_arith_expr,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2152 LEFTSHIFT, RIGHTSHIFT));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2153 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2154
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
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_arith_expr(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 return (validate_ntype(tree, arith_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2160 && 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
2161 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2162
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2163
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2164 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2165 validate_term(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2166 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2167 int pos = 1;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2168 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2169 int res = (validate_ntype(tree, term)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2170 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2171 && validate_factor(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2172
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2173 for ( ; res && (pos < nch); pos += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2174 res = (((TYPE(CHILD(tree, pos)) == STAR)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2175 || (TYPE(CHILD(tree, pos)) == SLASH)
27517
51abb5e86226 Teach the parsermodule about floor division. Fixes
Michael W. Hudson <mwh@python.net>
parents: 26979
diff changeset
2176 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2177 || (TYPE(CHILD(tree, pos)) == PERCENT))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2178 && validate_factor(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2179
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2180 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2181 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2182
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2183
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2184 /* factor:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2185 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2186 * factor: ('+'|'-'|'~') factor | power
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2187 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2188 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2189 validate_factor(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2190 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2191 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2192 int res = (validate_ntype(tree, factor)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2193 && (((nch == 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2194 && ((TYPE(CHILD(tree, 0)) == PLUS)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2195 || (TYPE(CHILD(tree, 0)) == MINUS)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2196 || (TYPE(CHILD(tree, 0)) == TILDE))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2197 && validate_factor(CHILD(tree, 1)))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2198 || ((nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2199 && validate_power(CHILD(tree, 0)))));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2200 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2201 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2202
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2203
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2204 /* power:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2205 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2206 * power: atom trailer* ('**' factor)*
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2207 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2208 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2209 validate_power(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2210 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2211 int pos = 1;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2212 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2213 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
2214 && validate_atom(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2215
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2216 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
2217 res = validate_trailer(CHILD(tree, pos++));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2218 if (res && (pos < nch)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2219 if (!is_even(nch - pos)) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2220 err_string("illegal number of nodes for 'power'");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2221 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2222 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2223 for ( ; res && (pos < (nch - 1)); pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2224 res = (validate_doublestar(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2225 && validate_factor(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2226 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2227 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2228 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2229
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2230
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2231 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2232 validate_atom(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2233 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2234 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2235 int nch = NCH(tree);
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2236 int res = validate_ntype(tree, atom);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2237
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2238 if (res && nch < 1)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2239 res = validate_numnodes(tree, nch+1, "atom");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2240 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2241 switch (TYPE(CHILD(tree, 0))) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2242 case LPAR:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2243 res = ((nch <= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2244 && (validate_rparen(CHILD(tree, nch - 1))));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2245
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2246 if (res && (nch == 3)) {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2247 if (TYPE(CHILD(tree, 1))==yield_expr)
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2248 res = validate_yield_expr(CHILD(tree, 1));
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2249 else
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2250 res = validate_testlist_comp(CHILD(tree, 1));
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2251 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2252 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2253 case LSQB:
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2254 if (nch == 2)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2255 res = validate_ntype(CHILD(tree, 1), RSQB);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2256 else if (nch == 3)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2257 res = (validate_testlist_comp(CHILD(tree, 1))
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2258 && validate_ntype(CHILD(tree, 2), RSQB));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2259 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2260 res = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2261 err_string("illegal list display atom");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2262 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2263 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2264 case LBRACE:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2265 res = ((nch <= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2266 && 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
2267
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2268 if (res && (nch == 3))
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2269 res = validate_dictorsetmaker(CHILD(tree, 1));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2270 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2271 case NAME:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2272 case NUMBER:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2273 res = (nch == 1);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2274 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2275 case STRING:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2276 for (pos = 1; res && (pos < nch); ++pos)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2277 res = validate_ntype(CHILD(tree, pos), STRING);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2278 break;
39783
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
2279 case DOT:
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
2280 res = (nch == 3 &&
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
2281 validate_ntype(CHILD(tree, 1), DOT) &&
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
2282 validate_ntype(CHILD(tree, 2), DOT));
7a0ad110e91e Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents: 39782
diff changeset
2283 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2284 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2285 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2286 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2287 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2288 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2289 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2290 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2291
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2292
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2293 /* testlist_comp:
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2294 * test ( comp_for | (',' test)* [','] )
14129
2c2ae86dfe87 validate_listmaker(): Revise to match Skip's latest changes to the
Fred Drake <fdrake@acm.org>
parents: 14113
diff changeset
2295 */
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2296 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2297 validate_testlist_comp(node *tree)
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2298 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2299 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2300 int ok = nch;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2301
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2302 if (nch == 0)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2303 err_string("missing child nodes of testlist_comp");
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2304 else {
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2305 ok = validate_test(CHILD(tree, 0));
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2306 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2307
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2308 /*
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2309 * comp_for | (',' test)* [',']
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2310 */
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2311 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for)
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2312 ok = validate_comp_for(CHILD(tree, 1));
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2313 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2314 /* (',' test)* [','] */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2315 int i = 1;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2316 while (ok && nch - i >= 2) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2317 ok = (validate_comma(CHILD(tree, i))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2318 && validate_test(CHILD(tree, i+1)));
14129
2c2ae86dfe87 validate_listmaker(): Revise to match Skip's latest changes to the
Fred Drake <fdrake@acm.org>
parents: 14113
diff changeset
2319 i += 2;
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2320 }
14129
2c2ae86dfe87 validate_listmaker(): Revise to match Skip's latest changes to the
Fred Drake <fdrake@acm.org>
parents: 14113
diff changeset
2321 if (ok && i == nch-1)
2c2ae86dfe87 validate_listmaker(): Revise to match Skip's latest changes to the
Fred Drake <fdrake@acm.org>
parents: 14113
diff changeset
2322 ok = validate_comma(CHILD(tree, i));
2c2ae86dfe87 validate_listmaker(): Revise to match Skip's latest changes to the
Fred Drake <fdrake@acm.org>
parents: 14113
diff changeset
2323 else if (i != nch) {
2c2ae86dfe87 validate_listmaker(): Revise to match Skip's latest changes to the
Fred Drake <fdrake@acm.org>
parents: 14113
diff changeset
2324 ok = 0;
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2325 err_string("illegal trailing nodes for testlist_comp");
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2326 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2327 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2328 return ok;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2329 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2330
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2331 /* decorator:
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2332 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2333 */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2334 static int
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2335 validate_decorator(node *tree)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2336 {
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2337 int ok;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2338 int nch = NCH(tree);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2339 ok = (validate_ntype(tree, decorator) &&
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2340 (nch == 3 || nch == 5 || nch == 6) &&
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2341 validate_at(CHILD(tree, 0)) &&
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2342 validate_dotted_name(CHILD(tree, 1)) &&
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2343 validate_newline(RCHILD(tree, -1)));
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2344
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2345 if (ok && nch != 3) {
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2346 ok = (validate_lparen(CHILD(tree, 2)) &&
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2347 validate_rparen(RCHILD(tree, -2)));
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2348
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2349 if (ok && nch == 6)
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2350 ok = validate_arglist(CHILD(tree, 3));
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2351 }
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2352
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2353 return ok;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2354 }
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2355
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2356 /* decorators:
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2357 * decorator+
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2358 */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2359 static int
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2360 validate_decorators(node *tree)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2361 {
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
2362 int i, nch, ok;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2363 nch = NCH(tree);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2364 ok = validate_ntype(tree, decorators) && nch >= 1;
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2365
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2366 for (i = 0; ok && i < nch; ++i)
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2367 ok = validate_decorator(CHILD(tree, i));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2368
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2369 return ok;
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2370 }
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2371
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2372 /* funcdef:
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
2373 *
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2374 * -5 -4 -3 -2 -1
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2375 * 'def' NAME parameters ':' suite
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2376 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2377 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2378 validate_funcdef(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2379 {
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2380 int nch = NCH(tree);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2381 int ok = (validate_ntype(tree, funcdef)
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2382 && (nch == 5)
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2383 && validate_name(RCHILD(tree, -5), "def")
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2384 && validate_ntype(RCHILD(tree, -4), NAME)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2385 && validate_colon(RCHILD(tree, -2))
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2386 && validate_parameters(RCHILD(tree, -3))
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2387 && validate_suite(RCHILD(tree, -1)));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2388 return ok;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2389 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2390
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2391
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2392 /* decorated
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2393 * decorators (classdef | funcdef)
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2394 */
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2395 static int
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2396 validate_decorated(node *tree)
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2397 {
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2398 int nch = NCH(tree);
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2399 int ok = (validate_ntype(tree, decorated)
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2400 && (nch == 2)
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2401 && validate_decorators(RCHILD(tree, -2))
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2402 && (validate_funcdef(RCHILD(tree, -1))
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2403 || validate_class(RCHILD(tree, -1)))
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2404 );
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2405 return ok;
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2406 }
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2407
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2408 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2409 validate_lambdef(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2410 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2411 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2412 int res = (validate_ntype(tree, lambdef)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2413 && ((nch == 3) || (nch == 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2414 && validate_name(CHILD(tree, 0), "lambda")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2415 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2416 && validate_test(CHILD(tree, nch - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2417
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2418 if (res && (nch == 4))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2419 res = validate_varargslist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2420 else if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2421 (void) validate_numnodes(tree, 3, "lambdef");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2422
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2423 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2424 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2425
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2426
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2427 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2428 validate_lambdef_nocond(node *tree)
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2429 {
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2430 int nch = NCH(tree);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2431 int res = (validate_ntype(tree, lambdef_nocond)
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2432 && ((nch == 3) || (nch == 4))
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2433 && validate_name(CHILD(tree, 0), "lambda")
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2434 && validate_colon(CHILD(tree, nch - 2))
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2435 && validate_test(CHILD(tree, nch - 1)));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2436
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2437 if (res && (nch == 4))
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2438 res = validate_varargslist(CHILD(tree, 1));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2439 else if (!res && !PyErr_Occurred())
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2440 (void) validate_numnodes(tree, 3, "lambdef_nocond");
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2441
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2442 return (res);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2443 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2444
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2445
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2446 /* arglist:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2447 *
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2448 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2449 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2450 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2451 validate_arglist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2452 {
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2453 int nch = NCH(tree);
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2454 int i = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2455 int ok = 1;
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2456
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2457 if (nch <= 0)
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2458 /* 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
2459 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
2460
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2461 if (nch > 1) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2462 for (i=0; i<nch; i++) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2463 if (TYPE(CHILD(tree, i)) == argument) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2464 node *ch = CHILD(tree, i);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2465 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2466 err_string("need '(', ')' for generator expression");
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2467 return 0;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2468 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2469 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2470 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2471 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2472
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2473 while (ok && nch-i >= 2) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2474 /* skip leading (argument ',') */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2475 ok = (validate_argument(CHILD(tree, i))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2476 && validate_comma(CHILD(tree, i+1)));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2477 if (ok)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2478 i += 2;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2479 else
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2480 PyErr_Clear();
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2481 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2482 ok = 1;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2483 if (nch-i > 0) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2484 /*
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2485 * argument | '*' test [',' '**' test] | '**' test
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2486 */
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2487 int sym = TYPE(CHILD(tree, i));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2488
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2489 if (sym == argument) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2490 ok = validate_argument(CHILD(tree, i));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2491 if (ok && i+1 != nch) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2492 err_string("illegal arglist specification"
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2493 " (extra stuff on end)");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2494 ok = 0;
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2495 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2496 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2497 else if (sym == STAR) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2498 ok = validate_star(CHILD(tree, i));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2499 if (ok && (nch-i == 2))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2500 ok = validate_test(CHILD(tree, i+1));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2501 else if (ok && (nch-i == 5))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2502 ok = (validate_test(CHILD(tree, i+1))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2503 && validate_comma(CHILD(tree, i+2))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2504 && validate_doublestar(CHILD(tree, i+3))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2505 && validate_test(CHILD(tree, i+4)));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2506 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2507 err_string("illegal use of '*' in arglist");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2508 ok = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2509 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2510 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2511 else if (sym == DOUBLESTAR) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2512 if (nch-i == 2)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2513 ok = (validate_doublestar(CHILD(tree, i))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2514 && validate_test(CHILD(tree, i+1)));
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2515 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2516 err_string("illegal use of '**' in arglist");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2517 ok = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2518 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2519 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2520 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2521 err_string("illegal arglist specification");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2522 ok = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2523 }
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2524 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2525 return (ok);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2526 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2527
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
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2530 /* argument:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2531 *
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2532 * [test '='] test [comp_for]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2533 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2534 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2535 validate_argument(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2536 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2537 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2538 int res = (validate_ntype(tree, argument)
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2539 && ((nch == 1) || (nch == 2) || (nch == 3))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2540 && validate_test(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2541
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2542 if (res && (nch == 2))
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2543 res = validate_comp_for(CHILD(tree, 1));
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2544 else if (res && (nch == 3))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2545 res = (validate_equal(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2546 && validate_test(CHILD(tree, 2)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2547
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2548 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2549 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2550
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2551
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2552
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2553 /* trailer:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2554 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2555 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2556 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2557 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2558 validate_trailer(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2559 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2560 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2561 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
2562
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2563 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2564 switch (TYPE(CHILD(tree, 0))) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2565 case LPAR:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2566 res = validate_rparen(CHILD(tree, nch - 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2567 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2568 res = validate_arglist(CHILD(tree, 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2569 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2570 case LSQB:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2571 res = (validate_numnodes(tree, 3, "trailer")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2572 && validate_subscriptlist(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2573 && validate_ntype(CHILD(tree, 2), RSQB));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2574 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2575 case DOT:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2576 res = (validate_numnodes(tree, 2, "trailer")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2577 && validate_ntype(CHILD(tree, 1), NAME));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2578 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2579 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2580 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2581 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2582 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2583 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2584 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2585 (void) validate_numnodes(tree, 2, "trailer");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2586 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2587 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2588 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2589
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2590
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2591 /* subscriptlist:
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2592 *
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2593 * subscript (',' subscript)* [',']
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2594 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2595 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2596 validate_subscriptlist(node *tree)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2597 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2598 return (validate_repeating_list(tree, subscriptlist,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2599 validate_subscript, "subscriptlist"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2600 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2601
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2602
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2603 /* subscript:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2604 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2605 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2606 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2607 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2608 validate_subscript(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2609 {
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2610 int offset = 0;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2611 int nch = NCH(tree);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2612 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
2613
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2614 if (!res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2615 if (!PyErr_Occurred())
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2616 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
2617 return (0);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2618 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2619 if (TYPE(CHILD(tree, 0)) == DOT)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2620 /* take care of ('.' '.' '.') possibility */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2621 return (validate_numnodes(tree, 3, "subscript")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2622 && validate_dot(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2623 && validate_dot(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2624 && validate_dot(CHILD(tree, 2)));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2625 if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2626 if (TYPE(CHILD(tree, 0)) == test)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2627 res = validate_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2628 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2629 res = validate_colon(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2630 return (res);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2631 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2632 /* Must be [test] ':' [test] [sliceop],
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2633 * 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
2634 * 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
2635 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2636 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
2637 res = validate_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2638 offset = 1;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2639 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2640 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2641 res = validate_colon(CHILD(tree, offset));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2642 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2643 int rem = nch - ++offset;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2644 if (rem) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2645 if (TYPE(CHILD(tree, offset)) == test) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2646 res = validate_test(CHILD(tree, offset));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2647 ++offset;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2648 --rem;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2649 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2650 if (res && rem)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2651 res = validate_sliceop(CHILD(tree, offset));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2652 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2653 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2654 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2655 }
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
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2658 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2659 validate_sliceop(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2660 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2661 int nch = NCH(tree);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2662 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
2663 && validate_ntype(tree, sliceop);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2664 if (!res && !PyErr_Occurred()) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2665 res = validate_numnodes(tree, 1, "sliceop");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2666 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2667 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2668 res = validate_colon(CHILD(tree, 0));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2669 if (res && (nch == 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2670 res = validate_test(CHILD(tree, 1));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2671
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2672 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2673 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2674
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2675
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2676 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2677 validate_exprlist(node *tree)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2678 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2679 return (validate_repeating_list(tree, exprlist,
41699
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2680 validate_star_expr, "exprlist"));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2681 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2682
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2683
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2684 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2685 validate_dictorsetmaker(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2686 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2687 int nch = NCH(tree);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2688 int res = (validate_ntype(tree, dictorsetmaker)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2689 && (nch >= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2690 && validate_test(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2691 && validate_colon(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2692 && validate_test(CHILD(tree, 2)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2693
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2694 if (res && ((nch % 4) == 0))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2695 res = validate_comma(CHILD(tree, --nch));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2696 else if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2697 res = ((nch % 4) == 3);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2698
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2699 if (res && (nch > 3)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2700 int pos = 3;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2701 /* ( ',' test ':' test )* */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2702 while (res && (pos < nch)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2703 res = (validate_comma(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2704 && validate_test(CHILD(tree, pos + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2705 && validate_colon(CHILD(tree, pos + 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2706 && validate_test(CHILD(tree, pos + 3)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2707 pos += 4;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2708 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2709 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2710 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2711 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2712
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2713
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2714 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2715 validate_eval_input(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2716 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2717 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2718 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2719 int res = (validate_ntype(tree, eval_input)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2720 && (nch >= 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2721 && validate_testlist(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2722 && 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
2723
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2724 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
2725 res = validate_ntype(CHILD(tree, pos), NEWLINE);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2726
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2727 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2728 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2729
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2730
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2731 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2732 validate_node(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2733 {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2734 int nch = 0; /* num. children on current node */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2735 int res = 1; /* result value */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2736 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
2737
18247
e3cf3348feca Properly use &&. Closes bug #434989.
Martin v. Löwis <martin@v.loewis.de>
parents: 15751
diff changeset
2738 while (res && (tree != 0)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2739 nch = NCH(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2740 next = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2741 switch (TYPE(tree)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2742 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2743 * Definition nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2744 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2745 case funcdef:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2746 res = validate_funcdef(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2747 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2748 case classdef:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2749 res = validate_class(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2750 break;
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2751 case decorated:
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2752 res = validate_decorated(tree);
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2753 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2754 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2755 * "Trivial" parse tree nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2756 * (Why did I call these trivial?)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2757 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2758 case stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2759 res = validate_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2760 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2761 case small_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2762 /*
40774
793e0323d4d6 Essential changes for print function changes.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
2763 * expr_stmt | del_stmt | pass_stmt | flow_stmt
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39701
diff changeset
2764 * | import_stmt | global_stmt | assert_stmt
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2765 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2766 res = validate_small_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2767 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2768 case flow_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2769 res = (validate_numnodes(tree, 1, "flow_stmt")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2770 && ((TYPE(CHILD(tree, 0)) == break_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2771 || (TYPE(CHILD(tree, 0)) == continue_stmt)
18646
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
2772 || (TYPE(CHILD(tree, 0)) == yield_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2773 || (TYPE(CHILD(tree, 0)) == return_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2774 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2775 if (res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2776 next = CHILD(tree, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2777 else if (nch == 1)
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2778 err_string("illegal flow_stmt type");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2779 break;
18646
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
2780 case yield_stmt:
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
2781 res = validate_yield_stmt(tree);
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
2782 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2783 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2784 * Compound statements.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2785 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2786 case simple_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2787 res = validate_simple_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2788 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2789 case compound_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2790 res = validate_compound_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2791 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2792 /*
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
2793 * Fundamental statements.
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2794 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2795 case expr_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2796 res = validate_expr_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2797 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2798 case del_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2799 res = validate_del_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2800 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2801 case pass_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2802 res = (validate_numnodes(tree, 1, "pass")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2803 && validate_name(CHILD(tree, 0), "pass"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2804 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2805 case break_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2806 res = (validate_numnodes(tree, 1, "break")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2807 && validate_name(CHILD(tree, 0), "break"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2808 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2809 case continue_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2810 res = (validate_numnodes(tree, 1, "continue")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2811 && validate_name(CHILD(tree, 0), "continue"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2812 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2813 case return_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2814 res = validate_return_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2815 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2816 case raise_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2817 res = validate_raise_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2818 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2819 case import_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2820 res = validate_import_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2821 break;
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
2822 case import_name:
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
2823 res = validate_import_name(tree);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
2824 break;
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
2825 case import_from:
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
2826 res = validate_import_from(tree);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
2827 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2828 case global_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2829 res = validate_global_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2830 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2831 case assert_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2832 res = validate_assert_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2833 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2834 case if_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2835 res = validate_if(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2836 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2837 case while_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2838 res = validate_while(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2839 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2840 case for_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2841 res = validate_for(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2842 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2843 case try_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2844 res = validate_try(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2845 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2846 case suite:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2847 res = validate_suite(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2848 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2849 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2850 * Expression nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2851 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2852 case testlist:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2853 res = validate_testlist(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2854 break;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2855 case yield_expr:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2856 res = validate_yield_expr(tree);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2857 break;
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
2858 case testlist1:
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
2859 res = validate_testlist1(tree);
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
2860 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2861 case test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2862 res = validate_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2863 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2864 case and_test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2865 res = validate_and_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2866 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2867 case not_test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2868 res = validate_not_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2869 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2870 case comparison:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2871 res = validate_comparison(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2872 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2873 case exprlist:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2874 res = validate_exprlist(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2875 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2876 case comp_op:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2877 res = validate_comp_op(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2878 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2879 case expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2880 res = validate_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2881 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2882 case xor_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2883 res = validate_xor_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2884 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2885 case and_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2886 res = validate_and_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2887 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2888 case shift_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2889 res = validate_shift_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2890 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2891 case arith_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2892 res = validate_arith_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2893 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2894 case term:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2895 res = validate_term(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2896 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2897 case factor:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2898 res = validate_factor(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2899 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2900 case power:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2901 res = validate_power(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2902 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2903 case atom:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2904 res = validate_atom(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2905 break;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2906
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2907 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2908 /* Hopefully never reached! */
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2909 err_string("unrecognized node type");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2910 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2911 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2912 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2913 tree = next;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2914 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2915 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2916 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2917
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2918
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2919 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2920 validate_expr_tree(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2921 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2922 int res = validate_eval_input(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2923
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2924 if (!res && !PyErr_Occurred())
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2925 err_string("could not validate expression tuple");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2926
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2927 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2928 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2929
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2930
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2931 /* file_input:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2932 * (NEWLINE | stmt)* ENDMARKER
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2933 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2934 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2935 validate_file_input(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2936 {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
2937 int j;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2938 int nch = NCH(tree) - 1;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2939 int res = ((nch >= 0)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2940 && validate_ntype(CHILD(tree, nch), ENDMARKER));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2941
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
2942 for (j = 0; res && (j < nch); ++j) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2943 if (TYPE(CHILD(tree, j)) == stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2944 res = validate_stmt(CHILD(tree, j));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2945 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2946 res = validate_newline(CHILD(tree, j));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2947 }
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
2948 /* 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
2949 * 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
2950 * this, we have some debugging to do.
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2951 */
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2952 if (!res && !PyErr_Occurred())
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2953 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
2954
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2955 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2956 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2957
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2958 static int
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2959 validate_encoding_decl(node *tree)
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2960 {
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2961 int nch = NCH(tree);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2962 int res = ((nch == 1)
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2963 && validate_file_input(CHILD(tree, 0)));
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2964
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2965 if (!res && !PyErr_Occurred())
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2966 err_string("Error Parsing encoding_decl");
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2967
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2968 return res;
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
2969 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2970
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2971 static PyObject*
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2972 pickle_constructor = NULL;
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2973
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2974
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2975 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2976 parser__pickler(PyObject *self, PyObject *args)
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2977 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
2978 NOTE(ARGUNUSED(self))
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2979 PyObject *result = NULL;
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
2980 PyObject *st = NULL;
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2981 PyObject *empty_dict = NULL;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2982
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
2983 if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2984 PyObject *newargs;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2985 PyObject *tuple;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2986
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2987 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
2988 goto finally;
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
2989 if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2990 goto finally;
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
2991 tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2992 if (tuple != NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2993 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2994 Py_DECREF(tuple);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2995 }
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
2996 Py_DECREF(empty_dict);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2997 Py_DECREF(newargs);
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2998 }
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
2999 finally:
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
3000 Py_XDECREF(empty_dict);
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
3001
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3002 return (result);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3003 }
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3004
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3005
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3006 /* Functions exported by this module. Most of this should probably
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3007 * be converted into an ST object with methods, but that is better
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3008 * 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
3009 * 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
3010 * inheritance.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3011 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3012 static PyMethodDef parser_functions[] = {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3013 {"ast2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3014 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3015 {"ast2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3016 PyDoc_STR("Creates a list-tree representation of an ST.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3017 {"compileast", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3018 PyDoc_STR("Compiles an ST object into a code object.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3019 {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3020 PyDoc_STR("Compiles an ST object into a code object.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3021 {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3022 PyDoc_STR("Creates an ST object from an expression.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3023 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3024 PyDoc_STR("Determines if an ST object was created from an expression.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3025 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3026 PyDoc_STR("Determines if an ST object was created from a suite.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3027 {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3028 PyDoc_STR("Creates an ST object from a suite.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3029 {"sequence2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3030 PyDoc_STR("Creates an ST object from a tree representation.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3031 {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3032 PyDoc_STR("Creates an ST object from a tree representation.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3033 {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3034 PyDoc_STR("Creates a tuple-tree representation of an ST.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3035 {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3036 PyDoc_STR("Creates a list-tree representation of an ST.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3037 {"tuple2ast", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3038 PyDoc_STR("Creates an ST object from a tree representation.")},
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3039 {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3040 PyDoc_STR("Creates an ST object from a tree representation.")},
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3041
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3042 /* private stuff: support pickle module */
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3043 {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
3044 PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3045
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
3046 {NULL, NULL, 0, NULL}
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3047 };
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3048
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3049
24363
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
3050 PyMODINIT_FUNC initparser(void); /* supply a prototype */
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
3051
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
3052 PyMODINIT_FUNC
13692
793bbad6d09c ANSIfy a bit more.
Thomas Wouters <thomas@python.org>
parents: 13667
diff changeset
3053 initparser(void)
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
3054 {
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3055 PyObject *module, *copyreg;
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3056
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
3057 Py_TYPE(&PyST_Type) = &PyType_Type;
5039
38032267968c Initialize the ob_type field of PyAST_Type dynamically (in
Guido van Rossum <guido@python.org>
parents: 4600
diff changeset
3058 module = Py_InitModule("parser", parser_functions);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
3059 if (module == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
3060 return;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3061
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
3062 if (parser_error == 0)
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
3063 parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3064
29952
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3065 if (parser_error == 0)
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3066 /* caller will check PyErr_Occurred() */
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3067 return;
29952
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3068 /* CAUTION: The code next used to skip bumping the refcount on
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3069 * parser_error. That's a disaster if initparser() gets called more
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3070 * than once. By incref'ing, we ensure that each module dict that
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3071 * gets created owns its reference to the shared parser_error object,
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3072 * and the file static parser_error vrbl owns a reference too.
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3073 */
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3074 Py_INCREF(parser_error);
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3075 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3076 return;
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3077
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3078 Py_INCREF(&PyST_Type);
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3079 PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3080 Py_INCREF(&PyST_Type);
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3081 PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3082
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3083 PyModule_AddStringConstant(module, "__copyright__",
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3084 parser_copyright_string);
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3085 PyModule_AddStringConstant(module, "__doc__",
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3086 parser_doc_string);
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3087 PyModule_AddStringConstant(module, "__version__",
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3088 parser_version_string);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3089
18748
e5bafddd1da0 Elaborate a comment.
Fred Drake <fdrake@acm.org>
parents: 18669
diff changeset
3090 /* Register to support pickling.
e5bafddd1da0 Elaborate a comment.
Fred Drake <fdrake@acm.org>
parents: 18669
diff changeset
3091 * If this fails, the import of this module will fail because an
e5bafddd1da0 Elaborate a comment.
Fred Drake <fdrake@acm.org>
parents: 18669
diff changeset
3092 * exception will be raised here; should we clear the exception?
e5bafddd1da0 Elaborate a comment.
Fred Drake <fdrake@acm.org>
parents: 18669
diff changeset
3093 */
44441
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
3094 copyreg = PyImport_ImportModuleNoBlock("copy_reg");
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3095 if (copyreg != NULL) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3096 PyObject *func, *pickler;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3097
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3098 func = PyObject_GetAttrString(copyreg, "pickle");
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3099 pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3100 pickler = PyObject_GetAttrString(module, "_pickler");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3101 Py_XINCREF(pickle_constructor);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3102 if ((func != NULL) && (pickle_constructor != NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3103 && (pickler != NULL)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3104 PyObject *res;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3105
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
3106 res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
3107 pickle_constructor, NULL);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3108 Py_XDECREF(res);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3109 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3110 Py_XDECREF(func);
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3111 Py_XDECREF(pickle_constructor);
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3112 Py_XDECREF(pickler);
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3113 Py_DECREF(copyreg);
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3114 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3115 }