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