/Modules/parsermodule.c

http://unladen-swallow.googlecode.com/ · C · 3405 lines · 2521 code · 428 blank · 456 comment · 1103 complexity · b2835d6145228b011be75992b6438b0a MD5 · raw file

Large files are truncated click here to view the full file

  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 int parser_compare(PyST_Object *left, PyST_Object *right);
  148. static PyObject *parser_getattr(PyObject *self, char *name);
  149. static
  150. PyTypeObject PyST_Type = {
  151. PyVarObject_HEAD_INIT(NULL, 0)
  152. "parser.st", /* tp_name */
  153. (int) sizeof(PyST_Object), /* tp_basicsize */
  154. 0, /* tp_itemsize */
  155. (destructor)parser_free, /* tp_dealloc */
  156. 0, /* tp_print */
  157. parser_getattr, /* tp_getattr */
  158. 0, /* tp_setattr */
  159. (cmpfunc)parser_compare, /* tp_compare */
  160. 0, /* tp_repr */
  161. 0, /* tp_as_number */
  162. 0, /* tp_as_sequence */
  163. 0, /* tp_as_mapping */
  164. 0, /* tp_hash */
  165. 0, /* tp_call */
  166. 0, /* tp_str */
  167. 0, /* tp_getattro */
  168. 0, /* tp_setattro */
  169. /* Functions to access object as input/output buffer */
  170. 0, /* tp_as_buffer */
  171. Py_TPFLAGS_DEFAULT, /* tp_flags */
  172. /* __doc__ */
  173. "Intermediate representation of a Python parse tree."
  174. }; /* PyST_Type */
  175. static int
  176. parser_compare_nodes(node *left, node *right)
  177. {
  178. int j;
  179. if (TYPE(left) < TYPE(right))
  180. return (-1);
  181. if (TYPE(right) < TYPE(left))
  182. return (1);
  183. if (ISTERMINAL(TYPE(left)))
  184. return (strcmp(STR(left), STR(right)));
  185. if (NCH(left) < NCH(right))
  186. return (-1);
  187. if (NCH(right) < NCH(left))
  188. return (1);
  189. for (j = 0; j < NCH(left); ++j) {
  190. int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
  191. if (v != 0)
  192. return (v);
  193. }
  194. return (0);
  195. }
  196. /* int parser_compare(PyST_Object* left, PyST_Object* right)
  197. *
  198. * Comparison function used by the Python operators ==, !=, <, >, <=, >=
  199. * This really just wraps a call to parser_compare_nodes() with some easy
  200. * checks and protection code.
  201. *
  202. */
  203. static int
  204. parser_compare(PyST_Object *left, PyST_Object *right)
  205. {
  206. if (left == right)
  207. return (0);
  208. if ((left == 0) || (right == 0))
  209. return (-1);
  210. return (parser_compare_nodes(left->st_node, right->st_node));
  211. }
  212. /* parser_newstobject(node* st)
  213. *
  214. * Allocates a new Python object representing an ST. This is simply the
  215. * 'wrapper' object that holds a node* and allows it to be passed around in
  216. * Python code.
  217. *
  218. */
  219. static PyObject*
  220. parser_newstobject(node *st, int type)
  221. {
  222. PyST_Object* o = PyObject_New(PyST_Object, &PyST_Type);
  223. if (o != 0) {
  224. o->st_node = st;
  225. o->st_type = type;
  226. o->st_flags.cf_flags = 0;
  227. }
  228. else {
  229. PyNode_Free(st);
  230. }
  231. return ((PyObject*)o);
  232. }
  233. /* void parser_free(PyST_Object* st)
  234. *
  235. * This is called by a del statement that reduces the reference count to 0.
  236. *
  237. */
  238. static void
  239. parser_free(PyST_Object *st)
  240. {
  241. PyNode_Free(st->st_node);
  242. PyObject_Del(st);
  243. }
  244. /* parser_st2tuple(PyObject* self, PyObject* args, PyObject* kw)
  245. *
  246. * This provides conversion from a node* to a tuple object that can be
  247. * returned to the Python-level caller. The ST object is not modified.
  248. *
  249. */
  250. static PyObject*
  251. parser_st2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
  252. {
  253. PyObject *line_option = 0;
  254. PyObject *col_option = 0;
  255. PyObject *res = 0;
  256. int ok;
  257. static char *keywords[] = {"ast", "line_info", "col_info", NULL};
  258. if (self == NULL) {
  259. ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2tuple", keywords,
  260. &PyST_Type, &self, &line_option,
  261. &col_option);
  262. }
  263. else
  264. ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:totuple", &keywords[1],
  265. &line_option, &col_option);
  266. if (ok != 0) {
  267. int lineno = 0;
  268. int col_offset = 0;
  269. if (line_option != NULL) {
  270. lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0;
  271. }
  272. if (col_option != NULL) {
  273. col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
  274. }
  275. /*
  276. * Convert ST into a tuple representation. Use Guido's function,
  277. * since it's known to work already.
  278. */
  279. res = node2tuple(((PyST_Object*)self)->st_node,
  280. PyTuple_New, PyTuple_SetItem, lineno, col_offset);
  281. }
  282. return (res);
  283. }
  284. static PyObject*
  285. parser_ast2tuple(PyST_Object *self, PyObject *args, PyObject *kw)
  286. {
  287. if (PyErr_WarnPy3k("ast2tuple is removed in 3.x; use st2tuple", 1) < 0)
  288. return NULL;
  289. return parser_st2tuple(self, args, kw);
  290. }
  291. /* parser_st2list(PyObject* self, PyObject* args, PyObject* kw)
  292. *
  293. * This provides conversion from a node* to a list object that can be
  294. * returned to the Python-level caller. The ST object is not modified.
  295. *
  296. */
  297. static PyObject*
  298. parser_st2list(PyST_Object *self, PyObject *args, PyObject *kw)
  299. {
  300. PyObject *line_option = 0;
  301. PyObject *col_option = 0;
  302. PyObject *res = 0;
  303. int ok;
  304. static char *keywords[] = {"ast", "line_info", "col_info", NULL};
  305. if (self == NULL)
  306. ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|OO:st2list", keywords,
  307. &PyST_Type, &self, &line_option,
  308. &col_option);
  309. else
  310. ok = PyArg_ParseTupleAndKeywords(args, kw, "|OO:tolist", &keywords[1],
  311. &line_option, &col_option);
  312. if (ok) {
  313. int lineno = 0;
  314. int col_offset = 0;
  315. if (line_option != 0) {
  316. lineno = PyObject_IsTrue(line_option) ? 1 : 0;
  317. }
  318. if (col_option != NULL) {
  319. col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0;
  320. }
  321. /*
  322. * Convert ST into a tuple representation. Use Guido's function,
  323. * since it's known to work already.
  324. */
  325. res = node2tuple(self->st_node,
  326. PyList_New, PyList_SetItem, lineno, col_offset);
  327. }
  328. return (res);
  329. }
  330. static PyObject*
  331. parser_ast2list(PyST_Object *self, PyObject *args, PyObject *kw)
  332. {
  333. if (PyErr_WarnPy3k("ast2list is removed in 3.x; use st2list", 1) < 0)
  334. return NULL;
  335. return parser_st2list(self, args, kw);
  336. }
  337. /* parser_compilest(PyObject* self, PyObject* args)
  338. *
  339. * This function creates code objects from the parse tree represented by
  340. * the passed-in data object. An optional file name is passed in as well.
  341. *
  342. */
  343. static PyObject*
  344. parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw)
  345. {
  346. PyObject* res = 0;
  347. PyArena* arena;
  348. mod_ty mod;
  349. char* str = "<syntax-tree>";
  350. int ok;
  351. static char *keywords[] = {"ast", "filename", NULL};
  352. if (self == NULL)
  353. ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|s:compilest", keywords,
  354. &PyST_Type, &self, &str);
  355. else
  356. ok = PyArg_ParseTupleAndKeywords(args, kw, "|s:compile", &keywords[1],
  357. &str);
  358. if (ok) {
  359. arena = PyArena_New();
  360. if (arena) {
  361. mod = PyAST_FromNode(self->st_node, &(self->st_flags), str, arena);
  362. if (mod) {
  363. res = (PyObject *)PyAST_Compile(mod, str, &(self->st_flags), arena);
  364. }
  365. PyArena_Free(arena);
  366. }
  367. }
  368. return (res);
  369. }
  370. static PyObject*
  371. parser_compileast(PyST_Object *self, PyObject *args, PyObject *kw)
  372. {
  373. if (PyErr_WarnPy3k("compileast is removed in 3.x; use compilest", 1) < 0)
  374. return NULL;
  375. return parser_compilest(self, args, kw);
  376. }
  377. /* PyObject* parser_isexpr(PyObject* self, PyObject* args)
  378. * PyObject* parser_issuite(PyObject* self, PyObject* args)
  379. *
  380. * Checks the passed-in ST object to determine if it is an expression or
  381. * a statement suite, respectively. The return is a Python truth value.
  382. *
  383. */
  384. static PyObject*
  385. parser_isexpr(PyST_Object *self, PyObject *args, PyObject *kw)
  386. {
  387. PyObject* res = 0;
  388. int ok;
  389. static char *keywords[] = {"ast", NULL};
  390. if (self == NULL)
  391. ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:isexpr", keywords,
  392. &PyST_Type, &self);
  393. else
  394. ok = PyArg_ParseTupleAndKeywords(args, kw, ":isexpr", &keywords[1]);
  395. if (ok) {
  396. /* Check to see if the ST represents an expression or not. */
  397. res = (self->st_type == PyST_EXPR) ? Py_True : Py_False;
  398. Py_INCREF(res);
  399. }
  400. return (res);
  401. }
  402. static PyObject*
  403. parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
  404. {
  405. PyObject* res = 0;
  406. int ok;
  407. static char *keywords[] = {"ast", NULL};
  408. if (self == NULL)
  409. ok = PyArg_ParseTupleAndKeywords(args, kw, "O!:issuite", keywords,
  410. &PyST_Type, &self);
  411. else
  412. ok = PyArg_ParseTupleAndKeywords(args, kw, ":issuite", &keywords[1]);
  413. if (ok) {
  414. /* Check to see if the ST represents an expression or not. */
  415. res = (self->st_type == PyST_EXPR) ? Py_False : Py_True;
  416. Py_INCREF(res);
  417. }
  418. return (res);
  419. }
  420. #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
  421. static PyMethodDef
  422. parser_methods[] = {
  423. {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
  424. PyDoc_STR("Compile this ST object into a code object.")},
  425. {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
  426. PyDoc_STR("Determines if this ST object was created from an expression.")},
  427. {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
  428. PyDoc_STR("Determines if this ST object was created from a suite.")},
  429. {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
  430. PyDoc_STR("Creates a list-tree representation of this ST.")},
  431. {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
  432. PyDoc_STR("Creates a tuple-tree representation of this ST.")},
  433. {NULL, NULL, 0, NULL}
  434. };
  435. static PyObject*
  436. parser_getattr(PyObject *self, char *name)
  437. {
  438. return (Py_FindMethod(parser_methods, self, name));
  439. }
  440. /* err_string(char* message)
  441. *
  442. * Sets the error string for an exception of type ParserError.
  443. *
  444. */
  445. static void
  446. err_string(char *message)
  447. {
  448. PyErr_SetString(parser_error, message);
  449. }
  450. /* PyObject* parser_do_parse(PyObject* args, int type)
  451. *
  452. * Internal function to actually execute the parse and return the result if
  453. * successful or set an exception if not.
  454. *
  455. */
  456. static PyObject*
  457. parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
  458. {
  459. char* string = 0;
  460. PyObject* res = 0;
  461. int flags = 0;
  462. perrdetail err;
  463. static char *keywords[] = {"source", NULL};
  464. if (PyArg_ParseTupleAndKeywords(args, kw, argspec, keywords, &string)) {
  465. node* n = PyParser_ParseStringFlagsFilenameEx(string, NULL,
  466. &_PyParser_Grammar,
  467. (type == PyST_EXPR)
  468. ? eval_input : file_input,
  469. &err, &flags);
  470. if (n) {
  471. res = parser_newstobject(n, type);
  472. if (res)
  473. ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
  474. }
  475. else
  476. PyParser_SetError(&err);
  477. }
  478. return (res);
  479. }
  480. /* PyObject* parser_expr(PyObject* self, PyObject* args)
  481. * PyObject* parser_suite(PyObject* self, PyObject* args)
  482. *
  483. * External interfaces to the parser itself. Which is called determines if
  484. * the parser attempts to recognize an expression ('eval' form) or statement
  485. * suite ('exec' form). The real work is done by parser_do_parse() above.
  486. *
  487. */
  488. static PyObject*
  489. parser_expr(PyST_Object *self, PyObject *args, PyObject *kw)
  490. {
  491. NOTE(ARGUNUSED(self))
  492. return (parser_do_parse(args, kw, "s:expr", PyST_EXPR));
  493. }
  494. static PyObject*
  495. parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
  496. {
  497. NOTE(ARGUNUSED(self))
  498. return (parser_do_parse(args, kw, "s:suite", PyST_SUITE));
  499. }
  500. /* This is the messy part of the code. Conversion from a tuple to an ST
  501. * object requires that the input tuple be valid without having to rely on
  502. * catching an exception from the compiler. This is done to allow the
  503. * compiler itself to remain fast, since most of its input will come from
  504. * the parser directly, and therefore be known to be syntactically correct.
  505. * This validation is done to ensure that we don't core dump the compile
  506. * phase, returning an exception instead.
  507. *
  508. * Two aspects can be broken out in this code: creating a node tree from
  509. * the tuple passed in, and verifying that it is indeed valid. It may be
  510. * advantageous to expand the number of ST types to include funcdefs and
  511. * lambdadefs to take advantage of the optimizer, recognizing those STs
  512. * here. They are not necessary, and not quite as useful in a raw form.
  513. * For now, let's get expressions and suites working reliably.
  514. */
  515. static node* build_node_tree(PyObject *tuple);
  516. static int validate_expr_tree(node *tree);
  517. static int validate_file_input(node *tree);
  518. static int validate_encoding_decl(node *tree);
  519. /* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
  520. *
  521. * This is the public function, called from the Python code. It receives a
  522. * single tuple object from the caller, and creates an ST object if the
  523. * tuple can be validated. It does this by checking the first code of the
  524. * tuple, and, if acceptable, builds the internal representation. If this
  525. * step succeeds, the internal representation is validated as fully as
  526. * possible with the various validate_*() routines defined below.
  527. *
  528. * This function must be changed if support is to be added for PyST_FRAGMENT
  529. * ST objects.
  530. *
  531. */
  532. static PyObject*
  533. parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
  534. {
  535. NOTE(ARGUNUSED(self))
  536. PyObject *st = 0;
  537. PyObject *tuple;
  538. node *tree;
  539. static char *keywords[] = {"sequence", NULL};
  540. if (!PyArg_ParseTupleAndKeywords(args, kw, "O:sequence2st", keywords,
  541. &tuple))
  542. return (0);
  543. if (!PySequence_Check(tuple)) {
  544. PyErr_SetString(PyExc_ValueError,
  545. "sequence2st() requires a single sequence argument");
  546. return (0);
  547. }
  548. /*
  549. * Convert the tree to the internal form before checking it.
  550. */
  551. tree = build_node_tree(tuple);
  552. if (tree != 0) {
  553. int start_sym = TYPE(tree);
  554. if (start_sym == eval_input) {
  555. /* Might be an eval form. */
  556. if (validate_expr_tree(tree))
  557. st = parser_newstobject(tree, PyST_EXPR);
  558. else
  559. PyNode_Free(tree);
  560. }
  561. else if (start_sym == file_input) {
  562. /* This looks like an exec form so far. */
  563. if (validate_file_input(tree))
  564. st = parser_newstobject(tree, PyST_SUITE);
  565. else
  566. PyNode_Free(tree);
  567. }
  568. else if (start_sym == encoding_decl) {
  569. /* This looks like an encoding_decl so far. */
  570. if (validate_encoding_decl(tree))
  571. st = parser_newstobject(tree, PyST_SUITE);
  572. else
  573. PyNode_Free(tree);
  574. }
  575. else {
  576. /* This is a fragment, at best. */
  577. PyNode_Free(tree);
  578. err_string("parse tree does not use a valid start symbol");
  579. }
  580. }
  581. /* Make sure we throw an exception on all errors. We should never
  582. * get this, but we'd do well to be sure something is done.
  583. */
  584. if (st == NULL && !PyErr_Occurred())
  585. err_string("unspecified ST error occurred");
  586. return st;
  587. }
  588. static PyObject*
  589. parser_tuple2ast(PyST_Object *self, PyObject *args, PyObject *kw)
  590. {
  591. if (PyErr_WarnPy3k("tuple2ast is removed in 3.x; use tuple2st", 1) < 0)
  592. return NULL;
  593. return parser_tuple2st(self, args, kw);
  594. }
  595. /* node* build_node_children()
  596. *
  597. * Iterate across the children of the current non-terminal node and build
  598. * their structures. If successful, return the root of this portion of
  599. * the tree, otherwise, 0. Any required exception will be specified already,
  600. * and no memory will have been deallocated.
  601. *
  602. */
  603. static node*
  604. build_node_children(PyObject *tuple, node *root, int *line_num)
  605. {
  606. Py_ssize_t len = PyObject_Size(tuple);
  607. Py_ssize_t i;
  608. int err;
  609. for (i = 1; i < len; ++i) {
  610. /* elem must always be a sequence, however simple */
  611. PyObject* elem = PySequence_GetItem(tuple, i);
  612. int ok = elem != NULL;
  613. long type = 0;
  614. char *strn = 0;
  615. if (ok)
  616. ok = PySequence_Check(elem);
  617. if (ok) {
  618. PyObject *temp = PySequence_GetItem(elem, 0);
  619. if (temp == NULL)
  620. ok = 0;
  621. else {
  622. ok = PyInt_Check(temp);
  623. if (ok)
  624. type = PyInt_AS_LONG(temp);
  625. Py_DECREF(temp);
  626. }
  627. }
  628. if (!ok) {
  629. PyObject *err = Py_BuildValue("os", elem,
  630. "Illegal node construct.");
  631. PyErr_SetObject(parser_error, err);
  632. Py_XDECREF(err);
  633. Py_XDECREF(elem);
  634. return (0);
  635. }
  636. if (ISTERMINAL(type)) {
  637. Py_ssize_t len = PyObject_Size(elem);
  638. PyObject *temp;
  639. if ((len != 2) && (len != 3)) {
  640. err_string("terminal nodes must have 2 or 3 entries");
  641. return 0;
  642. }
  643. temp = PySequence_GetItem(elem, 1);
  644. if (temp == NULL)
  645. return 0;
  646. if (!PyString_Check(temp)) {
  647. PyErr_Format(parser_error,
  648. "second item in terminal node must be a string,"
  649. " found %s",
  650. Py_TYPE(temp)->tp_name);
  651. Py_DECREF(temp);
  652. return 0;
  653. }
  654. if (len == 3) {
  655. PyObject *o = PySequence_GetItem(elem, 2);
  656. if (o != NULL) {
  657. if (PyInt_Check(o))
  658. *line_num = PyInt_AS_LONG(o);
  659. else {
  660. PyErr_Format(parser_error,
  661. "third item in terminal node must be an"
  662. " integer, found %s",
  663. Py_TYPE(temp)->tp_name);
  664. Py_DECREF(o);
  665. Py_DECREF(temp);
  666. return 0;
  667. }
  668. Py_DECREF(o);
  669. }
  670. }
  671. len = PyString_GET_SIZE(temp) + 1;
  672. strn = (char *)PyObject_MALLOC(len);
  673. if (strn != NULL)
  674. (void) memcpy(strn, PyString_AS_STRING(temp), len);
  675. Py_DECREF(temp);
  676. }
  677. else if (!ISNONTERMINAL(type)) {
  678. /*
  679. * It has to be one or the other; this is an error.
  680. * Throw an exception.
  681. */
  682. PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
  683. PyErr_SetObject(parser_error, err);
  684. Py_XDECREF(err);
  685. Py_XDECREF(elem);
  686. return (0);
  687. }
  688. err = PyNode_AddChild(root, type, strn, *line_num, 0);
  689. if (err == E_NOMEM) {
  690. PyObject_FREE(strn);
  691. return (node *) PyErr_NoMemory();
  692. }
  693. if (err == E_OVERFLOW) {
  694. PyObject_FREE(strn);
  695. PyErr_SetString(PyExc_ValueError,
  696. "unsupported number of child nodes");
  697. return NULL;
  698. }
  699. if (ISNONTERMINAL(type)) {
  700. node* new_child = CHILD(root, i - 1);
  701. if (new_child != build_node_children(elem, new_child, line_num)) {
  702. Py_XDECREF(elem);
  703. return (0);
  704. }
  705. }
  706. else if (type == NEWLINE) { /* It's true: we increment the */
  707. ++(*line_num); /* line number *after* the newline! */
  708. }
  709. Py_XDECREF(elem);
  710. }
  711. return root;
  712. }
  713. static node*
  714. build_node_tree(PyObject *tuple)
  715. {
  716. node* res = 0;
  717. PyObject *temp = PySequence_GetItem(tuple, 0);
  718. long num = -1;
  719. if (temp != NULL)
  720. num = PyInt_AsLong(temp);
  721. Py_XDECREF(temp);
  722. if (ISTERMINAL(num)) {
  723. /*
  724. * The tuple is simple, but it doesn't start with a start symbol.
  725. * Throw an exception now and be done with it.
  726. */
  727. tuple = Py_BuildValue("os", tuple,
  728. "Illegal syntax-tree; cannot start with terminal symbol.");
  729. PyErr_SetObject(parser_error, tuple);
  730. Py_XDECREF(tuple);
  731. }
  732. else if (ISNONTERMINAL(num)) {
  733. /*
  734. * Not efficient, but that can be handled later.
  735. */
  736. int line_num = 0;
  737. PyObject *encoding = NULL;
  738. if (num == encoding_decl) {
  739. encoding = PySequence_GetItem(tuple, 2);
  740. /* tuple isn't borrowed anymore here, need to DECREF */
  741. tuple = PySequence_GetSlice(tuple, 0, 2);
  742. }
  743. res = PyNode_New(num);
  744. if (res != NULL) {
  745. if (res != build_node_children(tuple, res, &line_num)) {
  746. PyNode_Free(res);
  747. res = NULL;
  748. }
  749. if (res && encoding) {
  750. Py_ssize_t len;
  751. len = PyString_GET_SIZE(encoding) + 1;
  752. res->n_str = (char *)PyObject_MALLOC(len);
  753. if (res->n_str != NULL)
  754. (void) memcpy(res->n_str, PyString_AS_STRING(encoding), len);
  755. Py_DECREF(encoding);
  756. Py_DECREF(tuple);
  757. }
  758. }
  759. }
  760. else {
  761. /* The tuple is illegal -- if the number is neither TERMINAL nor
  762. * NONTERMINAL, we can't use it. Not sure the implementation
  763. * allows this condition, but the API doesn't preclude it.
  764. */
  765. PyObject *err = Py_BuildValue("os", tuple,
  766. "Illegal component tuple.");
  767. PyErr_SetObject(parser_error, err);
  768. Py_XDECREF(err);
  769. }
  770. return (res);
  771. }
  772. /*
  773. * Validation routines used within the validation section:
  774. */
  775. static int validate_terminal(node *terminal, int type, char *string);
  776. #define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
  777. #define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
  778. #define validate_colon(ch) validate_terminal(ch, COLON, ":")
  779. #define validate_comma(ch) validate_terminal(ch, COMMA, ",")
  780. #define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
  781. #define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
  782. #define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
  783. #define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
  784. #define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
  785. #define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
  786. #define validate_semi(ch) validate_terminal(ch, SEMI, ";")
  787. #define validate_star(ch) validate_terminal(ch, STAR, "*")
  788. #define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
  789. #define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
  790. #define validate_dot(ch) validate_terminal(ch, DOT, ".")
  791. #define validate_at(ch) validate_terminal(ch, AT, "@")
  792. #define validate_name(ch, str) validate_terminal(ch, NAME, str)
  793. #define VALIDATER(n) static int validate_##n(node *tree)
  794. VALIDATER(node); VALIDATER(small_stmt);
  795. VALIDATER(class); VALIDATER(node);
  796. VALIDATER(parameters); VALIDATER(suite);
  797. VALIDATER(testlist); VALIDATER(varargslist);
  798. VALIDATER(fpdef); VALIDATER(fplist);
  799. VALIDATER(stmt); VALIDATER(simple_stmt);
  800. VALIDATER(expr_stmt); VALIDATER(power);
  801. VALIDATER(print_stmt); VALIDATER(del_stmt);
  802. VALIDATER(return_stmt); VALIDATER(list_iter);
  803. VALIDATER(raise_stmt); VALIDATER(import_stmt);
  804. VALIDATER(import_name); VALIDATER(import_from);
  805. VALIDATER(global_stmt); VALIDATER(list_if);
  806. VALIDATER(assert_stmt); VALIDATER(list_for);
  807. VALIDATER(exec_stmt); VALIDATER(compound_stmt);
  808. VALIDATER(while); VALIDATER(for);
  809. VALIDATER(try); VALIDATER(except_clause);
  810. VALIDATER(test); VALIDATER(and_test);
  811. VALIDATER(not_test); VALIDATER(comparison);
  812. VALIDATER(comp_op); VALIDATER(expr);
  813. VALIDATER(xor_expr); VALIDATER(and_expr);
  814. VALIDATER(shift_expr); VALIDATER(arith_expr);
  815. VALIDATER(term); VALIDATER(factor);
  816. VALIDATER(atom); VALIDATER(lambdef);
  817. VALIDATER(trailer); VALIDATER(subscript);
  818. VALIDATER(subscriptlist); VALIDATER(sliceop);
  819. VALIDATER(exprlist); VALIDATER(dictmaker);
  820. VALIDATER(arglist); VALIDATER(argument);
  821. VALIDATER(listmaker); VALIDATER(yield_stmt);
  822. VALIDATER(testlist1); VALIDATER(gen_for);
  823. VALIDATER(gen_iter); VALIDATER(gen_if);
  824. VALIDATER(testlist_gexp); VALIDATER(yield_expr);
  825. VALIDATER(yield_or_testlist); VALIDATER(or_test);
  826. VALIDATER(old_test); VALIDATER(old_lambdef);
  827. #undef VALIDATER
  828. #define is_even(n) (((n) & 1) == 0)
  829. #define is_odd(n) (((n) & 1) == 1)
  830. static int
  831. validate_ntype(node *n, int t)
  832. {
  833. if (TYPE(n) != t) {
  834. PyErr_Format(parser_error, "Expected node type %d, got %d.",
  835. t, TYPE(n));
  836. return 0;
  837. }
  838. return 1;
  839. }
  840. /* Verifies that the number of child nodes is exactly 'num', raising
  841. * an exception if it isn't. The exception message does not indicate
  842. * the exact number of nodes, allowing this to be used to raise the
  843. * "right" exception when the wrong number of nodes is present in a
  844. * specific variant of a statement's syntax. This is commonly used
  845. * in that fashion.
  846. */
  847. static int
  848. validate_numnodes(node *n, int num, const char *const name)
  849. {
  850. if (NCH(n) != num) {
  851. PyErr_Format(parser_error,
  852. "Illegal number of children for %s node.", name);
  853. return 0;
  854. }
  855. return 1;
  856. }
  857. static int
  858. validate_terminal(node *terminal, int type, char *string)
  859. {
  860. int res = (validate_ntype(terminal, type)
  861. && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
  862. if (!res && !PyErr_Occurred()) {
  863. PyErr_Format(parser_error,
  864. "Illegal terminal: expected \"%s\"", string);
  865. }
  866. return (res);
  867. }
  868. /* X (',' X) [',']
  869. */
  870. static int
  871. validate_repeating_list(node *tree, int ntype, int (*vfunc)(node *),
  872. const char *const name)
  873. {
  874. int nch = NCH(tree);
  875. int res = (nch && validate_ntype(tree, ntype)
  876. && vfunc(CHILD(tree, 0)));
  877. if (!res && !PyErr_Occurred())
  878. (void) validate_numnodes(tree, 1, name);
  879. else {
  880. if (is_even(nch))
  881. res = validate_comma(CHILD(tree, --nch));
  882. if (res && nch > 1) {
  883. int pos = 1;
  884. for ( ; res && pos < nch; pos += 2)
  885. res = (validate_comma(CHILD(tree, pos))
  886. && vfunc(CHILD(tree, pos + 1)));
  887. }
  888. }
  889. return (res);
  890. }
  891. /* validate_class()
  892. *
  893. * classdef:
  894. * 'class' NAME ['(' testlist ')'] ':' suite
  895. */
  896. static int
  897. validate_class(node *tree)
  898. {
  899. int nch = NCH(tree);
  900. int res = (validate_ntype(tree, classdef) &&
  901. ((nch == 4) || (nch == 6) || (nch == 7)));
  902. if (res) {
  903. res = (validate_name(CHILD(tree, 0), "class")
  904. && validate_ntype(CHILD(tree, 1), NAME)
  905. && validate_colon(CHILD(tree, nch - 2))
  906. && validate_suite(CHILD(tree, nch - 1)));
  907. }
  908. else {
  909. (void) validate_numnodes(tree, 4, "class");
  910. }
  911. if (res) {
  912. if (nch == 7) {
  913. res = ((validate_lparen(CHILD(tree, 2)) &&
  914. validate_testlist(CHILD(tree, 3)) &&
  915. validate_rparen(CHILD(tree, 4))));
  916. }
  917. else if (nch == 6) {
  918. res = (validate_lparen(CHILD(tree,2)) &&
  919. validate_rparen(CHILD(tree,3)));
  920. }
  921. }
  922. return (res);
  923. }
  924. /* if_stmt:
  925. * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
  926. */
  927. static int
  928. validate_if(node *tree)
  929. {
  930. int nch = NCH(tree);
  931. int res = (validate_ntype(tree, if_stmt)
  932. && (nch >= 4)
  933. && validate_name(CHILD(tree, 0), "if")
  934. && validate_test(CHILD(tree, 1))
  935. && validate_colon(CHILD(tree, 2))
  936. && validate_suite(CHILD(tree, 3)));
  937. if (res && ((nch % 4) == 3)) {
  938. /* ... 'else' ':' suite */
  939. res = (validate_name(CHILD(tree, nch - 3), "else")
  940. && validate_colon(CHILD(tree, nch - 2))
  941. && validate_suite(CHILD(tree, nch - 1)));
  942. nch -= 3;
  943. }
  944. else if (!res && !PyErr_Occurred())
  945. (void) validate_numnodes(tree, 4, "if");
  946. if ((nch % 4) != 0)
  947. /* Will catch the case for nch < 4 */
  948. res = validate_numnodes(tree, 0, "if");
  949. else if (res && (nch > 4)) {
  950. /* ... ('elif' test ':' suite)+ ... */
  951. int j = 4;
  952. while ((j < nch) && res) {
  953. res = (validate_name(CHILD(tree, j), "elif")
  954. && validate_colon(CHILD(tree, j + 2))
  955. && validate_test(CHILD(tree, j + 1))
  956. && validate_suite(CHILD(tree, j + 3)));
  957. j += 4;
  958. }
  959. }
  960. return (res);
  961. }
  962. /* parameters:
  963. * '(' [varargslist] ')'
  964. *
  965. */
  966. static int
  967. validate_parameters(node *tree)
  968. {
  969. int nch = NCH(tree);
  970. int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
  971. if (res) {
  972. res = (validate_lparen(CHILD(tree, 0))
  973. && validate_rparen(CHILD(tree, nch - 1)));
  974. if (res && (nch == 3))
  975. res = validate_varargslist(CHILD(tree, 1));
  976. }
  977. else {
  978. (void) validate_numnodes(tree, 2, "parameters");
  979. }
  980. return (res);
  981. }
  982. /* validate_suite()
  983. *
  984. * suite:
  985. * simple_stmt
  986. * | NEWLINE INDENT stmt+ DEDENT
  987. */
  988. static int
  989. validate_suite(node *tree)
  990. {
  991. int nch = NCH(tree);
  992. int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
  993. if (res && (nch == 1))
  994. res = validate_simple_stmt(CHILD(tree, 0));
  995. else if (res) {
  996. /* NEWLINE INDENT stmt+ DEDENT */
  997. res = (validate_newline(CHILD(tree, 0))
  998. && validate_indent(CHILD(tree, 1))
  999. && validate_stmt(CHILD(tree, 2))
  1000. && validate_dedent(CHILD(tree, nch - 1)));
  1001. if (res && (nch > 4)) {
  1002. int i = 3;
  1003. --nch; /* forget the DEDENT */
  1004. for ( ; res && (i < nch); ++i)
  1005. res = validate_stmt(CHILD(tree, i));
  1006. }
  1007. else if (nch < 4)
  1008. res = validate_numnodes(tree, 4, "suite");
  1009. }
  1010. return (res);
  1011. }
  1012. static int
  1013. validate_testlist(node *tree)
  1014. {
  1015. return (validate_repeating_list(tree, testlist,
  1016. validate_test, "testlist"));
  1017. }
  1018. static int
  1019. validate_testlist1(node *tree)
  1020. {
  1021. return (validate_repeating_list(tree, testlist1,
  1022. validate_test, "testlist1"));
  1023. }
  1024. static int
  1025. validate_testlist_safe(node *tree)
  1026. {
  1027. return (validate_repeating_list(tree, testlist_safe,
  1028. validate_old_test, "testlist_safe"));
  1029. }
  1030. /* '*' NAME [',' '**' NAME] | '**' NAME
  1031. */
  1032. static int
  1033. validate_varargslist_trailer(node *tree, int start)
  1034. {
  1035. int nch = NCH(tree);
  1036. int res = 0;
  1037. int sym;
  1038. if (nch <= start) {
  1039. err_string("expected variable argument trailer for varargslist");
  1040. return 0;
  1041. }
  1042. sym = TYPE(CHILD(tree, start));
  1043. if (sym == STAR) {
  1044. /*
  1045. * ('*' NAME [',' '**' NAME]
  1046. */
  1047. if (nch-start == 2)
  1048. res = validate_name(CHILD(tree, start+1), NULL);
  1049. else if (nch-start == 5)
  1050. res = (validate_name(CHILD(tree, start+1), NULL)
  1051. && validate_comma(CHILD(tree, start+2))
  1052. && validate_doublestar(CHILD(tree, start+3))
  1053. && validate_name(CHILD(tree, start+4), NULL));
  1054. }
  1055. else if (sym == DOUBLESTAR) {
  1056. /*
  1057. * '**' NAME
  1058. */
  1059. if (nch-start == 2)
  1060. res = validate_name(CHILD(tree, start+1), NULL);
  1061. }
  1062. if (!res)
  1063. err_string("illegal variable argument trailer for varargslist");
  1064. return res;
  1065. }
  1066. /* validate_varargslist()
  1067. *
  1068. * varargslist:
  1069. * (fpdef ['=' test] ',')*
  1070. * ('*' NAME [',' '**' NAME]
  1071. * | '**' NAME)
  1072. * | fpdef ['=' test] (',' fpdef ['=' test])* [',']
  1073. *
  1074. */
  1075. static int
  1076. validate_varargslist(node *tree)
  1077. {
  1078. int nch = NCH(tree);
  1079. int res = validate_ntype(tree, varargslist) && (nch != 0);
  1080. int sym;
  1081. if (!res)
  1082. return 0;
  1083. if (nch < 1) {
  1084. err_string("varargslist missing child nodes");
  1085. return 0;
  1086. }
  1087. sym = TYPE(CHILD(tree, 0));
  1088. if (sym == STAR || sym == DOUBLESTAR)
  1089. /* whole thing matches:
  1090. * '*' NAME [',' '**' NAME] | '**' NAME
  1091. */
  1092. res = validate_varargslist_trailer(tree, 0);
  1093. else if (sym == fpdef) {
  1094. int i = 0;
  1095. sym = TYPE(CHILD(tree, nch-1));
  1096. if (sym == NAME) {
  1097. /*
  1098. * (fpdef ['=' test] ',')+
  1099. * ('*' NAME [',' '**' NAME]
  1100. * | '**' NAME)
  1101. */
  1102. /* skip over (fpdef ['=' test] ',')+ */
  1103. while (res && (i+2 <= nch)) {
  1104. res = validate_fpdef(CHILD(tree, i));
  1105. ++i;
  1106. if (res && TYPE(CHILD(tree, i)) == EQUAL && (i+2 <= nch)) {
  1107. res = (validate_equal(CHILD(tree, i))
  1108. && validate_test(CHILD(tree, i+1)));
  1109. if (res)
  1110. i += 2;
  1111. }
  1112. if (res && i < nch) {
  1113. res = validate_comma(CHILD(tree, i));
  1114. ++i;
  1115. if (res && i < nch
  1116. && (TYPE(CHILD(tree, i)) == DOUBLESTAR
  1117. || TYPE(CHILD(tree, i)) == STAR))
  1118. break;
  1119. }
  1120. }
  1121. /* ... '*' NAME [',' '**' NAME] | '**' NAME
  1122. * i --^^^
  1123. */
  1124. if (res)
  1125. res = validate_varargslist_trailer(tree, i);
  1126. }
  1127. else {
  1128. /*
  1129. * fpdef ['=' test] (',' fpdef ['=' test])* [',']
  1130. */
  1131. /* strip trailing comma node */
  1132. if (sym == COMMA) {
  1133. res = validate_comma(CHILD(tree, nch-1));
  1134. if (!res)
  1135. return 0;
  1136. --nch;
  1137. }
  1138. /*
  1139. * fpdef ['=' test] (',' fpdef ['=' test])*
  1140. */
  1141. res = validate_fpdef(CHILD(tree, 0));
  1142. ++i;
  1143. if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
  1144. res = (validate_equal(CHILD(tree, i))
  1145. && validate_test(CHILD(tree, i+1)));
  1146. i += 2;
  1147. }
  1148. /*
  1149. * ... (',' fpdef ['=' test])*
  1150. * i ---^^^
  1151. */
  1152. while (res && (nch - i) >= 2) {
  1153. res = (validate_comma(CHILD(tree, i))
  1154. && validate_fpdef(CHILD(tree, i+1)));
  1155. i += 2;
  1156. if (res && (nch - i) >= 2 && TYPE(CHILD(tree, i)) == EQUAL) {
  1157. res = (validate_equal(CHILD(tree, i))
  1158. && validate_test(CHILD(tree, i+1)));
  1159. i += 2;
  1160. }
  1161. }
  1162. if (res && nch - i != 0) {
  1163. res = 0;
  1164. err_string("illegal formation for varargslist");
  1165. }
  1166. }
  1167. }
  1168. return res;
  1169. }
  1170. /* list_iter: list_for | list_if
  1171. */
  1172. static int
  1173. validate_list_iter(node *tree)
  1174. {
  1175. int res = (validate_ntype(tree, list_iter)
  1176. && validate_numnodes(tree, 1, "list_iter"));
  1177. if (res && TYPE(CHILD(tree, 0)) == list_for)
  1178. res = validate_list_for(CHILD(tree, 0));
  1179. else
  1180. res = validate_list_if(CHILD(tree, 0));
  1181. return res;
  1182. }
  1183. /* gen_iter: gen_for | gen_if
  1184. */
  1185. static int
  1186. validate_gen_iter(node *tree)
  1187. {
  1188. int res = (validate_ntype(tree, gen_iter)
  1189. && validate_numnodes(tree, 1, "gen_iter"));
  1190. if (res && TYPE(CHILD(tree, 0)) == gen_for)
  1191. res = validate_gen_for(CHILD(tree, 0));
  1192. else
  1193. res = validate_gen_if(CHILD(tree, 0));
  1194. return res;
  1195. }
  1196. /* list_for: 'for' exprlist 'in' testlist [list_iter]
  1197. */
  1198. static int
  1199. validate_list_for(node *tree)
  1200. {
  1201. int nch = NCH(tree);
  1202. int res;
  1203. if (nch == 5)
  1204. res = validate_list_iter(CHILD(tree, 4));
  1205. else
  1206. res = validate_numnodes(tree, 4, "list_for");
  1207. if (res)
  1208. res = (validate_name(CHILD(tree, 0), "for")
  1209. && validate_exprlist(CHILD(tree, 1))
  1210. && validate_name(CHILD(tree, 2), "in")
  1211. && validate_testlist_safe(CHILD(tree, 3)));
  1212. return res;
  1213. }
  1214. /* gen_for: 'for' exprlist 'in' test [gen_iter]
  1215. */
  1216. static int
  1217. validate_gen_for(node *tree)
  1218. {
  1219. int nch = NCH(tree);
  1220. int res;
  1221. if (nch == 5)
  1222. res = validate_gen_iter(CHILD(tree, 4));
  1223. else
  1224. res = validate_numnodes(tree, 4, "gen_for");
  1225. if (res)
  1226. res = (validate_name(CHILD(tree, 0), "for")
  1227. && validate_exprlist(CHILD(tree, 1))
  1228. && validate_name(CHILD(tree, 2), "in")
  1229. && validate_or_test(CHILD(tree, 3)));
  1230. return res;
  1231. }
  1232. /* list_if: 'if' old_test [list_iter]
  1233. */
  1234. static int
  1235. validate_list_if(node *tree)
  1236. {
  1237. int nch = NCH(tree);
  1238. int res;
  1239. if (nch == 3)
  1240. res = validate_list_iter(CHILD(tree, 2));
  1241. else
  1242. res = validate_numnodes(tree, 2, "list_if");
  1243. if (res)
  1244. res = (validate_name(CHILD(tree, 0), "if")
  1245. && validate_old_test(CHILD(tree, 1)));
  1246. return res;
  1247. }
  1248. /* gen_if: 'if' old_test [gen_iter]
  1249. */
  1250. static int
  1251. validate_gen_if(node *tree)
  1252. {
  1253. int nch = NCH(tree);
  1254. int res;
  1255. if (nch == 3)
  1256. res = validate_gen_iter(CHILD(tree, 2));
  1257. else
  1258. res = validate_numnodes(tree, 2, "gen_if");
  1259. if (res)
  1260. res = (validate_name(CHILD(tree, 0), "if")
  1261. && validate_old_test(CHILD(tree, 1)));
  1262. return res;
  1263. }
  1264. /* validate_fpdef()
  1265. *
  1266. * fpdef:
  1267. * NAME
  1268. * | '(' fplist ')'
  1269. */
  1270. static int
  1271. validate_fpdef(node *tree)
  1272. {
  1273. int nch = NCH(tree);
  1274. int res = validate_ntype(tree, fpdef);
  1275. if (res) {
  1276. if (nch == 1)
  1277. res = validate_ntype(CHILD(tree, 0), NAME);
  1278. else if (nch == 3)
  1279. res = (validate_lparen(CHILD(tree, 0))
  1280. && validate_fplist(CHILD(tree, 1))
  1281. && validate_rparen(CHILD(tree, 2)));
  1282. else
  1283. res = validate_numnodes(tree, 1, "fpdef");
  1284. }
  1285. return (res);
  1286. }
  1287. static int
  1288. validate_fplist(node *tree)
  1289. {
  1290. return (validate_repeating_list(tree, fplist,
  1291. validate_fpdef, "fplist"));
  1292. }
  1293. /* simple_stmt | compound_stmt
  1294. *
  1295. */
  1296. static int
  1297. validate_stmt(node *tree)
  1298. {
  1299. int res = (validate_ntype(tree, stmt)
  1300. && validate_numnodes(tree, 1, "stmt"));
  1301. if (res) {
  1302. tree = CHILD(tree, 0);
  1303. if (TYPE(tree) == simple_stmt)
  1304. res = validate_simple_stmt(tree);
  1305. else
  1306. res = validate_compound_stmt(tree);
  1307. }
  1308. return (res);
  1309. }
  1310. /* small_stmt (';' small_stmt)* [';'] NEWLINE
  1311. *
  1312. */
  1313. static int
  1314. validate_simple_stmt(node *tree)
  1315. {
  1316. int nch = NCH(tree);
  1317. int res = (validate_ntype(tree, simple_stmt)
  1318. && (nch >= 2)
  1319. && validate_small_stmt(CHILD(tree, 0))
  1320. && validate_newline(CHILD(tree, nch - 1)));
  1321. if (nch < 2)
  1322. res = validate_numnodes(tree, 2, "simple_stmt");
  1323. --nch; /* forget the NEWLINE */
  1324. if (res && is_even(nch))
  1325. res = validate_semi(CHILD(tree, --nch));
  1326. if (res && (nch > 2)) {
  1327. int i;
  1328. for (i = 1; res && (i < nch); i += 2)
  1329. res = (validate_semi(CHILD(tree, i))
  1330. && validate_small_stmt(CHILD(tree, i + 1)));
  1331. }
  1332. return (res);
  1333. }
  1334. static int
  1335. validate_small_stmt(node *tree)
  1336. {
  1337. int nch = NCH(tree);
  1338. int res = validate_numnodes(tree, 1, "small_stmt");
  1339. if (res) {
  1340. int ntype = TYPE(CHILD(tree, 0));
  1341. if ( (ntype == expr_stmt)
  1342. || (ntype == print_stmt)
  1343. || (ntype == del_stmt)
  1344. || (ntype == pass_stmt)
  1345. || (ntype == flow_stmt)
  1346. || (ntype == import_stmt)
  1347. || (ntype == global_stmt)
  1348. || (ntype == assert_stmt)
  1349. || (ntype == exec_stmt))
  1350. res = validate_node(CHILD(tree, 0));
  1351. else {
  1352. res = 0;
  1353. err_string("illegal small_stmt child type");
  1354. }
  1355. }
  1356. else if (nch == 1) {
  1357. res = 0;
  1358. PyErr_Format(parser_error,
  1359. "Unrecognized child node of small_stmt: %d.",
  1360. TYPE(CHILD(tree, 0)));
  1361. }
  1362. return (res);
  1363. }
  1364. /* compound_stmt:
  1365. * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
  1366. */
  1367. static int
  1368. validate_compound_stmt(node *tree)
  1369. {
  1370. int res = (validate_ntype(tree, compound_stmt)
  1371. && validate_numnodes(tree, 1, "compound_stmt"));
  1372. int ntype;
  1373. if (!res)
  1374. return (0);
  1375. tree = CHILD(tree, 0);
  1376. ntype = TYPE(tree);
  1377. if ( (ntype == if_stmt)
  1378. || (ntype == while_stmt)
  1379. || (ntype == for_stmt)
  1380. || (ntype == try_stmt)
  1381. || (ntype == with_stmt)
  1382. || (ntype == funcdef)
  1383. || (ntype == classdef)
  1384. || (ntype == decorated))
  1385. res = validate_node(tree);
  1386. else {
  1387. res = 0;
  1388. PyErr_Format(parser_error,
  1389. "Illegal compound statement type: %d.", TYPE(tree));
  1390. }
  1391. return (res);
  1392. }
  1393. static int
  1394. validate_yield_or_testlist(node *tree)
  1395. {
  1396. if (TYPE(tree) == yield_expr)
  1397. return validate_yield_expr(tree);
  1398. else
  1399. return validate_testlist(tree);
  1400. }
  1401. static int
  1402. validate_expr_stmt(node *tree)
  1403. {
  1404. int j;
  1405. int nch = NCH(tree);
  1406. int res = (validate_ntype(tree, expr_stmt)
  1407. && is_odd(nch)
  1408. && validate_testlist(CHILD(tree, 0)));
  1409. if (res && nch == 3
  1410. && TYPE(CHILD(tree, 1)) == augassign) {
  1411. res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
  1412. && validate_yield_or_testlist(CHILD(tree, 2));
  1413. if (res) {
  1414. char *s = STR(CHILD(CHILD(tree, 1), 0));
  1415. res = (strcmp(s, "+=") == 0
  1416. || strcmp(s, "-=") == 0
  1417. || strcmp(s, "*=") == 0
  1418. || strcmp(s, "/=") == 0
  1419. || strcmp(s, "//=") == 0
  1420. || strcmp(s, "%=") == 0
  1421. || strcmp(s, "&=") == 0
  1422. || strcmp(s, "|=") == 0