You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3485 lines
101 KiB

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