annotate Modules/parsermodule.c @ 86913:8c9769b17171 3.2

Backout 7d399099334d.
author Georg Brandl <georg@python.org>
date Mon, 04 Nov 2013 07:44:29 +0100
parents fdf907708f49
children 15a391919deb
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 */
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
29 #include "Python-ast.h" /* mod_ty */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
30 #include "graminit.h" /* symbols defined in the grammar */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
31 #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
32 #include "errcode.h" /* error codes for PyNode_*() */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
33 #include "token.h" /* token definitions */
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
34 #include "grammar.h"
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
35 #include "parsetok.h"
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
36 /* ISTERMINAL() / ISNONTERMINAL() */
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
37 #undef Yield
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
38 #include "ast.h"
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
39
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
40 extern grammar _PyParser_Grammar; /* From graminit.c */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
41
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
42 #ifdef lint
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
43 #include <note.h>
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
44 #else
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
45 #define NOTE(x)
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
46 #endif
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
47
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
48 /* String constants used to initialize module attributes.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
49 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
50 */
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
51 static char parser_copyright_string[] =
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
52 "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
53 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
54 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
55 Centrum, Amsterdam, The Netherlands.";
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
56
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
57
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
58 PyDoc_STRVAR(parser_doc_string,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
59 "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
60
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23538
diff changeset
61 static char parser_version_string[] = "0.5";
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
62
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
63
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
64 typedef PyObject* (*SeqMaker) (Py_ssize_t length);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
65 typedef int (*SeqInserter) (PyObject* sequence,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
66 Py_ssize_t index,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
67 PyObject* element);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
68
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
69 /* The function below is copyrighted by Stichting Mathematisch Centrum. The
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
70 * original copyright statement is included below, and continues to apply
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
71 * in full to the function immediately following. All other material is
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
72 * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
73 * Institute and State University. Changes were made to comply with the
4099
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
74 * new naming conventions. Added arguments to provide support for creating
3a3b36716689 Patches to make it compile with K&R C
Guido van Rossum <guido@python.org>
parents: 4092
diff changeset
75 * lists as well as tuples, and optionally including the line numbers.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
76 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
77
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
79 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
80 node2tuple(node *n, /* node to convert */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
81 SeqMaker mkseq, /* create sequence */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
82 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
83 int lineno, /* include line numbers? */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
84 int col_offset) /* include column offsets? */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
85 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
86 if (n == NULL) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
87 Py_INCREF(Py_None);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
88 return (Py_None);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
89 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
90 if (ISNONTERMINAL(TYPE(n))) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
91 int i;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
92 PyObject *v;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
93 PyObject *w;
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
94
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24796
diff changeset
95 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
96 if (v == NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
97 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
98 w = PyLong_FromLong(TYPE(n));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
99 if (w == NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
100 Py_DECREF(v);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
101 return ((PyObject*) NULL);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
102 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
103 (void) addelem(v, 0, w);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
104 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
105 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
106 if (w == NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
107 Py_DECREF(v);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
108 return ((PyObject*) NULL);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
109 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
110 (void) addelem(v, i+1, w);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
111 }
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
112
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24796
diff changeset
113 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
114 (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
115 return (v);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
116 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
117 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
118 PyObject *result = mkseq(2 + lineno + col_offset);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
119 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
120 (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
121 (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
122 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
123 (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
124 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
125 (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
126 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
127 return (result);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
128 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
129 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
130 PyErr_SetString(PyExc_SystemError,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
131 "unrecognized parse tree node type");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
132 return ((PyObject*) NULL);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
133 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
134 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
135 /*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
136 * 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
137 */
1406
474823e5f7dc * parsermodule.c, Makefile, config.c: rudimentary interface to the Python
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
139
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 /* 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
142 * '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
143 * 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
144 * 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
145 * 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
146 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
147
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
148 #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
149 #define PyST_SUITE 2
3200
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
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
152 /* 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
153 * 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
154 * 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
155 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
156
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
157 static PyObject*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
158 parser_error = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
159
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
160
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
161 typedef struct {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
162 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
163 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
164 int st_type; /* EXPR or SUITE ? */
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
165 PyCompilerFlags st_flags; /* Parser and compiler flags */
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
166 } PyST_Object;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
167
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
168
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
169 static void parser_free(PyST_Object *st);
78391
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
170 static PyObject* parser_sizeof(PyST_Object *, void *);
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
171 static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op);
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
172 static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
173 static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
174 static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
175 static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
176 static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
177
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
178 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
179
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
180 static PyMethodDef parser_methods[] = {
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
181 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
182 PyDoc_STR("Compile this ST object into a code object.")},
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
183 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
184 PyDoc_STR("Determines if this ST object was created from an expression.")},
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
185 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
186 PyDoc_STR("Determines if this ST object was created from a suite.")},
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
187 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
188 PyDoc_STR("Creates a list-tree representation of this ST.")},
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
189 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
190 PyDoc_STR("Creates a tuple-tree representation of this ST.")},
78391
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
191 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS,
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
192 PyDoc_STR("Returns size in memory, in bytes.")},
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
193 {NULL, NULL, 0, NULL}
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
194 };
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
195
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
196 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
197 PyTypeObject PyST_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41824
diff changeset
198 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
199 "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
200 (int) sizeof(PyST_Object), /* tp_basicsize */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
201 0, /* tp_itemsize */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
202 (destructor)parser_free, /* tp_dealloc */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
203 0, /* tp_print */
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
204 0, /* tp_getattr */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
205 0, /* tp_setattr */
51271
87fbbf22adf3 Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51244
diff changeset
206 0, /* tp_reserved */
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
207 0, /* tp_repr */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
208 0, /* tp_as_number */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
209 0, /* tp_as_sequence */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
210 0, /* tp_as_mapping */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
211 0, /* tp_hash */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
212 0, /* tp_call */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
213 0, /* tp_str */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
214 0, /* tp_getattro */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
215 0, /* tp_setattro */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
216
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
217 /* 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
218 0, /* tp_as_buffer */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
219
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
220 Py_TPFLAGS_DEFAULT, /* tp_flags */
5613
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
221
190e35bbc15a Add doc string to type object.
Fred Drake <fdrake@acm.org>
parents: 5236
diff changeset
222 /* __doc__ */
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
223 "Intermediate representation of a Python parse tree.",
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
224 0, /* tp_traverse */
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
225 0, /* tp_clear */
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
226 parser_richcompare, /* tp_richcompare */
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
227 0, /* tp_weaklistoffset */
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
228 0, /* tp_iter */
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
229 0, /* tp_iternext */
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
230 parser_methods, /* tp_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
231 }; /* PyST_Type */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
232
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
233
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
234 /* PyST_Type isn't subclassable, so just check ob_type */
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
235 #define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type)
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
236
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
237 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
238 parser_compare_nodes(node *left, node *right)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
239 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
240 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
241
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
242 if (TYPE(left) < TYPE(right))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
243 return (-1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
244
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
245 if (TYPE(right) < TYPE(left))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
246 return (1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
247
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
248 if (ISTERMINAL(TYPE(left)))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
249 return (strcmp(STR(left), STR(right)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
250
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
251 if (NCH(left) < NCH(right))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
252 return (-1);
3200
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 if (NCH(right) < NCH(left))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
255 return (1);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
256
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
257 for (j = 0; j < NCH(left); ++j) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
258 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
259
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
260 if (v != 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
261 return (v);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
262 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
263 return (0);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
264 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
265
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
266 /* parser_richcompare(PyObject* left, PyObject* right, int op)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
267 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
268 * Comparison function used by the Python operators ==, !=, <, >, <=, >=
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
269 * 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
270 * checks and protection code.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
271 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
272 */
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
273
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
274 #define TEST_COND(cond) ((cond) ? Py_True : Py_False)
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
275
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
276 static PyObject *
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
277 parser_richcompare(PyObject *left, PyObject *right, int op)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
278 {
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
279 int result;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
280 PyObject *v;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
281
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
282 /* neither argument should be NULL, unless something's gone wrong */
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
283 if (left == NULL || right == NULL) {
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
284 PyErr_BadInternalCall();
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
285 return NULL;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
286 }
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
287
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
288 /* both arguments should be instances of PyST_Object */
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
289 if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
290 v = Py_NotImplemented;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
291 goto finished;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
292 }
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
293
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
294 if (left == right)
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
295 /* if arguments are identical, they're equal */
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
296 result = 0;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
297 else
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
298 result = parser_compare_nodes(((PyST_Object *)left)->st_node,
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
299 ((PyST_Object *)right)->st_node);
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
300
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
301 /* Convert return value to a Boolean */
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
302 switch (op) {
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
303 case Py_EQ:
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
304 v = TEST_COND(result == 0);
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
305 break;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
306 case Py_NE:
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
307 v = TEST_COND(result != 0);
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
308 break;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
309 case Py_LE:
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
310 v = TEST_COND(result <= 0);
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
311 break;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
312 case Py_GE:
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
313 v = TEST_COND(result >= 0);
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
314 break;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
315 case Py_LT:
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
316 v = TEST_COND(result < 0);
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
317 break;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
318 case Py_GT:
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
319 v = TEST_COND(result > 0);
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
320 break;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
321 default:
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
322 PyErr_BadArgument();
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
323 return NULL;
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
324 }
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
325 finished:
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
326 Py_INCREF(v);
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50079
diff changeset
327 return v;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
328 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
329
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
330 /* parser_newstobject(node* st)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
331 *
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
332 * 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
333 * '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
334 * Python code.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
335 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
336 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
337 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
338 parser_newstobject(node *st, int type)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
339 {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
340 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
341
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
342 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
343 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
344 o->st_type = type;
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
345 o->st_flags.cf_flags = 0;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
346 }
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
347 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
348 PyNode_Free(st);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
349 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
350 return ((PyObject*)o);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
351 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
352
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
353
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
354 /* void parser_free(PyST_Object* st)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
355 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
356 * 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
357 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
358 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
359 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
360 parser_free(PyST_Object *st)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
361 {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
362 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
363 PyObject_Del(st);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
364 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
365
78391
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
366 static PyObject *
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
367 parser_sizeof(PyST_Object *st, void *unused)
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
368 {
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
369 Py_ssize_t res;
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
370
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
371 res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node);
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
372 return PyLong_FromSsize_t(res);
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
373 }
91884d04de06 Closes #15512: Correct __sizeof__ support for parser
Jesus Cea <jcea@jcea.es>
parents: 76824
diff changeset
374
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
375
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
376 /* 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
377 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
378 * 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
379 * 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
380 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
381 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
382 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
383 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
384 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
385 PyObject *line_option = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
386 PyObject *col_option = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
387 PyObject *res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
388 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
389
48558
fcbd9f4f095c Remove "ast" aliases from the parser module.
Georg Brandl <georg@python.org>
parents: 48413
diff changeset
390 static char *keywords[] = {"st", "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
391
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
392 if (self == NULL || PyModule_Check(self)) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
393 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
394 &PyST_Type, &self, &line_option,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
395 &col_option);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
396 }
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
397 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
398 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
399 &line_option, &col_option);
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
400 if (ok != 0) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
401 int lineno = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
402 int col_offset = 0;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
403 if (line_option != NULL) {
78604
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
404 lineno = PyObject_IsTrue(line_option);
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
405 if (lineno < 0)
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
406 return NULL;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
407 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
408 if (col_option != NULL) {
78604
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
409 col_offset = PyObject_IsTrue(col_option);
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
410 if (col_offset < 0)
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
411 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
412 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
413 /*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
414 * 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
415 * since it's known to work already.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
416 */
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 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
418 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
419 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
420 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
421 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
422
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
423
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
424 /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
425 *
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
426 * 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
427 * 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
428 *
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
429 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
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_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
432 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
433 PyObject *line_option = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
434 PyObject *col_option = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
435 PyObject *res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
436 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
437
48558
fcbd9f4f095c Remove "ast" aliases from the parser module.
Georg Brandl <georg@python.org>
parents: 48413
diff changeset
438 static char *keywords[] = {"st", "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
439
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
440 if (self == NULL || PyModule_Check(self))
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
441 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
442 &PyST_Type, &self, &line_option,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
443 &col_option);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
444 else
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
445 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
446 &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
447 if (ok) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
448 int lineno = 0;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
449 int col_offset = 0;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
450 if (line_option != 0) {
78604
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
451 lineno = PyObject_IsTrue(line_option);
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
452 if (lineno < 0)
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
453 return NULL;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
454 }
78604
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
455 if (col_option != 0) {
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
456 col_offset = PyObject_IsTrue(col_option);
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
457 if (col_offset < 0)
56dc7b09f390 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78391
diff changeset
458 return NULL;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
459 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
460 /*
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
461 * 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
462 * since it's known to work already.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
463 */
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
464 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
465 PyList_New, PyList_SetItem, lineno, col_offset);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
466 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
467 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
468 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
469
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
470
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
471 /* parser_compilest(PyObject* self, PyObject* args)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
472 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
473 * 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
474 * 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
475 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
476 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
477 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
478 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
479 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
480 PyObject* res = 0;
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
481 PyArena* arena;
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
482 mod_ty mod;
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
483 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
484 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
485
48558
fcbd9f4f095c Remove "ast" aliases from the parser module.
Georg Brandl <georg@python.org>
parents: 48413
diff changeset
486 static char *keywords[] = {"st", "filename", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
487
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
488 if (self == NULL || PyModule_Check(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
489 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
490 &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
491 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
492 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
493 &str);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
494
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
495 if (ok) {
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
496 arena = PyArena_New();
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
497 if (arena) {
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
498 mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
499 if (mod) {
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
500 res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
501 }
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
502 PyArena_Free(arena);
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
503 }
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
504 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
505
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
506 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
507 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
508
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
509
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
510 /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
511 * PyObject* parser_issuite(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
512 *
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
513 * 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
514 * 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
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 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
518 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
519 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
520 PyObject* res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
521 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
522
48558
fcbd9f4f095c Remove "ast" aliases from the parser module.
Georg Brandl <georg@python.org>
parents: 48413
diff changeset
523 static char *keywords[] = {"st", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
524
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
525 if (self == NULL || PyModule_Check(self))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
526 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
527 &PyST_Type, &self);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
528 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
529 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
530
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
531 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
532 /* 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
533 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
534 Py_INCREF(res);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
535 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
536 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
537 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
538
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 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
541 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
542 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
543 PyObject* res = 0;
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
544 int ok;
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
545
48558
fcbd9f4f095c Remove "ast" aliases from the parser module.
Georg Brandl <georg@python.org>
parents: 48413
diff changeset
546 static char *keywords[] = {"st", NULL};
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
547
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
548 if (self == NULL || PyModule_Check(self))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
549 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
550 &PyST_Type, &self);
8039
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
551 else
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
552 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
553
a38550613af3 The documentation threatened it would happen -- the functions that should
Fred Drake <fdrake@acm.org>
parents: 8034
diff changeset
554 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
555 /* 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
556 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
557 Py_INCREF(res);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
558 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
559 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
560 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
561
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
562
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
563 /* err_string(char* message)
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
564 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
565 * 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
566 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
567 */
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
568 static void
13503
6484098f40fc ANSI-fying
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13466
diff changeset
569 err_string(char *message)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
570 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
571 PyErr_SetString(parser_error, message);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
572 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
573
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
574
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
575 /* 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
576 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
577 * 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
578 * 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
579 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
580 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
581 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
582 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
583 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
584 char* string = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
585 PyObject* res = 0;
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
586 int flags = 0;
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
587 perrdetail err;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
588
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
589 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
590
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
591 if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
592 node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
593 &_PyParser_Grammar,
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
594 (type == PyST_EXPR)
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
595 ? eval_input : file_input,
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
596 &err, &flags);
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
597
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
598 if (n) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
599 res = parser_newstobject(n, type);
49624
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
600 if (res)
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
601 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
602 }
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
603 else
f3234e43e1fe Merged revisions 67066 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
604 PyParser_SetError(&err);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
605 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
606 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
607 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
608
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
609
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
610 /* PyObject* parser_expr(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
611 * PyObject* parser_suite(PyObject* self, PyObject* args)
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
612 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
613 * 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
614 * 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
615 * 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
616 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
617 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
618 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
619 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
620 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
621 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
622 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
623 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
624
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
625
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
626 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
627 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
628 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
629 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
630 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
631 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
632
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
633
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
634
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
635 /* 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
636 * 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
637 * 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
638 * 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
639 * 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
640 * 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
641 * phase, returning an exception instead.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
642 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
643 * 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
644 * 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
645 * 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
646 * 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
647 * 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
648 * 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
649 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
650
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
651
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
652 static node* build_node_tree(PyObject *tuple);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
653 static int validate_expr_tree(node *tree);
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
654 static int validate_file_input(node *tree);
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
655 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
656
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
657 /* 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
658 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
659 * 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
660 * 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
661 * 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
662 * 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
663 * 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
664 * 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
665 *
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
666 * 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
667 * ST objects.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
668 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
669 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
670 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
671 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
672 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
673 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
674 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
675 PyObject *tuple;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
676 node *tree;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
677
36680
54eb5b1fb363 unconst.
Martin v. Löwis <martin@v.loewis.de>
parents: 36673
diff changeset
678 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
679
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
680 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
681 &tuple))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
682 return (0);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
683 if (!PySequence_Check(tuple)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
684 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
685 "sequence2st() requires a single sequence argument");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
686 return (0);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
687 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
688 /*
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
689 * 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
690 */
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
691 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
692 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
693 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
694 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
695 /* 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
696 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
697 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
698 else
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
699 PyNode_Free(tree);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
700 }
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
701 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
702 /* 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
703 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
704 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
705 else
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
706 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
707 }
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
708 else if (start_sym == encoding_decl) {
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
709 /* This looks like an encoding_decl so far. */
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
710 if (validate_encoding_decl(tree))
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
711 st = parser_newstobject(tree, PyST_SUITE);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
712 else
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
713 PyNode_Free(tree);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
714 }
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
715 else {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
716 /* 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
717 PyNode_Free(tree);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
718 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
719 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
720 }
80928
fdf907708f49 Issue #16714: use 'raise' exceptions, don't 'throw'.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78604
diff changeset
721 /* Make sure we raise an exception on all errors. We should never
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
722 * 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
723 */
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
724 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
725 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
726
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
727 return st;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
728 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
729
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
730
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
731 /* node* build_node_children()
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
732 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
733 * 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
734 * 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
735 * 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
736 * and no memory will have been deallocated.
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
737 *
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
738 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
739 static node*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
740 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
741 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
742 Py_ssize_t len = PyObject_Size(tuple);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
743 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
744 int err;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
745
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
746 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
747 /* 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
748 PyObject* elem = PySequence_GetItem(tuple, i);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
749 int ok = elem != NULL;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
750 long type = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
751 char *strn = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
752
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
753 if (ok)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
754 ok = PySequence_Check(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
755 if (ok) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
756 PyObject *temp = PySequence_GetItem(elem, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
757 if (temp == NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
758 ok = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
759 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
760 ok = PyLong_Check(temp);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
761 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
762 type = PyLong_AS_LONG(temp);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
763 Py_DECREF(temp);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
764 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
765 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
766 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
767 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
768 "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
769 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
770 Py_XDECREF(err);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
771 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
772 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
773 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
774 if (ISTERMINAL(type)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
775 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
776 PyObject *temp;
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42535
diff changeset
777 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
778
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
779 if ((len != 2) && (len != 3)) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
780 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
781 return 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
782 }
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
783 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
784 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
785 return 0;
42856
f3b3ffd13954 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42535
diff changeset
786 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
787 PyErr_Format(parser_error,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
788 "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
789 " 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
790 Py_TYPE(temp)->tp_name);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12487
diff changeset
791 Py_DECREF(temp);
42535
8d6867cb8a8d Fix refleaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
792 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
793 return 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
794 }
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
795 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
796 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
797 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
798 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
799 *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
800 else {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
801 PyErr_Format(parser_error,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
802 "third item in terminal node must be an"
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
803 " integer, found %s",
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
804 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
805 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
806 Py_DECREF(temp);
42535
8d6867cb8a8d Fix refleaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
807 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
808 return 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
809 }
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
810 Py_DECREF(o);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
811 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
812 }
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48558
diff changeset
813 temp_str = _PyUnicode_AsStringAndSize(temp, &len);
66799
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
814 if (temp_str == NULL) {
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
815 Py_DECREF(temp);
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
816 Py_XDECREF(elem);
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
817 return 0;
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
818 }
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
819 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
820 if (strn != NULL)
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
821 (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
822 Py_DECREF(temp);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
823 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
824 else if (!ISNONTERMINAL(type)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
825 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
826 * It has to be one or the other; this is an error.
80928
fdf907708f49 Issue #16714: use 'raise' exceptions, don't 'throw'.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78604
diff changeset
827 * Raise an exception.
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
828 */
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
829 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
830 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
831 Py_XDECREF(err);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
832 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
833 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
834 }
36786
57322cb2ca11 Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36712
diff changeset
835 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
836 if (err == E_NOMEM) {
42535
8d6867cb8a8d Fix refleaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
837 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
838 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
839 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
840 }
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
841 if (err == E_OVERFLOW) {
42535
8d6867cb8a8d Fix refleaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
842 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
843 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
844 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
845 "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
846 return NULL;
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
847 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
848
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
849 if (ISNONTERMINAL(type)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
850 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
851
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
852 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
853 Py_XDECREF(elem);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
854 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
855 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
856 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
857 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
858 ++(*line_num); /* line number *after* the newline! */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
859 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
860 Py_XDECREF(elem);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
861 }
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
862 return root;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
863 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
864
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
865
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
866 static node*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
867 build_node_tree(PyObject *tuple)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
868 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
869 node* res = 0;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
870 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
871 long num = -1;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
872
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
873 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
874 num = PyLong_AsLong(temp);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
875 Py_XDECREF(temp);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
876 if (ISTERMINAL(num)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
877 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
878 * The tuple is simple, but it doesn't start with a start symbol.
80928
fdf907708f49 Issue #16714: use 'raise' exceptions, don't 'throw'.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 78604
diff changeset
879 * Raise an exception now and be done with it.
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
880 */
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
881 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
882 "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
883 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
884 Py_XDECREF(tuple);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
885 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
886 else if (ISNONTERMINAL(num)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
887 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
888 * Not efficient, but that can be handled later.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
889 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
890 int line_num = 0;
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
891 PyObject *encoding = NULL;
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
892
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
893 if (num == encoding_decl) {
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
894 encoding = PySequence_GetItem(tuple, 2);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
895 /* 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
896 tuple = PySequence_GetSlice(tuple, 0, 2);
66799
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
897 if (tuple == NULL)
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
898 return NULL;
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
899 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
900 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
901 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
902 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
903 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
904 res = NULL;
42d0819546d6 Fix memory leak in the parser module: There were two leaks in
Fred Drake <fdrake@acm.org>
parents: 20432
diff changeset
905 }
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
906 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
907 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
908 const char *temp;
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48558
diff changeset
909 temp = _PyUnicode_AsStringAndSize(encoding, &len);
66799
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
910 if (temp == NULL) {
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
911 Py_DECREF(res);
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
912 Py_DECREF(encoding);
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
913 Py_DECREF(tuple);
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
914 return NULL;
50d170bb71c7 Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66556
diff changeset
915 }
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 44441
diff changeset
916 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
917 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
918 (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
919 Py_DECREF(encoding);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
920 Py_DECREF(tuple);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
921 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
922 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
923 }
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
924 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
925 /* 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
926 * 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
927 * 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
928 */
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
929 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
930 "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
931 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
932 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
933 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
934
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
935 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
936 }
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
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
939 /*
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
940 * Validation routines used within the validation section:
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
941 */
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
942 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
943
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
944 #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
945 #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
946 #define validate_colon(ch) validate_terminal(ch, COLON, ":")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
947 #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
948 #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
949 #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
950 #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
951 #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
952 #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
953 #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
954 #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
955 #define validate_star(ch) validate_terminal(ch, STAR, "*")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
956 #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
957 #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
958 #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
959 #define validate_at(ch) validate_terminal(ch, AT, "@")
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
960 #define validate_rarrow(ch) validate_terminal(ch, RARROW, "->")
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
961 #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
962
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
963 #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
964
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
965 VALIDATER(node); VALIDATER(small_stmt);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
966 VALIDATER(class); VALIDATER(node);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
967 VALIDATER(parameters); VALIDATER(suite);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
968 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
969 VALIDATER(vfpdef);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
970 VALIDATER(stmt); VALIDATER(simple_stmt);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
971 VALIDATER(expr_stmt); VALIDATER(power);
40774
793e0323d4d6 Essential changes for print function changes.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
972 VALIDATER(del_stmt);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
973 VALIDATER(return_stmt); VALIDATER(raise_stmt);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
974 VALIDATER(import_stmt); VALIDATER(import_stmt);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
975 VALIDATER(import_name); VALIDATER(yield_stmt);
76647
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
976 VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
977 VALIDATER(assert_stmt);
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
978 VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
979 VALIDATER(while); VALIDATER(for);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
980 VALIDATER(try); VALIDATER(except_clause);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
981 VALIDATER(test); VALIDATER(and_test);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
982 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
983 VALIDATER(comp_op);
41699
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
984 VALIDATER(star_expr); VALIDATER(expr);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
985 VALIDATER(xor_expr); VALIDATER(and_expr);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
986 VALIDATER(shift_expr); VALIDATER(arith_expr);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
987 VALIDATER(term); VALIDATER(factor);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
988 VALIDATER(atom); VALIDATER(lambdef);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
989 VALIDATER(trailer); VALIDATER(subscript);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
990 VALIDATER(subscriptlist); VALIDATER(sliceop);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
991 VALIDATER(exprlist); VALIDATER(dictorsetmaker);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
992 VALIDATER(arglist); VALIDATER(argument);
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
993 VALIDATER(comp_for);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
994 VALIDATER(comp_iter); VALIDATER(comp_if);
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
995 VALIDATER(testlist_comp); VALIDATER(yield_expr);
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
996 VALIDATER(or_test);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
997 VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
998
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
999 #undef VALIDATER
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1000
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1001 #define is_even(n) (((n) & 1) == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1002 #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
1003
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1004
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1005 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1006 validate_ntype(node *n, int t)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1007 {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1008 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
1009 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
1010 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
1011 return 0;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1012 }
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1013 return 1;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1014 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1015
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1016
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
1017 /* 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
1018 * 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
1019 * 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
1020 * "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
1021 * 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
1022 * in that fashion.
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
1023 */
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1024 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1025 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
1026 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1027 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
1028 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
1029 "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
1030 return 0;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1031 }
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1032 return 1;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1033 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1034
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1035
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1036 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1037 validate_terminal(node *terminal, int type, char *string)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1038 {
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1039 int res = (validate_ntype(terminal, type)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1040 && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1041
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1042 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
1043 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
1044 "Illegal terminal: expected \"%s\"", string);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1045 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1046 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1047 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1048
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1049
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1050 /* X (',' X) [',']
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1051 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1052 static int
13667
a6c7bfdd6777 Even more ANSIfication: fix as many function pointers and declarations as
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1053 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
1054 const char *const name)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1055 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1056 int nch = NCH(tree);
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1057 int res = (nch && validate_ntype(tree, ntype)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1058 && vfunc(CHILD(tree, 0)));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1059
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1060 if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1061 (void) validate_numnodes(tree, 1, name);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1062 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1063 if (is_even(nch))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1064 res = validate_comma(CHILD(tree, --nch));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1065 if (res && nch > 1) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1066 int pos = 1;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1067 for ( ; res && pos < nch; pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1068 res = (validate_comma(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1069 && vfunc(CHILD(tree, pos + 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1070 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1071 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1072 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1073 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1074
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1075
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1076 /* validate_class()
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1077 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1078 * classdef:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1079 * 'class' NAME ['(' testlist ')'] ':' suite
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1080 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1081 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1082 validate_class(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1083 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1084 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
1085 int res = (validate_ntype(tree, classdef) &&
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1086 ((nch == 4) || (nch == 6) || (nch == 7)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1087
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1088 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1089 res = (validate_name(CHILD(tree, 0), "class")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1090 && validate_ntype(CHILD(tree, 1), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1091 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1092 && validate_suite(CHILD(tree, nch - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1093 }
34887
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
1094 else {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1095 (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
1096 }
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
1097
34887
7c2780c9e51b Flush out support for ``class B(): pass`` syntax by adding support to the
Brett Cannon <bcannon@gmail.com>
parents: 33353
diff changeset
1098 if (res) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1099 if (nch == 7) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1100 res = ((validate_lparen(CHILD(tree, 2)) &&
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1101 validate_arglist(CHILD(tree, 3)) &&
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1102 validate_rparen(CHILD(tree, 4))));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1103 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1104 else if (nch == 6) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1105 res = (validate_lparen(CHILD(tree,2)) &&
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1106 validate_rparen(CHILD(tree,3)));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1107 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1108 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1109 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1110 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1111
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1112
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1113 /* if_stmt:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1114 * '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
1115 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1116 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1117 validate_if(node *tree)
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1118 {
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1119 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1120 int res = (validate_ntype(tree, if_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1121 && (nch >= 4)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1122 && validate_name(CHILD(tree, 0), "if")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1123 && validate_test(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1124 && validate_colon(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1125 && validate_suite(CHILD(tree, 3)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1126
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1127 if (res && ((nch % 4) == 3)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1128 /* ... 'else' ':' suite */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1129 res = (validate_name(CHILD(tree, nch - 3), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1130 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1131 && validate_suite(CHILD(tree, nch - 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1132 nch -= 3;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1133 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1134 else if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1135 (void) validate_numnodes(tree, 4, "if");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1136 if ((nch % 4) != 0)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1137 /* Will catch the case for nch < 4 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1138 res = validate_numnodes(tree, 0, "if");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1139 else if (res && (nch > 4)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1140 /* ... ('elif' test ':' suite)+ ... */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1141 int j = 4;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1142 while ((j < nch) && res) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1143 res = (validate_name(CHILD(tree, j), "elif")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1144 && validate_colon(CHILD(tree, j + 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1145 && validate_test(CHILD(tree, j + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1146 && validate_suite(CHILD(tree, j + 3)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1147 j += 4;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1148 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1149 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1150 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1151 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1152
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1153
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1154 /* parameters:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1155 * '(' [varargslist] ')'
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1156 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1157 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1158 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1159 validate_parameters(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1160 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1161 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1162 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
1163
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1164 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1165 res = (validate_lparen(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1166 && validate_rparen(CHILD(tree, nch - 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1167 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1168 res = validate_varargslist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1169 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1170 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1171 (void) validate_numnodes(tree, 2, "parameters");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1172 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1173 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1174 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1175
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1176
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1177 /* validate_suite()
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1178 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1179 * suite:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1180 * simple_stmt
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1181 * | NEWLINE INDENT stmt+ DEDENT
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1182 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1183 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1184 validate_suite(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1185 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1186 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1187 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
1188
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1189 if (res && (nch == 1))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1190 res = validate_simple_stmt(CHILD(tree, 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1191 else if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1192 /* NEWLINE INDENT stmt+ DEDENT */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1193 res = (validate_newline(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1194 && validate_indent(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1195 && validate_stmt(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1196 && validate_dedent(CHILD(tree, nch - 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1197
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1198 if (res && (nch > 4)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1199 int i = 3;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1200 --nch; /* forget the DEDENT */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1201 for ( ; res && (i < nch); ++i)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1202 res = validate_stmt(CHILD(tree, i));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1203 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1204 else if (nch < 4)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1205 res = validate_numnodes(tree, 4, "suite");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1206 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1207 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1208 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1209
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1210
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1211 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1212 validate_testlist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1213 {
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1214 return (validate_repeating_list(tree, testlist,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1215 validate_test, "testlist"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1216 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1217
20432
56141b86a7f2 Very subtle syntax change: in a list comprehension, the testlist in
Guido van Rossum <guido@python.org>
parents: 19274
diff changeset
1218 static int
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1219 validate_testlist_star_expr(node *tl)
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1220 {
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1221 return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1222 "testlist"));
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1223 }
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1224
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 21313
diff changeset
1225
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1226 /* validate either vfpdef or tfpdef.
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1227 * vfpdef: NAME
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1228 * tfpdef: NAME [':' test]
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1229 */
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1230 static int
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1231 validate_vfpdef(node *tree)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1232 {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1233 int nch = NCH(tree);
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1234 if (TYPE(tree) == vfpdef) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1235 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
1236 }
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1237 else if (TYPE(tree) == tfpdef) {
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1238 if (nch == 1) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1239 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
1240 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1241 else if (nch == 3) {
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1242 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
1243 validate_colon(CHILD(tree, 1)) &&
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1244 validate_test(CHILD(tree, 2));
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1245 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1246 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1247 return 0;
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1248 }
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1249
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1250 /* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1251 * ..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
1252 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1253 static int
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1254 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
1255 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1256 int nch = NCH(tree);
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1257 int res = 0;
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1258
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1259 if (nch <= start) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1260 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
1261 return 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1262 }
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1263 if (TYPE(CHILD(tree, start)) == STAR) {
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1264 /*
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1265 * '*' [vfpdef]
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1266 */
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1267 res = validate_star(CHILD(tree, start++));
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1268 if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1269 TYPE(CHILD(tree, start)) == tfpdef))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1270 res = validate_vfpdef(CHILD(tree, start++));
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1271 /*
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1272 * (',' vfpdef ['=' test])*
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1273 */
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1274 while (res && start + 1 < nch && (
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1275 TYPE(CHILD(tree, start + 1)) == vfpdef ||
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1276 TYPE(CHILD(tree, start + 1)) == tfpdef)) {
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1277 res = (validate_comma(CHILD(tree, start++))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1278 && validate_vfpdef(CHILD(tree, start++)));
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1279 if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1280 res = (validate_equal(CHILD(tree, start++))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1281 && validate_test(CHILD(tree, start++)));
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1282 }
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1283 /*
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1284 * [',' '**' vfpdef]
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1285 */
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1286 if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1287 res = (validate_comma(CHILD(tree, start++))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1288 && validate_doublestar(CHILD(tree, start++))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1289 && validate_vfpdef(CHILD(tree, start++)));
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1290 }
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1291 else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1292 /*
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1293 * '**' vfpdef
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1294 */
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1295 if (start + 1 < nch)
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1296 res = (validate_doublestar(CHILD(tree, start++))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1297 && validate_vfpdef(CHILD(tree, start++)));
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1298 else {
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1299 res = 0;
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1300 err_string("expected vfpdef after ** in varargslist trailer");
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39783
diff changeset
1301 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1302 }
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1303 else {
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1304 res = 0;
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1305 err_string("expected * or ** in varargslist trailer");
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1306 }
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1307
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1308 if (res && start != nch) {
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1309 res = 0;
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1310 err_string("unexpected extra children in varargslist trailer");
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
1311 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1312 return res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1313 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1314
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1315
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1316 /* validate_varargslist()
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1317 *
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1318 * Validate typedargslist or varargslist.
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1319 *
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1320 * typedargslist: ((tfpdef ['=' test] ',')*
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1321 * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1322 * '**' tfpdef)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1323 * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1324 * tfpdef: NAME [':' test]
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1325 * varargslist: ((vfpdef ['=' test] ',')*
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1326 * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1327 * '**' vfpdef)
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1328 * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1329 * vfpdef: NAME
3774
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_varargslist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1334 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1335 int nch = NCH(tree);
40529
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1336 int res = (TYPE(tree) == varargslist ||
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1337 TYPE(tree) == typedargslist) &&
54cd47788811 PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents: 40450
diff changeset
1338 (nch != 0);
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1339 int sym;
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1340 node *ch;
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1341 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
1342
15506
ac830c9e9a83 validate_varargslist(): Fix two bugs in this function, one that affected
Fred Drake <fdrake@acm.org>
parents: 15318
diff changeset
1343 if (!res)
ac830c9e9a83 validate_varargslist(): Fix two bugs in this function, one that affected
Fred Drake <fdrake@acm.org>
parents: 15318
diff changeset
1344 return 0;
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1345 if (nch < 1) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1346 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
1347 return 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1348 }
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1349 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
1350 ch = CHILD(tree, i);
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1351 sym = TYPE(ch);
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1352 if (sym == vfpdef || sym == tfpdef) {
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1353 /* validate (vfpdef ['=' test] ',')+ */
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1354 res = validate_vfpdef(ch);
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1355 ++i;
15506
ac830c9e9a83 validate_varargslist(): Fix two bugs in this function, one that affected
Fred Drake <fdrake@acm.org>
parents: 15318
diff changeset
1356 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
1357 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
1358 && 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
1359 if (res)
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1360 i += 2;
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1361 }
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1362 if (res && i < nch) {
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1363 res = validate_comma(CHILD(tree, i));
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1364 ++i;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1365 }
41717
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1366 } else if (sym == DOUBLESTAR || sym == STAR) {
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1367 res = validate_varargslist_trailer(tree, i);
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1368 break;
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1369 } else {
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1370 res = 0;
9ad84070b22d Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41699
diff changeset
1371 err_string("illegal formation for varargslist");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1372 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1373 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1374 return res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1375 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1376
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1377
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1378 /* 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
1379 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1380 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1381 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
1382 {
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1383 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
1384 && validate_numnodes(tree, 1, "comp_iter"));
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1385 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
1386 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
1387 else
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1388 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
1389
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1390 return res;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1391 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1392
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1393 /* 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
1394 */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1395 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1396 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
1397 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1398 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1399 int res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1400
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1401 if (nch == 5)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1402 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
1403 else
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1404 res = validate_numnodes(tree, 4, "comp_for");
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1405
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1406 if (res)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1407 res = (validate_name(CHILD(tree, 0), "for")
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1408 && validate_exprlist(CHILD(tree, 1))
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1409 && validate_name(CHILD(tree, 2), "in")
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
1410 && validate_or_test(CHILD(tree, 3)));
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1411
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1412 return res;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1413 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
1414
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1415 /* 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
1416 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1417 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1418 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
1419 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1420 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1421 int res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1422
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1423 if (nch == 3)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1424 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
1425 else
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
1426 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
1427
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1428 if (res)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1429 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
1430 && 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
1431
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1432 return res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1433 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1434
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1435
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1436 /* simple_stmt | compound_stmt
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 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1439 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1440 validate_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1441 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1442 int res = (validate_ntype(tree, stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1443 && validate_numnodes(tree, 1, "stmt"));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1444
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1445 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1446 tree = CHILD(tree, 0);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1447
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1448 if (TYPE(tree) == simple_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1449 res = validate_simple_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1450 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1451 res = validate_compound_stmt(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1452 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1453 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1454 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1455
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1456
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1457 /* small_stmt (';' small_stmt)* [';'] NEWLINE
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1458 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1459 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1460 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1461 validate_simple_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1462 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1463 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1464 int res = (validate_ntype(tree, simple_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1465 && (nch >= 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1466 && validate_small_stmt(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1467 && validate_newline(CHILD(tree, nch - 1)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1468
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1469 if (nch < 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1470 res = validate_numnodes(tree, 2, "simple_stmt");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1471 --nch; /* forget the NEWLINE */
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1472 if (res && is_even(nch))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1473 res = validate_semi(CHILD(tree, --nch));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1474 if (res && (nch > 2)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1475 int i;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1476
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1477 for (i = 1; res && (i < nch); i += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1478 res = (validate_semi(CHILD(tree, i))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1479 && 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
1480 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1481 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1482 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1483
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1484
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1485 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1486 validate_small_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1487 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1488 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
1489 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
1490
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1491 if (res) {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1492 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
1493
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1494 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
1495 || (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
1496 || (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
1497 || (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
1498 || (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
1499 || (ntype == global_stmt)
76647
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1500 || (ntype == nonlocal_stmt)
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39701
diff changeset
1501 || (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
1502 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
1503 else {
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1504 res = 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1505 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
1506 }
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1507 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1508 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
1509 res = 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1510 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
1511 "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
1512 TYPE(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1513 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1514 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1515 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1516
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1517
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1518 /* compound_stmt:
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
1519 * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1520 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1521 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1522 validate_compound_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1523 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1524 int res = (validate_ntype(tree, compound_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1525 && 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
1526 int ntype;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1527
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1528 if (!res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1529 return (0);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1530
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1531 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
1532 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
1533 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
1534 || (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
1535 || (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
1536 || (ntype == try_stmt)
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
1537 || (ntype == with_stmt)
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1538 || (ntype == funcdef)
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
1539 || (ntype == classdef)
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
1540 || (ntype == decorated))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1541 res = validate_node(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1542 else {
14533
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1543 res = 0;
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1544 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
1545 "Illegal compound statement type: %d.", TYPE(tree));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1546 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1547 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1548 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1549
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1550 static int
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1551 validate_yield_or_testlist(node *tree, int tse)
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1552 {
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1553 if (TYPE(tree) == yield_expr) {
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1554 return validate_yield_expr(tree);
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1555 }
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1556 else {
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1557 if (tse)
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1558 return validate_testlist_star_expr(tree);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1559 else
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1560 return validate_testlist(tree);
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1561 }
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1562 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1563
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1564 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1565 validate_expr_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1566 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1567 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1568 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1569 int res = (validate_ntype(tree, expr_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1570 && is_odd(nch)
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1571 && validate_testlist_star_expr(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1572
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1573 if (res && nch == 3
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1574 && 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
1575 res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1576 && validate_yield_or_testlist(CHILD(tree, 2), 0);
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1577
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1578 if (res) {
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1579 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
1580
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1581 res = (strcmp(s, "+=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1582 || strcmp(s, "-=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1583 || strcmp(s, "*=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1584 || strcmp(s, "/=") == 0
27517
51abb5e86226 Teach the parsermodule about floor division. Fixes
Michael W. Hudson <mwh@python.net>
parents: 26979
diff changeset
1585 || strcmp(s, "//=") == 0
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1586 || strcmp(s, "%=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1587 || strcmp(s, "&=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1588 || strcmp(s, "|=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1589 || strcmp(s, "^=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1590 || strcmp(s, "<<=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1591 || strcmp(s, ">>=") == 0
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1592 || strcmp(s, "**=") == 0);
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1593 if (!res)
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62791
diff changeset
1594 err_string("illegal augmented assignment operator");
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1595 }
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1596 }
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1597 else {
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1598 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
1599 res = validate_equal(CHILD(tree, j))
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
1600 && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
1601 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1602 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1603 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1604
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1605
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1606 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1607 validate_del_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1608 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1609 return (validate_numnodes(tree, 2, "del_stmt")
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1610 && validate_name(CHILD(tree, 0), "del")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1611 && validate_exprlist(CHILD(tree, 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1612 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1613
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1614
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1615 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1616 validate_return_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1617 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1618 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1619 int res = (validate_ntype(tree, return_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1620 && ((nch == 1) || (nch == 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1621 && validate_name(CHILD(tree, 0), "return"));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1622
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1623 if (res && (nch == 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1624 res = validate_testlist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1625
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1626 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1627 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1628
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1629
76818
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1630 /*
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1631 * raise_stmt:
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1632 *
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1633 * 'raise' [test ['from' test]]
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1634 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1635 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1636 validate_raise_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1637 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1638 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1639 int res = (validate_ntype(tree, raise_stmt)
76818
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1640 && ((nch == 1) || (nch == 2) || (nch == 4)));
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1641
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1642 if (!res && !PyErr_Occurred())
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1643 (void) validate_numnodes(tree, 2, "raise");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1644
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1645 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1646 res = validate_name(CHILD(tree, 0), "raise");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1647 if (res && (nch >= 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1648 res = validate_test(CHILD(tree, 1));
76818
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1649 if (res && (nch == 4)) {
fc17f70292f6 Issue #14701: Add missing support for 'raise ... from' in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76647
diff changeset
1650 res = (validate_name(CHILD(tree, 2), "from")
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1651 && validate_test(CHILD(tree, 3)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1652 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1653 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1654 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1655 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1656
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1657
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1658 /* 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
1659 */
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1660 static int
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1661 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
1662 {
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1663 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
1664 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
1665 && ((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
1666 && 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
1667
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1668 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
1669 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
1670
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1671 return (res);
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1672 }
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1673
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1674
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
1675 /* yield_stmt: yield_expr
18646
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1676 */
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1677 static int
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1678 validate_yield_stmt(node *tree)
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1679 {
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1680 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
1681 && 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
1682 && validate_yield_expr(CHILD(tree, 0)));
18646
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1683 }
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1684
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
1685
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1686 static int
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1687 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
1688 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1689 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1690 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
1691
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1692 if (ok) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1693 if (nch == 1)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1694 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
1695 else if (nch == 3)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1696 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
1697 && 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
1698 && 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
1699 else
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1700 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
1701 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1702 return ok;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1703 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1704
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1705
15751
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1706 /* dotted_name: NAME ("." NAME)*
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1707 */
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1708 static int
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1709 validate_dotted_name(node *tree)
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1710 {
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1711 int nch = NCH(tree);
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1712 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
1713 && is_odd(nch)
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1714 && 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
1715 int i;
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1716
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1717 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
1718 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
1719 && 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
1720 }
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1721 return res;
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1722 }
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1723
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1724
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1725 /* 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
1726 */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1727 static int
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1728 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
1729 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1730 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1731 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
1732
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1733 if (res) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1734 if (nch == 1)
15751
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1735 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
1736 else if (nch == 3)
15751
939b8fffc452 Fix problems with validation of import statement parse trees.
Fred Drake <fdrake@acm.org>
parents: 15506
diff changeset
1737 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
1738 && 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
1739 && 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
1740 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1741 res = 0;
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
1742 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
1743 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1744 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1745 return res;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1746 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1747
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
1748
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1749 /* dotted_as_name (',' dotted_as_name)* */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1750 static int
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1751 validate_dotted_as_names(node *tree)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1752 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1753 int nch = NCH(tree);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1754 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1755 int i;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1756
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1757 for (i = 1; res && (i < nch); i += 2)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1758 res = (validate_comma(CHILD(tree, i))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1759 && validate_dotted_as_name(CHILD(tree, i + 1)));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1760 return (res);
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1761 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1762
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1763
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1764 /* import_as_name (',' import_as_name)* [','] */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1765 static int
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1766 validate_import_as_names(node *tree)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1767 {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1768 int nch = NCH(tree);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1769 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
1770 int i;
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1771
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1772 for (i = 1; res && (i + 1 < nch); i += 2)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1773 res = (validate_comma(CHILD(tree, i))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1774 && validate_import_as_name(CHILD(tree, i + 1)));
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1775 return (res);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1776 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1777
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1778
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1779 /* 'import' dotted_as_names */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1780 static int
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1781 validate_import_name(node *tree)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1782 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1783 return (validate_ntype(tree, import_name)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1784 && validate_numnodes(tree, 2, "import_name")
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1785 && validate_name(CHILD(tree, 0), "import")
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1786 && validate_dotted_as_names(CHILD(tree, 1)));
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1787 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1788
62791
3aa22428582a Merged revisions 82555 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62789
diff changeset
1789 /* Helper function to count the number of leading dots (or ellipsis tokens) in
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1790 * 'from ...module import name'
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1791 */
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1792 static int
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1793 count_from_dots(node *tree)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1794 {
62791
3aa22428582a Merged revisions 82555 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62789
diff changeset
1795 int i;
3aa22428582a Merged revisions 82555 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62789
diff changeset
1796 for (i = 1; i < NCH(tree); i++)
3aa22428582a Merged revisions 82555 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62789
diff changeset
1797 if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
3aa22428582a Merged revisions 82555 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62789
diff changeset
1798 break;
3aa22428582a Merged revisions 82555 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62789
diff changeset
1799 return i - 1;
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1800 }
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36680
diff changeset
1801
62789
692b7989cd92 Merged revisions 82552-82553 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62774
diff changeset
1802 /* import_from: ('from' ('.'* dotted_name | '.'+)
692b7989cd92 Merged revisions 82552-82553 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62774
diff changeset
1803 * 'import' ('*' | '(' import_as_names ')' | import_as_names))
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1804 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1805 static int
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1806 validate_import_from(node *tree)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1807 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1808 int nch = NCH(tree);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1809 int ndots = count_from_dots(tree);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1810 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1811 int offset = ndots + havename;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1812 int res = validate_ntype(tree, import_from)
62789
692b7989cd92 Merged revisions 82552-82553 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62774
diff changeset
1813 && (offset >= 1)
692b7989cd92 Merged revisions 82552-82553 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62774
diff changeset
1814 && (nch >= 3 + offset)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1815 && validate_name(CHILD(tree, 0), "from")
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1816 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1817 && validate_name(CHILD(tree, offset + 1), "import");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1818
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1819 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1820 res = ((nch == offset + 5)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1821 && validate_lparen(CHILD(tree, offset + 2))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1822 && validate_import_as_names(CHILD(tree, offset + 3))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1823 && validate_rparen(CHILD(tree, offset + 4)));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1824 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1825 res = validate_import_as_names(CHILD(tree, offset + 2));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1826 return (res);
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1827 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1828
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1829
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1830 /* import_stmt: import_name | import_from */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1831 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1832 validate_import_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1833 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1834 int nch = NCH(tree);
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1835 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
1836
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1837 if (res) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1838 int ntype = TYPE(CHILD(tree, 0));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1839
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
1840 if (ntype == import_name || ntype == import_from)
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1841 res = validate_node(CHILD(tree, 0));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1842 else {
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1843 res = 0;
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1844 err_string("illegal import_stmt child type");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1845 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1846 }
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1847 else if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1848 res = 0;
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1849 PyErr_Format(parser_error,
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1850 "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
1851 TYPE(CHILD(tree, 0)));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33166
diff changeset
1852 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1853 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1854 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1855
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1856
76647
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1857 /* global_stmt:
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1858 *
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1859 * 'global' NAME (',' NAME)*
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1860 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1861 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1862 validate_global_stmt(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1863 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1864 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1865 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1866 int res = (validate_ntype(tree, global_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1867 && is_even(nch) && (nch >= 2));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1868
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
1869 if (!res && !PyErr_Occurred())
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
1870 err_string("illegal global statement");
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
1871
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1872 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1873 res = (validate_name(CHILD(tree, 0), "global")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1874 && validate_ntype(CHILD(tree, 1), NAME));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1875 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
1876 res = (validate_comma(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1877 && validate_ntype(CHILD(tree, j + 1), NAME));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1878
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1879 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1880 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1881
76647
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1882 /* nonlocal_stmt:
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1883 *
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1884 * 'nonlocal' NAME (',' NAME)*
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1885 */
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1886 static int
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1887 validate_nonlocal_stmt(node *tree)
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1888 {
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1889 int j;
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1890 int nch = NCH(tree);
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1891 int res = (validate_ntype(tree, nonlocal_stmt)
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1892 && is_even(nch) && (nch >= 2));
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1893
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1894 if (!res && !PyErr_Occurred())
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1895 err_string("illegal nonlocal statement");
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1896
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1897 if (res)
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1898 res = (validate_name(CHILD(tree, 0), "nonlocal")
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1899 && validate_ntype(CHILD(tree, 1), NAME));
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1900 for (j = 2; res && (j < nch); j += 2)
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1901 res = (validate_comma(CHILD(tree, j))
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1902 && validate_ntype(CHILD(tree, j + 1), NAME));
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1903
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1904 return res;
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
1905 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1906
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1907 /* assert_stmt:
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1908 *
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1909 * 'assert' test [',' test]
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1910 */
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1911 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1912 validate_assert_stmt(node *tree)
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1913 {
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1914 int nch = NCH(tree);
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1915 int res = (validate_ntype(tree, assert_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1916 && ((nch == 2) || (nch == 4))
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
1917 && (validate_name(CHILD(tree, 0), "assert"))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1918 && validate_test(CHILD(tree, 1)));
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1919
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1920 if (!res && !PyErr_Occurred())
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
1921 err_string("illegal assert statement");
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1922 if (res && (nch > 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1923 res = (validate_comma(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1924 && validate_test(CHILD(tree, 3)));
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1925
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1926 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1927 }
5236
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1928
b4e671bc1331 Support assert stmt.
Guido van Rossum <guido@python.org>
parents: 5039
diff changeset
1929
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1930 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1931 validate_while(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1932 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1933 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1934 int res = (validate_ntype(tree, while_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1935 && ((nch == 4) || (nch == 7))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1936 && validate_name(CHILD(tree, 0), "while")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1937 && validate_test(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1938 && validate_colon(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1939 && validate_suite(CHILD(tree, 3)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1940
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1941 if (res && (nch == 7))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1942 res = (validate_name(CHILD(tree, 4), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1943 && validate_colon(CHILD(tree, 5))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1944 && validate_suite(CHILD(tree, 6)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1945
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1946 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1947 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1948
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1949
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1950 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1951 validate_for(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1952 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1953 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1954 int res = (validate_ntype(tree, for_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1955 && ((nch == 6) || (nch == 9))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1956 && validate_name(CHILD(tree, 0), "for")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1957 && validate_exprlist(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1958 && validate_name(CHILD(tree, 2), "in")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1959 && validate_testlist(CHILD(tree, 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1960 && validate_colon(CHILD(tree, 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1961 && validate_suite(CHILD(tree, 5)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1962
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1963 if (res && (nch == 9))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1964 res = (validate_name(CHILD(tree, 6), "else")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1965 && validate_colon(CHILD(tree, 7))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1966 && validate_suite(CHILD(tree, 8)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1967
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1968 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1969 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1970
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1971
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1972 /* try_stmt:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1973 * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
50079
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
1974 ['finally' ':' suite]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1975 * | 'try' ':' suite 'finally' ':' suite
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1976 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1977 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
1978 static int
13503
6484098f40fc ANSI-fying
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13466
diff changeset
1979 validate_try(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1980 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
1981 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1982 int pos = 3;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1983 int res = (validate_ntype(tree, try_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1984 && (nch >= 6) && ((nch % 3) == 0));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1985
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
1986 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1987 res = (validate_name(CHILD(tree, 0), "try")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1988 && validate_colon(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1989 && validate_suite(CHILD(tree, 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1990 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1991 && 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
1992 else if (!PyErr_Occurred()) {
13325
7bac10a9bb61 Andrew Kuchling <akuchlin@mems-exchange.org>:
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
1993 const char* name = "except";
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1994 if (TYPE(CHILD(tree, nch - 3)) != except_clause)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
1995 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
1996
635f69123349 Simplify some of the code. Use PyErr_Format() instead of sprintf(), etc.
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
1997 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
1998 "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
1999 }
50079
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2000 /* Handle try/finally statement */
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2001 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2002 (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2003 res = (validate_numnodes(tree, 6, "try/finally")
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2004 && validate_colon(CHILD(tree, 4))
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2005 && validate_suite(CHILD(tree, 5)));
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2006 return (res);
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2007 }
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2008 /* try/except statement: skip past except_clause sections */
54082
17d1946540d4 Merged revisions 72645 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
2009 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2010 res = (validate_except_clause(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2011 && validate_colon(CHILD(tree, pos + 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2012 && validate_suite(CHILD(tree, pos + 2)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2013 pos += 3;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2014 }
50079
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2015 /* skip else clause */
54082
17d1946540d4 Merged revisions 72645 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
2016 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
50079
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2017 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2018 res = (validate_colon(CHILD(tree, pos + 1))
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2019 && validate_suite(CHILD(tree, pos + 2)));
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2020 pos += 3;
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2021 }
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2022 if (res && pos < nch) {
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2023 /* last clause must be a finally */
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2024 res = (validate_name(CHILD(tree, pos), "finally")
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2025 && validate_numnodes(tree, pos + 3, "try/except/finally")
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2026 && validate_colon(CHILD(tree, pos + 1))
a86cdf49b821 Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents: 49908
diff changeset
2027 && validate_suite(CHILD(tree, pos + 2)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2028 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2029 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2030 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2031
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2032
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2033 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2034 validate_except_clause(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2035 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2036 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2037 int res = (validate_ntype(tree, except_clause)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2038 && ((nch == 1) || (nch == 2) || (nch == 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2039 && validate_name(CHILD(tree, 0), "except"));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2040
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2041 if (res && (nch > 1))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2042 res = validate_test(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2043 if (res && (nch == 4))
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40529
diff changeset
2044 res = (validate_name(CHILD(tree, 2), "as")
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40529
diff changeset
2045 && validate_ntype(CHILD(tree, 3), NAME));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2046
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2047 return (res);
12416
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
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2050
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2051 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2052 validate_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2053 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2054 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2055 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
2056
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2057 if (res && (TYPE(CHILD(tree, 0)) == lambdef))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2058 res = ((nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2059 && validate_lambdef(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2060 else if (res) {
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2061 res = validate_or_test(CHILD(tree, 0));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2062 res = (res && (nch == 1 || (nch == 5 &&
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2063 validate_name(CHILD(tree, 1), "if") &&
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2064 validate_or_test(CHILD(tree, 2)) &&
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2065 validate_name(CHILD(tree, 3), "else") &&
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2066 validate_test(CHILD(tree, 4)))));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2067 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2068 return (res);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2069 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2070
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2071 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2072 validate_test_nocond(node *tree)
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2073 {
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2074 int nch = NCH(tree);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2075 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
2076
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2077 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
2078 res = (validate_lambdef_nocond(CHILD(tree, 0)));
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2079 else if (res) {
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2080 res = (validate_or_test(CHILD(tree, 0)));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2081 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2082 return (res);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2083 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2084
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2085 static int
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2086 validate_or_test(node *tree)
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2087 {
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2088 int nch = NCH(tree);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2089 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
2090
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2091 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2092 int pos;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2093 res = validate_and_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2094 for (pos = 1; res && (pos < nch); pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2095 res = (validate_name(CHILD(tree, pos), "or")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2096 && 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
2097 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2098 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2099 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2100
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2101
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2102 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2103 validate_and_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2104 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2105 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2106 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2107 int res = (validate_ntype(tree, and_test)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2108 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2109 && validate_not_test(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2110
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2111 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
2112 res = (validate_name(CHILD(tree, pos), "and")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2113 && validate_not_test(CHILD(tree, 0)));
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 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2116 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2117
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2118
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2119 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2120 validate_not_test(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2121 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2122 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2123 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
2124
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2125 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2126 if (nch == 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2127 res = (validate_name(CHILD(tree, 0), "not")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2128 && validate_not_test(CHILD(tree, 1)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2129 else if (nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2130 res = validate_comparison(CHILD(tree, 0));
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 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2133 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2134
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2135
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2136 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2137 validate_comparison(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2138 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2139 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2140 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2141 int res = (validate_ntype(tree, comparison)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2142 && is_odd(nch)
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2143 && validate_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2144
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2145 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
2146 res = (validate_comp_op(CHILD(tree, pos))
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2147 && validate_expr(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2148
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2149 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2150 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2151
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2152
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2153 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2154 validate_comp_op(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2155 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2156 int res = 0;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2157 int nch = NCH(tree);
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2158
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2159 if (!validate_ntype(tree, comp_op))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2160 return (0);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2161 if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2162 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2163 * 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
2164 * 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
2165 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2166 tree = CHILD(tree, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2167 switch (TYPE(tree)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2168 case LESS:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2169 case GREATER:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2170 case EQEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2171 case EQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2172 case LESSEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2173 case GREATEREQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2174 case NOTEQUAL:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2175 res = 1;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2176 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2177 case NAME:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2178 res = ((strcmp(STR(tree), "in") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2179 || (strcmp(STR(tree), "is") == 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2180 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
2181 PyErr_Format(parser_error,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2182 "illegal operator '%s'", STR(tree));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2183 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2184 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2185 default:
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2186 err_string("illegal comparison operator type");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2187 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2188 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2189 }
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
2190 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
2191 res = (validate_ntype(CHILD(tree, 0), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2192 && validate_ntype(CHILD(tree, 1), NAME)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2193 && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2194 && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2195 || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2196 && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2197 if (!res && !PyErr_Occurred())
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2198 err_string("unknown comparison operator");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2199 }
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
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2204 static int
41699
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2205 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
2206 {
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2207 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
2208 if (!res) return res;
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2209 if (!validate_numnodes(tree, 2, "star_expr"))
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2210 return 0;
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2211 return validate_ntype(CHILD(tree, 0), STAR) && \
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2212 validate_expr(CHILD(tree, 1));
41699
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2213 }
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2214
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2215
e16c8e5c5ca4 Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41464
diff changeset
2216 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2217 validate_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2218 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2219 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2220 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2221 int res = (validate_ntype(tree, expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2222 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2223 && validate_xor_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2224
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2225 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
2226 res = (validate_xor_expr(CHILD(tree, j))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2227 && validate_vbar(CHILD(tree, j - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2228
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2229 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2230 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2231
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2232
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2233 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2234 validate_xor_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2235 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2236 int j;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2237 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2238 int res = (validate_ntype(tree, xor_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2239 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2240 && validate_and_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2241
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2242 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
2243 res = (validate_circumflex(CHILD(tree, j - 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2244 && validate_and_expr(CHILD(tree, j)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2245
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2246 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2247 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2248
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2249
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2250 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2251 validate_and_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2252 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2253 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2254 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2255 int res = (validate_ntype(tree, and_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2256 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2257 && validate_shift_expr(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2258
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2259 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
2260 res = (validate_ampersand(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2261 && validate_shift_expr(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2262
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2263 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2264 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2265
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2266
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2267 static int
13503
6484098f40fc ANSI-fying
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13466
diff changeset
2268 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
2269 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2270 int pos = 1;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2271 int nch = NCH(tree);
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2272 int res = (is_odd(nch)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2273 && (*termvalid)(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2274
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2275 for ( ; res && (pos < nch); pos += 2) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2276 if (TYPE(CHILD(tree, pos)) != op1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2277 res = validate_ntype(CHILD(tree, pos), op2);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2278 if (res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2279 res = (*termvalid)(CHILD(tree, pos + 1));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2280 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2281 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2282 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2283
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2284
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2285 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2286 validate_shift_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2287 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2288 return (validate_ntype(tree, shift_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2289 && validate_chain_two_ops(tree, validate_arith_expr,
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2290 LEFTSHIFT, RIGHTSHIFT));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2291 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2292
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2293
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2294 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2295 validate_arith_expr(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2296 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2297 return (validate_ntype(tree, arith_expr)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2298 && 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
2299 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2300
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2301
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2302 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2303 validate_term(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2304 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2305 int pos = 1;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2306 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2307 int res = (validate_ntype(tree, term)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2308 && is_odd(nch)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2309 && validate_factor(CHILD(tree, 0)));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2310
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2311 for ( ; res && (pos < nch); pos += 2)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2312 res = (((TYPE(CHILD(tree, pos)) == STAR)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2313 || (TYPE(CHILD(tree, pos)) == SLASH)
27517
51abb5e86226 Teach the parsermodule about floor division. Fixes
Michael W. Hudson <mwh@python.net>
parents: 26979
diff changeset
2314 || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2315 || (TYPE(CHILD(tree, pos)) == PERCENT))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2316 && validate_factor(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2317
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2318 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2319 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2320
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2321
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2322 /* factor:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2323 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2324 * factor: ('+'|'-'|'~') factor | power
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2325 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2326 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2327 validate_factor(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2328 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2329 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2330 int res = (validate_ntype(tree, factor)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2331 && (((nch == 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2332 && ((TYPE(CHILD(tree, 0)) == PLUS)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2333 || (TYPE(CHILD(tree, 0)) == MINUS)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2334 || (TYPE(CHILD(tree, 0)) == TILDE))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2335 && validate_factor(CHILD(tree, 1)))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2336 || ((nch == 1)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2337 && validate_power(CHILD(tree, 0)))));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2338 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2339 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2340
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2341
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2342 /* power:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2343 *
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2344 * power: atom trailer* ('**' factor)*
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2345 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2346 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2347 validate_power(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2348 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2349 int pos = 1;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2350 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2351 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
2352 && validate_atom(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2353
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2354 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
2355 res = validate_trailer(CHILD(tree, pos++));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2356 if (res && (pos < nch)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2357 if (!is_even(nch - pos)) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
2358 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
2359 return (0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2360 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2361 for ( ; res && (pos < (nch - 1)); pos += 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2362 res = (validate_doublestar(CHILD(tree, pos))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2363 && validate_factor(CHILD(tree, pos + 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2364 }
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2365 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2366 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2367
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2368
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2369 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2370 validate_atom(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2371 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2372 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2373 int nch = NCH(tree);
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2374 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
2375
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2376 if (res && nch < 1)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2377 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
2378 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2379 switch (TYPE(CHILD(tree, 0))) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2380 case LPAR:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2381 res = ((nch <= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2382 && (validate_rparen(CHILD(tree, nch - 1))));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2383
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
2384 if (res && (nch == 3)) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2385 if (TYPE(CHILD(tree, 1))==yield_expr)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2386 res = validate_yield_expr(CHILD(tree, 1));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2387 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2388 res = validate_testlist_comp(CHILD(tree, 1));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2389 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2390 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2391 case LSQB:
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2392 if (nch == 2)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2393 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
2394 else if (nch == 3)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2395 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
2396 && 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
2397 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2398 res = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2399 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
2400 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2401 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2402 case LBRACE:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2403 res = ((nch <= 3)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2404 && 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
2405
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2406 if (res && (nch == 3))
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2407 res = validate_dictorsetmaker(CHILD(tree, 1));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2408 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2409 case NAME:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2410 case NUMBER:
76824
2b1cc84bf1d9 Issue #14741: Fix missing support for ellipsis in parser module.
Mark Dickinson <mdickinson@enthought.com>
parents: 76822
diff changeset
2411 case ELLIPSIS:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2412 res = (nch == 1);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2413 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2414 case STRING:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2415 for (pos = 1; res && (pos < nch); ++pos)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2416 res = validate_ntype(CHILD(tree, pos), STRING);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2417 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2418 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2419 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2420 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2421 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2422 }
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
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2427 /* testlist_comp:
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2428 * 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
2429 */
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2430 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2431 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
2432 {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2433 int nch = NCH(tree);
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2434 int ok = nch;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2435
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2436 if (nch == 0)
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2437 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
2438 else {
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2439 ok = validate_test_or_star_expr(CHILD(tree, 0));
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2440 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2441
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2442 /*
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2443 * comp_for | (',' test)* [',']
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2444 */
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2445 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
2446 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
2447 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2448 /* (',' test)* [','] */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2449 int i = 1;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2450 while (ok && nch - i >= 2) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2451 ok = (validate_comma(CHILD(tree, i))
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2452 && validate_test_or_star_expr(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
2453 i += 2;
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2454 }
14129
2c2ae86dfe87 validate_listmaker(): Revise to match Skip's latest changes to the
Fred Drake <fdrake@acm.org>
parents: 14113
diff changeset
2455 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
2456 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
2457 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
2458 ok = 0;
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2459 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
2460 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2461 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2462 return ok;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2463 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2464
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2465 /* decorator:
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2466 * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2467 */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2468 static int
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2469 validate_decorator(node *tree)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2470 {
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2471 int ok;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2472 int nch = NCH(tree);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2473 ok = (validate_ntype(tree, decorator) &&
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2474 (nch == 3 || nch == 5 || nch == 6) &&
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2475 validate_at(CHILD(tree, 0)) &&
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2476 validate_dotted_name(CHILD(tree, 1)) &&
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2477 validate_newline(RCHILD(tree, -1)));
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2478
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2479 if (ok && nch != 3) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2480 ok = (validate_lparen(CHILD(tree, 2)) &&
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2481 validate_rparen(RCHILD(tree, -2)));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2482
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2483 if (ok && nch == 6)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2484 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
2485 }
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2486
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2487 return ok;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2488 }
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2489
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2490 /* decorators:
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2491 * decorator+
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2492 */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2493 static int
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2494 validate_decorators(node *tree)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2495 {
43862
d4f9706cb46b Patch 1144 by David Binger, fix for parser module. With unittest.
Guido van Rossum <guido@python.org>
parents: 42856
diff changeset
2496 int i, nch, ok;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2497 nch = NCH(tree);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2498 ok = validate_ntype(tree, decorators) && nch >= 1;
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2499
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2500 for (i = 0; ok && i < nch; ++i)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2501 ok = validate_decorator(CHILD(tree, i));
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2502
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2503 return ok;
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 32888
diff changeset
2504 }
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2505
54338
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2506 /* with_item:
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2507 * test ['as' expr]
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2508 */
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2509 static int
54338
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2510 validate_with_item(node *tree)
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2511 {
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2512 int nch = NCH(tree);
54338
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2513 int ok = (validate_ntype(tree, with_item)
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2514 && (nch == 1 || nch == 3)
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2515 && validate_test(CHILD(tree, 0)));
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
2516 if (ok && nch == 3)
54338
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2517 ok = (validate_name(CHILD(tree, 1), "as")
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2518 && validate_expr(CHILD(tree, 2)));
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2519 return ok;
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2520 }
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2521
54338
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2522 /* with_stmt:
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2523 * 0 1 ... -2 -1
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2524 * 'with' with_item (',' with_item)* ':' suite
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2525 */
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2526 static int
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2527 validate_with_stmt(node *tree)
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2528 {
54338
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2529 int i;
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2530 int nch = NCH(tree);
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2531 int ok = (validate_ntype(tree, with_stmt)
54338
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2532 && (nch % 2 == 0)
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2533 && validate_name(CHILD(tree, 0), "with")
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2534 && validate_colon(RCHILD(tree, -2))
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2535 && validate_suite(RCHILD(tree, -1)));
54338
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2536 for (i = 1; ok && i < nch - 2; i += 2)
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2537 ok = validate_with_item(CHILD(tree, i));
287ce30d08b5 Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54082
diff changeset
2538 return ok;
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2539 }
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
2540
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2541 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2542
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2543 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2544 validate_funcdef(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2545 {
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32121
diff changeset
2546 int nch = NCH(tree);
76635
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2547 int res = validate_ntype(tree, funcdef);
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2548 if (res) {
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2549 if (nch == 5) {
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2550 res = (validate_name(CHILD(tree, 0), "def")
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2551 && validate_ntype(CHILD(tree, 1), NAME)
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2552 && validate_parameters(CHILD(tree, 2))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2553 && validate_colon(CHILD(tree, 3))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2554 && validate_suite(CHILD(tree, 4)));
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2555 }
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2556 else if (nch == 7) {
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2557 res = (validate_name(CHILD(tree, 0), "def")
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2558 && validate_ntype(CHILD(tree, 1), NAME)
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2559 && validate_parameters(CHILD(tree, 2))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2560 && validate_rarrow(CHILD(tree, 3))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2561 && validate_test(CHILD(tree, 4))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2562 && validate_colon(CHILD(tree, 5))
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2563 && validate_suite(CHILD(tree, 6)));
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2564 }
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2565 else {
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2566 res = 0;
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2567 err_string("illegal number of children for funcdef");
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2568 }
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2569 }
9f57d66689ca Issue #9154: Fix parser module to understand function annotations.
Mark Dickinson <mdickinson@enthought.com>
parents: 68489
diff changeset
2570 return res;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2571 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2572
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2573
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2574 /* decorated
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2575 * decorators (classdef | funcdef)
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2576 */
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2577 static int
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2578 validate_decorated(node *tree)
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2579 {
62774
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2580 int nch = NCH(tree);
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2581 int ok = (validate_ntype(tree, decorated)
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2582 && (nch == 2)
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2583 && validate_decorators(RCHILD(tree, -2)));
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2584 if (TYPE(RCHILD(tree, -1)) == funcdef)
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2585 ok = ok && validate_funcdef(RCHILD(tree, -1));
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2586 else
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2587 ok = ok && validate_class(RCHILD(tree, -1));
18282039c5c9 Merged revisions 82537 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 61467
diff changeset
2588 return ok;
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2589 }
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41717
diff changeset
2590
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2591 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2592 validate_lambdef(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2593 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2594 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2595 int res = (validate_ntype(tree, lambdef)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2596 && ((nch == 3) || (nch == 4))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2597 && validate_name(CHILD(tree, 0), "lambda")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2598 && validate_colon(CHILD(tree, nch - 2))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2599 && validate_test(CHILD(tree, nch - 1)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2600
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2601 if (res && (nch == 4))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2602 res = validate_varargslist(CHILD(tree, 1));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2603 else if (!res && !PyErr_Occurred())
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2604 (void) validate_numnodes(tree, 3, "lambdef");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2605
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2606 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2607 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2608
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2609
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2610 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2611 validate_lambdef_nocond(node *tree)
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2612 {
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2613 int nch = NCH(tree);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2614 int res = (validate_ntype(tree, lambdef_nocond)
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2615 && ((nch == 3) || (nch == 4))
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2616 && validate_name(CHILD(tree, 0), "lambda")
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2617 && validate_colon(CHILD(tree, nch - 2))
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2618 && validate_test(CHILD(tree, nch - 1)));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2619
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2620 if (res && (nch == 4))
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2621 res = validate_varargslist(CHILD(tree, 1));
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2622 else if (!res && !PyErr_Occurred())
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2623 (void) validate_numnodes(tree, 3, "lambdef_nocond");
36673
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2624
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2625 return (res);
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2626 }
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2627
2ddea5535f08 Update for PEP 308 patch.
Thomas Wouters <thomas@python.org>
parents: 36518
diff changeset
2628
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2629 /* arglist:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2630 *
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2631 * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2632 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2633 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2634 validate_arglist(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2635 {
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2636 int nch = NCH(tree);
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2637 int i = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2638 int ok = 1;
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2639
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2640 if (nch <= 0)
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2641 /* 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
2642 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
2643
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2644 if (nch > 1) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2645 for (i=0; i<nch; i++) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2646 if (TYPE(CHILD(tree, i)) == argument) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2647 node *ch = CHILD(tree, i);
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2648 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
2649 err_string("need '(', ')' for generator expression");
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2650 return 0;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2651 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2652 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2653 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2654 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2655
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2656 while (ok && nch-i >= 2) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2657 /* skip leading (argument ',') */
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2658 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
2659 && 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
2660 if (ok)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2661 i += 2;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2662 else
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2663 PyErr_Clear();
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2664 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2665 ok = 1;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2666 if (nch-i > 0) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2667 /*
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2668 * argument | '*' test [',' '**' test] | '**' test
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2669 */
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2670 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
2671
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2672 if (sym == argument) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2673 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
2674 if (ok && i+1 != nch) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2675 err_string("illegal arglist specification"
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2676 " (extra stuff on end)");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2677 ok = 0;
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2678 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2679 }
14110
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2680 else if (sym == STAR) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2681 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
2682 if (ok && (nch-i == 2))
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2683 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
2684 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
2685 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
2686 && 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
2687 && 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
2688 && 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
2689 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2690 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
2691 ok = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2692 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2693 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2694 else if (sym == DOUBLESTAR) {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2695 if (nch-i == 2)
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2696 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
2697 && 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
2698 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2699 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
2700 ok = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2701 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2702 }
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2703 else {
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2704 err_string("illegal arglist specification");
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2705 ok = 0;
0abfca0035dc Update to reflect recent grammar changes (list comprehensions, extended
Fred Drake <fdrake@acm.org>
parents: 13692
diff changeset
2706 }
12487
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2707 }
f1e867ad7d85 validate_arglist(): Re-written to reflect extended call syntax.
Fred Drake <fdrake@acm.org>
parents: 12416
diff changeset
2708 return (ok);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2709 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2710
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2711
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2712
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2713 /* argument:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2714 *
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2715 * [test '='] test [comp_for]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2716 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2717 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2718 validate_argument(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2719 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2720 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2721 int res = (validate_ntype(tree, argument)
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2722 && ((nch == 1) || (nch == 2) || (nch == 3))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2723 && validate_test(CHILD(tree, 0)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2724
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2725 if (res && (nch == 2))
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2726 res = validate_comp_for(CHILD(tree, 1));
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 30979
diff changeset
2727 else if (res && (nch == 3))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2728 res = (validate_equal(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2729 && validate_test(CHILD(tree, 2)));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2730
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2731 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2732 }
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2733
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2734
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2735
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2736 /* trailer:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2737 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2738 * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2739 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2740 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2741 validate_trailer(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2742 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2743 int nch = NCH(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2744 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
2745
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2746 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2747 switch (TYPE(CHILD(tree, 0))) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2748 case LPAR:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2749 res = validate_rparen(CHILD(tree, nch - 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2750 if (res && (nch == 3))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2751 res = validate_arglist(CHILD(tree, 1));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2752 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2753 case LSQB:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2754 res = (validate_numnodes(tree, 3, "trailer")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2755 && validate_subscriptlist(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2756 && validate_ntype(CHILD(tree, 2), RSQB));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2757 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2758 case DOT:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2759 res = (validate_numnodes(tree, 2, "trailer")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2760 && validate_ntype(CHILD(tree, 1), NAME));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2761 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2762 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2763 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2764 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2765 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2766 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2767 else {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2768 (void) validate_numnodes(tree, 2, "trailer");
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2769 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2770 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2771 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2772
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2773
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2774 /* subscriptlist:
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2775 *
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2776 * subscript (',' subscript)* [',']
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2777 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2778 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2779 validate_subscriptlist(node *tree)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2780 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2781 return (validate_repeating_list(tree, subscriptlist,
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2782 validate_subscript, "subscriptlist"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2783 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2784
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2785
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2786 /* subscript:
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2787 *
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2788 * '.' '.' '.' | test | [test] ':' [test] [sliceop]
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2789 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2790 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2791 validate_subscript(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2792 {
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2793 int offset = 0;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2794 int nch = NCH(tree);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2795 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
2796
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2797 if (!res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2798 if (!PyErr_Occurred())
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2799 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
2800 return (0);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2801 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2802 if (TYPE(CHILD(tree, 0)) == DOT)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2803 /* take care of ('.' '.' '.') possibility */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2804 return (validate_numnodes(tree, 3, "subscript")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2805 && validate_dot(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2806 && validate_dot(CHILD(tree, 1))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2807 && validate_dot(CHILD(tree, 2)));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2808 if (nch == 1) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2809 if (TYPE(CHILD(tree, 0)) == test)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2810 res = validate_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2811 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2812 res = validate_colon(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2813 return (res);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2814 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2815 /* Must be [test] ':' [test] [sliceop],
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2816 * 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
2817 * 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
2818 */
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2819 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
2820 res = validate_test(CHILD(tree, 0));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2821 offset = 1;
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2822 }
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2823 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2824 res = validate_colon(CHILD(tree, offset));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2825 if (res) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2826 int rem = nch - ++offset;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2827 if (rem) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2828 if (TYPE(CHILD(tree, offset)) == test) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2829 res = validate_test(CHILD(tree, offset));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2830 ++offset;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2831 --rem;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2832 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2833 if (res && rem)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2834 res = validate_sliceop(CHILD(tree, offset));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2835 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2836 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2837 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2838 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2839
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2840
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2841 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2842 validate_sliceop(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2843 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2844 int nch = NCH(tree);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2845 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
2846 && validate_ntype(tree, sliceop);
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2847 if (!res && !PyErr_Occurred()) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2848 res = validate_numnodes(tree, 1, "sliceop");
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2849 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2850 if (res)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2851 res = validate_colon(CHILD(tree, 0));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2852 if (res && (nch == 2))
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2853 res = validate_test(CHILD(tree, 1));
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2854
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2855 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2856 }
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2857
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2858
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2859 static int
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2860 validate_test_or_star_expr(node *n)
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2861 {
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2862 if (TYPE(n) == test)
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2863 return validate_test(n);
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2864 return validate_star_expr(n);
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2865 }
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2866
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2867 static int
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2868 validate_expr_or_star_expr(node *n)
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2869 {
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2870 if (TYPE(n) == expr)
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2871 return validate_expr(n);
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2872 return validate_star_expr(n);
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2873 }
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2874
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2875
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2876 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2877 validate_exprlist(node *tree)
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2878 {
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2879 return (validate_repeating_list(tree, exprlist,
56237
7ee140b7ef93 fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents: 54338
diff changeset
2880 validate_expr_or_star_expr, "exprlist"));
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2881 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2882
76822
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2883 /*
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2884 * dictorsetmaker:
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2885 *
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2886 * (test ':' test (comp_for | (',' test ':' test)* [','])) |
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2887 * (test (comp_for | (',' test)* [',']))
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2888 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2889 static int
41464
060c79f6d919 Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents: 40774
diff changeset
2890 validate_dictorsetmaker(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2891 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2892 int nch = NCH(tree);
76822
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2893 int res;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2894 int i = 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2895
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2896 res = validate_ntype(tree, dictorsetmaker);
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2897 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2898 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2899
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2900 if (nch - i < 1) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2901 (void) validate_numnodes(tree, 1, "dictorsetmaker");
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2902 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2903 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2904
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2905 res = validate_test(CHILD(tree, i++));
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2906 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2907 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2908
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2909 if (nch - i >= 2 && TYPE(CHILD(tree, i)) == COLON) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2910 /* Dictionary display or dictionary comprehension. */
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2911 res = (validate_colon(CHILD(tree, i++))
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2912 && validate_test(CHILD(tree, i++)));
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2913 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2914 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2915
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2916 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2917 /* Dictionary comprehension. */
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2918 res = validate_comp_for(CHILD(tree, i++));
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2919 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2920 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2921 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2922 else {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2923 /* Dictionary display. */
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2924 while (nch - i >= 4) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2925 res = (validate_comma(CHILD(tree, i++))
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2926 && validate_test(CHILD(tree, i++))
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2927 && validate_colon(CHILD(tree, i++))
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2928 && validate_test(CHILD(tree, i++)));
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2929 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2930 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2931 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2932 if (nch - i == 1) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2933 res = validate_comma(CHILD(tree, i++));
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2934 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2935 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2936 }
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2937 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2938 }
76822
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2939 else {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2940 /* Set display or set comprehension. */
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2941 if (nch - i >= 1 && TYPE(CHILD(tree, i)) == comp_for) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2942 /* Set comprehension. */
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2943 res = validate_comp_for(CHILD(tree, i++));
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2944 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2945 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2946 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2947 else {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2948 /* Set display. */
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2949 while (nch - i >= 2) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2950 res = (validate_comma(CHILD(tree, i++))
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2951 && validate_test(CHILD(tree, i++)));
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2952 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2953 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2954 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2955 if (nch - i == 1) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2956 res = validate_comma(CHILD(tree, i++));
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2957 if (!res)
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2958 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2959 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2960 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2961 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2962
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2963 if (nch - i > 0) {
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2964 err_string("Illegal trailing nodes for dictorsetmaker.");
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2965 return 0;
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2966 }
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2967
129289144dfb Issue #14697: Fix missing parser module support for set displays and set comprehensions.
Mark Dickinson <mdickinson@enthought.com>
parents: 76818
diff changeset
2968 return 1;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2969 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2970
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2971
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2972 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2973 validate_eval_input(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2974 {
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2975 int pos;
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2976 int nch = NCH(tree);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2977 int res = (validate_ntype(tree, eval_input)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2978 && (nch >= 2)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2979 && validate_testlist(CHILD(tree, 0))
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2980 && 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
2981
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2982 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
2983 res = validate_ntype(CHILD(tree, pos), NEWLINE);
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2984
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2985 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2986 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2987
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
2988
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
2989 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2990 validate_node(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
2991 {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2992 int nch = 0; /* num. children on current node */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2993 int res = 1; /* result value */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2994 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
2995
18247
e3cf3348feca Properly use &&. Closes bug #434989.
Martin v. Löwis <martin@v.loewis.de>
parents: 15751
diff changeset
2996 while (res && (tree != 0)) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2997 nch = NCH(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2998 next = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
2999 switch (TYPE(tree)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3000 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3001 * Definition nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3002 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3003 case funcdef:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3004 res = validate_funcdef(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3005 break;
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
3006 case with_stmt:
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
3007 res = validate_with_stmt(tree);
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49633
diff changeset
3008 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3009 case classdef:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3010 res = validate_class(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3011 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3012 case decorated:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3013 res = validate_decorated(tree);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3014 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3015 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3016 * "Trivial" parse tree nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3017 * (Why did I call these trivial?)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3018 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3019 case stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3020 res = validate_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3021 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3022 case small_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3023 /*
76647
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
3024 * expr_stmt | del_stmt | pass_stmt | flow_stmt |
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
3025 * import_stmt | global_stmt | nonlocal_stmt | assert_stmt
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3026 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3027 res = validate_small_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3028 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3029 case flow_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3030 res = (validate_numnodes(tree, 1, "flow_stmt")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3031 && ((TYPE(CHILD(tree, 0)) == break_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3032 || (TYPE(CHILD(tree, 0)) == continue_stmt)
18646
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
3033 || (TYPE(CHILD(tree, 0)) == yield_stmt)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3034 || (TYPE(CHILD(tree, 0)) == return_stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3035 || (TYPE(CHILD(tree, 0)) == raise_stmt)));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3036 if (res)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3037 next = CHILD(tree, 0);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3038 else if (nch == 1)
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
3039 err_string("illegal flow_stmt type");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3040 break;
18646
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
3041 case yield_stmt:
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
3042 res = validate_yield_stmt(tree);
c43fe213ee10 Add support for yield statements.
Fred Drake <fdrake@acm.org>
parents: 18247
diff changeset
3043 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3044 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3045 * Compound statements.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3046 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3047 case simple_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3048 res = validate_simple_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3049 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3050 case compound_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3051 res = validate_compound_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3052 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3053 /*
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
3054 * Fundamental statements.
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3055 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3056 case expr_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3057 res = validate_expr_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3058 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3059 case del_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3060 res = validate_del_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3061 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3062 case pass_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3063 res = (validate_numnodes(tree, 1, "pass")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3064 && validate_name(CHILD(tree, 0), "pass"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3065 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3066 case break_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3067 res = (validate_numnodes(tree, 1, "break")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3068 && validate_name(CHILD(tree, 0), "break"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3069 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3070 case continue_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3071 res = (validate_numnodes(tree, 1, "continue")
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3072 && validate_name(CHILD(tree, 0), "continue"));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3073 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3074 case return_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3075 res = validate_return_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3076 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3077 case raise_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3078 res = validate_raise_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3079 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3080 case import_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3081 res = validate_import_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3082 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3083 case import_name:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3084 res = validate_import_name(tree);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3085 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3086 case import_from:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3087 res = validate_import_from(tree);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3088 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3089 case global_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3090 res = validate_global_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3091 break;
76647
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
3092 case nonlocal_stmt:
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
3093 res = validate_nonlocal_stmt(tree);
b7e491b9094f Issue #14696: Fix parser module to understand 'nonlocal' declarations.
Mark Dickinson <mdickinson@enthought.com>
parents: 76635
diff changeset
3094 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3095 case assert_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3096 res = validate_assert_stmt(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3097 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3098 case if_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3099 res = validate_if(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3100 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3101 case while_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3102 res = validate_while(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3103 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3104 case for_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3105 res = validate_for(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3106 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3107 case try_stmt:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3108 res = validate_try(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3109 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3110 case suite:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3111 res = validate_suite(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3112 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3113 /*
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3114 * Expression nodes.
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3115 */
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3116 case testlist:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3117 res = validate_testlist(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3118 break;
35298
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
3119 case yield_expr:
b2fe5f8fcfdd PEP 342 implementation. Per Guido's comments, the generator throw()
Phillip J. Eby <pje@telecommunity.com>
parents: 34887
diff changeset
3120 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
3121 break;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3122 case test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3123 res = validate_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3124 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3125 case and_test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3126 res = validate_and_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3127 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3128 case not_test:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3129 res = validate_not_test(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3130 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3131 case comparison:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3132 res = validate_comparison(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3133 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3134 case exprlist:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3135 res = validate_exprlist(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3136 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3137 case comp_op:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3138 res = validate_comp_op(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3139 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3140 case expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3141 res = validate_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3142 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3143 case xor_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3144 res = validate_xor_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3145 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3146 case and_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3147 res = validate_and_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3148 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3149 case shift_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3150 res = validate_shift_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3151 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3152 case arith_expr:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3153 res = validate_arith_expr(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3154 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3155 case term:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3156 res = validate_term(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3157 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3158 case factor:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3159 res = validate_factor(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3160 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3161 case power:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3162 res = validate_power(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3163 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3164 case atom:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3165 res = validate_atom(tree);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3166 break;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3167
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3168 default:
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3169 /* Hopefully never reached! */
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
3170 err_string("unrecognized node type");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3171 res = 0;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3172 break;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3173 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3174 tree = next;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3175 }
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3176 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3177 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3178
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3179
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
3180 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3181 validate_expr_tree(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3182 {
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3183 int res = validate_eval_input(tree);
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3184
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3185 if (!res && !PyErr_Occurred())
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
3186 err_string("could not validate expression tuple");
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3187
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3188 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3189 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3190
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3191
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3192 /* file_input:
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3193 * (NEWLINE | stmt)* ENDMARKER
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3194 */
4084
7f83e24d1d86 New batch from Fred
Guido van Rossum <guido@python.org>
parents: 3774
diff changeset
3195 static int
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3196 validate_file_input(node *tree)
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3197 {
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3198 int j;
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3199 int nch = NCH(tree) - 1;
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3200 int res = ((nch >= 0)
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3201 && validate_ntype(CHILD(tree, nch), ENDMARKER));
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3202
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3203 for (j = 0; res && (j < nch); ++j) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3204 if (TYPE(CHILD(tree, j)) == stmt)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3205 res = validate_stmt(CHILD(tree, j));
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3206 else
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3207 res = validate_newline(CHILD(tree, j));
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3208 }
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
3209 /* 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
3210 * 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
3211 * this, we have some debugging to do.
3774
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3212 */
4c78d73c5667 New submission by Fred Drake.
Guido van Rossum <guido@python.org>
parents: 3336
diff changeset
3213 if (!res && !PyErr_Occurred())
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14533
diff changeset
3214 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
3215
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3216 return (res);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3217 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3218
27767
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3219 static int
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3220 validate_encoding_decl(node *tree)
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3221 {
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3222 int nch = NCH(tree);
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3223 int res = ((nch == 1)
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3224 && validate_file_input(CHILD(tree, 0)));
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3225
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3226 if (!res && !PyErr_Occurred())
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3227 err_string("Error Parsing encoding_decl");
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3228
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3229 return res;
82cbf1178e4f Apply logistix's patch from
Michael W. Hudson <mwh@python.net>
parents: 27517
diff changeset
3230 }
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3231
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3232 static PyObject*
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3233 pickle_constructor = NULL;
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3234
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3235
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3236 static PyObject*
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3237 parser__pickler(PyObject *self, PyObject *args)
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3238 {
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
3239 NOTE(ARGUNUSED(self))
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3240 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
3241 PyObject *st = NULL;
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
3242 PyObject *empty_dict = NULL;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3243
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3244 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
3245 PyObject *newargs;
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3246 PyObject *tuple;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3247
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
3248 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
3249 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
3250 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
3251 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
3252 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
3253 if (tuple != NULL) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3254 result = Py_BuildValue("O(O)", pickle_constructor, tuple);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3255 Py_DECREF(tuple);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3256 }
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
3257 Py_DECREF(empty_dict);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3258 Py_DECREF(newargs);
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3259 }
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3260 finally:
11297
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
3261 Py_XDECREF(empty_dict);
2560236ecedb parser__pickler(): Don't drop the third argument to
Fred Drake <fdrake@acm.org>
parents: 11266
diff changeset
3262
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3263 return (result);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3264 }
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3265
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3266
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3267 /* 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
3268 * 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
3269 * 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
3270 * 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
3271 * inheritance.
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3272 */
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3273 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
3274 {"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
3275 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
3276 {"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
3277 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
3278 {"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
3279 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
3280 {"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
3281 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
3282 {"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
3283 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
3284 {"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
3285 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
3286 {"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
3287 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
3288 {"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
3289 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
3290 {"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
3291 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
3292
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3293 /* 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
3294 {"_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
3295 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
3296
8196
885de49b445c Made lint a bit happier.
Fred Drake <fdrake@acm.org>
parents: 8184
diff changeset
3297 {NULL, NULL, 0, NULL}
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3298 };
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3299
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3300
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3301
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3302 static struct PyModuleDef parsermodule = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3303 PyModuleDef_HEAD_INIT,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3304 "parser",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3305 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3306 -1,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3307 parser_functions,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3308 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3309 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3310 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3311 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3312 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3313
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3314 PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */
24363
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
3315
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
3316 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3317 PyInit_parser(void)
14214
b5fb175ad528 Update the parser module to support augmented assignment.
Fred Drake <fdrake@acm.org>
parents: 14129
diff changeset
3318 {
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3319 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
3320
48413
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
3321 if (PyType_Ready(&PyST_Type) < 0)
7e56016a6565 #3247 Get rid of Py_FindMethod; use tp_members instead.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47580
diff changeset
3322 return NULL;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3323 module = PyModule_Create(&parsermodule);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
3324 if (module == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54338
diff changeset
3325 return NULL;
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3326
11266
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
3327 if (parser_error == 0)
c0b86a44a120 Added keyword parameter support to all public interfaces; keyword
Fred Drake <fdrake@acm.org>
parents: 10215
diff changeset
3328 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
3329
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
3330 if (parser_error == 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3331 return NULL;
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
3332 /* CAUTION: The code next used to skip bumping the refcount on
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3333 * parser_error. That's a disaster if PyInit_parser() gets called more
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
3334 * 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
3335 * 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
3336 * 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
3337 */
1cbc6b7836dc Merge 23c1-branch back into the head. Barry will send email about the
Tim Peters <tim.peters@gmail.com>
parents: 28551
diff changeset
3338 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
3339 if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3340 return NULL;
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
3341
18669
6d4a530fe7c1 The syntax trees handled by this module are not "abstract," so take the
Fred Drake <fdrake@acm.org>
parents: 18646
diff changeset
3342 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
3343 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
3344
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3345 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
3346 parser_copyright_string);
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3347 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
3348 parser_doc_string);
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3349 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
3350 parser_version_string);
3200
8f5ab1a6c914 Fred Drake's new parser module
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
3351
18748
e5bafddd1da0 Elaborate a comment.
Fred Drake <fdrake@acm.org>
parents: 18669
diff changeset
3352 /* Register to support pickling.
e5bafddd1da0 Elaborate a comment.
Fred Drake <fdrake@acm.org>
parents: 18669
diff changeset
3353 * 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
3354 * exception will be raised here; should we clear the exception?
e5bafddd1da0 Elaborate a comment.
Fred Drake <fdrake@acm.org>
parents: 18669
diff changeset
3355 */
46867
dd5513b472dc Rename copy_reg module to copyreg.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46644
diff changeset
3356 copyreg = PyImport_ImportModuleNoBlock("copyreg");
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3357 if (copyreg != NULL) {
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3358 PyObject *func, *pickler;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3359
19274
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3360 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
3361 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
3362 pickler = PyObject_GetAttrString(module, "_pickler");
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3363 Py_XINCREF(pickle_constructor);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3364 if ((func != NULL) && (pickle_constructor != NULL)
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3365 && (pickler != NULL)) {
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3366 PyObject *res;
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3367
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
3368 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
3369 pickle_constructor, NULL);
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3370 Py_XDECREF(res);
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3371 }
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3372 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
3373 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
3374 Py_XDECREF(pickler);
5501958edc36 Use the abstract object interfaces when digging around in module objects
Fred Drake <fdrake@acm.org>
parents: 18748
diff changeset
3375 Py_DECREF(copyreg);
8034
cd3092069f5a Added support for pickling ast objects.
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
3376 }
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46867
diff changeset
3377 return module;
12416
95e296e67a12 ANSI-fy & de-tabify the source.
Fred Drake <fdrake@acm.org>
parents: 11828
diff changeset
3378 }