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