Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1 | /* parsermodule.c |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 2 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 3 | * Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic |
| 4 | * Institute and State University, Blacksburg, Virginia, USA. |
| 5 | * Portions copyright 1991-1995 by Stichting Mathematisch Centrum, |
| 6 | * Amsterdam, The Netherlands. Copying is permitted under the terms |
| 7 | * associated with the main Python distribution, with the additional |
| 8 | * restriction that this additional notice be included and maintained |
| 9 | * on all distributed copies. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 10 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 11 | * This module serves to replace the original parser module written |
| 12 | * by Guido. The functionality is not matched precisely, but the |
| 13 | * original may be implemented on top of this. This is desirable |
| 14 | * since the source of the text to be parsed is now divorced from |
| 15 | * this interface. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 16 | * |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 17 | * Unlike the prior interface, the ability to give a parse tree |
| 18 | * produced by Python code as a tuple to the compiler is enabled by |
| 19 | * this module. See the documentation for more details. |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 20 | * |
| 21 | * I've added some annotations that help with the lint code-checking |
| 22 | * program, but they're not complete by a long shot. The real errors |
| 23 | * that lint detects are gone, but there are still warnings with |
| 24 | * Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations |
| 25 | * look like "NOTE(...)". |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 26 | * |
| 27 | * To debug parser errors like |
| 28 | * "parser.ParserError: Expected node type 12, got 333." |
| 29 | * decode symbol numbers using the automatically-generated files |
| 30 | * Lib/symbol.h and Include/token.h. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 33 | #include "Python.h" /* general Python API */ |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 34 | #include "Python-ast.h" /* mod_ty */ |
Victor Stinner | 5f2df88 | 2018-11-12 00:56:19 +0100 | [diff] [blame] | 35 | #include "ast.h" |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 36 | #include "graminit.h" /* symbols defined in the grammar */ |
| 37 | #include "node.h" /* internal parser structure */ |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 38 | #include "errcode.h" /* error codes for PyNode_*() */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 39 | #include "token.h" /* token definitions */ |
Victor Stinner | 5f2df88 | 2018-11-12 00:56:19 +0100 | [diff] [blame] | 40 | /* ISTERMINAL() / ISNONTERMINAL() */ |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 41 | #include "grammar.h" |
| 42 | #include "parsetok.h" |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 43 | |
| 44 | extern grammar _PyParser_Grammar; /* From graminit.c */ |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 45 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 46 | #ifdef lint |
| 47 | #include <note.h> |
| 48 | #else |
| 49 | #define NOTE(x) |
| 50 | #endif |
| 51 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 52 | /* String constants used to initialize module attributes. |
| 53 | * |
| 54 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 55 | static const char parser_copyright_string[] = |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 56 | "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\ |
Guido van Rossum | 2a28846 | 1996-08-21 21:55:43 +0000 | [diff] [blame] | 57 | University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\ |
| 58 | Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\ |
| 59 | Centrum, Amsterdam, The Netherlands."; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 60 | |
| 61 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 62 | PyDoc_STRVAR(parser_doc_string, |
| 63 | "This is an interface to Python's internal parser."); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 64 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 65 | static const char parser_version_string[] = "0.5"; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 66 | |
| 67 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 68 | typedef PyObject* (*SeqMaker) (Py_ssize_t length); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 69 | typedef int (*SeqInserter) (PyObject* sequence, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 70 | Py_ssize_t index, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 71 | PyObject* element); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 72 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 73 | /* The function below is copyrighted by Stichting Mathematisch Centrum. The |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 74 | * original copyright statement is included below, and continues to apply |
| 75 | * in full to the function immediately following. All other material is |
| 76 | * original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic |
| 77 | * Institute and State University. Changes were made to comply with the |
Guido van Rossum | 2a28846 | 1996-08-21 21:55:43 +0000 | [diff] [blame] | 78 | * new naming conventions. Added arguments to provide support for creating |
| 79 | * lists as well as tuples, and optionally including the line numbers. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 80 | */ |
| 81 | |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 83 | static PyObject* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 84 | node2tuple(node *n, /* node to convert */ |
| 85 | SeqMaker mkseq, /* create sequence */ |
| 86 | SeqInserter addelem, /* func. to add elem. in seq. */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 87 | int lineno, /* include line numbers? */ |
| 88 | int col_offset) /* include column offsets? */ |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 89 | { |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 90 | PyObject *result = NULL, *w; |
| 91 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 92 | if (n == NULL) { |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 93 | Py_RETURN_NONE; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 94 | } |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 95 | |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 96 | if (ISNONTERMINAL(TYPE(n))) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 97 | int i; |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 98 | |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 99 | result = mkseq(1 + NCH(n) + (TYPE(n) == encoding_decl)); |
| 100 | if (result == NULL) |
| 101 | goto error; |
| 102 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 103 | w = PyLong_FromLong(TYPE(n)); |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 104 | if (w == NULL) |
| 105 | goto error; |
| 106 | (void) addelem(result, 0, w); |
| 107 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 108 | for (i = 0; i < NCH(n); i++) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 109 | w = node2tuple(CHILD(n, i), mkseq, addelem, lineno, col_offset); |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 110 | if (w == NULL) |
| 111 | goto error; |
| 112 | (void) addelem(result, i+1, w); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 113 | } |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 114 | |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 115 | if (TYPE(n) == encoding_decl) { |
| 116 | w = PyUnicode_FromString(STR(n)); |
| 117 | if (w == NULL) |
| 118 | goto error; |
| 119 | (void) addelem(result, i+1, w); |
| 120 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 121 | } |
| 122 | else if (ISTERMINAL(TYPE(n))) { |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 123 | result = mkseq(2 + lineno + col_offset); |
| 124 | if (result == NULL) |
| 125 | goto error; |
| 126 | |
| 127 | w = PyLong_FromLong(TYPE(n)); |
| 128 | if (w == NULL) |
| 129 | goto error; |
| 130 | (void) addelem(result, 0, w); |
| 131 | |
| 132 | w = PyUnicode_FromString(STR(n)); |
| 133 | if (w == NULL) |
| 134 | goto error; |
| 135 | (void) addelem(result, 1, w); |
| 136 | |
Serhiy Storchaka | e5362ea | 2018-04-19 01:55:37 +0300 | [diff] [blame] | 137 | if (lineno) { |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 138 | w = PyLong_FromLong(n->n_lineno); |
| 139 | if (w == NULL) |
| 140 | goto error; |
| 141 | (void) addelem(result, 2, w); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 142 | } |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 143 | |
Serhiy Storchaka | e5362ea | 2018-04-19 01:55:37 +0300 | [diff] [blame] | 144 | if (col_offset) { |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 145 | w = PyLong_FromLong(n->n_col_offset); |
| 146 | if (w == NULL) |
| 147 | goto error; |
Serhiy Storchaka | e5362ea | 2018-04-19 01:55:37 +0300 | [diff] [blame] | 148 | (void) addelem(result, 2 + lineno, w); |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 149 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 150 | } |
| 151 | else { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 152 | PyErr_SetString(PyExc_SystemError, |
| 153 | "unrecognized parse tree node type"); |
| 154 | return ((PyObject*) NULL); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 155 | } |
Victor Stinner | df4572c | 2013-07-12 01:35:10 +0200 | [diff] [blame] | 156 | return result; |
| 157 | |
| 158 | error: |
| 159 | Py_XDECREF(result); |
| 160 | return NULL; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 161 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 162 | /* |
| 163 | * End of material copyrighted by Stichting Mathematisch Centrum. |
| 164 | */ |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 166 | |
| 167 | |
| 168 | /* There are two types of intermediate objects we're interested in: |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 169 | * '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] | 170 | * field of the object type to identify which any given object represents. |
| 171 | * These should probably go in an external header to allow other extensions |
| 172 | * 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] | 173 | */ |
| 174 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 175 | #define PyST_EXPR 1 |
| 176 | #define PyST_SUITE 2 |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 177 | |
| 178 | |
| 179 | /* These are the internal objects and definitions required to implement the |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 180 | * 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] | 181 | * naming style, but the code uses the new naming convention. |
| 182 | */ |
| 183 | |
| 184 | static PyObject* |
| 185 | parser_error = 0; |
| 186 | |
| 187 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 188 | typedef struct { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 189 | PyObject_HEAD /* standard object header */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 190 | node* st_node; /* the node* returned by the parser */ |
| 191 | int st_type; /* EXPR or SUITE ? */ |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 192 | PyCompilerFlags st_flags; /* Parser and compiler flags */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 193 | } PyST_Object; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 194 | |
| 195 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 196 | static void parser_free(PyST_Object *st); |
Jesus Cea | e9c5318 | 2012-08-03 14:28:37 +0200 | [diff] [blame] | 197 | static PyObject* parser_sizeof(PyST_Object *, void *); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 198 | static PyObject* parser_richcompare(PyObject *left, PyObject *right, int op); |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 199 | static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *); |
| 200 | static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *); |
| 201 | static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *); |
| 202 | static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *); |
| 203 | static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 204 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 205 | #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS) |
| 206 | |
| 207 | static PyMethodDef parser_methods[] = { |
| 208 | {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE, |
| 209 | PyDoc_STR("Compile this ST object into a code object.")}, |
| 210 | {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE, |
| 211 | PyDoc_STR("Determines if this ST object was created from an expression.")}, |
| 212 | {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE, |
| 213 | PyDoc_STR("Determines if this ST object was created from a suite.")}, |
| 214 | {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE, |
| 215 | PyDoc_STR("Creates a list-tree representation of this ST.")}, |
| 216 | {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE, |
| 217 | PyDoc_STR("Creates a tuple-tree representation of this ST.")}, |
Jesus Cea | e9c5318 | 2012-08-03 14:28:37 +0200 | [diff] [blame] | 218 | {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS, |
| 219 | PyDoc_STR("Returns size in memory, in bytes.")}, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 220 | {NULL, NULL, 0, NULL} |
| 221 | }; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 222 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 223 | static |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 224 | PyTypeObject PyST_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 225 | PyVarObject_HEAD_INIT(NULL, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 226 | "parser.st", /* tp_name */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 227 | (int) sizeof(PyST_Object), /* tp_basicsize */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 228 | 0, /* tp_itemsize */ |
| 229 | (destructor)parser_free, /* tp_dealloc */ |
| 230 | 0, /* tp_print */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 231 | 0, /* tp_getattr */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 232 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 233 | 0, /* tp_reserved */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 234 | 0, /* tp_repr */ |
| 235 | 0, /* tp_as_number */ |
| 236 | 0, /* tp_as_sequence */ |
| 237 | 0, /* tp_as_mapping */ |
| 238 | 0, /* tp_hash */ |
| 239 | 0, /* tp_call */ |
| 240 | 0, /* tp_str */ |
| 241 | 0, /* tp_getattro */ |
| 242 | 0, /* tp_setattro */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 243 | |
| 244 | /* Functions to access object as input/output buffer */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 245 | 0, /* tp_as_buffer */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 246 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 247 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Fred Drake | 69b9ae4 | 1997-05-23 04:04:17 +0000 | [diff] [blame] | 248 | |
| 249 | /* __doc__ */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 250 | "Intermediate representation of a Python parse tree.", |
| 251 | 0, /* tp_traverse */ |
| 252 | 0, /* tp_clear */ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 253 | parser_richcompare, /* tp_richcompare */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 254 | 0, /* tp_weaklistoffset */ |
| 255 | 0, /* tp_iter */ |
| 256 | 0, /* tp_iternext */ |
| 257 | parser_methods, /* tp_methods */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 258 | }; /* PyST_Type */ |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 259 | |
| 260 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 261 | /* PyST_Type isn't subclassable, so just check ob_type */ |
| 262 | #define PyST_Object_Check(v) ((v)->ob_type == &PyST_Type) |
| 263 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 264 | static int |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 265 | parser_compare_nodes(node *left, node *right) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 266 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 267 | int j; |
Guido van Rossum | 52f2c05 | 1993-11-10 12:53:24 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 269 | if (TYPE(left) < TYPE(right)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 270 | return (-1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 271 | |
| 272 | if (TYPE(right) < TYPE(left)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 273 | return (1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 274 | |
| 275 | if (ISTERMINAL(TYPE(left))) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 276 | return (strcmp(STR(left), STR(right))); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 277 | |
| 278 | if (NCH(left) < NCH(right)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 279 | return (-1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 280 | |
| 281 | if (NCH(right) < NCH(left)) |
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 | |
| 284 | for (j = 0; j < NCH(left); ++j) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 285 | int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j)); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 286 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 287 | if (v != 0) |
| 288 | return (v); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 289 | } |
| 290 | return (0); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 291 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 292 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 293 | /* parser_richcompare(PyObject* left, PyObject* right, int op) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 294 | * |
| 295 | * Comparison function used by the Python operators ==, !=, <, >, <=, >= |
| 296 | * This really just wraps a call to parser_compare_nodes() with some easy |
| 297 | * checks and protection code. |
| 298 | * |
| 299 | */ |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 300 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 301 | static PyObject * |
| 302 | parser_richcompare(PyObject *left, PyObject *right, int op) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 303 | { |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 304 | int result; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 305 | |
| 306 | /* neither argument should be NULL, unless something's gone wrong */ |
| 307 | if (left == NULL || right == NULL) { |
| 308 | PyErr_BadInternalCall(); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | /* both arguments should be instances of PyST_Object */ |
| 313 | if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 314 | Py_RETURN_NOTIMPLEMENTED; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 317 | if (left == right) |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 318 | /* if arguments are identical, they're equal */ |
| 319 | result = 0; |
| 320 | else |
| 321 | result = parser_compare_nodes(((PyST_Object *)left)->st_node, |
| 322 | ((PyST_Object *)right)->st_node); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 323 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 324 | Py_RETURN_RICHCOMPARE(result, 0, op); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 325 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 326 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 327 | /* parser_newstobject(node* st) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 328 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 329 | * 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] | 330 | * 'wrapper' object that holds a node* and allows it to be passed around in |
| 331 | * Python code. |
| 332 | * |
| 333 | */ |
| 334 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 335 | parser_newstobject(node *st, int type) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 336 | { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 337 | PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 338 | |
| 339 | if (o != 0) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 340 | o->st_node = st; |
| 341 | o->st_type = type; |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 342 | o->st_flags.cf_flags = 0; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 343 | } |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 344 | else { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 345 | PyNode_Free(st); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 346 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 347 | return ((PyObject*)o); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 348 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 349 | |
| 350 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 351 | /* void parser_free(PyST_Object* st) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 352 | * |
| 353 | * This is called by a del statement that reduces the reference count to 0. |
| 354 | * |
| 355 | */ |
| 356 | static void |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 357 | parser_free(PyST_Object *st) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 358 | { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 359 | PyNode_Free(st->st_node); |
| 360 | PyObject_Del(st); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 361 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 362 | |
Jesus Cea | e9c5318 | 2012-08-03 14:28:37 +0200 | [diff] [blame] | 363 | static PyObject * |
| 364 | parser_sizeof(PyST_Object *st, void *unused) |
| 365 | { |
| 366 | Py_ssize_t res; |
| 367 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 368 | res = _PyObject_SIZE(Py_TYPE(st)) + _PyNode_SizeOf(st->st_node); |
Jesus Cea | e9c5318 | 2012-08-03 14:28:37 +0200 | [diff] [blame] | 369 | return PyLong_FromSsize_t(res); |
| 370 | } |
| 371 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 372 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 373 | /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 374 | * |
| 375 | * 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] | 376 | * 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] | 377 | * |
| 378 | */ |
| 379 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 380 | parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 381 | { |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 382 | int line_info = 0; |
| 383 | int col_info = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 384 | PyObject *res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 385 | int ok; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 386 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 387 | static char *keywords[] = {"st", "line_info", "col_info", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 388 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 389 | if (self == NULL || PyModule_Check(self)) { |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 390 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2tuple", keywords, |
| 391 | &PyST_Type, &self, &line_info, |
| 392 | &col_info); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 393 | } |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 394 | else |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 395 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:totuple", &keywords[1], |
| 396 | &line_info, &col_info); |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 397 | if (ok != 0) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 398 | /* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 399 | * Convert ST into a tuple representation. Use Guido's function, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 400 | * since it's known to work already. |
| 401 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 402 | res = node2tuple(((PyST_Object*)self)->st_node, |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 403 | PyTuple_New, PyTuple_SetItem, line_info, col_info); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 404 | } |
| 405 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 406 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 407 | |
| 408 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 409 | /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 410 | * |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 411 | * 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] | 412 | * 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] | 413 | * |
| 414 | */ |
| 415 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 416 | parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 417 | { |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 418 | int line_info = 0; |
| 419 | int col_info = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 420 | PyObject *res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 421 | int ok; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 422 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 423 | static char *keywords[] = {"st", "line_info", "col_info", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 424 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 425 | if (self == NULL || PyModule_Check(self)) |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 426 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|pp:st2list", keywords, |
| 427 | &PyST_Type, &self, &line_info, |
| 428 | &col_info); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 429 | else |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 430 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|pp:tolist", &keywords[1], |
| 431 | &line_info, &col_info); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 432 | if (ok) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 433 | /* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 434 | * Convert ST into a tuple representation. Use Guido's function, |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 435 | * since it's known to work already. |
| 436 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 437 | res = node2tuple(self->st_node, |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 438 | PyList_New, PyList_SetItem, line_info, col_info); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 439 | } |
| 440 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 441 | } |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 442 | |
| 443 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 444 | /* parser_compilest(PyObject* self, PyObject* args) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 445 | * |
| 446 | * This function creates code objects from the parse tree represented by |
| 447 | * the passed-in data object. An optional file name is passed in as well. |
| 448 | * |
| 449 | */ |
| 450 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 451 | parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 452 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 453 | PyObject* res = NULL; |
| 454 | PyArena* arena = NULL; |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 455 | mod_ty mod; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 456 | PyObject* filename = NULL; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 457 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 458 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 459 | static char *keywords[] = {"st", "filename", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 460 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 461 | if (self == NULL || PyModule_Check(self)) |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 462 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords, |
| 463 | &PyST_Type, &self, |
| 464 | PyUnicode_FSDecoder, &filename); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 465 | else |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 466 | ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1], |
| 467 | PyUnicode_FSDecoder, &filename); |
| 468 | if (!ok) |
| 469 | goto error; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 470 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 471 | if (filename == NULL) { |
| 472 | filename = PyUnicode_FromString("<syntax-tree>"); |
| 473 | if (filename == NULL) |
| 474 | goto error; |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 475 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 476 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 477 | arena = PyArena_New(); |
| 478 | if (!arena) |
| 479 | goto error; |
| 480 | |
| 481 | mod = PyAST_FromNodeObject(self->st_node, &self->st_flags, |
| 482 | filename, arena); |
| 483 | if (!mod) |
| 484 | goto error; |
| 485 | |
| 486 | res = (PyObject *)PyAST_CompileObject(mod, filename, |
| 487 | &self->st_flags, -1, arena); |
| 488 | error: |
| 489 | Py_XDECREF(filename); |
| 490 | if (arena != NULL) |
| 491 | PyArena_Free(arena); |
| 492 | return res; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 493 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 494 | |
| 495 | |
| 496 | /* PyObject* parser_isexpr(PyObject* self, PyObject* args) |
| 497 | * PyObject* parser_issuite(PyObject* self, PyObject* args) |
| 498 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 499 | * 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] | 500 | * a statement suite, respectively. The return is a Python truth value. |
| 501 | * |
| 502 | */ |
| 503 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 504 | parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 505 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 506 | PyObject* res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 507 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 508 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 509 | static char *keywords[] = {"st", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 510 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 511 | if (self == NULL || PyModule_Check(self)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 512 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 513 | &PyST_Type, &self); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 514 | else |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 515 | ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 516 | |
| 517 | if (ok) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 518 | /* Check to see if the ST represents an expression or not. */ |
| 519 | res = (self->st_type == PyST_EXPR) ? Py_True : Py_False; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 520 | Py_INCREF(res); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 521 | } |
| 522 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 523 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 524 | |
| 525 | |
| 526 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 527 | parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 528 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 529 | PyObject* res = 0; |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 530 | int ok; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 531 | |
Georg Brandl | 30704ea0 | 2008-07-23 15:07:12 +0000 | [diff] [blame] | 532 | static char *keywords[] = {"st", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 533 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 534 | if (self == NULL || PyModule_Check(self)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 535 | ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 536 | &PyST_Type, &self); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 537 | else |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 538 | ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]); |
Fred Drake | 503d8d6 | 1998-04-13 18:45:18 +0000 | [diff] [blame] | 539 | |
| 540 | if (ok) { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 541 | /* Check to see if the ST represents an expression or not. */ |
| 542 | res = (self->st_type == PyST_EXPR) ? Py_False : Py_True; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 543 | Py_INCREF(res); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 544 | } |
| 545 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 546 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 547 | |
| 548 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 549 | /* err_string(const char* message) |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 550 | * |
| 551 | * Sets the error string for an exception of type ParserError. |
| 552 | * |
| 553 | */ |
| 554 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 555 | err_string(const char *message) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 556 | { |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 557 | PyErr_SetString(parser_error, message); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 558 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 559 | |
| 560 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 561 | /* PyObject* parser_do_parse(PyObject* args, int type) |
| 562 | * |
| 563 | * Internal function to actually execute the parse and return the result if |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 564 | * successful or set an exception if not. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 565 | * |
| 566 | */ |
| 567 | static PyObject* |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 568 | parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 569 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 570 | char* string = 0; |
| 571 | PyObject* res = 0; |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 572 | int flags = 0; |
| 573 | perrdetail err; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 574 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 575 | static char *keywords[] = {"source", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 576 | |
| 577 | if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) { |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 578 | node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL, |
| 579 | &_PyParser_Grammar, |
| 580 | (type == PyST_EXPR) |
| 581 | ? eval_input : file_input, |
| 582 | &err, &flags); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | if (n) { |
| 585 | res = parser_newstobject(n, type); |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 586 | if (res) |
| 587 | ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK; |
| 588 | } |
Benjamin Peterson | f719957d | 2011-06-04 22:06:42 -0500 | [diff] [blame] | 589 | else { |
Benjamin Peterson | f216c94 | 2008-10-31 02:28:05 +0000 | [diff] [blame] | 590 | PyParser_SetError(&err); |
Benjamin Peterson | f719957d | 2011-06-04 22:06:42 -0500 | [diff] [blame] | 591 | } |
Benjamin Peterson | f0cdbad | 2011-06-05 22:14:05 -0500 | [diff] [blame] | 592 | PyParser_ClearError(&err); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 593 | } |
| 594 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 595 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 596 | |
| 597 | |
| 598 | /* PyObject* parser_expr(PyObject* self, PyObject* args) |
| 599 | * PyObject* parser_suite(PyObject* self, PyObject* args) |
| 600 | * |
| 601 | * External interfaces to the parser itself. Which is called determines if |
| 602 | * the parser attempts to recognize an expression ('eval' form) or statement |
| 603 | * suite ('exec' form). The real work is done by parser_do_parse() above. |
| 604 | * |
| 605 | */ |
| 606 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 607 | parser_expr(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:expr", PyST_EXPR)); |
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 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 615 | parser_suite(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 616 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 617 | NOTE(ARGUNUSED(self)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 618 | return (parser_do_parse(args, kw, "s:suite", PyST_SUITE)); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 619 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 620 | |
| 621 | |
| 622 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 623 | /* 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] | 624 | * object requires that the input tuple be valid without having to rely on |
| 625 | * catching an exception from the compiler. This is done to allow the |
| 626 | * compiler itself to remain fast, since most of its input will come from |
| 627 | * the parser directly, and therefore be known to be syntactically correct. |
| 628 | * This validation is done to ensure that we don't core dump the compile |
| 629 | * phase, returning an exception instead. |
| 630 | * |
| 631 | * Two aspects can be broken out in this code: creating a node tree from |
| 632 | * 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] | 633 | * advantageous to expand the number of ST types to include funcdefs and |
| 634 | * lambdadefs to take advantage of the optimizer, recognizing those STs |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 635 | * here. They are not necessary, and not quite as useful in a raw form. |
| 636 | * For now, let's get expressions and suites working reliably. |
| 637 | */ |
| 638 | |
| 639 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 640 | static node* build_node_tree(PyObject *tuple); |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 641 | |
| 642 | static int |
| 643 | validate_node(node *tree) |
| 644 | { |
| 645 | int type = TYPE(tree); |
| 646 | int nch = NCH(tree); |
| 647 | dfa *nt_dfa; |
| 648 | state *dfa_state; |
| 649 | int pos, arc; |
| 650 | |
| 651 | assert(ISNONTERMINAL(type)); |
| 652 | type -= NT_OFFSET; |
| 653 | if (type >= _PyParser_Grammar.g_ndfas) { |
| 654 | PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree)); |
| 655 | return 0; |
| 656 | } |
| 657 | nt_dfa = &_PyParser_Grammar.g_dfa[type]; |
| 658 | REQ(tree, nt_dfa->d_type); |
| 659 | |
| 660 | /* Run the DFA for this nonterminal. */ |
| 661 | dfa_state = &nt_dfa->d_state[nt_dfa->d_initial]; |
| 662 | for (pos = 0; pos < nch; ++pos) { |
| 663 | node *ch = CHILD(tree, pos); |
| 664 | int ch_type = TYPE(ch); |
| 665 | for (arc = 0; arc < dfa_state->s_narcs; ++arc) { |
| 666 | short a_label = dfa_state->s_arc[arc].a_lbl; |
| 667 | assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels); |
| 668 | if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 669 | /* The child is acceptable; if non-terminal, validate it recursively. */ |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 670 | if (ISNONTERMINAL(ch_type) && !validate_node(ch)) |
| 671 | return 0; |
| 672 | |
| 673 | /* Update the state, and move on to the next child. */ |
| 674 | dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow]; |
| 675 | goto arc_found; |
| 676 | } |
| 677 | } |
| 678 | /* What would this state have accepted? */ |
| 679 | { |
| 680 | short a_label = dfa_state->s_arc->a_lbl; |
| 681 | int next_type; |
| 682 | if (!a_label) /* Wouldn't accept any more children */ |
| 683 | goto illegal_num_children; |
| 684 | |
| 685 | next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type; |
| 686 | if (ISNONTERMINAL(next_type)) |
| 687 | PyErr_Format(parser_error, "Expected node type %d, got %d.", |
| 688 | next_type, ch_type); |
| 689 | else |
| 690 | PyErr_Format(parser_error, "Illegal terminal: expected %s.", |
| 691 | _PyParser_TokenNames[next_type]); |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | arc_found: |
| 696 | continue; |
| 697 | } |
| 698 | /* Are we in a final state? If so, return 1 for successful validation. */ |
| 699 | for (arc = 0; arc < dfa_state->s_narcs; ++arc) { |
| 700 | if (!dfa_state->s_arc[arc].a_lbl) { |
| 701 | return 1; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | illegal_num_children: |
| 706 | PyErr_Format(parser_error, |
| 707 | "Illegal number of children for %s node.", nt_dfa->d_name); |
| 708 | return 0; |
| 709 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 710 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 711 | /* PyObject* parser_tuple2st(PyObject* self, PyObject* args) |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 712 | * |
| 713 | * 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] | 714 | * 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] | 715 | * tuple can be validated. It does this by checking the first code of the |
| 716 | * tuple, and, if acceptable, builds the internal representation. If this |
| 717 | * step succeeds, the internal representation is validated as fully as |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 718 | * possible with the recursive validate_node() routine defined above. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 719 | * |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 720 | * This function must be changed if support is to be added for PyST_FRAGMENT |
| 721 | * ST objects. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 722 | * |
| 723 | */ |
| 724 | static PyObject* |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 725 | parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 726 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 727 | NOTE(ARGUNUSED(self)) |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 728 | PyObject *st = 0; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 729 | PyObject *tuple; |
| 730 | node *tree; |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 731 | |
Martin v. Löwis | b79afb6 | 2006-02-27 17:01:22 +0000 | [diff] [blame] | 732 | static char *keywords[] = {"sequence", NULL}; |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 733 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 734 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords, |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 735 | &tuple)) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 736 | return (0); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 737 | if (!PySequence_Check(tuple)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 738 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 739 | "sequence2st() requires a single sequence argument"); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 740 | return (0); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 741 | } |
| 742 | /* |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 743 | * Convert the tree to the internal form before checking it. |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 744 | */ |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 745 | tree = build_node_tree(tuple); |
| 746 | if (tree != 0) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 747 | node *validation_root = NULL; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 748 | int tree_type = 0; |
| 749 | switch (TYPE(tree)) { |
| 750 | case eval_input: |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 751 | /* Might be an eval form. */ |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 752 | tree_type = PyST_EXPR; |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 753 | validation_root = tree; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 754 | break; |
| 755 | case encoding_decl: |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 756 | /* This looks like an encoding_decl so far. */ |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 757 | if (NCH(tree) == 1) { |
| 758 | tree_type = PyST_SUITE; |
| 759 | validation_root = CHILD(tree, 0); |
| 760 | } |
| 761 | else { |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 762 | err_string("Error Parsing encoding_decl"); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 763 | } |
| 764 | break; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 765 | case file_input: |
| 766 | /* This looks like an exec form so far. */ |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 767 | tree_type = PyST_SUITE; |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 768 | validation_root = tree; |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 769 | break; |
| 770 | default: |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 771 | /* This is a fragment, at best. */ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 772 | err_string("parse tree does not use a valid start symbol"); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 773 | } |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 774 | |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 775 | if (validation_root != NULL && validate_node(validation_root)) |
Benjamin Peterson | 53595c4 | 2016-06-02 11:30:18 -0700 | [diff] [blame] | 776 | st = parser_newstobject(tree, tree_type); |
| 777 | else |
| 778 | PyNode_Free(tree); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 779 | } |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 780 | /* 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] | 781 | * get this, but we'd do well to be sure something is done. |
| 782 | */ |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 783 | if (st == NULL && !PyErr_Occurred()) |
| 784 | err_string("unspecified ST error occurred"); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 785 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 786 | return st; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 787 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 788 | |
| 789 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 790 | /* node* build_node_children() |
| 791 | * |
| 792 | * Iterate across the children of the current non-terminal node and build |
| 793 | * their structures. If successful, return the root of this portion of |
| 794 | * the tree, otherwise, 0. Any required exception will be specified already, |
| 795 | * and no memory will have been deallocated. |
| 796 | * |
| 797 | */ |
| 798 | static node* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 799 | build_node_children(PyObject *tuple, node *root, int *line_num) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 800 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 801 | Py_ssize_t len = PyObject_Size(tuple); |
| 802 | Py_ssize_t i; |
| 803 | int err; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 804 | |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 805 | if (len < 0) { |
| 806 | return NULL; |
| 807 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 808 | for (i = 1; i < len; ++i) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 809 | /* elem must always be a sequence, however simple */ |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 810 | PyObject* elem = PySequence_GetItem(tuple, i); |
| 811 | int ok = elem != NULL; |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 812 | int type = 0; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 813 | char *strn = 0; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 814 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 815 | if (ok) |
| 816 | ok = PySequence_Check(elem); |
| 817 | if (ok) { |
| 818 | PyObject *temp = PySequence_GetItem(elem, 0); |
| 819 | if (temp == NULL) |
| 820 | ok = 0; |
| 821 | else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 822 | ok = PyLong_Check(temp); |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 823 | if (ok) { |
| 824 | type = _PyLong_AsInt(temp); |
| 825 | if (type == -1 && PyErr_Occurred()) { |
| 826 | Py_DECREF(temp); |
| 827 | Py_DECREF(elem); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 828 | return NULL; |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 829 | } |
| 830 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 831 | Py_DECREF(temp); |
| 832 | } |
| 833 | } |
| 834 | if (!ok) { |
Victor Stinner | 5f8d485 | 2014-01-02 11:49:27 +0100 | [diff] [blame] | 835 | PyObject *err = Py_BuildValue("Os", elem, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 836 | "Illegal node construct."); |
| 837 | PyErr_SetObject(parser_error, err); |
| 838 | Py_XDECREF(err); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 839 | Py_XDECREF(elem); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 840 | return NULL; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 841 | } |
| 842 | if (ISTERMINAL(type)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 843 | Py_ssize_t len = PyObject_Size(elem); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 844 | PyObject *temp; |
Neal Norwitz | 3fcbea5 | 2007-08-26 04:51:28 +0000 | [diff] [blame] | 845 | const char *temp_str; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 846 | |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 847 | if ((len != 2) && (len != 3)) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 848 | err_string("terminal nodes must have 2 or 3 entries"); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 849 | Py_DECREF(elem); |
| 850 | return NULL; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 851 | } |
| 852 | temp = PySequence_GetItem(elem, 1); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 853 | if (temp == NULL) { |
| 854 | Py_DECREF(elem); |
| 855 | return NULL; |
| 856 | } |
Neal Norwitz | 3fcbea5 | 2007-08-26 04:51:28 +0000 | [diff] [blame] | 857 | if (!PyUnicode_Check(temp)) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 858 | PyErr_Format(parser_error, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 859 | "second item in terminal node must be a string," |
| 860 | " found %s", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 861 | Py_TYPE(temp)->tp_name); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 862 | Py_DECREF(temp); |
Neal Norwitz | 2cde0eb | 2007-08-11 04:58:43 +0000 | [diff] [blame] | 863 | Py_DECREF(elem); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 864 | return NULL; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 865 | } |
| 866 | if (len == 3) { |
| 867 | PyObject *o = PySequence_GetItem(elem, 2); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 868 | if (o == NULL) { |
| 869 | Py_DECREF(temp); |
| 870 | Py_DECREF(elem); |
| 871 | return NULL; |
| 872 | } |
| 873 | if (PyLong_Check(o)) { |
| 874 | int num = _PyLong_AsInt(o); |
| 875 | if (num == -1 && PyErr_Occurred()) { |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 876 | Py_DECREF(o); |
| 877 | Py_DECREF(temp); |
Neal Norwitz | 2cde0eb | 2007-08-11 04:58:43 +0000 | [diff] [blame] | 878 | Py_DECREF(elem); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 879 | return NULL; |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 880 | } |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 881 | *line_num = num; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 882 | } |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 883 | else { |
| 884 | PyErr_Format(parser_error, |
| 885 | "third item in terminal node must be an" |
| 886 | " integer, found %s", |
| 887 | Py_TYPE(temp)->tp_name); |
| 888 | Py_DECREF(o); |
| 889 | Py_DECREF(temp); |
| 890 | Py_DECREF(elem); |
| 891 | return NULL; |
| 892 | } |
| 893 | Py_DECREF(o); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 894 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 895 | temp_str = PyUnicode_AsUTF8AndSize(temp, &len); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 896 | if (temp_str == NULL) { |
| 897 | Py_DECREF(temp); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 898 | Py_DECREF(elem); |
| 899 | return NULL; |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 900 | } |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 901 | strn = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 902 | if (strn == NULL) { |
| 903 | Py_DECREF(temp); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 904 | Py_DECREF(elem); |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 905 | PyErr_NoMemory(); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 906 | return NULL; |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 907 | } |
| 908 | (void) memcpy(strn, temp_str, len + 1); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 909 | Py_DECREF(temp); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 910 | } |
| 911 | else if (!ISNONTERMINAL(type)) { |
| 912 | /* |
| 913 | * It has to be one or the other; this is an error. |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 914 | * Raise an exception. |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 915 | */ |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 916 | PyObject *err = Py_BuildValue("Os", elem, "unknown node type."); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 917 | PyErr_SetObject(parser_error, err); |
| 918 | Py_XDECREF(err); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 919 | Py_DECREF(elem); |
| 920 | return NULL; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 921 | } |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 922 | err = PyNode_AddChild(root, type, strn, *line_num, 0); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 923 | if (err == E_NOMEM) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 924 | Py_DECREF(elem); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 925 | PyObject_FREE(strn); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 926 | PyErr_NoMemory(); |
| 927 | return NULL; |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 928 | } |
| 929 | if (err == E_OVERFLOW) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 930 | Py_DECREF(elem); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 931 | PyObject_FREE(strn); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 932 | PyErr_SetString(PyExc_ValueError, |
| 933 | "unsupported number of child nodes"); |
| 934 | return NULL; |
| 935 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 936 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 937 | if (ISNONTERMINAL(type)) { |
| 938 | node* new_child = CHILD(root, i - 1); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 939 | |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 940 | if (new_child != build_node_children(elem, new_child, line_num)) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 941 | Py_DECREF(elem); |
| 942 | return NULL; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | else if (type == NEWLINE) { /* It's true: we increment the */ |
| 946 | ++(*line_num); /* line number *after* the newline! */ |
| 947 | } |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 948 | Py_DECREF(elem); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 949 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 950 | return root; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 951 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 952 | |
| 953 | |
| 954 | static node* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 955 | build_node_tree(PyObject *tuple) |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 956 | { |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 957 | node* res = 0; |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 958 | PyObject *temp = PySequence_GetItem(tuple, 0); |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 959 | long num = -1; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 960 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 961 | if (temp != NULL) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 962 | num = PyLong_AsLong(temp); |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 963 | Py_XDECREF(temp); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 964 | if (ISTERMINAL(num)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 965 | /* |
| 966 | * The tuple is simple, but it doesn't start with a start symbol. |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 967 | * Raise an exception now and be done with it. |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 968 | */ |
Victor Stinner | 6684bdf | 2013-07-17 00:13:52 +0200 | [diff] [blame] | 969 | tuple = Py_BuildValue("Os", tuple, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 970 | "Illegal syntax-tree; cannot start with terminal symbol."); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 971 | PyErr_SetObject(parser_error, tuple); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 972 | Py_XDECREF(tuple); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 973 | } |
| 974 | else if (ISNONTERMINAL(num)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 975 | /* |
| 976 | * Not efficient, but that can be handled later. |
| 977 | */ |
| 978 | int line_num = 0; |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 979 | PyObject *encoding = NULL; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 980 | |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 981 | if (num == encoding_decl) { |
| 982 | encoding = PySequence_GetItem(tuple, 2); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 983 | if (encoding == NULL) { |
| 984 | PyErr_SetString(parser_error, "missed encoding"); |
| 985 | return NULL; |
| 986 | } |
| 987 | if (!PyUnicode_Check(encoding)) { |
| 988 | PyErr_Format(parser_error, |
| 989 | "encoding must be a string, found %.200s", |
| 990 | Py_TYPE(encoding)->tp_name); |
| 991 | Py_DECREF(encoding); |
| 992 | return NULL; |
| 993 | } |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 994 | /* tuple isn't borrowed anymore here, need to DECREF */ |
| 995 | tuple = PySequence_GetSlice(tuple, 0, 2); |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 996 | if (tuple == NULL) { |
| 997 | Py_DECREF(encoding); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 998 | return NULL; |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 999 | } |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1000 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1001 | res = PyNode_New(num); |
Fred Drake | 8b55b2d | 2001-12-05 22:10:44 +0000 | [diff] [blame] | 1002 | if (res != NULL) { |
| 1003 | if (res != build_node_children(tuple, res, &line_num)) { |
| 1004 | PyNode_Free(res); |
| 1005 | res = NULL; |
| 1006 | } |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1007 | if (res && encoding) { |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1008 | Py_ssize_t len; |
Neal Norwitz | 3fcbea5 | 2007-08-26 04:51:28 +0000 | [diff] [blame] | 1009 | const char *temp; |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 1010 | temp = PyUnicode_AsUTF8AndSize(encoding, &len); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1011 | if (temp == NULL) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1012 | PyNode_Free(res); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1013 | Py_DECREF(encoding); |
| 1014 | Py_DECREF(tuple); |
| 1015 | return NULL; |
| 1016 | } |
Alexandre Vassalotti | a85998a | 2008-05-03 18:24:43 +0000 | [diff] [blame] | 1017 | res->n_str = (char *)PyObject_MALLOC(len + 1); |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 1018 | if (res->n_str == NULL) { |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1019 | PyNode_Free(res); |
Victor Stinner | 3bd6abd | 2013-07-12 01:33:59 +0200 | [diff] [blame] | 1020 | Py_DECREF(encoding); |
| 1021 | Py_DECREF(tuple); |
| 1022 | PyErr_NoMemory(); |
| 1023 | return NULL; |
| 1024 | } |
| 1025 | (void) memcpy(res->n_str, temp, len + 1); |
Michael W. Hudson | df1252d | 2003-02-08 18:05:10 +0000 | [diff] [blame] | 1026 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1027 | } |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1028 | if (encoding != NULL) { |
| 1029 | Py_DECREF(encoding); |
| 1030 | Py_DECREF(tuple); |
| 1031 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1032 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1033 | else { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1034 | /* The tuple is illegal -- if the number is neither TERMINAL nor |
Fred Drake | 0ac9b07 | 2000-09-12 21:58:06 +0000 | [diff] [blame] | 1035 | * NONTERMINAL, we can't use it. Not sure the implementation |
| 1036 | * allows this condition, but the API doesn't preclude it. |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1037 | */ |
Serhiy Storchaka | a79f4c2 | 2017-04-19 21:09:21 +0300 | [diff] [blame] | 1038 | PyObject *err = Py_BuildValue("Os", tuple, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1039 | "Illegal component tuple."); |
| 1040 | PyErr_SetObject(parser_error, err); |
| 1041 | Py_XDECREF(err); |
| 1042 | } |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1043 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1044 | return (res); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1045 | } |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1046 | |
| 1047 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1048 | static PyObject* |
| 1049 | pickle_constructor = NULL; |
| 1050 | |
| 1051 | |
| 1052 | static PyObject* |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1053 | parser__pickler(PyObject *self, PyObject *args) |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1054 | { |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 1055 | NOTE(ARGUNUSED(self)) |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1056 | PyObject *result = NULL; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1057 | PyObject *st = NULL; |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 1058 | PyObject *empty_dict = NULL; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1059 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1060 | if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1061 | PyObject *newargs; |
| 1062 | PyObject *tuple; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1063 | |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 1064 | if ((empty_dict = PyDict_New()) == NULL) |
| 1065 | goto finally; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1066 | if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL) |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1067 | goto finally; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1068 | tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1069 | if (tuple != NULL) { |
| 1070 | result = Py_BuildValue("O(O)", pickle_constructor, tuple); |
| 1071 | Py_DECREF(tuple); |
| 1072 | } |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1073 | Py_DECREF(newargs); |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1074 | } |
| 1075 | finally: |
Fred Drake | 2a6875e | 1999-09-20 22:32:18 +0000 | [diff] [blame] | 1076 | Py_XDECREF(empty_dict); |
| 1077 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1078 | return (result); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1079 | } |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1080 | |
| 1081 | |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1082 | /* Functions exported by this module. Most of this should probably |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1083 | * 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] | 1084 | * done directly in Python, allowing subclasses to be created directly. |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1085 | * We'd really have to write a wrapper around it all anyway to allow |
| 1086 | * inheritance. |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1087 | */ |
| 1088 | static PyMethodDef parser_functions[] = { |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1089 | {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1090 | PyDoc_STR("Compiles an ST object into a code object.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1091 | {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1092 | PyDoc_STR("Creates an ST object from an expression.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1093 | {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1094 | PyDoc_STR("Determines if an ST object was created from an expression.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1095 | {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1096 | PyDoc_STR("Determines if an ST object was created from a suite.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1097 | {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1098 | PyDoc_STR("Creates an ST object from a suite.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1099 | {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1100 | PyDoc_STR("Creates an ST object from a tree representation.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1101 | {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1102 | PyDoc_STR("Creates a tuple-tree representation of an ST.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1103 | {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1104 | PyDoc_STR("Creates a list-tree representation of an ST.")}, |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1105 | {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1106 | PyDoc_STR("Creates an ST object from a tree representation.")}, |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1107 | |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1108 | /* private stuff: support pickle module */ |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1109 | {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 1110 | 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] | 1111 | |
Fred Drake | 268397f | 1998-04-29 14:16:32 +0000 | [diff] [blame] | 1112 | {NULL, NULL, 0, NULL} |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1113 | }; |
| 1114 | |
| 1115 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1116 | |
| 1117 | static struct PyModuleDef parsermodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | PyModuleDef_HEAD_INIT, |
| 1119 | "parser", |
| 1120 | NULL, |
| 1121 | -1, |
| 1122 | parser_functions, |
| 1123 | NULL, |
| 1124 | NULL, |
| 1125 | NULL, |
| 1126 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1127 | }; |
| 1128 | |
| 1129 | PyMODINIT_FUNC PyInit_parser(void); /* supply a prototype */ |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1130 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1131 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1132 | PyInit_parser(void) |
Fred Drake | 28f739a | 2000-08-25 22:42:40 +0000 | [diff] [blame] | 1133 | { |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1134 | PyObject *module, *copyreg; |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1135 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 1136 | if (PyType_Ready(&PyST_Type) < 0) |
| 1137 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1138 | module = PyModule_Create(&parsermodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1139 | if (module == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | return NULL; |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1141 | |
Fred Drake | 7a15ba5 | 1999-09-09 14:21:52 +0000 | [diff] [blame] | 1142 | if (parser_error == 0) |
| 1143 | parser_error = PyErr_NewException("parser.ParserError", NULL, NULL); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1144 | |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 1145 | if (parser_error == 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1146 | return NULL; |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 1147 | /* CAUTION: The code next used to skip bumping the refcount on |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1148 | * parser_error. That's a disaster if PyInit_parser() gets called more |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 1149 | * than once. By incref'ing, we ensure that each module dict that |
| 1150 | * gets created owns its reference to the shared parser_error object, |
| 1151 | * and the file static parser_error vrbl owns a reference too. |
| 1152 | */ |
| 1153 | Py_INCREF(parser_error); |
| 1154 | if (PyModule_AddObject(module, "ParserError", parser_error) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1155 | return NULL; |
Tim Peters | 6a62725 | 2003-07-21 14:25:23 +0000 | [diff] [blame] | 1156 | |
Fred Drake | c2683dd | 2001-07-17 19:32:05 +0000 | [diff] [blame] | 1157 | Py_INCREF(&PyST_Type); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1158 | PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type); |
Guido van Rossum | 3d602e3 | 1996-07-21 02:33:56 +0000 | [diff] [blame] | 1159 | |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1160 | PyModule_AddStringConstant(module, "__copyright__", |
| 1161 | parser_copyright_string); |
| 1162 | PyModule_AddStringConstant(module, "__doc__", |
| 1163 | parser_doc_string); |
| 1164 | PyModule_AddStringConstant(module, "__version__", |
| 1165 | parser_version_string); |
Guido van Rossum | d9e9f9c | 1995-10-11 17:35:38 +0000 | [diff] [blame] | 1166 | |
Fred Drake | 78bdb9b | 2001-07-19 20:17:15 +0000 | [diff] [blame] | 1167 | /* Register to support pickling. |
| 1168 | * If this fails, the import of this module will fail because an |
| 1169 | * exception will be raised here; should we clear the exception? |
| 1170 | */ |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 1171 | copyreg = PyImport_ImportModuleNoBlock("copyreg"); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1172 | if (copyreg != NULL) { |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1173 | PyObject *func, *pickler; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1174 | _Py_IDENTIFIER(pickle); |
| 1175 | _Py_IDENTIFIER(sequence2st); |
| 1176 | _Py_IDENTIFIER(_pickler); |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1177 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1178 | func = _PyObject_GetAttrId(copyreg, &PyId_pickle); |
| 1179 | pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st); |
| 1180 | pickler = _PyObject_GetAttrId(module, &PyId__pickler); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1181 | Py_XINCREF(pickle_constructor); |
| 1182 | if ((func != NULL) && (pickle_constructor != NULL) |
| 1183 | && (pickler != NULL)) { |
| 1184 | PyObject *res; |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1185 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1186 | res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler, |
| 1187 | pickle_constructor, NULL); |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1188 | Py_XDECREF(res); |
| 1189 | } |
| 1190 | Py_XDECREF(func); |
Fred Drake | 13130bc | 2001-08-15 16:44:56 +0000 | [diff] [blame] | 1191 | Py_XDECREF(pickle_constructor); |
| 1192 | Py_XDECREF(pickler); |
| 1193 | Py_DECREF(copyreg); |
Fred Drake | 43f8f9b | 1998-04-13 16:25:46 +0000 | [diff] [blame] | 1194 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1195 | return module; |
Fred Drake | ff9ea48 | 2000-04-19 13:54:15 +0000 | [diff] [blame] | 1196 | } |