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