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