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