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