Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1 | /* parsermodule.c |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 3 | * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic |
| 4 | * Institute and State University, Blacksburg, Virginia, USA. |
| 5 | * Portions copyright 1991-1995 by Stichting Mathematisch Centrum, |
| 6 | * Amsterdam, The Netherlands. Copying is permitted under the terms |
| 7 | * associated with the main Python distribution, with the additional |
| 8 | * restriction that this additional notice be included and maintained |
| 9 | * on all distributed copies. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 10 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 11 | * This module serves to replace the original parser module written |
| 12 | * by Guido. The functionality is not matched precisely, but the |
| 13 | * original may be implemented on top of this. This is desirable |
| 14 | * since the source of the text to be parsed is now divorced from |
| 15 | * this interface. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 16 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 17 | * Unlike the prior interface, the ability to give a parse tree |
| 18 | * produced by Python code as a tuple to the compiler is enabled by |
| 19 | * this module. See the documentation for more details. |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 20 | * |
| 21 | * I've added some annotations that help with the lint code-checking |
| 22 | * program, but they're not complete by a long shot. The real errors |
| 23 | * that lint detects are gone, but there are still warnings with |
| 24 | * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations |
| 25 | * look like "NOTE(...)". |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 28 | #include "Python.h" /* general Python API */ |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 29 | #include "Python-ast.h" /* mod_ty */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 30 | #include "graminit.h" /* symbols defined in the grammar */ |
| 31 | #include "node.h" /* internal parser structure */ |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 32 | #include "errcode.h" /* error codes for PyNode_*() */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 33 | #include "token.h" /* token definitions */ |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 34 | #include "grammar.h" |
| 35 | #include "parsetok.h" |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 36 | /* ISTERMINAL() / ISNONTERMINAL() */ |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 37 | #include "compile.h" |
| 38 | #undef Yield |
| 39 | #include "ast.h" |
| 40 | #include "pyarena.h" |
| 41 | |
| 42 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 43 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 44 | #ifdef lint |
| 45 | #include <note.h> |
| 46 | #else |
| 47 | #define NOTE(x) |
| 48 | #endif |
| 49 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 50 | /* String constants used to initialize module attributes. |
| 51 | * |
| 52 | */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 53 | static char parser_copyright_string[] = |
| 54 | "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\ |
Guido van Rossum | 2a28846 | 1996-08-21 21:55:43 +0000 | [diff] [blame] | 55 | University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\ |
| 56 | Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\ |
| 57 | Centrum, Amsterdam, The Netherlands."; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 58 | |
| 59 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 60 | PyDoc_STRVAR(parser_doc_string, |
| 61 | "This is an interface to Python's internal parser."); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 62 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 63 | static char parser_version_string[] = "0.5"; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 64 | |
| 65 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 66 | typedef PyObject* (*SeqMaker) (Py_ssize_t length); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 67 | typedef int (*SeqInserter) (PyObject* sequence, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 68 | Py_ssize_t index, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 69 | PyObject* element); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 70 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 71 | /* The function below is copyrighted by Stichting Mathematisch Centrum. The |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 72 | * original copyright statement is included below, and continues to apply |
| 73 | * in full to the function immediately following. All other material is |
| 74 | * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic |
| 75 | * Institute and State University. Changes were made to comply with the |
Guido van Rossum | 2a28846 | 1996-08-21 21:55:43 +0000 | [diff] [blame] | 76 | * new naming conventions. Added arguments to provide support for creating |
| 77 | * lists as well as tuples, and optionally including the line numbers. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 78 | */ |
| 79 | |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 81 | static PyObject* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 82 | node2tuple(node *n, /* node to convert */ |
| 83 | SeqMaker mkseq, /* create sequence */ |
| 84 | SeqInserter addelem, /* func. to add elem. in seq. */ |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 85 | int lineno, /* include line numbers? */ |
| 86 | int col_offset) /* include column offsets? */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 87 | { |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 88 | if (n == NULL) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 89 | Py_INCREF(Py_None); |
| 90 | return (Py_None); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 91 | } |
| 92 | if (ISNONTERMINAL(TYPE(n))) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 93 | int i; |
| 94 | PyObject *v; |
| 95 | PyObject *w; |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 96 | |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 97 | v = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 98 | if (v == NULL) |
| 99 | return (v); |
| 100 | w = PyInt_FromLong(TYPE(n)); |
| 101 | if (w == NULL) { |
| 102 | Py_DECREF(v); |
| 103 | return ((PyObject*) NULL); |
| 104 | } |
| 105 | (void) addelem(v, 0, w); |
| 106 | for (i = 0; i < NCH(n); i++) { |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 107 | w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 108 | if (w == NULL) { |
| 109 | Py_DECREF(v); |
| 110 | return ((PyObject*) NULL); |
| 111 | } |
| 112 | (void) addelem(v, i+1, w); |
| 113 | } |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 114 | |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 115 | if (TYPE(n) == encoding_decl) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 116 | (void) addelem(v, i+1, PyString_FromString(STR(n))); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 117 | return (v); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 118 | } |
| 119 | else if (ISTERMINAL(TYPE(n))) { |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 120 | PyObject *result = mkseq(2 + lineno + col_offset); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 121 | if (result != NULL) { |
| 122 | (void) addelem(result, 0, PyInt_FromLong(TYPE(n))); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 123 | (void) addelem(result, 1, PyString_FromString(STR(n))); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 124 | if (lineno == 1) |
| 125 | (void) addelem(result, 2, PyInt_FromLong(n->n_lineno)); |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 126 | if (col_offset == 1) |
| 127 | (void) addelem(result, 3, PyInt_FromLong(n->n_col_offset)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 128 | } |
| 129 | return (result); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 130 | } |
| 131 | else { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 132 | PyErr_SetString(PyExc_SystemError, |
| 133 | "unrecognized parse tree node type"); |
| 134 | return ((PyObject*) NULL); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 135 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 136 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 137 | /* |
| 138 | * End of material copyrighted by Stichting Mathematisch Centrum. |
| 139 | */ |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | /* There are two types of intermediate objects we're interested in: |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 144 | * 'eval' and 'exec' types. These constants can be used in the st_type |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 145 | * field of the object type to identify which any given object represents. |
| 146 | * These should probably go in an external header to allow other extensions |
| 147 | * to use them, but then, we really should be using C++ too. ;-) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 148 | */ |
| 149 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 150 | #define PyST_EXPR 1 |
| 151 | #define PyST_SUITE 2 |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 152 | |
| 153 | |
| 154 | /* These are the internal objects and definitions required to implement the |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 155 | * ST type. Most of the internal names are more reminiscent of the 'old' |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 156 | * naming style, but the code uses the new naming convention. |
| 157 | */ |
| 158 | |
| 159 | static PyObject* |
| 160 | parser_error = 0; |
| 161 | |
| 162 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 163 | typedef struct { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 164 | PyObject_HEAD /* standard object header */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 165 | node* st_node; /* the node* returned by the parser */ |
| 166 | int st_type; /* EXPR or SUITE ? */ |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 167 | PyCompilerFlags st_flags; /* Parser and compiler flags */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 168 | } PyST_Object; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 169 | |
| 170 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 171 | static void parser_free(PyST_Object *st); |
| 172 | static int parser_compare(PyST_Object *left, PyST_Object *right); |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 173 | static PyObject *parser_getattr(PyObject *self, char *name); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 175 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 176 | static |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 177 | PyTypeObject PyST_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 178 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 179 | "parser.st", /* tp_name */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 180 | (int) sizeof(PyST_Object), /* tp_basicsize */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 181 | 0, /* tp_itemsize */ |
| 182 | (destructor)parser_free, /* tp_dealloc */ |
| 183 | 0, /* tp_print */ |
| 184 | parser_getattr, /* tp_getattr */ |
| 185 | 0, /* tp_setattr */ |
| 186 | (cmpfunc)parser_compare, /* tp_compare */ |
| 187 | 0, /* tp_repr */ |
| 188 | 0, /* tp_as_number */ |
| 189 | 0, /* tp_as_sequence */ |
| 190 | 0, /* tp_as_mapping */ |
| 191 | 0, /* tp_hash */ |
| 192 | 0, /* tp_call */ |
| 193 | 0, /* tp_str */ |
| 194 | 0, /* tp_getattro */ |
| 195 | 0, /* tp_setattro */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 196 | |
| 197 | /* Functions to access object as input/output buffer */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 198 | 0, /* tp_as_buffer */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 199 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 200 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 201 | |
| 202 | /* __doc__ */ |
| 203 | "Intermediate representation of a Python parse tree." |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 204 | }; /* PyST_Type */ |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 205 | |
| 206 | |
| 207 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 208 | parser_compare_nodes(node *left, node *right) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 209 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 210 | int j; |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 212 | if (TYPE(left) < TYPE(right)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 213 | return (-1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 214 | |
| 215 | if (TYPE(right) < TYPE(left)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 216 | return (1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 217 | |
| 218 | if (ISTERMINAL(TYPE(left))) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 219 | return (strcmp(STR(left), STR(right))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 220 | |
| 221 | if (NCH(left) < NCH(right)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 222 | return (-1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 223 | |
| 224 | if (NCH(right) < NCH(left)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 225 | return (1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 226 | |
| 227 | for (j = 0; j < NCH(left); ++j) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 228 | int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 229 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 230 | if (v != 0) |
| 231 | return (v); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 232 | } |
| 233 | return (0); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 234 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 235 | |
| 236 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 237 | /* int parser_compare(PyST_Object* left, PyST_Object* right) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 238 | * |
| 239 | * Comparison function used by the Python operators ==, !=, <, >, <=, >= |
| 240 | * This really just wraps a call to parser_compare_nodes() with some easy |
| 241 | * checks and protection code. |
| 242 | * |
| 243 | */ |
| 244 | static int |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 245 | parser_compare(PyST_Object *left, PyST_Object *right) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 246 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 247 | if (left == right) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 248 | return (0); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 249 | |
| 250 | if ((left == 0) || (right == 0)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 251 | return (-1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 252 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 253 | return (parser_compare_nodes(left->st_node, right->st_node)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 254 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 255 | |
| 256 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 257 | /* parser_newstobject(node* st) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 258 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 259 | * Allocates a new Python object representing an ST. This is simply the |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 260 | * 'wrapper' object that holds a node* and allows it to be passed around in |
| 261 | * Python code. |
| 262 | * |
| 263 | */ |
| 264 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 265 | parser_newstobject(node *st, int type) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 266 | { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 267 | PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 268 | |
| 269 | if (o != 0) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 270 | o->st_node = st; |
| 271 | o->st_type = type; |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 272 | o->st_flags.cf_flags = 0; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 273 | } |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 274 | else { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 275 | PyNode_Free(st); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 276 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 277 | return ((PyObject*)o); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 278 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 279 | |
| 280 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 281 | /* void parser_free(PyST_Object* st) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 282 | * |
| 283 | * This is called by a del statement that reduces the reference count to 0. |
| 284 | * |
| 285 | */ |
| 286 | static void |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 287 | parser_free(PyST_Object *st) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 288 | { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 289 | PyNode_Free(st->st_node); |
| 290 | PyObject_Del(st); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 291 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 292 | |
| 293 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 294 | /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 295 | * |
| 296 | * This provides conversion from a node* to a tuple object that can be |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 297 | * returned to the Python-level caller. The ST object is not modified. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 298 | * |
| 299 | */ |
| 300 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 301 | parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 302 | { |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 303 | PyObject *line_option = 0; |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 304 | PyObject *col_option = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 305 | PyObject *res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 306 | int ok; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 307 | |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 308 | static char *keywords[] = {"ast", "line_info", "col_info", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 309 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 310 | if (self == NULL) { |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 311 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords, |
| 312 | &PyST_Type, &self, &line_option, |
| 313 | &col_option); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 314 | } |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 315 | else |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 316 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1], |
| 317 | &line_option, &col_option); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 318 | if (ok != 0) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 319 | int lineno = 0; |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 320 | int col_offset = 0; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 321 | if (line_option != NULL) { |
| 322 | lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0; |
| 323 | } |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 324 | if (col_option != NULL) { |
| 325 | col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0; |
| 326 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 327 | /* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 328 | * Convert ST into a tuple representation. Use Guido's function, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 329 | * since it's known to work already. |
| 330 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 331 | res = node2tuple(((PyST_Object*)self)->st_node, |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 332 | PyTuple_New, PyTuple_SetItem, lineno, col_offset); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 333 | } |
| 334 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 335 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 336 | |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 337 | static PyObject* |
| 338 | parser_ast2tuple(PyST_Object *self, PyObject *args, PyObject *kw) |
| 339 | { |
| 340 | if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0) |
| 341 | return NULL; |
| 342 | return parser_st2tuple(self, args, kw); |
| 343 | } |
| 344 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 345 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 346 | /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 347 | * |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 348 | * This provides conversion from a node* to a list object that can be |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 349 | * returned to the Python-level caller. The ST object is not modified. |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 350 | * |
| 351 | */ |
| 352 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 353 | parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 354 | { |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 355 | PyObject *line_option = 0; |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 356 | PyObject *col_option = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 357 | PyObject *res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 358 | int ok; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 359 | |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 360 | static char *keywords[] = {"ast", "line_info", "col_info", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 361 | |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 362 | if (self == NULL) |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 363 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords, |
| 364 | &PyST_Type, &self, &line_option, |
| 365 | &col_option); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 366 | else |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 367 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1], |
| 368 | &line_option, &col_option); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 369 | if (ok) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 370 | int lineno = 0; |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 371 | int col_offset = 0; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 372 | if (line_option != 0) { |
| 373 | lineno = PyObject_IsTrue(line_option) ? 1 : 0; |
| 374 | } |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 375 | if (col_option != NULL) { |
| 376 | col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0; |
| 377 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 378 | /* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 379 | * Convert ST into a tuple representation. Use Guido's function, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 380 | * since it's known to work already. |
| 381 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 382 | res = node2tuple(self->st_node, |
Jeremy Hylton | 60e96f6 | 2006-08-22 20:46:00 +0000 | [diff] [blame] | 383 | PyList_New, PyList_SetItem, lineno, col_offset); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 384 | } |
| 385 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 386 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 387 | |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 388 | static PyObject* |
| 389 | parser_ast2list(PyST_Object *self, PyObject *args, PyObject *kw) |
| 390 | { |
| 391 | if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0) |
| 392 | return NULL; |
| 393 | return parser_st2list(self, args, kw); |
| 394 | } |
| 395 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 396 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 397 | /* parser_compilest(PyObject* self, PyObject* args) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 398 | * |
| 399 | * This function creates code objects from the parse tree represented by |
| 400 | * the passed-in data object. An optional file name is passed in as well. |
| 401 | * |
| 402 | */ |
| 403 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 404 | parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 405 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 406 | PyObject* res = 0; |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 407 | PyArena* arena; |
| 408 | mod_ty mod; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 409 | char* str = "<syntax-tree>"; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 410 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 411 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 412 | static char *keywords[] = {"ast", "filename", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 413 | |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 414 | if (self == NULL) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 415 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords, |
| 416 | &PyST_Type, &self, &str); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 417 | else |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 418 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1], |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 419 | &str); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 420 | |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 421 | if (ok) { |
| 422 | arena = PyArena_New(); |
| 423 | if (arena) { |
| 424 | mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena); |
| 425 | if (mod) { |
| 426 | res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena); |
| 427 | } |
| 428 | PyArena_Free(arena); |
| 429 | } |
| 430 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 431 | |
| 432 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 433 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 434 | |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 435 | static PyObject* |
| 436 | parser_compileast(PyST_Object *self, PyObject *args, PyObject *kw) |
| 437 | { |
| 438 | if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0) |
| 439 | return NULL; |
| 440 | return parser_compilest(self, args, kw); |
| 441 | } |
| 442 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 443 | |
| 444 | /* PyObject* parser_isexpr(PyObject* self, PyObject* args) |
| 445 | * PyObject* parser_issuite(PyObject* self, PyObject* args) |
| 446 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 447 | * Checks the passed-in ST object to determine if it is an expression or |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 448 | * a statement suite, respectively. The return is a Python truth value. |
| 449 | * |
| 450 | */ |
| 451 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 452 | parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 453 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 454 | PyObject* res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 455 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 456 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 457 | static char *keywords[] = {"ast", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 458 | |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 459 | if (self == NULL) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 460 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 461 | &PyST_Type, &self); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 462 | else |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 463 | ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 464 | |
| 465 | if (ok) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 466 | /* Check to see if the ST represents an expression or not. */ |
| 467 | res = (self->st_type == PyST_EXPR) ? Py_True : Py_False; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 468 | Py_INCREF(res); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 469 | } |
| 470 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 471 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 472 | |
| 473 | |
| 474 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 475 | parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 476 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 477 | PyObject* res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 478 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 479 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 480 | static char *keywords[] = {"ast", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 481 | |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 482 | if (self == NULL) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 483 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 484 | &PyST_Type, &self); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 485 | else |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 486 | ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 487 | |
| 488 | if (ok) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 489 | /* Check to see if the ST represents an expression or not. */ |
| 490 | res = (self->st_type == PyST_EXPR) ? Py_False : Py_True; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 491 | Py_INCREF(res); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 492 | } |
| 493 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 494 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 495 | |
| 496 | |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 497 | #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS) |
| 498 | |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 499 | static PyMethodDef |
| 500 | parser_methods[] = { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 501 | {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 502 | PyDoc_STR("Compile this ST object into a code object.")}, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 503 | {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 504 | PyDoc_STR("Determines if this ST object was created from an expression.")}, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 505 | {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 506 | PyDoc_STR("Determines if this ST object was created from a suite.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 507 | {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 508 | PyDoc_STR("Creates a list-tree representation of this ST.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 509 | {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 510 | PyDoc_STR("Creates a tuple-tree representation of this ST.")}, |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 511 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 512 | {NULL, NULL, 0, NULL} |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 513 | }; |
| 514 | |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 515 | |
| 516 | static PyObject* |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 517 | parser_getattr(PyObject *self, char *name) |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 518 | { |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 519 | return (Py_FindMethod(parser_methods, self, name)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 520 | } |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 521 | |
| 522 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 523 | /* err_string(char* message) |
| 524 | * |
| 525 | * Sets the error string for an exception of type ParserError. |
| 526 | * |
| 527 | */ |
| 528 | static void |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 529 | err_string(char *message) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 530 | { |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 531 | PyErr_SetString(parser_error, message); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 532 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 533 | |
| 534 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 535 | /* PyObject* parser_do_parse(PyObject* args, int type) |
| 536 | * |
| 537 | * Internal function to actually execute the parse and return the result if |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 538 | * successful or set an exception if not. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 539 | * |
| 540 | */ |
| 541 | static PyObject* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 542 | parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 543 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 544 | char* string = 0; |
| 545 | PyObject* res = 0; |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 546 | int flags = 0; |
| 547 | perrdetail err; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 548 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 549 | static char *keywords[] = {"source", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 550 | |
| 551 | if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) { |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 552 | node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL, |
| 553 | &_PyParser_Grammar, |
| 554 | (type == PyST_EXPR) |
| 555 | ? eval_input : file_input, |
| 556 | &err, &flags); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 558 | if (n) { |
| 559 | res = parser_newstobject(n, type); |
Benjamin Peterson | dcee09d | 2008-10-31 02:16:05 +0000 | [diff] [blame] | 560 | if (res) |
| 561 | ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK; |
| 562 | } |
| 563 | else |
| 564 | PyParser_SetError(&err); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 565 | } |
| 566 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 567 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 568 | |
| 569 | |
| 570 | /* PyObject* parser_expr(PyObject* self, PyObject* args) |
| 571 | * PyObject* parser_suite(PyObject* self, PyObject* args) |
| 572 | * |
| 573 | * External interfaces to the parser itself. Which is called determines if |
| 574 | * the parser attempts to recognize an expression ('eval' form) or statement |
| 575 | * suite ('exec' form). The real work is done by parser_do_parse() above. |
| 576 | * |
| 577 | */ |
| 578 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 579 | parser_expr(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 580 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 581 | NOTE(ARGUNUSED(self)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 582 | return (parser_do_parse(args, kw, "s:expr", PyST_EXPR)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 583 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 584 | |
| 585 | |
| 586 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 587 | parser_suite(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 588 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 589 | NOTE(ARGUNUSED(self)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 590 | return (parser_do_parse(args, kw, "s:suite", PyST_SUITE)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 591 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 592 | |
| 593 | |
| 594 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 595 | /* This is the messy part of the code. Conversion from a tuple to an ST |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 596 | * object requires that the input tuple be valid without having to rely on |
| 597 | * catching an exception from the compiler. This is done to allow the |
| 598 | * compiler itself to remain fast, since most of its input will come from |
| 599 | * the parser directly, and therefore be known to be syntactically correct. |
| 600 | * This validation is done to ensure that we don't core dump the compile |
| 601 | * phase, returning an exception instead. |
| 602 | * |
| 603 | * Two aspects can be broken out in this code: creating a node tree from |
| 604 | * the tuple passed in, and verifying that it is indeed valid. It may be |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 605 | * advantageous to expand the number of ST types to include funcdefs and |
| 606 | * lambdadefs to take advantage of the optimizer, recognizing those STs |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 607 | * here. They are not necessary, and not quite as useful in a raw form. |
| 608 | * For now, let's get expressions and suites working reliably. |
| 609 | */ |
| 610 | |
| 611 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 612 | static node* build_node_tree(PyObject *tuple); |
| 613 | static int validate_expr_tree(node *tree); |
| 614 | static int validate_file_input(node *tree); |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 615 | static int validate_encoding_decl(node *tree); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 616 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 617 | /* PyObject* parser_tuple2st(PyObject* self, PyObject* args) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 618 | * |
| 619 | * This is the public function, called from the Python code. It receives a |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 620 | * single tuple object from the caller, and creates an ST object if the |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 621 | * tuple can be validated. It does this by checking the first code of the |
| 622 | * tuple, and, if acceptable, builds the internal representation. If this |
| 623 | * step succeeds, the internal representation is validated as fully as |
| 624 | * possible with the various validate_*() routines defined below. |
| 625 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 626 | * This function must be changed if support is to be added for PyST_FRAGMENT |
| 627 | * ST objects. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 628 | * |
| 629 | */ |
| 630 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 631 | parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 632 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 633 | NOTE(ARGUNUSED(self)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 634 | PyObject *st = 0; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 635 | PyObject *tuple; |
| 636 | node *tree; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 637 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 638 | static char *keywords[] = {"sequence", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 639 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 640 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords, |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 641 | &tuple)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 642 | return (0); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 643 | if (!PySequence_Check(tuple)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 644 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 645 | "sequence2st() requires a single sequence argument"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 646 | return (0); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 647 | } |
| 648 | /* |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 649 | * Convert the tree to the internal form before checking it. |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 650 | */ |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 651 | tree = build_node_tree(tuple); |
| 652 | if (tree != 0) { |
| 653 | int start_sym = TYPE(tree); |
| 654 | if (start_sym == eval_input) { |
| 655 | /* Might be an eval form. */ |
| 656 | if (validate_expr_tree(tree)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 657 | st = parser_newstobject(tree, PyST_EXPR); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 658 | else |
| 659 | PyNode_Free(tree); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 660 | } |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 661 | else if (start_sym == file_input) { |
| 662 | /* This looks like an exec form so far. */ |
| 663 | if (validate_file_input(tree)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 664 | st = parser_newstobject(tree, PyST_SUITE); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 665 | else |
| 666 | PyNode_Free(tree); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 667 | } |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 668 | else if (start_sym == encoding_decl) { |
| 669 | /* This looks like an encoding_decl so far. */ |
| 670 | if (validate_encoding_decl(tree)) |
| 671 | st = parser_newstobject(tree, PyST_SUITE); |
| 672 | else |
| 673 | PyNode_Free(tree); |
| 674 | } |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 675 | else { |
| 676 | /* This is a fragment, at best. */ |
| 677 | PyNode_Free(tree); |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 678 | err_string("parse tree does not use a valid start symbol"); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 679 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 680 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 681 | /* Make sure we throw an exception on all errors. We should never |
| 682 | * get this, but we'd do well to be sure something is done. |
| 683 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 684 | if (st == NULL && !PyErr_Occurred()) |
| 685 | err_string("unspecified ST error occurred"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 686 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 687 | return st; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 688 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 689 | |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 690 | static PyObject* |
| 691 | parser_tuple2ast(PyST_Object *self, PyObject *args, PyObject *kw) |
| 692 | { |
| 693 | if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0) |
| 694 | return NULL; |
| 695 | return parser_tuple2st(self, args, kw); |
| 696 | } |
| 697 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 699 | /* node* build_node_children() |
| 700 | * |
| 701 | * Iterate across the children of the current non-terminal node and build |
| 702 | * their structures. If successful, return the root of this portion of |
| 703 | * the tree, otherwise, 0. Any required exception will be specified already, |
| 704 | * and no memory will have been deallocated. |
| 705 | * |
| 706 | */ |
| 707 | static node* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 708 | build_node_children(PyObject *tuple, node *root, int *line_num) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 709 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 710 | Py_ssize_t len = PyObject_Size(tuple); |
| 711 | Py_ssize_t i; |
| 712 | int err; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 713 | |
| 714 | for (i = 1; i < len; ++i) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 715 | /* elem must always be a sequence, however simple */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 716 | PyObject* elem = PySequence_GetItem(tuple, i); |
| 717 | int ok = elem != NULL; |
| 718 | long type = 0; |
| 719 | char *strn = 0; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 720 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 721 | if (ok) |
| 722 | ok = PySequence_Check(elem); |
| 723 | if (ok) { |
| 724 | PyObject *temp = PySequence_GetItem(elem, 0); |
| 725 | if (temp == NULL) |
| 726 | ok = 0; |
| 727 | else { |
| 728 | ok = PyInt_Check(temp); |
| 729 | if (ok) |
| 730 | type = PyInt_AS_LONG(temp); |
| 731 | Py_DECREF(temp); |
| 732 | } |
| 733 | } |
| 734 | if (!ok) { |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 735 | PyObject *err = Py_BuildValue("os", elem, |
| 736 | "Illegal node construct."); |
| 737 | PyErr_SetObject(parser_error, err); |
| 738 | Py_XDECREF(err); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 739 | Py_XDECREF(elem); |
| 740 | return (0); |
| 741 | } |
| 742 | if (ISTERMINAL(type)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 743 | Py_ssize_t len = PyObject_Size(elem); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 744 | PyObject *temp; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 745 | |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 746 | if ((len != 2) && (len != 3)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 747 | err_string("terminal nodes must have 2 or 3 entries"); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | temp = PySequence_GetItem(elem, 1); |
| 751 | if (temp == NULL) |
| 752 | return 0; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 753 | if (!PyString_Check(temp)) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 754 | PyErr_Format(parser_error, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 755 | "second item in terminal node must be a string," |
| 756 | " found %s", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 757 | Py_TYPE(temp)->tp_name); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 758 | Py_DECREF(temp); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | if (len == 3) { |
| 762 | PyObject *o = PySequence_GetItem(elem, 2); |
| 763 | if (o != NULL) { |
| 764 | if (PyInt_Check(o)) |
| 765 | *line_num = PyInt_AS_LONG(o); |
| 766 | else { |
| 767 | PyErr_Format(parser_error, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 768 | "third item in terminal node must be an" |
| 769 | " integer, found %s", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 770 | Py_TYPE(temp)->tp_name); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 771 | Py_DECREF(o); |
| 772 | Py_DECREF(temp); |
| 773 | return 0; |
| 774 | } |
| 775 | Py_DECREF(o); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 778 | len = PyString_GET_SIZE(temp) + 1; |
Tim Peters | c9d78aa | 2006-03-26 23:27:58 +0000 | [diff] [blame] | 779 | strn = (char *)PyObject_MALLOC(len); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 780 | if (strn != NULL) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 781 | (void) memcpy(strn, PyString_AS_STRING(temp), len); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 782 | Py_DECREF(temp); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 783 | } |
| 784 | else if (!ISNONTERMINAL(type)) { |
| 785 | /* |
| 786 | * It has to be one or the other; this is an error. |
| 787 | * Throw an exception. |
| 788 | */ |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 789 | PyObject *err = Py_BuildValue("os", elem, "unknown node type."); |
| 790 | PyErr_SetObject(parser_error, err); |
| 791 | Py_XDECREF(err); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 792 | Py_XDECREF(elem); |
| 793 | return (0); |
| 794 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 795 | err = PyNode_AddChild(root, type, strn, *line_num, 0); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 796 | if (err == E_NOMEM) { |
Tim Peters | c9d78aa | 2006-03-26 23:27:58 +0000 | [diff] [blame] | 797 | PyObject_FREE(strn); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 798 | return (node *) PyErr_NoMemory(); |
| 799 | } |
| 800 | if (err == E_OVERFLOW) { |
Tim Peters | c9d78aa | 2006-03-26 23:27:58 +0000 | [diff] [blame] | 801 | PyObject_FREE(strn); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 802 | PyErr_SetString(PyExc_ValueError, |
| 803 | "unsupported number of child nodes"); |
| 804 | return NULL; |
| 805 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 806 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 807 | if (ISNONTERMINAL(type)) { |
| 808 | node* new_child = CHILD(root, i - 1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 809 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 810 | if (new_child != build_node_children(elem, new_child, line_num)) { |
| 811 | Py_XDECREF(elem); |
| 812 | return (0); |
| 813 | } |
| 814 | } |
| 815 | else if (type == NEWLINE) { /* It's true: we increment the */ |
| 816 | ++(*line_num); /* line number *after* the newline! */ |
| 817 | } |
| 818 | Py_XDECREF(elem); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 819 | } |
Tim Peters | c9d78aa | 2006-03-26 23:27:58 +0000 | [diff] [blame] | 820 | return root; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 821 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 822 | |
| 823 | |
| 824 | static node* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 825 | build_node_tree(PyObject *tuple) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 826 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 827 | node* res = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 828 | PyObject *temp = PySequence_GetItem(tuple, 0); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 829 | long num = -1; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 831 | if (temp != NULL) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 832 | num = PyInt_AsLong(temp); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 833 | Py_XDECREF(temp); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 834 | if (ISTERMINAL(num)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 835 | /* |
| 836 | * The tuple is simple, but it doesn't start with a start symbol. |
| 837 | * Throw an exception now and be done with it. |
| 838 | */ |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 839 | tuple = Py_BuildValue("os", tuple, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 840 | "Illegal syntax-tree; cannot start with terminal symbol."); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 841 | PyErr_SetObject(parser_error, tuple); |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 842 | Py_XDECREF(tuple); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 843 | } |
| 844 | else if (ISNONTERMINAL(num)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 845 | /* |
| 846 | * Not efficient, but that can be handled later. |
| 847 | */ |
| 848 | int line_num = 0; |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 849 | PyObject *encoding = NULL; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 850 | |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 851 | if (num == encoding_decl) { |
| 852 | encoding = PySequence_GetItem(tuple, 2); |
| 853 | /* tuple isn't borrowed anymore here, need to DECREF */ |
| 854 | tuple = PySequence_GetSlice(tuple, 0, 2); |
| 855 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 856 | res = PyNode_New(num); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 857 | if (res != NULL) { |
| 858 | if (res != build_node_children(tuple, res, &line_num)) { |
| 859 | PyNode_Free(res); |
| 860 | res = NULL; |
| 861 | } |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 862 | if (res && encoding) { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 863 | Py_ssize_t len; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 864 | len = PyString_GET_SIZE(encoding) + 1; |
Tim Peters | c9d78aa | 2006-03-26 23:27:58 +0000 | [diff] [blame] | 865 | res->n_str = (char *)PyObject_MALLOC(len); |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 866 | if (res->n_str != NULL) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 867 | (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len); |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 868 | Py_DECREF(encoding); |
| 869 | Py_DECREF(tuple); |
| 870 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 871 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 872 | } |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 873 | else { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 874 | /* The tuple is illegal -- if the number is neither TERMINAL nor |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 875 | * NONTERMINAL, we can't use it. Not sure the implementation |
| 876 | * allows this condition, but the API doesn't preclude it. |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 877 | */ |
Neal Norwitz | d1e0ef6 | 2006-03-20 04:08:12 +0000 | [diff] [blame] | 878 | PyObject *err = Py_BuildValue("os", tuple, |
| 879 | "Illegal component tuple."); |
| 880 | PyErr_SetObject(parser_error, err); |
| 881 | Py_XDECREF(err); |
| 882 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 883 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 884 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 885 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 886 | |
| 887 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 888 | /* |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 889 | * Validation routines used within the validation section: |
| 890 | */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 891 | static int validate_terminal(node *terminal, int type, char *string); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 892 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 893 | #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&") |
| 894 | #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^") |
| 895 | #define validate_colon(ch) validate_terminal(ch, COLON, ":") |
| 896 | #define validate_comma(ch) validate_terminal(ch, COMMA, ",") |
| 897 | #define validate_dedent(ch) validate_terminal(ch, DEDENT, "") |
| 898 | #define validate_equal(ch) validate_terminal(ch, EQUAL, "=") |
| 899 | #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL) |
| 900 | #define validate_lparen(ch) validate_terminal(ch, LPAR, "(") |
| 901 | #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL) |
| 902 | #define validate_rparen(ch) validate_terminal(ch, RPAR, ")") |
| 903 | #define validate_semi(ch) validate_terminal(ch, SEMI, ";") |
| 904 | #define validate_star(ch) validate_terminal(ch, STAR, "*") |
| 905 | #define validate_vbar(ch) validate_terminal(ch, VBAR, "|") |
| 906 | #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**") |
| 907 | #define validate_dot(ch) validate_terminal(ch, DOT, ".") |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 908 | #define validate_at(ch) validate_terminal(ch, AT, "@") |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 909 | #define validate_name(ch, str) validate_terminal(ch, NAME, str) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 910 | |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 911 | #define VALIDATER(n) static int validate_##n(node *tree) |
| 912 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 913 | VALIDATER(node); VALIDATER(small_stmt); |
| 914 | VALIDATER(class); VALIDATER(node); |
| 915 | VALIDATER(parameters); VALIDATER(suite); |
| 916 | VALIDATER(testlist); VALIDATER(varargslist); |
| 917 | VALIDATER(fpdef); VALIDATER(fplist); |
| 918 | VALIDATER(stmt); VALIDATER(simple_stmt); |
| 919 | VALIDATER(expr_stmt); VALIDATER(power); |
| 920 | VALIDATER(print_stmt); VALIDATER(del_stmt); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 921 | VALIDATER(return_stmt); VALIDATER(list_iter); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 922 | VALIDATER(raise_stmt); VALIDATER(import_stmt); |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 923 | VALIDATER(import_name); VALIDATER(import_from); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 924 | VALIDATER(global_stmt); VALIDATER(list_if); |
| 925 | VALIDATER(assert_stmt); VALIDATER(list_for); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 926 | VALIDATER(exec_stmt); VALIDATER(compound_stmt); |
| 927 | VALIDATER(while); VALIDATER(for); |
| 928 | VALIDATER(try); VALIDATER(except_clause); |
| 929 | VALIDATER(test); VALIDATER(and_test); |
| 930 | VALIDATER(not_test); VALIDATER(comparison); |
| 931 | VALIDATER(comp_op); VALIDATER(expr); |
| 932 | VALIDATER(xor_expr); VALIDATER(and_expr); |
| 933 | VALIDATER(shift_expr); VALIDATER(arith_expr); |
| 934 | VALIDATER(term); VALIDATER(factor); |
| 935 | VALIDATER(atom); VALIDATER(lambdef); |
| 936 | VALIDATER(trailer); VALIDATER(subscript); |
| 937 | VALIDATER(subscriptlist); VALIDATER(sliceop); |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 938 | VALIDATER(exprlist); VALIDATER(dictorsetmaker); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 939 | VALIDATER(arglist); VALIDATER(argument); |
Fred Drake | 02126f2 | 2001-07-17 02:59:15 +0000 | [diff] [blame] | 940 | VALIDATER(listmaker); VALIDATER(yield_stmt); |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 941 | VALIDATER(testlist1); VALIDATER(comp_for); |
| 942 | VALIDATER(comp_iter); VALIDATER(comp_if); |
| 943 | VALIDATER(testlist_comp); VALIDATER(yield_expr); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 944 | VALIDATER(yield_or_testlist); VALIDATER(or_test); |
| 945 | VALIDATER(old_test); VALIDATER(old_lambdef); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 946 | |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 947 | #undef VALIDATER |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 948 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 949 | #define is_even(n) (((n) & 1) == 0) |
| 950 | #define is_odd(n) (((n) & 1) == 1) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 951 | |
| 952 | |
| 953 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 954 | validate_ntype(node *n, int t) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 955 | { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 956 | if (TYPE(n) != t) { |
| 957 | PyErr_Format(parser_error, "Expected node type %d, got %d.", |
| 958 | t, TYPE(n)); |
| 959 | return 0; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 960 | } |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 961 | return 1; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 962 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 963 | |
| 964 | |
Fred Drake | e7ab64e | 2000-04-25 04:14:46 +0000 | [diff] [blame] | 965 | /* Verifies that the number of child nodes is exactly 'num', raising |
| 966 | * an exception if it isn't. The exception message does not indicate |
| 967 | * the exact number of nodes, allowing this to be used to raise the |
| 968 | * "right" exception when the wrong number of nodes is present in a |
| 969 | * specific variant of a statement's syntax. This is commonly used |
| 970 | * in that fashion. |
| 971 | */ |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 972 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 973 | validate_numnodes(node *n, int num, const char *const name) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 974 | { |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 975 | if (NCH(n) != num) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 976 | PyErr_Format(parser_error, |
| 977 | "Illegal number of children for %s node.", name); |
| 978 | return 0; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 979 | } |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 980 | return 1; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 981 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 982 | |
| 983 | |
| 984 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 985 | validate_terminal(node *terminal, int type, char *string) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 986 | { |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 987 | int res = (validate_ntype(terminal, type) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 988 | && ((string == 0) || (strcmp(string, STR(terminal)) == 0))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 989 | |
| 990 | if (!res && !PyErr_Occurred()) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 991 | PyErr_Format(parser_error, |
| 992 | "Illegal terminal: expected \"%s\"", string); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 993 | } |
| 994 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 995 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 996 | |
| 997 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 998 | /* X (',' X) [','] |
| 999 | */ |
| 1000 | static int |
Thomas Wouters | bd4bc4e | 2000-07-22 23:57:55 +0000 | [diff] [blame] | 1001 | validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *), |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1002 | const char *const name) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1003 | { |
| 1004 | int nch = NCH(tree); |
| 1005 | int res = (nch && validate_ntype(tree, ntype) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1006 | && vfunc(CHILD(tree, 0))); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1007 | |
| 1008 | if (!res && !PyErr_Occurred()) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1009 | (void) validate_numnodes(tree, 1, name); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1010 | else { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1011 | if (is_even(nch)) |
| 1012 | res = validate_comma(CHILD(tree, --nch)); |
| 1013 | if (res && nch > 1) { |
| 1014 | int pos = 1; |
| 1015 | for ( ; res && pos < nch; pos += 2) |
| 1016 | res = (validate_comma(CHILD(tree, pos)) |
| 1017 | && vfunc(CHILD(tree, pos + 1))); |
| 1018 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1019 | } |
| 1020 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1021 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1022 | |
| 1023 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1024 | /* validate_class() |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1025 | * |
| 1026 | * classdef: |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1027 | * 'class' NAME ['(' testlist ')'] ':' suite |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1028 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1029 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1030 | validate_class(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1031 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1032 | int nch = NCH(tree); |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 1033 | int res = (validate_ntype(tree, classdef) && |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1034 | ((nch == 4) || (nch == 6) || (nch == 7))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1035 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1036 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1037 | res = (validate_name(CHILD(tree, 0), "class") |
| 1038 | && validate_ntype(CHILD(tree, 1), NAME) |
| 1039 | && validate_colon(CHILD(tree, nch - 2)) |
| 1040 | && validate_suite(CHILD(tree, nch - 1))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1041 | } |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 1042 | else { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1043 | (void) validate_numnodes(tree, 4, "class"); |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 1044 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1045 | |
Brett Cannon | f418991 | 2005-04-09 02:30:16 +0000 | [diff] [blame] | 1046 | if (res) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1047 | if (nch == 7) { |
| 1048 | res = ((validate_lparen(CHILD(tree, 2)) && |
| 1049 | validate_testlist(CHILD(tree, 3)) && |
| 1050 | validate_rparen(CHILD(tree, 4)))); |
| 1051 | } |
| 1052 | else if (nch == 6) { |
| 1053 | res = (validate_lparen(CHILD(tree,2)) && |
| 1054 | validate_rparen(CHILD(tree,3))); |
| 1055 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1056 | } |
| 1057 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1058 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1059 | |
| 1060 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1061 | /* if_stmt: |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1062 | * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1063 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1064 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1065 | validate_if(node *tree) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1066 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1067 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1068 | int res = (validate_ntype(tree, if_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1069 | && (nch >= 4) |
| 1070 | && validate_name(CHILD(tree, 0), "if") |
| 1071 | && validate_test(CHILD(tree, 1)) |
| 1072 | && validate_colon(CHILD(tree, 2)) |
| 1073 | && validate_suite(CHILD(tree, 3))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (res && ((nch % 4) == 3)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1076 | /* ... 'else' ':' suite */ |
| 1077 | res = (validate_name(CHILD(tree, nch - 3), "else") |
| 1078 | && validate_colon(CHILD(tree, nch - 2)) |
| 1079 | && validate_suite(CHILD(tree, nch - 1))); |
| 1080 | nch -= 3; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1081 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1082 | else if (!res && !PyErr_Occurred()) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1083 | (void) validate_numnodes(tree, 4, "if"); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1084 | if ((nch % 4) != 0) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1085 | /* Will catch the case for nch < 4 */ |
| 1086 | res = validate_numnodes(tree, 0, "if"); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1087 | else if (res && (nch > 4)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1088 | /* ... ('elif' test ':' suite)+ ... */ |
| 1089 | int j = 4; |
| 1090 | while ((j < nch) && res) { |
| 1091 | res = (validate_name(CHILD(tree, j), "elif") |
| 1092 | && validate_colon(CHILD(tree, j + 2)) |
| 1093 | && validate_test(CHILD(tree, j + 1)) |
| 1094 | && validate_suite(CHILD(tree, j + 3))); |
| 1095 | j += 4; |
| 1096 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1097 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1098 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1099 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1100 | |
| 1101 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1102 | /* parameters: |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1103 | * '(' [varargslist] ')' |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1104 | * |
| 1105 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1106 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1107 | validate_parameters(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1108 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1109 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1110 | int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1112 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1113 | res = (validate_lparen(CHILD(tree, 0)) |
| 1114 | && validate_rparen(CHILD(tree, nch - 1))); |
| 1115 | if (res && (nch == 3)) |
| 1116 | res = validate_varargslist(CHILD(tree, 1)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1117 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1118 | else { |
| 1119 | (void) validate_numnodes(tree, 2, "parameters"); |
| 1120 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1121 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1122 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1123 | |
| 1124 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1125 | /* validate_suite() |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1126 | * |
| 1127 | * suite: |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1128 | * simple_stmt |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1129 | * | NEWLINE INDENT stmt+ DEDENT |
| 1130 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1131 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1132 | validate_suite(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1133 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1134 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1135 | int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1136 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1137 | if (res && (nch == 1)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1138 | res = validate_simple_stmt(CHILD(tree, 0)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1139 | else if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1140 | /* NEWLINE INDENT stmt+ DEDENT */ |
| 1141 | res = (validate_newline(CHILD(tree, 0)) |
| 1142 | && validate_indent(CHILD(tree, 1)) |
| 1143 | && validate_stmt(CHILD(tree, 2)) |
| 1144 | && validate_dedent(CHILD(tree, nch - 1))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1145 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1146 | if (res && (nch > 4)) { |
| 1147 | int i = 3; |
| 1148 | --nch; /* forget the DEDENT */ |
| 1149 | for ( ; res && (i < nch); ++i) |
| 1150 | res = validate_stmt(CHILD(tree, i)); |
| 1151 | } |
| 1152 | else if (nch < 4) |
| 1153 | res = validate_numnodes(tree, 4, "suite"); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1154 | } |
| 1155 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1156 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1157 | |
| 1158 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1159 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1160 | validate_testlist(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1161 | { |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1162 | return (validate_repeating_list(tree, testlist, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1163 | validate_test, "testlist")); |
| 1164 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1165 | |
| 1166 | |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 1167 | static int |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 1168 | validate_testlist1(node *tree) |
| 1169 | { |
| 1170 | return (validate_repeating_list(tree, testlist1, |
| 1171 | validate_test, "testlist1")); |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | static int |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 1176 | validate_testlist_safe(node *tree) |
| 1177 | { |
| 1178 | return (validate_repeating_list(tree, testlist_safe, |
Thomas Wouters | e2dd78c | 2006-02-27 16:25:11 +0000 | [diff] [blame] | 1179 | validate_old_test, "testlist_safe")); |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1183 | /* '*' NAME [',' '**' NAME] | '**' NAME |
| 1184 | */ |
| 1185 | static int |
| 1186 | validate_varargslist_trailer(node *tree, int start) |
| 1187 | { |
| 1188 | int nch = NCH(tree); |
| 1189 | int res = 0; |
| 1190 | int sym; |
| 1191 | |
| 1192 | if (nch <= start) { |
| 1193 | err_string("expected variable argument trailer for varargslist"); |
| 1194 | return 0; |
| 1195 | } |
| 1196 | sym = TYPE(CHILD(tree, start)); |
| 1197 | if (sym == STAR) { |
| 1198 | /* |
| 1199 | * ('*' NAME [',' '**' NAME] |
| 1200 | */ |
| 1201 | if (nch-start == 2) |
| 1202 | res = validate_name(CHILD(tree, start+1), NULL); |
| 1203 | else if (nch-start == 5) |
| 1204 | res = (validate_name(CHILD(tree, start+1), NULL) |
| 1205 | && validate_comma(CHILD(tree, start+2)) |
| 1206 | && validate_doublestar(CHILD(tree, start+3)) |
| 1207 | && validate_name(CHILD(tree, start+4), NULL)); |
| 1208 | } |
| 1209 | else if (sym == DOUBLESTAR) { |
| 1210 | /* |
| 1211 | * '**' NAME |
| 1212 | */ |
| 1213 | if (nch-start == 2) |
| 1214 | res = validate_name(CHILD(tree, start+1), NULL); |
| 1215 | } |
| 1216 | if (!res) |
| 1217 | err_string("illegal variable argument trailer for varargslist"); |
| 1218 | return res; |
| 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | /* validate_varargslist() |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1223 | * |
| 1224 | * varargslist: |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1225 | * (fpdef ['=' test] ',')* |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1226 | * ('*' NAME [',' '**' NAME] |
| 1227 | * | '**' NAME) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1228 | * | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 1229 | * |
| 1230 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1231 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1232 | validate_varargslist(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1233 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1234 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1235 | int res = validate_ntype(tree, varargslist) && (nch != 0); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1236 | int sym; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1237 | |
Fred Drake | b6429a2 | 2000-12-11 22:08:27 +0000 | [diff] [blame] | 1238 | if (!res) |
| 1239 | return 0; |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1240 | if (nch < 1) { |
| 1241 | err_string("varargslist missing child nodes"); |
| 1242 | return 0; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1243 | } |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1244 | sym = TYPE(CHILD(tree, 0)); |
| 1245 | if (sym == STAR || sym == DOUBLESTAR) |
Fred Drake | b6429a2 | 2000-12-11 22:08:27 +0000 | [diff] [blame] | 1246 | /* whole thing matches: |
| 1247 | * '*' NAME [',' '**' NAME] | '**' NAME |
| 1248 | */ |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1249 | res = validate_varargslist_trailer(tree, 0); |
| 1250 | else if (sym == fpdef) { |
| 1251 | int i = 0; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1252 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1253 | sym = TYPE(CHILD(tree, nch-1)); |
| 1254 | if (sym == NAME) { |
| 1255 | /* |
| 1256 | * (fpdef ['=' test] ',')+ |
| 1257 | * ('*' NAME [',' '**' NAME] |
| 1258 | * | '**' NAME) |
| 1259 | */ |
| 1260 | /* skip over (fpdef ['=' test] ',')+ */ |
| 1261 | while (res && (i+2 <= nch)) { |
| 1262 | res = validate_fpdef(CHILD(tree, i)); |
| 1263 | ++i; |
| 1264 | if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) { |
| 1265 | res = (validate_equal(CHILD(tree, i)) |
| 1266 | && validate_test(CHILD(tree, i+1))); |
| 1267 | if (res) |
| 1268 | i += 2; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1269 | } |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1270 | if (res && i < nch) { |
| 1271 | res = validate_comma(CHILD(tree, i)); |
Fred Drake | b6429a2 | 2000-12-11 22:08:27 +0000 | [diff] [blame] | 1272 | ++i; |
| 1273 | if (res && i < nch |
| 1274 | && (TYPE(CHILD(tree, i)) == DOUBLESTAR |
| 1275 | || TYPE(CHILD(tree, i)) == STAR)) |
| 1276 | break; |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
Fred Drake | b6429a2 | 2000-12-11 22:08:27 +0000 | [diff] [blame] | 1279 | /* ... '*' NAME [',' '**' NAME] | '**' NAME |
| 1280 | * i --^^^ |
| 1281 | */ |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1282 | if (res) |
| 1283 | res = validate_varargslist_trailer(tree, i); |
| 1284 | } |
| 1285 | else { |
| 1286 | /* |
| 1287 | * fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 1288 | */ |
Fred Drake | b6429a2 | 2000-12-11 22:08:27 +0000 | [diff] [blame] | 1289 | /* strip trailing comma node */ |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1290 | if (sym == COMMA) { |
| 1291 | res = validate_comma(CHILD(tree, nch-1)); |
| 1292 | if (!res) |
| 1293 | return 0; |
| 1294 | --nch; |
| 1295 | } |
| 1296 | /* |
| 1297 | * fpdef ['=' test] (',' fpdef ['=' test])* |
| 1298 | */ |
| 1299 | res = validate_fpdef(CHILD(tree, 0)); |
| 1300 | ++i; |
Fred Drake | b6429a2 | 2000-12-11 22:08:27 +0000 | [diff] [blame] | 1301 | if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) { |
| 1302 | res = (validate_equal(CHILD(tree, i)) |
| 1303 | && validate_test(CHILD(tree, i+1))); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1304 | i += 2; |
| 1305 | } |
| 1306 | /* |
| 1307 | * ... (',' fpdef ['=' test])* |
| 1308 | * i ---^^^ |
| 1309 | */ |
| 1310 | while (res && (nch - i) >= 2) { |
| 1311 | res = (validate_comma(CHILD(tree, i)) |
| 1312 | && validate_fpdef(CHILD(tree, i+1))); |
| 1313 | i += 2; |
Fred Drake | b6429a2 | 2000-12-11 22:08:27 +0000 | [diff] [blame] | 1314 | if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) { |
| 1315 | res = (validate_equal(CHILD(tree, i)) |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1316 | && validate_test(CHILD(tree, i+1))); |
Fred Drake | b6429a2 | 2000-12-11 22:08:27 +0000 | [diff] [blame] | 1317 | i += 2; |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | if (res && nch - i != 0) { |
| 1321 | res = 0; |
| 1322 | err_string("illegal formation for varargslist"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1323 | } |
| 1324 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1325 | } |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1326 | return res; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1327 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1328 | |
| 1329 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1330 | /* list_iter: list_for | list_if |
| 1331 | */ |
| 1332 | static int |
| 1333 | validate_list_iter(node *tree) |
| 1334 | { |
| 1335 | int res = (validate_ntype(tree, list_iter) |
| 1336 | && validate_numnodes(tree, 1, "list_iter")); |
| 1337 | if (res && TYPE(CHILD(tree, 0)) == list_for) |
| 1338 | res = validate_list_for(CHILD(tree, 0)); |
| 1339 | else |
| 1340 | res = validate_list_if(CHILD(tree, 0)); |
| 1341 | |
| 1342 | return res; |
| 1343 | } |
| 1344 | |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1345 | /* comp_iter: comp_for | comp_if |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1346 | */ |
| 1347 | static int |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1348 | validate_comp_iter(node *tree) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1349 | { |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1350 | int res = (validate_ntype(tree, comp_iter) |
| 1351 | && validate_numnodes(tree, 1, "comp_iter")); |
| 1352 | if (res && TYPE(CHILD(tree, 0)) == comp_for) |
| 1353 | res = validate_comp_for(CHILD(tree, 0)); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1354 | else |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1355 | res = validate_comp_if(CHILD(tree, 0)); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1356 | |
| 1357 | return res; |
| 1358 | } |
| 1359 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1360 | /* list_for: 'for' exprlist 'in' testlist [list_iter] |
| 1361 | */ |
| 1362 | static int |
| 1363 | validate_list_for(node *tree) |
| 1364 | { |
| 1365 | int nch = NCH(tree); |
| 1366 | int res; |
| 1367 | |
| 1368 | if (nch == 5) |
| 1369 | res = validate_list_iter(CHILD(tree, 4)); |
| 1370 | else |
| 1371 | res = validate_numnodes(tree, 4, "list_for"); |
| 1372 | |
| 1373 | if (res) |
| 1374 | res = (validate_name(CHILD(tree, 0), "for") |
| 1375 | && validate_exprlist(CHILD(tree, 1)) |
| 1376 | && validate_name(CHILD(tree, 2), "in") |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 1377 | && validate_testlist_safe(CHILD(tree, 3))); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1378 | |
| 1379 | return res; |
| 1380 | } |
| 1381 | |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1382 | /* comp_for: 'for' exprlist 'in' test [comp_iter] |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1383 | */ |
| 1384 | static int |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1385 | validate_comp_for(node *tree) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1386 | { |
| 1387 | int nch = NCH(tree); |
| 1388 | int res; |
| 1389 | |
| 1390 | if (nch == 5) |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1391 | res = validate_comp_iter(CHILD(tree, 4)); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1392 | else |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1393 | res = validate_numnodes(tree, 4, "comp_for"); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1394 | |
| 1395 | if (res) |
| 1396 | res = (validate_name(CHILD(tree, 0), "for") |
| 1397 | && validate_exprlist(CHILD(tree, 1)) |
| 1398 | && validate_name(CHILD(tree, 2), "in") |
Thomas Wouters | e2dd78c | 2006-02-27 16:25:11 +0000 | [diff] [blame] | 1399 | && validate_or_test(CHILD(tree, 3))); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1400 | |
| 1401 | return res; |
| 1402 | } |
| 1403 | |
Neal Norwitz | 4b194fa | 2006-04-12 05:24:39 +0000 | [diff] [blame] | 1404 | /* list_if: 'if' old_test [list_iter] |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1405 | */ |
| 1406 | static int |
| 1407 | validate_list_if(node *tree) |
| 1408 | { |
| 1409 | int nch = NCH(tree); |
| 1410 | int res; |
| 1411 | |
| 1412 | if (nch == 3) |
| 1413 | res = validate_list_iter(CHILD(tree, 2)); |
| 1414 | else |
| 1415 | res = validate_numnodes(tree, 2, "list_if"); |
| 1416 | |
| 1417 | if (res) |
| 1418 | res = (validate_name(CHILD(tree, 0), "if") |
Neal Norwitz | 4b194fa | 2006-04-12 05:24:39 +0000 | [diff] [blame] | 1419 | && validate_old_test(CHILD(tree, 1))); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1420 | |
| 1421 | return res; |
| 1422 | } |
| 1423 | |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1424 | /* comp_if: 'if' old_test [comp_iter] |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1425 | */ |
| 1426 | static int |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1427 | validate_comp_if(node *tree) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1428 | { |
| 1429 | int nch = NCH(tree); |
| 1430 | int res; |
| 1431 | |
| 1432 | if (nch == 3) |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1433 | res = validate_comp_iter(CHILD(tree, 2)); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1434 | else |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 1435 | res = validate_numnodes(tree, 2, "comp_if"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1436 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1437 | if (res) |
| 1438 | res = (validate_name(CHILD(tree, 0), "if") |
Neal Norwitz | 4b194fa | 2006-04-12 05:24:39 +0000 | [diff] [blame] | 1439 | && validate_old_test(CHILD(tree, 1))); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1440 | |
| 1441 | return res; |
| 1442 | } |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1443 | |
| 1444 | /* validate_fpdef() |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1445 | * |
| 1446 | * fpdef: |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1447 | * NAME |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1448 | * | '(' fplist ')' |
| 1449 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1450 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1451 | validate_fpdef(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1452 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1453 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1454 | int res = validate_ntype(tree, fpdef); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1455 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1456 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1457 | if (nch == 1) |
| 1458 | res = validate_ntype(CHILD(tree, 0), NAME); |
| 1459 | else if (nch == 3) |
| 1460 | res = (validate_lparen(CHILD(tree, 0)) |
| 1461 | && validate_fplist(CHILD(tree, 1)) |
| 1462 | && validate_rparen(CHILD(tree, 2))); |
| 1463 | else |
| 1464 | res = validate_numnodes(tree, 1, "fpdef"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1465 | } |
| 1466 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1467 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1468 | |
| 1469 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1470 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1471 | validate_fplist(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1472 | { |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1473 | return (validate_repeating_list(tree, fplist, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1474 | validate_fpdef, "fplist")); |
| 1475 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1476 | |
| 1477 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1478 | /* simple_stmt | compound_stmt |
| 1479 | * |
| 1480 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1481 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1482 | validate_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1483 | { |
| 1484 | int res = (validate_ntype(tree, stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1485 | && validate_numnodes(tree, 1, "stmt")); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1486 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1487 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1488 | tree = CHILD(tree, 0); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1489 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1490 | if (TYPE(tree) == simple_stmt) |
| 1491 | res = validate_simple_stmt(tree); |
| 1492 | else |
| 1493 | res = validate_compound_stmt(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1494 | } |
| 1495 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1496 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1497 | |
| 1498 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1499 | /* small_stmt (';' small_stmt)* [';'] NEWLINE |
| 1500 | * |
| 1501 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1502 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1503 | validate_simple_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1504 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1505 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1506 | int res = (validate_ntype(tree, simple_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1507 | && (nch >= 2) |
| 1508 | && validate_small_stmt(CHILD(tree, 0)) |
| 1509 | && validate_newline(CHILD(tree, nch - 1))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1510 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1511 | if (nch < 2) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1512 | res = validate_numnodes(tree, 2, "simple_stmt"); |
| 1513 | --nch; /* forget the NEWLINE */ |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1514 | if (res && is_even(nch)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1515 | res = validate_semi(CHILD(tree, --nch)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1516 | if (res && (nch > 2)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1517 | int i; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1518 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1519 | for (i = 1; res && (i < nch); i += 2) |
| 1520 | res = (validate_semi(CHILD(tree, i)) |
| 1521 | && validate_small_stmt(CHILD(tree, i + 1))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1522 | } |
| 1523 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1524 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1525 | |
| 1526 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1527 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1528 | validate_small_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1529 | { |
| 1530 | int nch = NCH(tree); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1531 | int res = validate_numnodes(tree, 1, "small_stmt"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1532 | |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1533 | if (res) { |
| 1534 | int ntype = TYPE(CHILD(tree, 0)); |
| 1535 | |
| 1536 | if ( (ntype == expr_stmt) |
| 1537 | || (ntype == print_stmt) |
| 1538 | || (ntype == del_stmt) |
| 1539 | || (ntype == pass_stmt) |
| 1540 | || (ntype == flow_stmt) |
| 1541 | || (ntype == import_stmt) |
| 1542 | || (ntype == global_stmt) |
| 1543 | || (ntype == assert_stmt) |
| 1544 | || (ntype == exec_stmt)) |
| 1545 | res = validate_node(CHILD(tree, 0)); |
| 1546 | else { |
| 1547 | res = 0; |
| 1548 | err_string("illegal small_stmt child type"); |
| 1549 | } |
| 1550 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1551 | else if (nch == 1) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1552 | res = 0; |
| 1553 | PyErr_Format(parser_error, |
| 1554 | "Unrecognized child node of small_stmt: %d.", |
| 1555 | TYPE(CHILD(tree, 0))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1556 | } |
| 1557 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1558 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1559 | |
| 1560 | |
| 1561 | /* compound_stmt: |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 1562 | * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1563 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1564 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1565 | validate_compound_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1566 | { |
| 1567 | int res = (validate_ntype(tree, compound_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1568 | && validate_numnodes(tree, 1, "compound_stmt")); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1569 | int ntype; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1570 | |
| 1571 | if (!res) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1572 | return (0); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1573 | |
| 1574 | tree = CHILD(tree, 0); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1575 | ntype = TYPE(tree); |
| 1576 | if ( (ntype == if_stmt) |
| 1577 | || (ntype == while_stmt) |
| 1578 | || (ntype == for_stmt) |
| 1579 | || (ntype == try_stmt) |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 1580 | || (ntype == with_stmt) |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1581 | || (ntype == funcdef) |
Christian Heimes | 5224d28 | 2008-02-23 15:01:05 +0000 | [diff] [blame] | 1582 | || (ntype == classdef) |
| 1583 | || (ntype == decorated)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1584 | res = validate_node(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1585 | else { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1586 | res = 0; |
| 1587 | PyErr_Format(parser_error, |
| 1588 | "Illegal compound statement type: %d.", TYPE(tree)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1589 | } |
| 1590 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1591 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1592 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1593 | static int |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1594 | validate_yield_or_testlist(node *tree) |
| 1595 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1596 | if (TYPE(tree) == yield_expr) |
| 1597 | return validate_yield_expr(tree); |
| 1598 | else |
| 1599 | return validate_testlist(tree); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1603 | validate_expr_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1604 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1605 | int j; |
| 1606 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1607 | int res = (validate_ntype(tree, expr_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1608 | && is_odd(nch) |
| 1609 | && validate_testlist(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1610 | |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1611 | if (res && nch == 3 |
| 1612 | && TYPE(CHILD(tree, 1)) == augassign) { |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1613 | res = validate_numnodes(CHILD(tree, 1), 1, "augassign") |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1614 | && validate_yield_or_testlist(CHILD(tree, 2)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1615 | |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1616 | if (res) { |
| 1617 | char *s = STR(CHILD(CHILD(tree, 1), 0)); |
| 1618 | |
| 1619 | res = (strcmp(s, "+=") == 0 |
| 1620 | || strcmp(s, "-=") == 0 |
| 1621 | || strcmp(s, "*=") == 0 |
| 1622 | || strcmp(s, "/=") == 0 |
Michael W. Hudson | 5e83b7a | 2003-01-29 14:20:23 +0000 | [diff] [blame] | 1623 | || strcmp(s, "//=") == 0 |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1624 | || strcmp(s, "%=") == 0 |
| 1625 | || strcmp(s, "&=") == 0 |
| 1626 | || strcmp(s, "|=") == 0 |
| 1627 | || strcmp(s, "^=") == 0 |
| 1628 | || strcmp(s, "<<=") == 0 |
| 1629 | || strcmp(s, ">>=") == 0 |
| 1630 | || strcmp(s, "**=") == 0); |
| 1631 | if (!res) |
| 1632 | err_string("illegal augmmented assignment operator"); |
| 1633 | } |
| 1634 | } |
| 1635 | else { |
| 1636 | for (j = 1; res && (j < nch); j += 2) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1637 | res = validate_equal(CHILD(tree, j)) |
| 1638 | && validate_yield_or_testlist(CHILD(tree, j + 1)); |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1639 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1640 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1641 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1642 | |
| 1643 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1644 | /* print_stmt: |
| 1645 | * |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1646 | * 'print' ( [ test (',' test)* [','] ] |
| 1647 | * | '>>' test [ (',' test)+ [','] ] ) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1648 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1649 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1650 | validate_print_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1651 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1652 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1653 | int res = (validate_ntype(tree, print_stmt) |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1654 | && (nch > 0) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1655 | && validate_name(CHILD(tree, 0), "print")); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1656 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1657 | if (res && nch > 1) { |
| 1658 | int sym = TYPE(CHILD(tree, 1)); |
| 1659 | int i = 1; |
| 1660 | int allow_trailing_comma = 1; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1661 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1662 | if (sym == test) |
| 1663 | res = validate_test(CHILD(tree, i++)); |
| 1664 | else { |
| 1665 | if (nch < 3) |
| 1666 | res = validate_numnodes(tree, 3, "print_stmt"); |
| 1667 | else { |
| 1668 | res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT) |
| 1669 | && validate_test(CHILD(tree, i+1))); |
| 1670 | i += 2; |
| 1671 | allow_trailing_comma = 0; |
| 1672 | } |
| 1673 | } |
| 1674 | if (res) { |
| 1675 | /* ... (',' test)* [','] */ |
| 1676 | while (res && i+2 <= nch) { |
| 1677 | res = (validate_comma(CHILD(tree, i)) |
| 1678 | && validate_test(CHILD(tree, i+1))); |
| 1679 | allow_trailing_comma = 1; |
| 1680 | i += 2; |
| 1681 | } |
| 1682 | if (res && !allow_trailing_comma) |
| 1683 | res = validate_numnodes(tree, i, "print_stmt"); |
| 1684 | else if (res && i < nch) |
| 1685 | res = validate_comma(CHILD(tree, i)); |
| 1686 | } |
| 1687 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1688 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1689 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1690 | |
| 1691 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1692 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1693 | validate_del_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1694 | { |
| 1695 | return (validate_numnodes(tree, 2, "del_stmt") |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1696 | && validate_name(CHILD(tree, 0), "del") |
| 1697 | && validate_exprlist(CHILD(tree, 1))); |
| 1698 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1699 | |
| 1700 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1701 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1702 | validate_return_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1703 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1704 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1705 | int res = (validate_ntype(tree, return_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1706 | && ((nch == 1) || (nch == 2)) |
| 1707 | && validate_name(CHILD(tree, 0), "return")); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1708 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1709 | if (res && (nch == 2)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1710 | res = validate_testlist(CHILD(tree, 1)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1711 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1712 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1713 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1714 | |
| 1715 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1716 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1717 | validate_raise_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1718 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1719 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1720 | int res = (validate_ntype(tree, raise_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1721 | && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1722 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1723 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1724 | res = validate_name(CHILD(tree, 0), "raise"); |
| 1725 | if (res && (nch >= 2)) |
| 1726 | res = validate_test(CHILD(tree, 1)); |
| 1727 | if (res && nch > 2) { |
| 1728 | res = (validate_comma(CHILD(tree, 2)) |
| 1729 | && validate_test(CHILD(tree, 3))); |
| 1730 | if (res && (nch > 4)) |
| 1731 | res = (validate_comma(CHILD(tree, 4)) |
| 1732 | && validate_test(CHILD(tree, 5))); |
| 1733 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1734 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1735 | else |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1736 | (void) validate_numnodes(tree, 2, "raise"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1737 | if (res && (nch == 4)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1738 | res = (validate_comma(CHILD(tree, 2)) |
| 1739 | && validate_test(CHILD(tree, 3))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1740 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1741 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1742 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1743 | |
| 1744 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1745 | /* yield_expr: 'yield' [testlist] |
| 1746 | */ |
| 1747 | static int |
| 1748 | validate_yield_expr(node *tree) |
| 1749 | { |
| 1750 | int nch = NCH(tree); |
| 1751 | int res = (validate_ntype(tree, yield_expr) |
| 1752 | && ((nch == 1) || (nch == 2)) |
| 1753 | && validate_name(CHILD(tree, 0), "yield")); |
| 1754 | |
| 1755 | if (res && (nch == 2)) |
| 1756 | res = validate_testlist(CHILD(tree, 1)); |
| 1757 | |
| 1758 | return (res); |
| 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | /* yield_stmt: yield_expr |
Fred Drake | 02126f2 | 2001-07-17 02:59:15 +0000 | [diff] [blame] | 1763 | */ |
| 1764 | static int |
| 1765 | validate_yield_stmt(node *tree) |
| 1766 | { |
| 1767 | return (validate_ntype(tree, yield_stmt) |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1768 | && validate_numnodes(tree, 1, "yield_stmt") |
| 1769 | && validate_yield_expr(CHILD(tree, 0))); |
Fred Drake | 02126f2 | 2001-07-17 02:59:15 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1773 | static int |
| 1774 | validate_import_as_name(node *tree) |
| 1775 | { |
| 1776 | int nch = NCH(tree); |
| 1777 | int ok = validate_ntype(tree, import_as_name); |
| 1778 | |
| 1779 | if (ok) { |
| 1780 | if (nch == 1) |
| 1781 | ok = validate_name(CHILD(tree, 0), NULL); |
| 1782 | else if (nch == 3) |
| 1783 | ok = (validate_name(CHILD(tree, 0), NULL) |
| 1784 | && validate_name(CHILD(tree, 1), "as") |
| 1785 | && validate_name(CHILD(tree, 2), NULL)); |
| 1786 | else |
| 1787 | ok = validate_numnodes(tree, 3, "import_as_name"); |
| 1788 | } |
| 1789 | return ok; |
| 1790 | } |
| 1791 | |
| 1792 | |
Fred Drake | 7113708 | 2001-01-07 05:59:59 +0000 | [diff] [blame] | 1793 | /* dotted_name: NAME ("." NAME)* |
| 1794 | */ |
| 1795 | static int |
| 1796 | validate_dotted_name(node *tree) |
| 1797 | { |
| 1798 | int nch = NCH(tree); |
| 1799 | int res = (validate_ntype(tree, dotted_name) |
| 1800 | && is_odd(nch) |
| 1801 | && validate_name(CHILD(tree, 0), NULL)); |
| 1802 | int i; |
| 1803 | |
| 1804 | for (i = 1; res && (i < nch); i += 2) { |
| 1805 | res = (validate_dot(CHILD(tree, i)) |
| 1806 | && validate_name(CHILD(tree, i+1), NULL)); |
| 1807 | } |
| 1808 | return res; |
| 1809 | } |
| 1810 | |
| 1811 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1812 | /* dotted_as_name: dotted_name [NAME NAME] |
| 1813 | */ |
| 1814 | static int |
| 1815 | validate_dotted_as_name(node *tree) |
| 1816 | { |
| 1817 | int nch = NCH(tree); |
| 1818 | int res = validate_ntype(tree, dotted_as_name); |
| 1819 | |
| 1820 | if (res) { |
| 1821 | if (nch == 1) |
Fred Drake | 7113708 | 2001-01-07 05:59:59 +0000 | [diff] [blame] | 1822 | res = validate_dotted_name(CHILD(tree, 0)); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1823 | else if (nch == 3) |
Fred Drake | 7113708 | 2001-01-07 05:59:59 +0000 | [diff] [blame] | 1824 | res = (validate_dotted_name(CHILD(tree, 0)) |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1825 | && validate_name(CHILD(tree, 1), "as") |
| 1826 | && validate_name(CHILD(tree, 2), NULL)); |
| 1827 | else { |
| 1828 | res = 0; |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1829 | err_string("illegal number of children for dotted_as_name"); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | return res; |
| 1833 | } |
| 1834 | |
| 1835 | |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1836 | /* dotted_as_name (',' dotted_as_name)* */ |
| 1837 | static int |
| 1838 | validate_dotted_as_names(node *tree) |
| 1839 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1840 | int nch = NCH(tree); |
| 1841 | int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0)); |
| 1842 | int i; |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1844 | for (i = 1; res && (i < nch); i += 2) |
| 1845 | res = (validate_comma(CHILD(tree, i)) |
| 1846 | && validate_dotted_as_name(CHILD(tree, i + 1))); |
| 1847 | return (res); |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | /* import_as_name (',' import_as_name)* [','] */ |
| 1852 | static int |
| 1853 | validate_import_as_names(node *tree) |
| 1854 | { |
| 1855 | int nch = NCH(tree); |
| 1856 | int res = validate_import_as_name(CHILD(tree, 0)); |
| 1857 | int i; |
| 1858 | |
| 1859 | for (i = 1; res && (i + 1 < nch); i += 2) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1860 | res = (validate_comma(CHILD(tree, i)) |
| 1861 | && validate_import_as_name(CHILD(tree, i + 1))); |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1862 | return (res); |
| 1863 | } |
| 1864 | |
| 1865 | |
| 1866 | /* 'import' dotted_as_names */ |
| 1867 | static int |
| 1868 | validate_import_name(node *tree) |
| 1869 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1870 | return (validate_ntype(tree, import_name) |
| 1871 | && validate_numnodes(tree, 2, "import_name") |
| 1872 | && validate_name(CHILD(tree, 0), "import") |
| 1873 | && validate_dotted_as_names(CHILD(tree, 1))); |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1876 | /* Helper function to count the number of leading dots in |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1877 | * 'from ...module import name' |
| 1878 | */ |
| 1879 | static int |
| 1880 | count_from_dots(node *tree) |
| 1881 | { |
| 1882 | int i; |
Benjamin Peterson | 6624a9f | 2008-11-03 15:14:51 +0000 | [diff] [blame] | 1883 | for (i = 1; i < NCH(tree); i++) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1884 | if (TYPE(CHILD(tree, i)) != DOT) |
| 1885 | break; |
Benjamin Peterson | 6624a9f | 2008-11-03 15:14:51 +0000 | [diff] [blame] | 1886 | return i-1; |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1887 | } |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1888 | |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 1889 | /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' | |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1890 | * import_as_names |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1891 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1892 | static int |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1893 | validate_import_from(node *tree) |
| 1894 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1895 | int nch = NCH(tree); |
| 1896 | int ndots = count_from_dots(tree); |
| 1897 | int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name); |
| 1898 | int offset = ndots + havename; |
| 1899 | int res = validate_ntype(tree, import_from) |
| 1900 | && (nch >= 4 + ndots) |
| 1901 | && validate_name(CHILD(tree, 0), "from") |
| 1902 | && (!havename || validate_dotted_name(CHILD(tree, ndots + 1))) |
| 1903 | && validate_name(CHILD(tree, offset + 1), "import"); |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1904 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1905 | if (res && TYPE(CHILD(tree, offset + 2)) == LPAR) |
| 1906 | res = ((nch == offset + 5) |
| 1907 | && validate_lparen(CHILD(tree, offset + 2)) |
| 1908 | && validate_import_as_names(CHILD(tree, offset + 3)) |
| 1909 | && validate_rparen(CHILD(tree, offset + 4))); |
| 1910 | else if (res && TYPE(CHILD(tree, offset + 2)) != STAR) |
| 1911 | res = validate_import_as_names(CHILD(tree, offset + 2)); |
| 1912 | return (res); |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | |
| 1916 | /* import_stmt: import_name | import_from */ |
| 1917 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1918 | validate_import_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1919 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1920 | int nch = NCH(tree); |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1921 | int res = validate_numnodes(tree, 1, "import_stmt"); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1922 | |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1923 | if (res) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1924 | int ntype = TYPE(CHILD(tree, 0)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1926 | if (ntype == import_name || ntype == import_from) |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1927 | res = validate_node(CHILD(tree, 0)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1928 | else { |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1929 | res = 0; |
| 1930 | err_string("illegal import_stmt child type"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1931 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1932 | } |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1933 | else if (nch == 1) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1934 | res = 0; |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1935 | PyErr_Format(parser_error, |
| 1936 | "Unrecognized child node of import_stmt: %d.", |
| 1937 | TYPE(CHILD(tree, 0))); |
| 1938 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1939 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1940 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1941 | |
| 1942 | |
Anthony Baxter | 1a4ddae | 2004-08-31 10:07:13 +0000 | [diff] [blame] | 1943 | |
| 1944 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1945 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1946 | validate_global_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1947 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1948 | int j; |
| 1949 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1950 | int res = (validate_ntype(tree, global_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1951 | && is_even(nch) && (nch >= 2)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1952 | |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1953 | if (!res && !PyErr_Occurred()) |
| 1954 | err_string("illegal global statement"); |
| 1955 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1956 | if (res) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1957 | res = (validate_name(CHILD(tree, 0), "global") |
| 1958 | && validate_ntype(CHILD(tree, 1), NAME)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1959 | for (j = 2; res && (j < nch); j += 2) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1960 | res = (validate_comma(CHILD(tree, j)) |
| 1961 | && validate_ntype(CHILD(tree, j + 1), NAME)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1962 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1963 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1964 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1965 | |
| 1966 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1967 | /* exec_stmt: |
| 1968 | * |
| 1969 | * 'exec' expr ['in' test [',' test]] |
| 1970 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1971 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1972 | validate_exec_stmt(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1973 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1974 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1975 | int res = (validate_ntype(tree, exec_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1976 | && ((nch == 2) || (nch == 4) || (nch == 6)) |
| 1977 | && validate_name(CHILD(tree, 0), "exec") |
| 1978 | && validate_expr(CHILD(tree, 1))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1979 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1980 | if (!res && !PyErr_Occurred()) |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1981 | err_string("illegal exec statement"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1982 | if (res && (nch > 2)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1983 | res = (validate_name(CHILD(tree, 2), "in") |
| 1984 | && validate_test(CHILD(tree, 3))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1985 | if (res && (nch == 6)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1986 | res = (validate_comma(CHILD(tree, 4)) |
| 1987 | && validate_test(CHILD(tree, 5))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1988 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1989 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1990 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1991 | |
| 1992 | |
Guido van Rossum | 925e547 | 1997-04-02 05:32:13 +0000 | [diff] [blame] | 1993 | /* assert_stmt: |
| 1994 | * |
| 1995 | * 'assert' test [',' test] |
| 1996 | */ |
| 1997 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1998 | validate_assert_stmt(node *tree) |
Guido van Rossum | 925e547 | 1997-04-02 05:32:13 +0000 | [diff] [blame] | 1999 | { |
| 2000 | int nch = NCH(tree); |
| 2001 | int res = (validate_ntype(tree, assert_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2002 | && ((nch == 2) || (nch == 4)) |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 2003 | && (validate_name(CHILD(tree, 0), "assert")) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2004 | && validate_test(CHILD(tree, 1))); |
Guido van Rossum | 925e547 | 1997-04-02 05:32:13 +0000 | [diff] [blame] | 2005 | |
| 2006 | if (!res && !PyErr_Occurred()) |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2007 | err_string("illegal assert statement"); |
Guido van Rossum | 925e547 | 1997-04-02 05:32:13 +0000 | [diff] [blame] | 2008 | if (res && (nch > 2)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2009 | res = (validate_comma(CHILD(tree, 2)) |
| 2010 | && validate_test(CHILD(tree, 3))); |
Guido van Rossum | 925e547 | 1997-04-02 05:32:13 +0000 | [diff] [blame] | 2011 | |
| 2012 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2013 | } |
Guido van Rossum | 925e547 | 1997-04-02 05:32:13 +0000 | [diff] [blame] | 2014 | |
| 2015 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2016 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2017 | validate_while(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2018 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2019 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2020 | int res = (validate_ntype(tree, while_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2021 | && ((nch == 4) || (nch == 7)) |
| 2022 | && validate_name(CHILD(tree, 0), "while") |
| 2023 | && validate_test(CHILD(tree, 1)) |
| 2024 | && validate_colon(CHILD(tree, 2)) |
| 2025 | && validate_suite(CHILD(tree, 3))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2026 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2027 | if (res && (nch == 7)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2028 | res = (validate_name(CHILD(tree, 4), "else") |
| 2029 | && validate_colon(CHILD(tree, 5)) |
| 2030 | && validate_suite(CHILD(tree, 6))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2031 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2032 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2033 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2034 | |
| 2035 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2036 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2037 | validate_for(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2038 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2039 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2040 | int res = (validate_ntype(tree, for_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2041 | && ((nch == 6) || (nch == 9)) |
| 2042 | && validate_name(CHILD(tree, 0), "for") |
| 2043 | && validate_exprlist(CHILD(tree, 1)) |
| 2044 | && validate_name(CHILD(tree, 2), "in") |
| 2045 | && validate_testlist(CHILD(tree, 3)) |
| 2046 | && validate_colon(CHILD(tree, 4)) |
| 2047 | && validate_suite(CHILD(tree, 5))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2048 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2049 | if (res && (nch == 9)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2050 | res = (validate_name(CHILD(tree, 6), "else") |
| 2051 | && validate_colon(CHILD(tree, 7)) |
| 2052 | && validate_suite(CHILD(tree, 8))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2053 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2054 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2055 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2056 | |
| 2057 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2058 | /* try_stmt: |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2059 | * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
Georg Brandl | fe879e8 | 2008-12-05 12:09:41 +0000 | [diff] [blame] | 2060 | ['finally' ':' suite] |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2061 | * | 'try' ':' suite 'finally' ':' suite |
| 2062 | * |
| 2063 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2064 | static int |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 2065 | validate_try(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2066 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2067 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2068 | int pos = 3; |
| 2069 | int res = (validate_ntype(tree, try_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2070 | && (nch >= 6) && ((nch % 3) == 0)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2071 | |
| 2072 | if (res) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2073 | res = (validate_name(CHILD(tree, 0), "try") |
| 2074 | && validate_colon(CHILD(tree, 1)) |
| 2075 | && validate_suite(CHILD(tree, 2)) |
| 2076 | && validate_colon(CHILD(tree, nch - 2)) |
| 2077 | && validate_suite(CHILD(tree, nch - 1))); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 2078 | else if (!PyErr_Occurred()) { |
Fred Drake | 22269b5 | 2000-07-03 18:07:43 +0000 | [diff] [blame] | 2079 | const char* name = "except"; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2080 | if (TYPE(CHILD(tree, nch - 3)) != except_clause) |
| 2081 | name = STR(CHILD(tree, nch - 3)); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 2082 | |
| 2083 | PyErr_Format(parser_error, |
| 2084 | "Illegal number of children for try/%s node.", name); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2085 | } |
Georg Brandl | fe879e8 | 2008-12-05 12:09:41 +0000 | [diff] [blame] | 2086 | /* Handle try/finally statement */ |
| 2087 | if (res && (TYPE(CHILD(tree, pos)) == NAME) && |
| 2088 | (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) { |
| 2089 | res = (validate_numnodes(tree, 6, "try/finally") |
| 2090 | && validate_colon(CHILD(tree, 4)) |
| 2091 | && validate_suite(CHILD(tree, 5))); |
| 2092 | return (res); |
| 2093 | } |
| 2094 | /* try/except statement: skip past except_clause sections */ |
Antoine Pitrou | 42b5bcf | 2009-05-14 21:48:09 +0000 | [diff] [blame] | 2095 | while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2096 | res = (validate_except_clause(CHILD(tree, pos)) |
| 2097 | && validate_colon(CHILD(tree, pos + 1)) |
| 2098 | && validate_suite(CHILD(tree, pos + 2))); |
| 2099 | pos += 3; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2100 | } |
Georg Brandl | fe879e8 | 2008-12-05 12:09:41 +0000 | [diff] [blame] | 2101 | /* skip else clause */ |
Antoine Pitrou | 42b5bcf | 2009-05-14 21:48:09 +0000 | [diff] [blame] | 2102 | if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) && |
Georg Brandl | fe879e8 | 2008-12-05 12:09:41 +0000 | [diff] [blame] | 2103 | (strcmp(STR(CHILD(tree, pos)), "else") == 0)) { |
| 2104 | res = (validate_colon(CHILD(tree, pos + 1)) |
| 2105 | && validate_suite(CHILD(tree, pos + 2))); |
| 2106 | pos += 3; |
| 2107 | } |
| 2108 | if (res && pos < nch) { |
| 2109 | /* last clause must be a finally */ |
| 2110 | res = (validate_name(CHILD(tree, pos), "finally") |
| 2111 | && validate_numnodes(tree, pos + 3, "try/except/finally") |
| 2112 | && validate_colon(CHILD(tree, pos + 1)) |
| 2113 | && validate_suite(CHILD(tree, pos + 2))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2114 | } |
| 2115 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2116 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2117 | |
| 2118 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2119 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2120 | validate_except_clause(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2121 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2122 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2123 | int res = (validate_ntype(tree, except_clause) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2124 | && ((nch == 1) || (nch == 2) || (nch == 4)) |
| 2125 | && validate_name(CHILD(tree, 0), "except")); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2126 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2127 | if (res && (nch > 1)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2128 | res = validate_test(CHILD(tree, 1)); |
Mark Dickinson | 070f0ab | 2010-06-30 16:27:57 +0000 | [diff] [blame] | 2129 | if (res && (nch == 4)) { |
| 2130 | if (TYPE(CHILD(tree, 2)) == NAME) |
| 2131 | res = validate_name(CHILD(tree, 2), "as"); |
| 2132 | else |
| 2133 | res = validate_comma(CHILD(tree, 2)); |
| 2134 | res = res && validate_test(CHILD(tree, 3)); |
| 2135 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2136 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2137 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2138 | |
| 2139 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2140 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2141 | validate_test(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2142 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2143 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2144 | int res = validate_ntype(tree, test) && is_odd(nch); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2145 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2146 | if (res && (TYPE(CHILD(tree, 0)) == lambdef)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2147 | res = ((nch == 1) |
| 2148 | && validate_lambdef(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2149 | else if (res) { |
Thomas Wouters | e2dd78c | 2006-02-27 16:25:11 +0000 | [diff] [blame] | 2150 | res = validate_or_test(CHILD(tree, 0)); |
| 2151 | res = (res && (nch == 1 || (nch == 5 && |
| 2152 | validate_name(CHILD(tree, 1), "if") && |
| 2153 | validate_or_test(CHILD(tree, 2)) && |
| 2154 | validate_name(CHILD(tree, 3), "else") && |
| 2155 | validate_test(CHILD(tree, 4))))); |
| 2156 | } |
| 2157 | return (res); |
| 2158 | } |
| 2159 | |
| 2160 | static int |
| 2161 | validate_old_test(node *tree) |
| 2162 | { |
| 2163 | int nch = NCH(tree); |
| 2164 | int res = validate_ntype(tree, old_test) && (nch == 1); |
| 2165 | |
| 2166 | if (res && (TYPE(CHILD(tree, 0)) == old_lambdef)) |
| 2167 | res = (validate_old_lambdef(CHILD(tree, 0))); |
| 2168 | else if (res) { |
| 2169 | res = (validate_or_test(CHILD(tree, 0))); |
| 2170 | } |
| 2171 | return (res); |
| 2172 | } |
| 2173 | |
| 2174 | static int |
| 2175 | validate_or_test(node *tree) |
| 2176 | { |
| 2177 | int nch = NCH(tree); |
| 2178 | int res = validate_ntype(tree, or_test) && is_odd(nch); |
| 2179 | |
| 2180 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2181 | int pos; |
| 2182 | res = validate_and_test(CHILD(tree, 0)); |
| 2183 | for (pos = 1; res && (pos < nch); pos += 2) |
| 2184 | res = (validate_name(CHILD(tree, pos), "or") |
| 2185 | && validate_and_test(CHILD(tree, pos + 1))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2186 | } |
| 2187 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2188 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2189 | |
| 2190 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2191 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2192 | validate_and_test(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2193 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2194 | int pos; |
| 2195 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2196 | int res = (validate_ntype(tree, and_test) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2197 | && is_odd(nch) |
| 2198 | && validate_not_test(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2199 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2200 | for (pos = 1; res && (pos < nch); pos += 2) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2201 | res = (validate_name(CHILD(tree, pos), "and") |
| 2202 | && validate_not_test(CHILD(tree, 0))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2203 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2204 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2205 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2206 | |
| 2207 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2208 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2209 | validate_not_test(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2210 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2211 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2212 | int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2213 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2214 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2215 | if (nch == 2) |
| 2216 | res = (validate_name(CHILD(tree, 0), "not") |
| 2217 | && validate_not_test(CHILD(tree, 1))); |
| 2218 | else if (nch == 1) |
| 2219 | res = validate_comparison(CHILD(tree, 0)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2220 | } |
| 2221 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2222 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2223 | |
| 2224 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2225 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2226 | validate_comparison(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2227 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2228 | int pos; |
| 2229 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2230 | int res = (validate_ntype(tree, comparison) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2231 | && is_odd(nch) |
| 2232 | && validate_expr(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2233 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2234 | for (pos = 1; res && (pos < nch); pos += 2) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2235 | res = (validate_comp_op(CHILD(tree, pos)) |
| 2236 | && validate_expr(CHILD(tree, pos + 1))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2237 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2238 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2239 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2240 | |
| 2241 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2242 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2243 | validate_comp_op(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2244 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2245 | int res = 0; |
| 2246 | int nch = NCH(tree); |
| 2247 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2248 | if (!validate_ntype(tree, comp_op)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2249 | return (0); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2250 | if (nch == 1) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2251 | /* |
| 2252 | * Only child will be a terminal with a well-defined symbolic name |
| 2253 | * or a NAME with a string of either 'is' or 'in' |
| 2254 | */ |
| 2255 | tree = CHILD(tree, 0); |
| 2256 | switch (TYPE(tree)) { |
| 2257 | case LESS: |
| 2258 | case GREATER: |
| 2259 | case EQEQUAL: |
| 2260 | case EQUAL: |
| 2261 | case LESSEQUAL: |
| 2262 | case GREATEREQUAL: |
| 2263 | case NOTEQUAL: |
| 2264 | res = 1; |
| 2265 | break; |
| 2266 | case NAME: |
| 2267 | res = ((strcmp(STR(tree), "in") == 0) |
| 2268 | || (strcmp(STR(tree), "is") == 0)); |
| 2269 | if (!res) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 2270 | PyErr_Format(parser_error, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2271 | "illegal operator '%s'", STR(tree)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2272 | } |
| 2273 | break; |
| 2274 | default: |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2275 | err_string("illegal comparison operator type"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2276 | break; |
| 2277 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2278 | } |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 2279 | else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2280 | res = (validate_ntype(CHILD(tree, 0), NAME) |
| 2281 | && validate_ntype(CHILD(tree, 1), NAME) |
| 2282 | && (((strcmp(STR(CHILD(tree, 0)), "is") == 0) |
| 2283 | && (strcmp(STR(CHILD(tree, 1)), "not") == 0)) |
| 2284 | || ((strcmp(STR(CHILD(tree, 0)), "not") == 0) |
| 2285 | && (strcmp(STR(CHILD(tree, 1)), "in") == 0)))); |
| 2286 | if (!res && !PyErr_Occurred()) |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2287 | err_string("unknown comparison operator"); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2288 | } |
| 2289 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2290 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2291 | |
| 2292 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2293 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2294 | validate_expr(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2295 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2296 | int j; |
| 2297 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2298 | int res = (validate_ntype(tree, expr) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2299 | && is_odd(nch) |
| 2300 | && validate_xor_expr(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2301 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2302 | for (j = 2; res && (j < nch); j += 2) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2303 | res = (validate_xor_expr(CHILD(tree, j)) |
| 2304 | && validate_vbar(CHILD(tree, j - 1))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2305 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2306 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2307 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2308 | |
| 2309 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2310 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2311 | validate_xor_expr(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2312 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2313 | int j; |
| 2314 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2315 | int res = (validate_ntype(tree, xor_expr) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2316 | && is_odd(nch) |
| 2317 | && validate_and_expr(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2318 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2319 | for (j = 2; res && (j < nch); j += 2) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2320 | res = (validate_circumflex(CHILD(tree, j - 1)) |
| 2321 | && validate_and_expr(CHILD(tree, j))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2322 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2323 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2324 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2325 | |
| 2326 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2327 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2328 | validate_and_expr(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2329 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2330 | int pos; |
| 2331 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2332 | int res = (validate_ntype(tree, and_expr) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2333 | && is_odd(nch) |
| 2334 | && validate_shift_expr(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2335 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2336 | for (pos = 1; res && (pos < nch); pos += 2) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2337 | res = (validate_ampersand(CHILD(tree, pos)) |
| 2338 | && validate_shift_expr(CHILD(tree, pos + 1))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2339 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2340 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2341 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2342 | |
| 2343 | |
| 2344 | static int |
Peter Schneider-Kamp | 286da3b | 2000-07-10 12:43:58 +0000 | [diff] [blame] | 2345 | validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2346 | { |
| 2347 | int pos = 1; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2348 | int nch = NCH(tree); |
| 2349 | int res = (is_odd(nch) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2350 | && (*termvalid)(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2351 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2352 | for ( ; res && (pos < nch); pos += 2) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2353 | if (TYPE(CHILD(tree, pos)) != op1) |
| 2354 | res = validate_ntype(CHILD(tree, pos), op2); |
| 2355 | if (res) |
| 2356 | res = (*termvalid)(CHILD(tree, pos + 1)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2357 | } |
| 2358 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2359 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2360 | |
| 2361 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2362 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2363 | validate_shift_expr(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2364 | { |
| 2365 | return (validate_ntype(tree, shift_expr) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2366 | && validate_chain_two_ops(tree, validate_arith_expr, |
| 2367 | LEFTSHIFT, RIGHTSHIFT)); |
| 2368 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2369 | |
| 2370 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2371 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2372 | validate_arith_expr(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2373 | { |
| 2374 | return (validate_ntype(tree, arith_expr) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2375 | && validate_chain_two_ops(tree, validate_term, PLUS, MINUS)); |
| 2376 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2377 | |
| 2378 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2379 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2380 | validate_term(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2381 | { |
| 2382 | int pos = 1; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2383 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2384 | int res = (validate_ntype(tree, term) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2385 | && is_odd(nch) |
| 2386 | && validate_factor(CHILD(tree, 0))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2387 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2388 | for ( ; res && (pos < nch); pos += 2) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2389 | res = (((TYPE(CHILD(tree, pos)) == STAR) |
| 2390 | || (TYPE(CHILD(tree, pos)) == SLASH) |
Michael W. Hudson | 5e83b7a | 2003-01-29 14:20:23 +0000 | [diff] [blame] | 2391 | || (TYPE(CHILD(tree, pos)) == DOUBLESLASH) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2392 | || (TYPE(CHILD(tree, pos)) == PERCENT)) |
| 2393 | && validate_factor(CHILD(tree, pos + 1))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2394 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2395 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2396 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2397 | |
| 2398 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2399 | /* factor: |
| 2400 | * |
| 2401 | * factor: ('+'|'-'|'~') factor | power |
| 2402 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2403 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2404 | validate_factor(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2405 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2406 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2407 | int res = (validate_ntype(tree, factor) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2408 | && (((nch == 2) |
| 2409 | && ((TYPE(CHILD(tree, 0)) == PLUS) |
| 2410 | || (TYPE(CHILD(tree, 0)) == MINUS) |
| 2411 | || (TYPE(CHILD(tree, 0)) == TILDE)) |
| 2412 | && validate_factor(CHILD(tree, 1))) |
| 2413 | || ((nch == 1) |
| 2414 | && validate_power(CHILD(tree, 0))))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2415 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2416 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2417 | |
| 2418 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2419 | /* power: |
| 2420 | * |
| 2421 | * power: atom trailer* ('**' factor)* |
| 2422 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2423 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2424 | validate_power(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2425 | { |
| 2426 | int pos = 1; |
| 2427 | int nch = NCH(tree); |
| 2428 | int res = (validate_ntype(tree, power) && (nch >= 1) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2429 | && validate_atom(CHILD(tree, 0))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2430 | |
| 2431 | while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2432 | res = validate_trailer(CHILD(tree, pos++)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2433 | if (res && (pos < nch)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2434 | if (!is_even(nch - pos)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2435 | err_string("illegal number of nodes for 'power'"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2436 | return (0); |
| 2437 | } |
| 2438 | for ( ; res && (pos < (nch - 1)); pos += 2) |
| 2439 | res = (validate_doublestar(CHILD(tree, pos)) |
| 2440 | && validate_factor(CHILD(tree, pos + 1))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2441 | } |
| 2442 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2443 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2444 | |
| 2445 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2446 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2447 | validate_atom(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2448 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2449 | int pos; |
| 2450 | int nch = NCH(tree); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2451 | int res = validate_ntype(tree, atom); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2452 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2453 | if (res && nch < 1) |
| 2454 | res = validate_numnodes(tree, nch+1, "atom"); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2455 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2456 | switch (TYPE(CHILD(tree, 0))) { |
| 2457 | case LPAR: |
| 2458 | res = ((nch <= 3) |
| 2459 | && (validate_rparen(CHILD(tree, nch - 1)))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2460 | |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 2461 | if (res && (nch == 3)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2462 | if (TYPE(CHILD(tree, 1))==yield_expr) |
| 2463 | res = validate_yield_expr(CHILD(tree, 1)); |
| 2464 | else |
| 2465 | res = validate_testlist_comp(CHILD(tree, 1)); |
| 2466 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2467 | break; |
| 2468 | case LSQB: |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2469 | if (nch == 2) |
| 2470 | res = validate_ntype(CHILD(tree, 1), RSQB); |
| 2471 | else if (nch == 3) |
| 2472 | res = (validate_listmaker(CHILD(tree, 1)) |
| 2473 | && validate_ntype(CHILD(tree, 2), RSQB)); |
| 2474 | else { |
| 2475 | res = 0; |
| 2476 | err_string("illegal list display atom"); |
| 2477 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2478 | break; |
| 2479 | case LBRACE: |
| 2480 | res = ((nch <= 3) |
| 2481 | && validate_ntype(CHILD(tree, nch - 1), RBRACE)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2482 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2483 | if (res && (nch == 3)) |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 2484 | res = validate_dictorsetmaker(CHILD(tree, 1)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2485 | break; |
| 2486 | case BACKQUOTE: |
| 2487 | res = ((nch == 3) |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2488 | && validate_testlist1(CHILD(tree, 1)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2489 | && validate_ntype(CHILD(tree, 2), BACKQUOTE)); |
| 2490 | break; |
| 2491 | case NAME: |
| 2492 | case NUMBER: |
| 2493 | res = (nch == 1); |
| 2494 | break; |
| 2495 | case STRING: |
| 2496 | for (pos = 1; res && (pos < nch); ++pos) |
| 2497 | res = validate_ntype(CHILD(tree, pos), STRING); |
| 2498 | break; |
| 2499 | default: |
| 2500 | res = 0; |
| 2501 | break; |
| 2502 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2503 | } |
| 2504 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2505 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2506 | |
| 2507 | |
Fred Drake | 85bf3bb | 2000-08-23 15:35:26 +0000 | [diff] [blame] | 2508 | /* listmaker: |
| 2509 | * test ( list_for | (',' test)* [','] ) |
| 2510 | */ |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2511 | static int |
| 2512 | validate_listmaker(node *tree) |
| 2513 | { |
| 2514 | int nch = NCH(tree); |
| 2515 | int ok = nch; |
| 2516 | |
| 2517 | if (nch == 0) |
| 2518 | err_string("missing child nodes of listmaker"); |
| 2519 | else |
| 2520 | ok = validate_test(CHILD(tree, 0)); |
| 2521 | |
| 2522 | /* |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2523 | * list_for | (',' test)* [','] |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2524 | */ |
Fred Drake | 85bf3bb | 2000-08-23 15:35:26 +0000 | [diff] [blame] | 2525 | if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for) |
| 2526 | ok = validate_list_for(CHILD(tree, 1)); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2527 | else { |
| 2528 | /* (',' test)* [','] */ |
| 2529 | int i = 1; |
| 2530 | while (ok && nch - i >= 2) { |
| 2531 | ok = (validate_comma(CHILD(tree, i)) |
| 2532 | && validate_test(CHILD(tree, i+1))); |
Fred Drake | 85bf3bb | 2000-08-23 15:35:26 +0000 | [diff] [blame] | 2533 | i += 2; |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2534 | } |
Fred Drake | 85bf3bb | 2000-08-23 15:35:26 +0000 | [diff] [blame] | 2535 | if (ok && i == nch-1) |
| 2536 | ok = validate_comma(CHILD(tree, i)); |
| 2537 | else if (i != nch) { |
| 2538 | ok = 0; |
| 2539 | err_string("illegal trailing nodes for listmaker"); |
| 2540 | } |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2541 | } |
| 2542 | return ok; |
| 2543 | } |
| 2544 | |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2545 | /* testlist_comp: |
| 2546 | * test ( comp_for | (',' test)* [','] ) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2547 | */ |
| 2548 | static int |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2549 | validate_testlist_comp(node *tree) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2550 | { |
| 2551 | int nch = NCH(tree); |
| 2552 | int ok = nch; |
| 2553 | |
| 2554 | if (nch == 0) |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2555 | err_string("missing child nodes of testlist_comp"); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2556 | else { |
| 2557 | ok = validate_test(CHILD(tree, 0)); |
| 2558 | } |
| 2559 | |
| 2560 | /* |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2561 | * comp_for | (',' test)* [','] |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2562 | */ |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2563 | if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for) |
| 2564 | ok = validate_comp_for(CHILD(tree, 1)); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2565 | else { |
| 2566 | /* (',' test)* [','] */ |
| 2567 | int i = 1; |
| 2568 | while (ok && nch - i >= 2) { |
| 2569 | ok = (validate_comma(CHILD(tree, i)) |
| 2570 | && validate_test(CHILD(tree, i+1))); |
| 2571 | i += 2; |
| 2572 | } |
| 2573 | if (ok && i == nch-1) |
| 2574 | ok = validate_comma(CHILD(tree, i)); |
| 2575 | else if (i != nch) { |
| 2576 | ok = 0; |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2577 | err_string("illegal trailing nodes for testlist_comp"); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | return ok; |
| 2581 | } |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2582 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2583 | /* decorator: |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 2584 | * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2585 | */ |
| 2586 | static int |
| 2587 | validate_decorator(node *tree) |
| 2588 | { |
| 2589 | int ok; |
| 2590 | int nch = NCH(tree); |
| 2591 | ok = (validate_ntype(tree, decorator) && |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2592 | (nch == 3 || nch == 5 || nch == 6) && |
| 2593 | validate_at(CHILD(tree, 0)) && |
| 2594 | validate_dotted_name(CHILD(tree, 1)) && |
| 2595 | validate_newline(RCHILD(tree, -1))); |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2596 | |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 2597 | if (ok && nch != 3) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2598 | ok = (validate_lparen(CHILD(tree, 2)) && |
| 2599 | validate_rparen(RCHILD(tree, -2))); |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2600 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2601 | if (ok && nch == 6) |
| 2602 | ok = validate_arglist(CHILD(tree, 3)); |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | return ok; |
| 2606 | } |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 2607 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2608 | /* decorators: |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 2609 | * decorator+ |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2610 | */ |
| 2611 | static int |
| 2612 | validate_decorators(node *tree) |
| 2613 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2614 | int i, nch, ok; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2615 | nch = NCH(tree); |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 2616 | ok = validate_ntype(tree, decorators) && nch >= 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2617 | |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 2618 | for (i = 0; ok && i < nch; ++i) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2619 | ok = validate_decorator(CHILD(tree, i)); |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2620 | |
| 2621 | return ok; |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 2622 | } |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2623 | |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 2624 | /* with_item: |
| 2625 | * test ['as' expr] |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 2626 | */ |
| 2627 | static int |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 2628 | validate_with_item(node *tree) |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 2629 | { |
| 2630 | int nch = NCH(tree); |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 2631 | int ok = (validate_ntype(tree, with_item) |
| 2632 | && (nch == 1 || nch == 3) |
| 2633 | && validate_test(CHILD(tree, 0))); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2634 | if (ok && nch == 3) |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 2635 | ok = (validate_name(CHILD(tree, 1), "as") |
| 2636 | && validate_expr(CHILD(tree, 2))); |
| 2637 | return ok; |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 2640 | /* with_stmt: |
| 2641 | * 0 1 ... -2 -1 |
| 2642 | * 'with' with_item (',' with_item)* ':' suite |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 2643 | */ |
| 2644 | static int |
| 2645 | validate_with_stmt(node *tree) |
| 2646 | { |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 2647 | int i; |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 2648 | int nch = NCH(tree); |
| 2649 | int ok = (validate_ntype(tree, with_stmt) |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 2650 | && (nch % 2 == 0) |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 2651 | && validate_name(CHILD(tree, 0), "with") |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 2652 | && validate_colon(RCHILD(tree, -2)) |
| 2653 | && validate_suite(RCHILD(tree, -1))); |
Georg Brandl | 944f684 | 2009-05-25 21:02:56 +0000 | [diff] [blame] | 2654 | for (i = 1; ok && i < nch - 2; i += 2) |
| 2655 | ok = validate_with_item(CHILD(tree, i)); |
| 2656 | return ok; |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2659 | /* funcdef: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2660 | * |
Christian Heimes | 5224d28 | 2008-02-23 15:01:05 +0000 | [diff] [blame] | 2661 | * -5 -4 -3 -2 -1 |
| 2662 | * 'def' NAME parameters ':' suite |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2663 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2664 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2665 | validate_funcdef(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2666 | { |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2667 | int nch = NCH(tree); |
| 2668 | int ok = (validate_ntype(tree, funcdef) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2669 | && (nch == 5) |
| 2670 | && validate_name(RCHILD(tree, -5), "def") |
| 2671 | && validate_ntype(RCHILD(tree, -4), NAME) |
| 2672 | && validate_colon(RCHILD(tree, -2)) |
| 2673 | && validate_parameters(RCHILD(tree, -3)) |
| 2674 | && validate_suite(RCHILD(tree, -1))); |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 2675 | return ok; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2676 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2677 | |
| 2678 | |
Christian Heimes | 5224d28 | 2008-02-23 15:01:05 +0000 | [diff] [blame] | 2679 | /* decorated |
| 2680 | * decorators (classdef | funcdef) |
| 2681 | */ |
| 2682 | static int |
| 2683 | validate_decorated(node *tree) |
| 2684 | { |
Mark Dickinson | a7ee59b | 2010-07-04 16:23:54 +0000 | [diff] [blame^] | 2685 | int nch = NCH(tree); |
| 2686 | int ok = (validate_ntype(tree, decorated) |
| 2687 | && (nch == 2) |
| 2688 | && validate_decorators(RCHILD(tree, -2))); |
| 2689 | if (TYPE(RCHILD(tree, -1)) == funcdef) |
| 2690 | ok = ok && validate_funcdef(RCHILD(tree, -1)); |
| 2691 | else |
| 2692 | ok = ok && validate_class(RCHILD(tree, -1)); |
| 2693 | return ok; |
Christian Heimes | 5224d28 | 2008-02-23 15:01:05 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2696 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2697 | validate_lambdef(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2698 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2699 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2700 | int res = (validate_ntype(tree, lambdef) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2701 | && ((nch == 3) || (nch == 4)) |
| 2702 | && validate_name(CHILD(tree, 0), "lambda") |
| 2703 | && validate_colon(CHILD(tree, nch - 2)) |
| 2704 | && validate_test(CHILD(tree, nch - 1))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2705 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2706 | if (res && (nch == 4)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2707 | res = validate_varargslist(CHILD(tree, 1)); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2708 | else if (!res && !PyErr_Occurred()) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2709 | (void) validate_numnodes(tree, 3, "lambdef"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2710 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2711 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2712 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2713 | |
| 2714 | |
Thomas Wouters | e2dd78c | 2006-02-27 16:25:11 +0000 | [diff] [blame] | 2715 | static int |
| 2716 | validate_old_lambdef(node *tree) |
| 2717 | { |
| 2718 | int nch = NCH(tree); |
| 2719 | int res = (validate_ntype(tree, old_lambdef) |
| 2720 | && ((nch == 3) || (nch == 4)) |
| 2721 | && validate_name(CHILD(tree, 0), "lambda") |
| 2722 | && validate_colon(CHILD(tree, nch - 2)) |
| 2723 | && validate_test(CHILD(tree, nch - 1))); |
| 2724 | |
| 2725 | if (res && (nch == 4)) |
| 2726 | res = validate_varargslist(CHILD(tree, 1)); |
| 2727 | else if (!res && !PyErr_Occurred()) |
| 2728 | (void) validate_numnodes(tree, 3, "old_lambdef"); |
| 2729 | |
| 2730 | return (res); |
| 2731 | } |
| 2732 | |
| 2733 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2734 | /* arglist: |
| 2735 | * |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2736 | * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2737 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2738 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2739 | validate_arglist(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2740 | { |
Fred Drake | e7ab64e | 2000-04-25 04:14:46 +0000 | [diff] [blame] | 2741 | int nch = NCH(tree); |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2742 | int i = 0; |
| 2743 | int ok = 1; |
Fred Drake | e7ab64e | 2000-04-25 04:14:46 +0000 | [diff] [blame] | 2744 | |
| 2745 | if (nch <= 0) |
| 2746 | /* raise the right error from having an invalid number of children */ |
| 2747 | return validate_numnodes(tree, nch + 1, "arglist"); |
| 2748 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2749 | if (nch > 1) { |
| 2750 | for (i=0; i<nch; i++) { |
| 2751 | if (TYPE(CHILD(tree, i)) == argument) { |
| 2752 | node *ch = CHILD(tree, i); |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2753 | if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) { |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2754 | err_string("need '(', ')' for generator expression"); |
| 2755 | return 0; |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2761 | while (ok && nch-i >= 2) { |
| 2762 | /* skip leading (argument ',') */ |
| 2763 | ok = (validate_argument(CHILD(tree, i)) |
| 2764 | && validate_comma(CHILD(tree, i+1))); |
| 2765 | if (ok) |
| 2766 | i += 2; |
| 2767 | else |
| 2768 | PyErr_Clear(); |
| 2769 | } |
| 2770 | ok = 1; |
| 2771 | if (nch-i > 0) { |
| 2772 | /* |
| 2773 | * argument | '*' test [',' '**' test] | '**' test |
Fred Drake | e7ab64e | 2000-04-25 04:14:46 +0000 | [diff] [blame] | 2774 | */ |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2775 | int sym = TYPE(CHILD(tree, i)); |
| 2776 | |
| 2777 | if (sym == argument) { |
| 2778 | ok = validate_argument(CHILD(tree, i)); |
| 2779 | if (ok && i+1 != nch) { |
| 2780 | err_string("illegal arglist specification" |
| 2781 | " (extra stuff on end)"); |
| 2782 | ok = 0; |
Fred Drake | e7ab64e | 2000-04-25 04:14:46 +0000 | [diff] [blame] | 2783 | } |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2784 | } |
| 2785 | else if (sym == STAR) { |
| 2786 | ok = validate_star(CHILD(tree, i)); |
| 2787 | if (ok && (nch-i == 2)) |
| 2788 | ok = validate_test(CHILD(tree, i+1)); |
| 2789 | else if (ok && (nch-i == 5)) |
| 2790 | ok = (validate_test(CHILD(tree, i+1)) |
| 2791 | && validate_comma(CHILD(tree, i+2)) |
| 2792 | && validate_doublestar(CHILD(tree, i+3)) |
| 2793 | && validate_test(CHILD(tree, i+4))); |
Fred Drake | e7ab64e | 2000-04-25 04:14:46 +0000 | [diff] [blame] | 2794 | else { |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2795 | err_string("illegal use of '*' in arglist"); |
| 2796 | ok = 0; |
Fred Drake | e7ab64e | 2000-04-25 04:14:46 +0000 | [diff] [blame] | 2797 | } |
| 2798 | } |
Fred Drake | cff283c | 2000-08-21 22:24:43 +0000 | [diff] [blame] | 2799 | else if (sym == DOUBLESTAR) { |
| 2800 | if (nch-i == 2) |
| 2801 | ok = (validate_doublestar(CHILD(tree, i)) |
| 2802 | && validate_test(CHILD(tree, i+1))); |
| 2803 | else { |
| 2804 | err_string("illegal use of '**' in arglist"); |
| 2805 | ok = 0; |
| 2806 | } |
| 2807 | } |
| 2808 | else { |
| 2809 | err_string("illegal arglist specification"); |
| 2810 | ok = 0; |
| 2811 | } |
Fred Drake | e7ab64e | 2000-04-25 04:14:46 +0000 | [diff] [blame] | 2812 | } |
| 2813 | return (ok); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2814 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2815 | |
| 2816 | |
| 2817 | |
| 2818 | /* argument: |
| 2819 | * |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2820 | * [test '='] test [comp_for] |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2821 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2822 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2823 | validate_argument(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2824 | { |
| 2825 | int nch = NCH(tree); |
| 2826 | int res = (validate_ntype(tree, argument) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2827 | && ((nch == 1) || (nch == 2) || (nch == 3)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2828 | && validate_test(CHILD(tree, 0))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2829 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2830 | if (res && (nch == 2)) |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2831 | res = validate_comp_for(CHILD(tree, 1)); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2832 | else if (res && (nch == 3)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2833 | res = (validate_equal(CHILD(tree, 1)) |
| 2834 | && validate_test(CHILD(tree, 2))); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2835 | |
| 2836 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2837 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2838 | |
| 2839 | |
| 2840 | |
| 2841 | /* trailer: |
| 2842 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2843 | * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2844 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2845 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2846 | validate_trailer(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2847 | { |
| 2848 | int nch = NCH(tree); |
| 2849 | int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2850 | |
| 2851 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2852 | switch (TYPE(CHILD(tree, 0))) { |
| 2853 | case LPAR: |
| 2854 | res = validate_rparen(CHILD(tree, nch - 1)); |
| 2855 | if (res && (nch == 3)) |
| 2856 | res = validate_arglist(CHILD(tree, 1)); |
| 2857 | break; |
| 2858 | case LSQB: |
| 2859 | res = (validate_numnodes(tree, 3, "trailer") |
| 2860 | && validate_subscriptlist(CHILD(tree, 1)) |
| 2861 | && validate_ntype(CHILD(tree, 2), RSQB)); |
| 2862 | break; |
| 2863 | case DOT: |
| 2864 | res = (validate_numnodes(tree, 2, "trailer") |
| 2865 | && validate_ntype(CHILD(tree, 1), NAME)); |
| 2866 | break; |
| 2867 | default: |
| 2868 | res = 0; |
| 2869 | break; |
| 2870 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2871 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2872 | else { |
| 2873 | (void) validate_numnodes(tree, 2, "trailer"); |
| 2874 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2875 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2876 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2877 | |
| 2878 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2879 | /* subscriptlist: |
| 2880 | * |
| 2881 | * subscript (',' subscript)* [','] |
| 2882 | */ |
| 2883 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2884 | validate_subscriptlist(node *tree) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2885 | { |
| 2886 | return (validate_repeating_list(tree, subscriptlist, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2887 | validate_subscript, "subscriptlist")); |
| 2888 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2889 | |
| 2890 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2891 | /* subscript: |
| 2892 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2893 | * '.' '.' '.' | test | [test] ':' [test] [sliceop] |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2894 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2895 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2896 | validate_subscript(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2897 | { |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2898 | int offset = 0; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2899 | int nch = NCH(tree); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2900 | int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2901 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2902 | if (!res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2903 | if (!PyErr_Occurred()) |
| 2904 | err_string("invalid number of arguments for subscript node"); |
| 2905 | return (0); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2906 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2907 | if (TYPE(CHILD(tree, 0)) == DOT) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2908 | /* take care of ('.' '.' '.') possibility */ |
| 2909 | return (validate_numnodes(tree, 3, "subscript") |
| 2910 | && validate_dot(CHILD(tree, 0)) |
| 2911 | && validate_dot(CHILD(tree, 1)) |
| 2912 | && validate_dot(CHILD(tree, 2))); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2913 | if (nch == 1) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2914 | if (TYPE(CHILD(tree, 0)) == test) |
| 2915 | res = validate_test(CHILD(tree, 0)); |
| 2916 | else |
| 2917 | res = validate_colon(CHILD(tree, 0)); |
| 2918 | return (res); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2919 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2920 | /* Must be [test] ':' [test] [sliceop], |
| 2921 | * but at least one of the optional components will |
| 2922 | * be present, but we don't know which yet. |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2923 | */ |
| 2924 | if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2925 | res = validate_test(CHILD(tree, 0)); |
| 2926 | offset = 1; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2927 | } |
| 2928 | if (res) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2929 | res = validate_colon(CHILD(tree, offset)); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2930 | if (res) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2931 | int rem = nch - ++offset; |
| 2932 | if (rem) { |
| 2933 | if (TYPE(CHILD(tree, offset)) == test) { |
| 2934 | res = validate_test(CHILD(tree, offset)); |
| 2935 | ++offset; |
| 2936 | --rem; |
| 2937 | } |
| 2938 | if (res && rem) |
| 2939 | res = validate_sliceop(CHILD(tree, offset)); |
| 2940 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2941 | } |
| 2942 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2943 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2944 | |
| 2945 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2946 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2947 | validate_sliceop(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2948 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2949 | int nch = NCH(tree); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2950 | int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop")) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2951 | && validate_ntype(tree, sliceop); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2952 | if (!res && !PyErr_Occurred()) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2953 | res = validate_numnodes(tree, 1, "sliceop"); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2954 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2955 | if (res) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2956 | res = validate_colon(CHILD(tree, 0)); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2957 | if (res && (nch == 2)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2958 | res = validate_test(CHILD(tree, 1)); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2959 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2960 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2961 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2962 | |
| 2963 | |
| 2964 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2965 | validate_exprlist(node *tree) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2966 | { |
| 2967 | return (validate_repeating_list(tree, exprlist, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2968 | validate_expr, "exprlist")); |
| 2969 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2970 | |
| 2971 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2972 | /* |
| 2973 | * dictorsetmaker: |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2974 | * |
| 2975 | * (test ':' test (comp_for | (',' test ':' test)* [','])) | |
| 2976 | * (test (comp_for | (',' test)* [','])) |
| 2977 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 2978 | static int |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 2979 | validate_dictorsetmaker(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2980 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2981 | int nch = NCH(tree); |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 2982 | int ok = validate_ntype(tree, dictorsetmaker); |
| 2983 | int i = 0; |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2984 | int check_trailing_comma = 0; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2985 | |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 2986 | assert(nch > 0); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 2987 | |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 2988 | if (ok && (nch == 1 || TYPE(CHILD(tree, 1)) == COMMA)) { |
| 2989 | /* We got a set: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2990 | * test (',' test)* [','] |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 2991 | */ |
| 2992 | ok = validate_test(CHILD(tree, i++)); |
| 2993 | while (ok && nch - i >= 2) { |
| 2994 | ok = (validate_comma(CHILD(tree, i)) |
| 2995 | && validate_test(CHILD(tree, i+1))); |
| 2996 | i += 2; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 2997 | } |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 2998 | check_trailing_comma = 1; |
| 2999 | } |
| 3000 | else if (ok && TYPE(CHILD(tree, 1)) == comp_for) { |
| 3001 | /* We got a set comprehension: |
| 3002 | * test comp_for |
| 3003 | */ |
| 3004 | ok = (validate_test(CHILD(tree, 0)) |
| 3005 | && validate_comp_for(CHILD(tree, 1))); |
| 3006 | } |
| 3007 | else if (ok && NCH(tree) > 3 && TYPE(CHILD(tree, 3)) == comp_for) { |
| 3008 | /* We got a dict comprehension: |
| 3009 | * test ':' test comp_for |
| 3010 | */ |
| 3011 | ok = (validate_test(CHILD(tree, 0)) |
| 3012 | && validate_colon(CHILD(tree, 1)) |
| 3013 | && validate_test(CHILD(tree, 2)) |
| 3014 | && validate_comp_for(CHILD(tree, 3))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3015 | } |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 3016 | else if (ok) { |
| 3017 | /* We got a dict: |
| 3018 | * test ':' test (',' test ':' test)* [','] |
| 3019 | */ |
| 3020 | if (nch >= 3) { |
| 3021 | ok = (validate_test(CHILD(tree, i)) |
| 3022 | && validate_colon(CHILD(tree, i+1)) |
| 3023 | && validate_test(CHILD(tree, i+2))); |
| 3024 | i += 3; |
| 3025 | } |
| 3026 | else { |
| 3027 | ok = 0; |
| 3028 | err_string("illegal number of nodes for dictorsetmaker"); |
| 3029 | } |
| 3030 | |
| 3031 | while (ok && nch - i >= 4) { |
| 3032 | ok = (validate_comma(CHILD(tree, i)) |
| 3033 | && validate_test(CHILD(tree, i+1)) |
| 3034 | && validate_colon(CHILD(tree, i+2)) |
| 3035 | && validate_test(CHILD(tree, i+3))); |
| 3036 | i += 4; |
| 3037 | } |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 3038 | check_trailing_comma = 1; |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 3039 | } |
Alexandre Vassalotti | b646547 | 2010-01-11 22:36:12 +0000 | [diff] [blame] | 3040 | if (ok && check_trailing_comma) { |
Alexandre Vassalotti | ee936a2 | 2010-01-09 23:35:54 +0000 | [diff] [blame] | 3041 | if (i == nch-1) |
| 3042 | ok = validate_comma(CHILD(tree, i)); |
| 3043 | else if (i != nch) { |
| 3044 | ok = 0; |
| 3045 | err_string("illegal trailing nodes for dictorsetmaker"); |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | return ok; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3050 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3051 | |
| 3052 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 3053 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3054 | validate_eval_input(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3055 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3056 | int pos; |
| 3057 | int nch = NCH(tree); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3058 | int res = (validate_ntype(tree, eval_input) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3059 | && (nch >= 2) |
| 3060 | && validate_testlist(CHILD(tree, 0)) |
| 3061 | && validate_ntype(CHILD(tree, nch - 1), ENDMARKER)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3062 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3063 | for (pos = 1; res && (pos < (nch - 1)); ++pos) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3064 | res = validate_ntype(CHILD(tree, pos), NEWLINE); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3065 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3066 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3067 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3068 | |
| 3069 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 3070 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3071 | validate_node(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3072 | { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3073 | int nch = 0; /* num. children on current node */ |
| 3074 | int res = 1; /* result value */ |
| 3075 | node* next = 0; /* node to process after this one */ |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3076 | |
Martin v. Löwis | b28f6e7 | 2001-06-23 19:55:38 +0000 | [diff] [blame] | 3077 | while (res && (tree != 0)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3078 | nch = NCH(tree); |
| 3079 | next = 0; |
| 3080 | switch (TYPE(tree)) { |
| 3081 | /* |
| 3082 | * Definition nodes. |
| 3083 | */ |
| 3084 | case funcdef: |
| 3085 | res = validate_funcdef(tree); |
| 3086 | break; |
Benjamin Peterson | 9dfe6a8 | 2008-11-24 04:09:03 +0000 | [diff] [blame] | 3087 | case with_stmt: |
| 3088 | res = validate_with_stmt(tree); |
| 3089 | break; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3090 | case classdef: |
| 3091 | res = validate_class(tree); |
| 3092 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3093 | case decorated: |
| 3094 | res = validate_decorated(tree); |
| 3095 | break; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3096 | /* |
| 3097 | * "Trivial" parse tree nodes. |
| 3098 | * (Why did I call these trivial?) |
| 3099 | */ |
| 3100 | case stmt: |
| 3101 | res = validate_stmt(tree); |
| 3102 | break; |
| 3103 | case small_stmt: |
| 3104 | /* |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 3105 | * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3106 | * | import_stmt | global_stmt | exec_stmt | assert_stmt |
| 3107 | */ |
| 3108 | res = validate_small_stmt(tree); |
| 3109 | break; |
| 3110 | case flow_stmt: |
| 3111 | res = (validate_numnodes(tree, 1, "flow_stmt") |
| 3112 | && ((TYPE(CHILD(tree, 0)) == break_stmt) |
| 3113 | || (TYPE(CHILD(tree, 0)) == continue_stmt) |
Fred Drake | 02126f2 | 2001-07-17 02:59:15 +0000 | [diff] [blame] | 3114 | || (TYPE(CHILD(tree, 0)) == yield_stmt) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3115 | || (TYPE(CHILD(tree, 0)) == return_stmt) |
| 3116 | || (TYPE(CHILD(tree, 0)) == raise_stmt))); |
| 3117 | if (res) |
| 3118 | next = CHILD(tree, 0); |
| 3119 | else if (nch == 1) |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3120 | err_string("illegal flow_stmt type"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3121 | break; |
Fred Drake | 02126f2 | 2001-07-17 02:59:15 +0000 | [diff] [blame] | 3122 | case yield_stmt: |
| 3123 | res = validate_yield_stmt(tree); |
| 3124 | break; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3125 | /* |
| 3126 | * Compound statements. |
| 3127 | */ |
| 3128 | case simple_stmt: |
| 3129 | res = validate_simple_stmt(tree); |
| 3130 | break; |
| 3131 | case compound_stmt: |
| 3132 | res = validate_compound_stmt(tree); |
| 3133 | break; |
| 3134 | /* |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 3135 | * Fundamental statements. |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3136 | */ |
| 3137 | case expr_stmt: |
| 3138 | res = validate_expr_stmt(tree); |
| 3139 | break; |
| 3140 | case print_stmt: |
| 3141 | res = validate_print_stmt(tree); |
| 3142 | break; |
| 3143 | case del_stmt: |
| 3144 | res = validate_del_stmt(tree); |
| 3145 | break; |
| 3146 | case pass_stmt: |
| 3147 | res = (validate_numnodes(tree, 1, "pass") |
| 3148 | && validate_name(CHILD(tree, 0), "pass")); |
| 3149 | break; |
| 3150 | case break_stmt: |
| 3151 | res = (validate_numnodes(tree, 1, "break") |
| 3152 | && validate_name(CHILD(tree, 0), "break")); |
| 3153 | break; |
| 3154 | case continue_stmt: |
| 3155 | res = (validate_numnodes(tree, 1, "continue") |
| 3156 | && validate_name(CHILD(tree, 0), "continue")); |
| 3157 | break; |
| 3158 | case return_stmt: |
| 3159 | res = validate_return_stmt(tree); |
| 3160 | break; |
| 3161 | case raise_stmt: |
| 3162 | res = validate_raise_stmt(tree); |
| 3163 | break; |
| 3164 | case import_stmt: |
| 3165 | res = validate_import_stmt(tree); |
| 3166 | break; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3167 | case import_name: |
| 3168 | res = validate_import_name(tree); |
| 3169 | break; |
| 3170 | case import_from: |
| 3171 | res = validate_import_from(tree); |
| 3172 | break; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3173 | case global_stmt: |
| 3174 | res = validate_global_stmt(tree); |
| 3175 | break; |
| 3176 | case exec_stmt: |
| 3177 | res = validate_exec_stmt(tree); |
| 3178 | break; |
| 3179 | case assert_stmt: |
| 3180 | res = validate_assert_stmt(tree); |
| 3181 | break; |
| 3182 | case if_stmt: |
| 3183 | res = validate_if(tree); |
| 3184 | break; |
| 3185 | case while_stmt: |
| 3186 | res = validate_while(tree); |
| 3187 | break; |
| 3188 | case for_stmt: |
| 3189 | res = validate_for(tree); |
| 3190 | break; |
| 3191 | case try_stmt: |
| 3192 | res = validate_try(tree); |
| 3193 | break; |
| 3194 | case suite: |
| 3195 | res = validate_suite(tree); |
| 3196 | break; |
| 3197 | /* |
| 3198 | * Expression nodes. |
| 3199 | */ |
| 3200 | case testlist: |
| 3201 | res = validate_testlist(tree); |
| 3202 | break; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 3203 | case yield_expr: |
| 3204 | res = validate_yield_expr(tree); |
| 3205 | break; |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3206 | case testlist1: |
| 3207 | res = validate_testlist1(tree); |
| 3208 | break; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3209 | case test: |
| 3210 | res = validate_test(tree); |
| 3211 | break; |
| 3212 | case and_test: |
| 3213 | res = validate_and_test(tree); |
| 3214 | break; |
| 3215 | case not_test: |
| 3216 | res = validate_not_test(tree); |
| 3217 | break; |
| 3218 | case comparison: |
| 3219 | res = validate_comparison(tree); |
| 3220 | break; |
| 3221 | case exprlist: |
| 3222 | res = validate_exprlist(tree); |
| 3223 | break; |
| 3224 | case comp_op: |
| 3225 | res = validate_comp_op(tree); |
| 3226 | break; |
| 3227 | case expr: |
| 3228 | res = validate_expr(tree); |
| 3229 | break; |
| 3230 | case xor_expr: |
| 3231 | res = validate_xor_expr(tree); |
| 3232 | break; |
| 3233 | case and_expr: |
| 3234 | res = validate_and_expr(tree); |
| 3235 | break; |
| 3236 | case shift_expr: |
| 3237 | res = validate_shift_expr(tree); |
| 3238 | break; |
| 3239 | case arith_expr: |
| 3240 | res = validate_arith_expr(tree); |
| 3241 | break; |
| 3242 | case term: |
| 3243 | res = validate_term(tree); |
| 3244 | break; |
| 3245 | case factor: |
| 3246 | res = validate_factor(tree); |
| 3247 | break; |
| 3248 | case power: |
| 3249 | res = validate_power(tree); |
| 3250 | break; |
| 3251 | case atom: |
| 3252 | res = validate_atom(tree); |
| 3253 | break; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3254 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3255 | default: |
| 3256 | /* Hopefully never reached! */ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3257 | err_string("unrecognized node type"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3258 | res = 0; |
| 3259 | break; |
| 3260 | } |
| 3261 | tree = next; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3262 | } |
| 3263 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3264 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3265 | |
| 3266 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 3267 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3268 | validate_expr_tree(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3269 | { |
| 3270 | int res = validate_eval_input(tree); |
| 3271 | |
| 3272 | if (!res && !PyErr_Occurred()) |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3273 | err_string("could not validate expression tuple"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3274 | |
| 3275 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3276 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3277 | |
| 3278 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3279 | /* file_input: |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3280 | * (NEWLINE | stmt)* ENDMARKER |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3281 | */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 3282 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3283 | validate_file_input(node *tree) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3284 | { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3285 | int j; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3286 | int nch = NCH(tree) - 1; |
| 3287 | int res = ((nch >= 0) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3288 | && validate_ntype(CHILD(tree, nch), ENDMARKER)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3289 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3290 | for (j = 0; res && (j < nch); ++j) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3291 | if (TYPE(CHILD(tree, j)) == stmt) |
| 3292 | res = validate_stmt(CHILD(tree, j)); |
| 3293 | else |
| 3294 | res = validate_newline(CHILD(tree, j)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3295 | } |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 3296 | /* This stays in to prevent any internal failures from getting to the |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3297 | * user. Hopefully, this won't be needed. If a user reports getting |
| 3298 | * this, we have some debugging to do. |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3299 | */ |
| 3300 | if (!res && !PyErr_Occurred()) |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3301 | err_string("VALIDATION FAILURE: report this to the maintainer!"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3302 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3303 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3304 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3305 | |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 3306 | static int |
| 3307 | validate_encoding_decl(node *tree) |
| 3308 | { |
| 3309 | int nch = NCH(tree); |
| 3310 | int res = ((nch == 1) |
| 3311 | && validate_file_input(CHILD(tree, 0))); |
| 3312 | |
| 3313 | if (!res && !PyErr_Occurred()) |
| 3314 | err_string("Error Parsing encoding_decl"); |
| 3315 | |
| 3316 | return res; |
| 3317 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3318 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3319 | static PyObject* |
| 3320 | pickle_constructor = NULL; |
| 3321 | |
| 3322 | |
| 3323 | static PyObject* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3324 | parser__pickler(PyObject *self, PyObject *args) |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3325 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 3326 | NOTE(ARGUNUSED(self)) |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3327 | PyObject *result = NULL; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3328 | PyObject *st = NULL; |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 3329 | PyObject *empty_dict = NULL; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3330 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3331 | if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3332 | PyObject *newargs; |
| 3333 | PyObject *tuple; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3334 | |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 3335 | if ((empty_dict = PyDict_New()) == NULL) |
| 3336 | goto finally; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3337 | if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3338 | goto finally; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3339 | tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3340 | if (tuple != NULL) { |
| 3341 | result = Py_BuildValue("O(O)", pickle_constructor, tuple); |
| 3342 | Py_DECREF(tuple); |
| 3343 | } |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 3344 | Py_DECREF(empty_dict); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3345 | Py_DECREF(newargs); |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3346 | } |
| 3347 | finally: |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 3348 | Py_XDECREF(empty_dict); |
| 3349 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3350 | return (result); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3351 | } |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3352 | |
| 3353 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3354 | /* Functions exported by this module. Most of this should probably |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3355 | * be converted into an ST object with methods, but that is better |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3356 | * done directly in Python, allowing subclasses to be created directly. |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3357 | * We'd really have to write a wrapper around it all anyway to allow |
| 3358 | * inheritance. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3359 | */ |
| 3360 | static PyMethodDef parser_functions[] = { |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 3361 | {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3362 | PyDoc_STR("Creates a tuple-tree representation of an ST.")}, |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 3363 | {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3364 | PyDoc_STR("Creates a list-tree representation of an ST.")}, |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 3365 | {"compileast", (PyCFunction)parser_compileast,PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3366 | PyDoc_STR("Compiles an ST object into a code object.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3367 | {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3368 | PyDoc_STR("Compiles an ST object into a code object.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3369 | {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3370 | PyDoc_STR("Creates an ST object from an expression.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3371 | {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3372 | PyDoc_STR("Determines if an ST object was created from an expression.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3373 | {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3374 | PyDoc_STR("Determines if an ST object was created from a suite.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3375 | {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3376 | PyDoc_STR("Creates an ST object from a suite.")}, |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 3377 | {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3378 | PyDoc_STR("Creates an ST object from a tree representation.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3379 | {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3380 | PyDoc_STR("Creates an ST object from a tree representation.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3381 | {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3382 | PyDoc_STR("Creates a tuple-tree representation of an ST.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3383 | {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3384 | PyDoc_STR("Creates a list-tree representation of an ST.")}, |
Georg Brandl | f9efabb | 2008-07-23 15:16:45 +0000 | [diff] [blame] | 3385 | {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3386 | PyDoc_STR("Creates an ST object from a tree representation.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3387 | {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3388 | PyDoc_STR("Creates an ST object from a tree representation.")}, |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3389 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3390 | /* private stuff: support pickle module */ |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 3391 | {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 3392 | PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")}, |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3393 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 3394 | {NULL, NULL, 0, NULL} |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3395 | }; |
| 3396 | |
| 3397 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 3398 | PyMODINIT_FUNC initparser(void); /* supply a prototype */ |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 3399 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 3400 | PyMODINIT_FUNC |
Thomas Wouters | 5c66986 | 2000-07-24 15:49:08 +0000 | [diff] [blame] | 3401 | initparser(void) |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 3402 | { |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 3403 | PyObject *module, *copyreg; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3404 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3405 | Py_TYPE(&PyST_Type) = &PyType_Type; |
Guido van Rossum | f2b2dac | 1997-01-23 23:29:44 +0000 | [diff] [blame] | 3406 | module = Py_InitModule("parser", parser_functions); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 3407 | if (module == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3408 | return; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3409 | |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 3410 | if (parser_error == 0) |
| 3411 | parser_error = PyErr_NewException("parser.ParserError", NULL, NULL); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3412 | |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 3413 | if (parser_error == 0) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3414 | /* caller will check PyErr_Occurred() */ |
| 3415 | return; |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 3416 | /* CAUTION: The code next used to skip bumping the refcount on |
| 3417 | * parser_error. That's a disaster if initparser() gets called more |
| 3418 | * than once. By incref'ing, we ensure that each module dict that |
| 3419 | * gets created owns its reference to the shared parser_error object, |
| 3420 | * and the file static parser_error vrbl owns a reference too. |
| 3421 | */ |
| 3422 | Py_INCREF(parser_error); |
| 3423 | if (PyModule_AddObject(module, "ParserError", parser_error) != 0) |
| 3424 | return; |
| 3425 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3426 | Py_INCREF(&PyST_Type); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 3427 | PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type); |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 3428 | Py_INCREF(&PyST_Type); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 3429 | PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 3430 | |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 3431 | PyModule_AddStringConstant(module, "__copyright__", |
| 3432 | parser_copyright_string); |
| 3433 | PyModule_AddStringConstant(module, "__doc__", |
| 3434 | parser_doc_string); |
| 3435 | PyModule_AddStringConstant(module, "__version__", |
| 3436 | parser_version_string); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 3437 | |
Fred Drake | 78bdb9b | 2001-07-19 20:17:15 +0000 | [diff] [blame] | 3438 | /* Register to support pickling. |
| 3439 | * If this fails, the import of this module will fail because an |
| 3440 | * exception will be raised here; should we clear the exception? |
| 3441 | */ |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 3442 | copyreg = PyImport_ImportModuleNoBlock("copy_reg"); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 3443 | if (copyreg != NULL) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3444 | PyObject *func, *pickler; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3445 | |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 3446 | func = PyObject_GetAttrString(copyreg, "pickle"); |
| 3447 | pickle_constructor = PyObject_GetAttrString(module, "sequence2st"); |
| 3448 | pickler = PyObject_GetAttrString(module, "_pickler"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3449 | Py_XINCREF(pickle_constructor); |
| 3450 | if ((func != NULL) && (pickle_constructor != NULL) |
| 3451 | && (pickler != NULL)) { |
| 3452 | PyObject *res; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3453 | |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 3454 | res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler, |
| 3455 | pickle_constructor, NULL); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3456 | Py_XDECREF(res); |
| 3457 | } |
| 3458 | Py_XDECREF(func); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 3459 | Py_XDECREF(pickle_constructor); |
| 3460 | Py_XDECREF(pickler); |
| 3461 | Py_DECREF(copyreg); |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 3462 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 3463 | } |