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