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