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