PageRenderTime 67ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/Modules/parsermodule.c

http://unladen-swallow.googlecode.com/
C | 3405 lines | 2521 code | 428 blank | 456 comment | 1103 complexity | b2835d6145228b011be75992b6438b0a MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause
  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
  1423. || strcmp(s, "^=") == 0
  1424. || strcmp(s, "<<=") == 0
  1425. || strcmp(s, ">>=") == 0
  1426. || strcmp(s, "**=") == 0);
  1427. if (!res)
  1428. err_string("illegal augmmented assignment operator");
  1429. }
  1430. }
  1431. else {
  1432. for (j = 1; res && (j < nch); j += 2)
  1433. res = validate_equal(CHILD(tree, j))
  1434. && validate_yield_or_testlist(CHILD(tree, j + 1));
  1435. }
  1436. return (res);
  1437. }
  1438. /* print_stmt:
  1439. *
  1440. * 'print' ( [ test (',' test)* [','] ]
  1441. * | '>>' test [ (',' test)+ [','] ] )
  1442. */
  1443. static int
  1444. validate_print_stmt(node *tree)
  1445. {
  1446. int nch = NCH(tree);
  1447. int res = (validate_ntype(tree, print_stmt)
  1448. && (nch > 0)
  1449. && validate_name(CHILD(tree, 0), "print"));
  1450. if (res && nch > 1) {
  1451. int sym = TYPE(CHILD(tree, 1));
  1452. int i = 1;
  1453. int allow_trailing_comma = 1;
  1454. if (sym == test)
  1455. res = validate_test(CHILD(tree, i++));
  1456. else {
  1457. if (nch < 3)
  1458. res = validate_numnodes(tree, 3, "print_stmt");
  1459. else {
  1460. res = (validate_ntype(CHILD(tree, i), RIGHTSHIFT)
  1461. && validate_test(CHILD(tree, i+1)));
  1462. i += 2;
  1463. allow_trailing_comma = 0;
  1464. }
  1465. }
  1466. if (res) {
  1467. /* ... (',' test)* [','] */
  1468. while (res && i+2 <= nch) {
  1469. res = (validate_comma(CHILD(tree, i))
  1470. && validate_test(CHILD(tree, i+1)));
  1471. allow_trailing_comma = 1;
  1472. i += 2;
  1473. }
  1474. if (res && !allow_trailing_comma)
  1475. res = validate_numnodes(tree, i, "print_stmt");
  1476. else if (res && i < nch)
  1477. res = validate_comma(CHILD(tree, i));
  1478. }
  1479. }
  1480. return (res);
  1481. }
  1482. static int
  1483. validate_del_stmt(node *tree)
  1484. {
  1485. return (validate_numnodes(tree, 2, "del_stmt")
  1486. && validate_name(CHILD(tree, 0), "del")
  1487. && validate_exprlist(CHILD(tree, 1)));
  1488. }
  1489. static int
  1490. validate_return_stmt(node *tree)
  1491. {
  1492. int nch = NCH(tree);
  1493. int res = (validate_ntype(tree, return_stmt)
  1494. && ((nch == 1) || (nch == 2))
  1495. && validate_name(CHILD(tree, 0), "return"));
  1496. if (res && (nch == 2))
  1497. res = validate_testlist(CHILD(tree, 1));
  1498. return (res);
  1499. }
  1500. static int
  1501. validate_raise_stmt(node *tree)
  1502. {
  1503. int nch = NCH(tree);
  1504. int res = (validate_ntype(tree, raise_stmt)
  1505. && ((nch == 1) || (nch == 2) || (nch == 4) || (nch == 6)));
  1506. if (res) {
  1507. res = validate_name(CHILD(tree, 0), "raise");
  1508. if (res && (nch >= 2))
  1509. res = validate_test(CHILD(tree, 1));
  1510. if (res && nch > 2) {
  1511. res = (validate_comma(CHILD(tree, 2))
  1512. && validate_test(CHILD(tree, 3)));
  1513. if (res && (nch > 4))
  1514. res = (validate_comma(CHILD(tree, 4))
  1515. && validate_test(CHILD(tree, 5)));
  1516. }
  1517. }
  1518. else
  1519. (void) validate_numnodes(tree, 2, "raise");
  1520. if (res && (nch == 4))
  1521. res = (validate_comma(CHILD(tree, 2))
  1522. && validate_test(CHILD(tree, 3)));
  1523. return (res);
  1524. }
  1525. /* yield_expr: 'yield' [testlist]
  1526. */
  1527. static int
  1528. validate_yield_expr(node *tree)
  1529. {
  1530. int nch = NCH(tree);
  1531. int res = (validate_ntype(tree, yield_expr)
  1532. && ((nch == 1) || (nch == 2))
  1533. && validate_name(CHILD(tree, 0), "yield"));
  1534. if (res && (nch == 2))
  1535. res = validate_testlist(CHILD(tree, 1));
  1536. return (res);
  1537. }
  1538. /* yield_stmt: yield_expr
  1539. */
  1540. static int
  1541. validate_yield_stmt(node *tree)
  1542. {
  1543. return (validate_ntype(tree, yield_stmt)
  1544. && validate_numnodes(tree, 1, "yield_stmt")
  1545. && validate_yield_expr(CHILD(tree, 0)));
  1546. }
  1547. static int
  1548. validate_import_as_name(node *tree)
  1549. {
  1550. int nch = NCH(tree);
  1551. int ok = validate_ntype(tree, import_as_name);
  1552. if (ok) {
  1553. if (nch == 1)
  1554. ok = validate_name(CHILD(tree, 0), NULL);
  1555. else if (nch == 3)
  1556. ok = (validate_name(CHILD(tree, 0), NULL)
  1557. && validate_name(CHILD(tree, 1), "as")
  1558. && validate_name(CHILD(tree, 2), NULL));
  1559. else
  1560. ok = validate_numnodes(tree, 3, "import_as_name");
  1561. }
  1562. return ok;
  1563. }
  1564. /* dotted_name: NAME ("." NAME)*
  1565. */
  1566. static int
  1567. validate_dotted_name(node *tree)
  1568. {
  1569. int nch = NCH(tree);
  1570. int res = (validate_ntype(tree, dotted_name)
  1571. && is_odd(nch)
  1572. && validate_name(CHILD(tree, 0), NULL));
  1573. int i;
  1574. for (i = 1; res && (i < nch); i += 2) {
  1575. res = (validate_dot(CHILD(tree, i))
  1576. && validate_name(CHILD(tree, i+1), NULL));
  1577. }
  1578. return res;
  1579. }
  1580. /* dotted_as_name: dotted_name [NAME NAME]
  1581. */
  1582. static int
  1583. validate_dotted_as_name(node *tree)
  1584. {
  1585. int nch = NCH(tree);
  1586. int res = validate_ntype(tree, dotted_as_name);
  1587. if (res) {
  1588. if (nch == 1)
  1589. res = validate_dotted_name(CHILD(tree, 0));
  1590. else if (nch == 3)
  1591. res = (validate_dotted_name(CHILD(tree, 0))
  1592. && validate_name(CHILD(tree, 1), "as")
  1593. && validate_name(CHILD(tree, 2), NULL));
  1594. else {
  1595. res = 0;
  1596. err_string("illegal number of children for dotted_as_name");
  1597. }
  1598. }
  1599. return res;
  1600. }
  1601. /* dotted_as_name (',' dotted_as_name)* */
  1602. static int
  1603. validate_dotted_as_names(node *tree)
  1604. {
  1605. int nch = NCH(tree);
  1606. int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
  1607. int i;
  1608. for (i = 1; res && (i < nch); i += 2)
  1609. res = (validate_comma(CHILD(tree, i))
  1610. && validate_dotted_as_name(CHILD(tree, i + 1)));
  1611. return (res);
  1612. }
  1613. /* import_as_name (',' import_as_name)* [','] */
  1614. static int
  1615. validate_import_as_names(node *tree)
  1616. {
  1617. int nch = NCH(tree);
  1618. int res = validate_import_as_name(CHILD(tree, 0));
  1619. int i;
  1620. for (i = 1; res && (i + 1 < nch); i += 2)
  1621. res = (validate_comma(CHILD(tree, i))
  1622. && validate_import_as_name(CHILD(tree, i + 1)));
  1623. return (res);
  1624. }
  1625. /* 'import' dotted_as_names */
  1626. static int
  1627. validate_import_name(node *tree)
  1628. {
  1629. return (validate_ntype(tree, import_name)
  1630. && validate_numnodes(tree, 2, "import_name")
  1631. && validate_name(CHILD(tree, 0), "import")
  1632. && validate_dotted_as_names(CHILD(tree, 1)));
  1633. }
  1634. /* Helper function to count the number of leading dots in
  1635. * 'from ...module import name'
  1636. */
  1637. static int
  1638. count_from_dots(node *tree)
  1639. {
  1640. int i;
  1641. for (i = 1; i < NCH(tree); i++)
  1642. if (TYPE(CHILD(tree, i)) != DOT)
  1643. break;
  1644. return i-1;
  1645. }
  1646. /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
  1647. * import_as_names
  1648. */
  1649. static int
  1650. validate_import_from(node *tree)
  1651. {
  1652. int nch = NCH(tree);
  1653. int ndots = count_from_dots(tree);
  1654. int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
  1655. int offset = ndots + havename;
  1656. int res = validate_ntype(tree, import_from)
  1657. && (nch >= 4 + ndots)
  1658. && validate_name(CHILD(tree, 0), "from")
  1659. && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
  1660. && validate_name(CHILD(tree, offset + 1), "import");
  1661. if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
  1662. res = ((nch == offset + 5)
  1663. && validate_lparen(CHILD(tree, offset + 2))
  1664. && validate_import_as_names(CHILD(tree, offset + 3))
  1665. && validate_rparen(CHILD(tree, offset + 4)));
  1666. else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
  1667. res = validate_import_as_names(CHILD(tree, offset + 2));
  1668. return (res);
  1669. }
  1670. /* import_stmt: import_name | import_from */
  1671. static int
  1672. validate_import_stmt(node *tree)
  1673. {
  1674. int nch = NCH(tree);
  1675. int res = validate_numnodes(tree, 1, "import_stmt");
  1676. if (res) {
  1677. int ntype = TYPE(CHILD(tree, 0));
  1678. if (ntype == import_name || ntype == import_from)
  1679. res = validate_node(CHILD(tree, 0));
  1680. else {
  1681. res = 0;
  1682. err_string("illegal import_stmt child type");
  1683. }
  1684. }
  1685. else if (nch == 1) {
  1686. res = 0;
  1687. PyErr_Format(parser_error,
  1688. "Unrecognized child node of import_stmt: %d.",
  1689. TYPE(CHILD(tree, 0)));
  1690. }
  1691. return (res);
  1692. }
  1693. static int
  1694. validate_global_stmt(node *tree)
  1695. {
  1696. int j;
  1697. int nch = NCH(tree);
  1698. int res = (validate_ntype(tree, global_stmt)
  1699. && is_even(nch) && (nch >= 2));
  1700. if (!res && !PyErr_Occurred())
  1701. err_string("illegal global statement");
  1702. if (res)
  1703. res = (validate_name(CHILD(tree, 0), "global")
  1704. && validate_ntype(CHILD(tree, 1), NAME));
  1705. for (j = 2; res && (j < nch); j += 2)
  1706. res = (validate_comma(CHILD(tree, j))
  1707. && validate_ntype(CHILD(tree, j + 1), NAME));
  1708. return (res);
  1709. }
  1710. /* exec_stmt:
  1711. *
  1712. * 'exec' expr ['in' test [',' test]]
  1713. */
  1714. static int
  1715. validate_exec_stmt(node *tree)
  1716. {
  1717. int nch = NCH(tree);
  1718. int res = (validate_ntype(tree, exec_stmt)
  1719. && ((nch == 2) || (nch == 4) || (nch == 6))
  1720. && validate_name(CHILD(tree, 0), "exec")
  1721. && validate_expr(CHILD(tree, 1)));
  1722. if (!res && !PyErr_Occurred())
  1723. err_string("illegal exec statement");
  1724. if (res && (nch > 2))
  1725. res = (validate_name(CHILD(tree, 2), "in")
  1726. && validate_test(CHILD(tree, 3)));
  1727. if (res && (nch == 6))
  1728. res = (validate_comma(CHILD(tree, 4))
  1729. && validate_test(CHILD(tree, 5)));
  1730. return (res);
  1731. }
  1732. /* assert_stmt:
  1733. *
  1734. * 'assert' test [',' test]
  1735. */
  1736. static int
  1737. validate_assert_stmt(node *tree)
  1738. {
  1739. int nch = NCH(tree);
  1740. int res = (validate_ntype(tree, assert_stmt)
  1741. && ((nch == 2) || (nch == 4))
  1742. && (validate_name(CHILD(tree, 0), "assert"))
  1743. && validate_test(CHILD(tree, 1)));
  1744. if (!res && !PyErr_Occurred())
  1745. err_string("illegal assert statement");
  1746. if (res && (nch > 2))
  1747. res = (validate_comma(CHILD(tree, 2))
  1748. && validate_test(CHILD(tree, 3)));
  1749. return (res);
  1750. }
  1751. static int
  1752. validate_while(node *tree)
  1753. {
  1754. int nch = NCH(tree);
  1755. int res = (validate_ntype(tree, while_stmt)
  1756. && ((nch == 4) || (nch == 7))
  1757. && validate_name(CHILD(tree, 0), "while")
  1758. && validate_test(CHILD(tree, 1))
  1759. && validate_colon(CHILD(tree, 2))
  1760. && validate_suite(CHILD(tree, 3)));
  1761. if (res && (nch == 7))
  1762. res = (validate_name(CHILD(tree, 4), "else")
  1763. && validate_colon(CHILD(tree, 5))
  1764. && validate_suite(CHILD(tree, 6)));
  1765. return (res);
  1766. }
  1767. static int
  1768. validate_for(node *tree)
  1769. {
  1770. int nch = NCH(tree);
  1771. int res = (validate_ntype(tree, for_stmt)
  1772. && ((nch == 6) || (nch == 9))
  1773. && validate_name(CHILD(tree, 0), "for")
  1774. && validate_exprlist(CHILD(tree, 1))
  1775. && validate_name(CHILD(tree, 2), "in")
  1776. && validate_testlist(CHILD(tree, 3))
  1777. && validate_colon(CHILD(tree, 4))
  1778. && validate_suite(CHILD(tree, 5)));
  1779. if (res && (nch == 9))
  1780. res = (validate_name(CHILD(tree, 6), "else")
  1781. && validate_colon(CHILD(tree, 7))
  1782. && validate_suite(CHILD(tree, 8)));
  1783. return (res);
  1784. }
  1785. /* try_stmt:
  1786. * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
  1787. ['finally' ':' suite]
  1788. * | 'try' ':' suite 'finally' ':' suite
  1789. *
  1790. */
  1791. static int
  1792. validate_try(node *tree)
  1793. {
  1794. int nch = NCH(tree);
  1795. int pos = 3;
  1796. int res = (validate_ntype(tree, try_stmt)
  1797. && (nch >= 6) && ((nch % 3) == 0));
  1798. if (res)
  1799. res = (validate_name(CHILD(tree, 0), "try")
  1800. && validate_colon(CHILD(tree, 1))
  1801. && validate_suite(CHILD(tree, 2))
  1802. && validate_colon(CHILD(tree, nch - 2))
  1803. && validate_suite(CHILD(tree, nch - 1)));
  1804. else if (!PyErr_Occurred()) {
  1805. const char* name = "except";
  1806. if (TYPE(CHILD(tree, nch - 3)) != except_clause)
  1807. name = STR(CHILD(tree, nch - 3));
  1808. PyErr_Format(parser_error,
  1809. "Illegal number of children for try/%s node.", name);
  1810. }
  1811. /* Handle try/finally statement */
  1812. if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
  1813. (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
  1814. res = (validate_numnodes(tree, 6, "try/finally")
  1815. && validate_colon(CHILD(tree, 4))
  1816. && validate_suite(CHILD(tree, 5)));
  1817. return (res);
  1818. }
  1819. /* try/except statement: skip past except_clause sections */
  1820. while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
  1821. res = (validate_except_clause(CHILD(tree, pos))
  1822. && validate_colon(CHILD(tree, pos + 1))
  1823. && validate_suite(CHILD(tree, pos + 2)));
  1824. pos += 3;
  1825. }
  1826. /* skip else clause */
  1827. if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
  1828. (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
  1829. res = (validate_colon(CHILD(tree, pos + 1))
  1830. && validate_suite(CHILD(tree, pos + 2)));
  1831. pos += 3;
  1832. }
  1833. if (res && pos < nch) {
  1834. /* last clause must be a finally */
  1835. res = (validate_name(CHILD(tree, pos), "finally")
  1836. && validate_numnodes(tree, pos + 3, "try/except/finally")
  1837. && validate_colon(CHILD(tree, pos + 1))
  1838. && validate_suite(CHILD(tree, pos + 2)));
  1839. }
  1840. return (res);
  1841. }
  1842. static int
  1843. validate_except_clause(node *tree)
  1844. {
  1845. int nch = NCH(tree);
  1846. int res = (validate_ntype(tree, except_clause)
  1847. && ((nch == 1) || (nch == 2) || (nch == 4))
  1848. && validate_name(CHILD(tree, 0), "except"));
  1849. if (res && (nch > 1))
  1850. res = validate_test(CHILD(tree, 1));
  1851. if (res && (nch == 4))
  1852. res = (validate_comma(CHILD(tree, 2))
  1853. && validate_test(CHILD(tree, 3)));
  1854. return (res);
  1855. }
  1856. static int
  1857. validate_test(node *tree)
  1858. {
  1859. int nch = NCH(tree);
  1860. int res = validate_ntype(tree, test) && is_odd(nch);
  1861. if (res && (TYPE(CHILD(tree, 0)) == lambdef))
  1862. res = ((nch == 1)
  1863. && validate_lambdef(CHILD(tree, 0)));
  1864. else if (res) {
  1865. res = validate_or_test(CHILD(tree, 0));
  1866. res = (res && (nch == 1 || (nch == 5 &&
  1867. validate_name(CHILD(tree, 1), "if") &&
  1868. validate_or_test(CHILD(tree, 2)) &&
  1869. validate_name(CHILD(tree, 3), "else") &&
  1870. validate_test(CHILD(tree, 4)))));
  1871. }
  1872. return (res);
  1873. }
  1874. static int
  1875. validate_old_test(node *tree)
  1876. {
  1877. int nch = NCH(tree);
  1878. int res = validate_ntype(tree, old_test) && (nch == 1);
  1879. if (res && (TYPE(CHILD(tree, 0)) == old_lambdef))
  1880. res = (validate_old_lambdef(CHILD(tree, 0)));
  1881. else if (res) {
  1882. res = (validate_or_test(CHILD(tree, 0)));
  1883. }
  1884. return (res);
  1885. }
  1886. static int
  1887. validate_or_test(node *tree)
  1888. {
  1889. int nch = NCH(tree);
  1890. int res = validate_ntype(tree, or_test) && is_odd(nch);
  1891. if (res) {
  1892. int pos;
  1893. res = validate_and_test(CHILD(tree, 0));
  1894. for (pos = 1; res && (pos < nch); pos += 2)
  1895. res = (validate_name(CHILD(tree, pos), "or")
  1896. && validate_and_test(CHILD(tree, pos + 1)));
  1897. }
  1898. return (res);
  1899. }
  1900. static int
  1901. validate_and_test(node *tree)
  1902. {
  1903. int pos;
  1904. int nch = NCH(tree);
  1905. int res = (validate_ntype(tree, and_test)
  1906. && is_odd(nch)
  1907. && validate_not_test(CHILD(tree, 0)));
  1908. for (pos = 1; res && (pos < nch); pos += 2)
  1909. res = (validate_name(CHILD(tree, pos), "and")
  1910. && validate_not_test(CHILD(tree, 0)));
  1911. return (res);
  1912. }
  1913. static int
  1914. validate_not_test(node *tree)
  1915. {
  1916. int nch = NCH(tree);
  1917. int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
  1918. if (res) {
  1919. if (nch == 2)
  1920. res = (validate_name(CHILD(tree, 0), "not")
  1921. && validate_not_test(CHILD(tree, 1)));
  1922. else if (nch == 1)
  1923. res = validate_comparison(CHILD(tree, 0));
  1924. }
  1925. return (res);
  1926. }
  1927. static int
  1928. validate_comparison(node *tree)
  1929. {
  1930. int pos;
  1931. int nch = NCH(tree);
  1932. int res = (validate_ntype(tree, comparison)
  1933. && is_odd(nch)
  1934. && validate_expr(CHILD(tree, 0)));
  1935. for (pos = 1; res && (pos < nch); pos += 2)
  1936. res = (validate_comp_op(CHILD(tree, pos))
  1937. && validate_expr(CHILD(tree, pos + 1)));
  1938. return (res);
  1939. }
  1940. static int
  1941. validate_comp_op(node *tree)
  1942. {
  1943. int res = 0;
  1944. int nch = NCH(tree);
  1945. if (!validate_ntype(tree, comp_op))
  1946. return (0);
  1947. if (nch == 1) {
  1948. /*
  1949. * Only child will be a terminal with a well-defined symbolic name
  1950. * or a NAME with a string of either 'is' or 'in'
  1951. */
  1952. tree = CHILD(tree, 0);
  1953. switch (TYPE(tree)) {
  1954. case LESS:
  1955. case GREATER:
  1956. case EQEQUAL:
  1957. case EQUAL:
  1958. case LESSEQUAL:
  1959. case GREATEREQUAL:
  1960. case NOTEQUAL:
  1961. res = 1;
  1962. break;
  1963. case NAME:
  1964. res = ((strcmp(STR(tree), "in") == 0)
  1965. || (strcmp(STR(tree), "is") == 0));
  1966. if (!res) {
  1967. PyErr_Format(parser_error,
  1968. "illegal operator '%s'", STR(tree));
  1969. }
  1970. break;
  1971. default:
  1972. err_string("illegal comparison operator type");
  1973. break;
  1974. }
  1975. }
  1976. else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
  1977. res = (validate_ntype(CHILD(tree, 0), NAME)
  1978. && validate_ntype(CHILD(tree, 1), NAME)
  1979. && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
  1980. && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
  1981. || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
  1982. && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
  1983. if (!res && !PyErr_Occurred())
  1984. err_string("unknown comparison operator");
  1985. }
  1986. return (res);
  1987. }
  1988. static int
  1989. validate_expr(node *tree)
  1990. {
  1991. int j;
  1992. int nch = NCH(tree);
  1993. int res = (validate_ntype(tree, expr)
  1994. && is_odd(nch)
  1995. && validate_xor_expr(CHILD(tree, 0)));
  1996. for (j = 2; res && (j < nch); j += 2)
  1997. res = (validate_xor_expr(CHILD(tree, j))
  1998. && validate_vbar(CHILD(tree, j - 1)));
  1999. return (res);
  2000. }
  2001. static int
  2002. validate_xor_expr(node *tree)
  2003. {
  2004. int j;
  2005. int nch = NCH(tree);
  2006. int res = (validate_ntype(tree, xor_expr)
  2007. && is_odd(nch)
  2008. && validate_and_expr(CHILD(tree, 0)));
  2009. for (j = 2; res && (j < nch); j += 2)
  2010. res = (validate_circumflex(CHILD(tree, j - 1))
  2011. && validate_and_expr(CHILD(tree, j)));
  2012. return (res);
  2013. }
  2014. static int
  2015. validate_and_expr(node *tree)
  2016. {
  2017. int pos;
  2018. int nch = NCH(tree);
  2019. int res = (validate_ntype(tree, and_expr)
  2020. && is_odd(nch)
  2021. && validate_shift_expr(CHILD(tree, 0)));
  2022. for (pos = 1; res && (pos < nch); pos += 2)
  2023. res = (validate_ampersand(CHILD(tree, pos))
  2024. && validate_shift_expr(CHILD(tree, pos + 1)));
  2025. return (res);
  2026. }
  2027. static int
  2028. validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
  2029. {
  2030. int pos = 1;
  2031. int nch = NCH(tree);
  2032. int res = (is_odd(nch)
  2033. && (*termvalid)(CHILD(tree, 0)));
  2034. for ( ; res && (pos < nch); pos += 2) {
  2035. if (TYPE(CHILD(tree, pos)) != op1)
  2036. res = validate_ntype(CHILD(tree, pos), op2);
  2037. if (res)
  2038. res = (*termvalid)(CHILD(tree, pos + 1));
  2039. }
  2040. return (res);
  2041. }
  2042. static int
  2043. validate_shift_expr(node *tree)
  2044. {
  2045. return (validate_ntype(tree, shift_expr)
  2046. && validate_chain_two_ops(tree, validate_arith_expr,
  2047. LEFTSHIFT, RIGHTSHIFT));
  2048. }
  2049. static int
  2050. validate_arith_expr(node *tree)
  2051. {
  2052. return (validate_ntype(tree, arith_expr)
  2053. && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
  2054. }
  2055. static int
  2056. validate_term(node *tree)
  2057. {
  2058. int pos = 1;
  2059. int nch = NCH(tree);
  2060. int res = (validate_ntype(tree, term)
  2061. && is_odd(nch)
  2062. && validate_factor(CHILD(tree, 0)));
  2063. for ( ; res && (pos < nch); pos += 2)
  2064. res = (((TYPE(CHILD(tree, pos)) == STAR)
  2065. || (TYPE(CHILD(tree, pos)) == SLASH)
  2066. || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
  2067. || (TYPE(CHILD(tree, pos)) == PERCENT))
  2068. && validate_factor(CHILD(tree, pos + 1)));
  2069. return (res);
  2070. }
  2071. /* factor:
  2072. *
  2073. * factor: ('+'|'-'|'~') factor | power
  2074. */
  2075. static int
  2076. validate_factor(node *tree)
  2077. {
  2078. int nch = NCH(tree);
  2079. int res = (validate_ntype(tree, factor)
  2080. && (((nch == 2)
  2081. && ((TYPE(CHILD(tree, 0)) == PLUS)
  2082. || (TYPE(CHILD(tree, 0)) == MINUS)
  2083. || (TYPE(CHILD(tree, 0)) == TILDE))
  2084. && validate_factor(CHILD(tree, 1)))
  2085. || ((nch == 1)
  2086. && validate_power(CHILD(tree, 0)))));
  2087. return (res);
  2088. }
  2089. /* power:
  2090. *
  2091. * power: atom trailer* ('**' factor)*
  2092. */
  2093. static int
  2094. validate_power(node *tree)
  2095. {
  2096. int pos = 1;
  2097. int nch = NCH(tree);
  2098. int res = (validate_ntype(tree, power) && (nch >= 1)
  2099. && validate_atom(CHILD(tree, 0)));
  2100. while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
  2101. res = validate_trailer(CHILD(tree, pos++));
  2102. if (res && (pos < nch)) {
  2103. if (!is_even(nch - pos)) {
  2104. err_string("illegal number of nodes for 'power'");
  2105. return (0);
  2106. }
  2107. for ( ; res && (pos < (nch - 1)); pos += 2)
  2108. res = (validate_doublestar(CHILD(tree, pos))
  2109. && validate_factor(CHILD(tree, pos + 1)));
  2110. }
  2111. return (res);
  2112. }
  2113. static int
  2114. validate_atom(node *tree)
  2115. {
  2116. int pos;
  2117. int nch = NCH(tree);
  2118. int res = validate_ntype(tree, atom);
  2119. if (res && nch < 1)
  2120. res = validate_numnodes(tree, nch+1, "atom");
  2121. if (res) {
  2122. switch (TYPE(CHILD(tree, 0))) {
  2123. case LPAR:
  2124. res = ((nch <= 3)
  2125. && (validate_rparen(CHILD(tree, nch - 1))));
  2126. if (res && (nch == 3)) {
  2127. if (TYPE(CHILD(tree, 1))==yield_expr)
  2128. res = validate_yield_expr(CHILD(tree, 1));
  2129. else
  2130. res = validate_testlist_gexp(CHILD(tree, 1));
  2131. }
  2132. break;
  2133. case LSQB:
  2134. if (nch == 2)
  2135. res = validate_ntype(CHILD(tree, 1), RSQB);
  2136. else if (nch == 3)
  2137. res = (validate_listmaker(CHILD(tree, 1))
  2138. && validate_ntype(CHILD(tree, 2), RSQB));
  2139. else {
  2140. res = 0;
  2141. err_string("illegal list display atom");
  2142. }
  2143. break;
  2144. case LBRACE:
  2145. res = ((nch <= 3)
  2146. && validate_ntype(CHILD(tree, nch - 1), RBRACE));
  2147. if (res && (nch == 3))
  2148. res = validate_dictmaker(CHILD(tree, 1));
  2149. break;
  2150. case BACKQUOTE:
  2151. res = ((nch == 3)
  2152. && validate_testlist1(CHILD(tree, 1))
  2153. && validate_ntype(CHILD(tree, 2), BACKQUOTE));
  2154. break;
  2155. case NAME:
  2156. case NUMBER:
  2157. res = (nch == 1);
  2158. break;
  2159. case STRING:
  2160. for (pos = 1; res && (pos < nch); ++pos)
  2161. res = validate_ntype(CHILD(tree, pos), STRING);
  2162. break;
  2163. default:
  2164. res = 0;
  2165. break;
  2166. }
  2167. }
  2168. return (res);
  2169. }
  2170. /* listmaker:
  2171. * test ( list_for | (',' test)* [','] )
  2172. */
  2173. static int
  2174. validate_listmaker(node *tree)
  2175. {
  2176. int nch = NCH(tree);
  2177. int ok = nch;
  2178. if (nch == 0)
  2179. err_string("missing child nodes of listmaker");
  2180. else
  2181. ok = validate_test(CHILD(tree, 0));
  2182. /*
  2183. * list_for | (',' test)* [',']
  2184. */
  2185. if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
  2186. ok = validate_list_for(CHILD(tree, 1));
  2187. else {
  2188. /* (',' test)* [','] */
  2189. int i = 1;
  2190. while (ok && nch - i >= 2) {
  2191. ok = (validate_comma(CHILD(tree, i))
  2192. && validate_test(CHILD(tree, i+1)));
  2193. i += 2;
  2194. }
  2195. if (ok && i == nch-1)
  2196. ok = validate_comma(CHILD(tree, i));
  2197. else if (i != nch) {
  2198. ok = 0;
  2199. err_string("illegal trailing nodes for listmaker");
  2200. }
  2201. }
  2202. return ok;
  2203. }
  2204. /* testlist_gexp:
  2205. * test ( gen_for | (',' test)* [','] )
  2206. */
  2207. static int
  2208. validate_testlist_gexp(node *tree)
  2209. {
  2210. int nch = NCH(tree);
  2211. int ok = nch;
  2212. if (nch == 0)
  2213. err_string("missing child nodes of testlist_gexp");
  2214. else {
  2215. ok = validate_test(CHILD(tree, 0));
  2216. }
  2217. /*
  2218. * gen_for | (',' test)* [',']
  2219. */
  2220. if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
  2221. ok = validate_gen_for(CHILD(tree, 1));
  2222. else {
  2223. /* (',' test)* [','] */
  2224. int i = 1;
  2225. while (ok && nch - i >= 2) {
  2226. ok = (validate_comma(CHILD(tree, i))
  2227. && validate_test(CHILD(tree, i+1)));
  2228. i += 2;
  2229. }
  2230. if (ok && i == nch-1)
  2231. ok = validate_comma(CHILD(tree, i));
  2232. else if (i != nch) {
  2233. ok = 0;
  2234. err_string("illegal trailing nodes for testlist_gexp");
  2235. }
  2236. }
  2237. return ok;
  2238. }
  2239. /* decorator:
  2240. * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
  2241. */
  2242. static int
  2243. validate_decorator(node *tree)
  2244. {
  2245. int ok;
  2246. int nch = NCH(tree);
  2247. ok = (validate_ntype(tree, decorator) &&
  2248. (nch == 3 || nch == 5 || nch == 6) &&
  2249. validate_at(CHILD(tree, 0)) &&
  2250. validate_dotted_name(CHILD(tree, 1)) &&
  2251. validate_newline(RCHILD(tree, -1)));
  2252. if (ok && nch != 3) {
  2253. ok = (validate_lparen(CHILD(tree, 2)) &&
  2254. validate_rparen(RCHILD(tree, -2)));
  2255. if (ok && nch == 6)
  2256. ok = validate_arglist(CHILD(tree, 3));
  2257. }
  2258. return ok;
  2259. }
  2260. /* decorators:
  2261. * decorator+
  2262. */
  2263. static int
  2264. validate_decorators(node *tree)
  2265. {
  2266. int i, nch, ok;
  2267. nch = NCH(tree);
  2268. ok = validate_ntype(tree, decorators) && nch >= 1;
  2269. for (i = 0; ok && i < nch; ++i)
  2270. ok = validate_decorator(CHILD(tree, i));
  2271. return ok;
  2272. }
  2273. /* with_var
  2274. with_var: 'as' expr
  2275. */
  2276. static int
  2277. validate_with_var(node *tree)
  2278. {
  2279. int nch = NCH(tree);
  2280. int ok = (validate_ntype(tree, with_var)
  2281. && (nch == 2)
  2282. && validate_name(CHILD(tree, 0), "as")
  2283. && validate_expr(CHILD(tree, 1)));
  2284. return ok;
  2285. }
  2286. /* with_stmt
  2287. * 0 1 2 -2 -1
  2288. with_stmt: 'with' test [ with_var ] ':' suite
  2289. */
  2290. static int
  2291. validate_with_stmt(node *tree)
  2292. {
  2293. int nch = NCH(tree);
  2294. int ok = (validate_ntype(tree, with_stmt)
  2295. && ((nch == 4) || (nch == 5))
  2296. && validate_name(CHILD(tree, 0), "with")
  2297. && validate_test(CHILD(tree, 1))
  2298. && (nch == 4 || validate_with_var(CHILD(tree, 2)))
  2299. && validate_colon(RCHILD(tree, -2))
  2300. && validate_suite(RCHILD(tree, -1)));
  2301. return ok;
  2302. }
  2303. /* funcdef:
  2304. *
  2305. * -5 -4 -3 -2 -1
  2306. * 'def' NAME parameters ':' suite
  2307. */
  2308. static int
  2309. validate_funcdef(node *tree)
  2310. {
  2311. int nch = NCH(tree);
  2312. int ok = (validate_ntype(tree, funcdef)
  2313. && (nch == 5)
  2314. && validate_name(RCHILD(tree, -5), "def")
  2315. && validate_ntype(RCHILD(tree, -4), NAME)
  2316. && validate_colon(RCHILD(tree, -2))
  2317. && validate_parameters(RCHILD(tree, -3))
  2318. && validate_suite(RCHILD(tree, -1)));
  2319. return ok;
  2320. }
  2321. /* decorated
  2322. * decorators (classdef | funcdef)
  2323. */
  2324. static int
  2325. validate_decorated(node *tree)
  2326. {
  2327. int nch = NCH(tree);
  2328. int ok = (validate_ntype(tree, decorated)
  2329. && (nch == 2)
  2330. && validate_decorators(RCHILD(tree, -2))
  2331. && (validate_funcdef(RCHILD(tree, -1))
  2332. || validate_class(RCHILD(tree, -1)))
  2333. );
  2334. return ok;
  2335. }
  2336. static int
  2337. validate_lambdef(node *tree)
  2338. {
  2339. int nch = NCH(tree);
  2340. int res = (validate_ntype(tree, lambdef)
  2341. && ((nch == 3) || (nch == 4))
  2342. && validate_name(CHILD(tree, 0), "lambda")
  2343. && validate_colon(CHILD(tree, nch - 2))
  2344. && validate_test(CHILD(tree, nch - 1)));
  2345. if (res && (nch == 4))
  2346. res = validate_varargslist(CHILD(tree, 1));
  2347. else if (!res && !PyErr_Occurred())
  2348. (void) validate_numnodes(tree, 3, "lambdef");
  2349. return (res);
  2350. }
  2351. static int
  2352. validate_old_lambdef(node *tree)
  2353. {
  2354. int nch = NCH(tree);
  2355. int res = (validate_ntype(tree, old_lambdef)
  2356. && ((nch == 3) || (nch == 4))
  2357. && validate_name(CHILD(tree, 0), "lambda")
  2358. && validate_colon(CHILD(tree, nch - 2))
  2359. && validate_test(CHILD(tree, nch - 1)));
  2360. if (res && (nch == 4))
  2361. res = validate_varargslist(CHILD(tree, 1));
  2362. else if (!res && !PyErr_Occurred())
  2363. (void) validate_numnodes(tree, 3, "old_lambdef");
  2364. return (res);
  2365. }
  2366. /* arglist:
  2367. *
  2368. * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
  2369. */
  2370. static int
  2371. validate_arglist(node *tree)
  2372. {
  2373. int nch = NCH(tree);
  2374. int i = 0;
  2375. int ok = 1;
  2376. if (nch <= 0)
  2377. /* raise the right error from having an invalid number of children */
  2378. return validate_numnodes(tree, nch + 1, "arglist");
  2379. if (nch > 1) {
  2380. for (i=0; i<nch; i++) {
  2381. if (TYPE(CHILD(tree, i)) == argument) {
  2382. node *ch = CHILD(tree, i);
  2383. if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
  2384. err_string("need '(', ')' for generator expression");
  2385. return 0;
  2386. }
  2387. }
  2388. }
  2389. }
  2390. while (ok && nch-i >= 2) {
  2391. /* skip leading (argument ',') */
  2392. ok = (validate_argument(CHILD(tree, i))
  2393. && validate_comma(CHILD(tree, i+1)));
  2394. if (ok)
  2395. i += 2;
  2396. else
  2397. PyErr_Clear();
  2398. }
  2399. ok = 1;
  2400. if (nch-i > 0) {
  2401. /*
  2402. * argument | '*' test [',' '**' test] | '**' test
  2403. */
  2404. int sym = TYPE(CHILD(tree, i));
  2405. if (sym == argument) {
  2406. ok = validate_argument(CHILD(tree, i));
  2407. if (ok && i+1 != nch) {
  2408. err_string("illegal arglist specification"
  2409. " (extra stuff on end)");
  2410. ok = 0;
  2411. }
  2412. }
  2413. else if (sym == STAR) {
  2414. ok = validate_star(CHILD(tree, i));
  2415. if (ok && (nch-i == 2))
  2416. ok = validate_test(CHILD(tree, i+1));
  2417. else if (ok && (nch-i == 5))
  2418. ok = (validate_test(CHILD(tree, i+1))
  2419. && validate_comma(CHILD(tree, i+2))
  2420. && validate_doublestar(CHILD(tree, i+3))
  2421. && validate_test(CHILD(tree, i+4)));
  2422. else {
  2423. err_string("illegal use of '*' in arglist");
  2424. ok = 0;
  2425. }
  2426. }
  2427. else if (sym == DOUBLESTAR) {
  2428. if (nch-i == 2)
  2429. ok = (validate_doublestar(CHILD(tree, i))
  2430. && validate_test(CHILD(tree, i+1)));
  2431. else {
  2432. err_string("illegal use of '**' in arglist");
  2433. ok = 0;
  2434. }
  2435. }
  2436. else {
  2437. err_string("illegal arglist specification");
  2438. ok = 0;
  2439. }
  2440. }
  2441. return (ok);
  2442. }
  2443. /* argument:
  2444. *
  2445. * [test '='] test [gen_for]
  2446. */
  2447. static int
  2448. validate_argument(node *tree)
  2449. {
  2450. int nch = NCH(tree);
  2451. int res = (validate_ntype(tree, argument)
  2452. && ((nch == 1) || (nch == 2) || (nch == 3))
  2453. && validate_test(CHILD(tree, 0)));
  2454. if (res && (nch == 2))
  2455. res = validate_gen_for(CHILD(tree, 1));
  2456. else if (res && (nch == 3))
  2457. res = (validate_equal(CHILD(tree, 1))
  2458. && validate_test(CHILD(tree, 2)));
  2459. return (res);
  2460. }
  2461. /* trailer:
  2462. *
  2463. * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
  2464. */
  2465. static int
  2466. validate_trailer(node *tree)
  2467. {
  2468. int nch = NCH(tree);
  2469. int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
  2470. if (res) {
  2471. switch (TYPE(CHILD(tree, 0))) {
  2472. case LPAR:
  2473. res = validate_rparen(CHILD(tree, nch - 1));
  2474. if (res && (nch == 3))
  2475. res = validate_arglist(CHILD(tree, 1));
  2476. break;
  2477. case LSQB:
  2478. res = (validate_numnodes(tree, 3, "trailer")
  2479. && validate_subscriptlist(CHILD(tree, 1))
  2480. && validate_ntype(CHILD(tree, 2), RSQB));
  2481. break;
  2482. case DOT:
  2483. res = (validate_numnodes(tree, 2, "trailer")
  2484. && validate_ntype(CHILD(tree, 1), NAME));
  2485. break;
  2486. default:
  2487. res = 0;
  2488. break;
  2489. }
  2490. }
  2491. else {
  2492. (void) validate_numnodes(tree, 2, "trailer");
  2493. }
  2494. return (res);
  2495. }
  2496. /* subscriptlist:
  2497. *
  2498. * subscript (',' subscript)* [',']
  2499. */
  2500. static int
  2501. validate_subscriptlist(node *tree)
  2502. {
  2503. return (validate_repeating_list(tree, subscriptlist,
  2504. validate_subscript, "subscriptlist"));
  2505. }
  2506. /* subscript:
  2507. *
  2508. * '.' '.' '.' | test | [test] ':' [test] [sliceop]
  2509. */
  2510. static int
  2511. validate_subscript(node *tree)
  2512. {
  2513. int offset = 0;
  2514. int nch = NCH(tree);
  2515. int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
  2516. if (!res) {
  2517. if (!PyErr_Occurred())
  2518. err_string("invalid number of arguments for subscript node");
  2519. return (0);
  2520. }
  2521. if (TYPE(CHILD(tree, 0)) == DOT)
  2522. /* take care of ('.' '.' '.') possibility */
  2523. return (validate_numnodes(tree, 3, "subscript")
  2524. && validate_dot(CHILD(tree, 0))
  2525. && validate_dot(CHILD(tree, 1))
  2526. && validate_dot(CHILD(tree, 2)));
  2527. if (nch == 1) {
  2528. if (TYPE(CHILD(tree, 0)) == test)
  2529. res = validate_test(CHILD(tree, 0));
  2530. else
  2531. res = validate_colon(CHILD(tree, 0));
  2532. return (res);
  2533. }
  2534. /* Must be [test] ':' [test] [sliceop],
  2535. * but at least one of the optional components will
  2536. * be present, but we don't know which yet.
  2537. */
  2538. if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
  2539. res = validate_test(CHILD(tree, 0));
  2540. offset = 1;
  2541. }
  2542. if (res)
  2543. res = validate_colon(CHILD(tree, offset));
  2544. if (res) {
  2545. int rem = nch - ++offset;
  2546. if (rem) {
  2547. if (TYPE(CHILD(tree, offset)) == test) {
  2548. res = validate_test(CHILD(tree, offset));
  2549. ++offset;
  2550. --rem;
  2551. }
  2552. if (res && rem)
  2553. res = validate_sliceop(CHILD(tree, offset));
  2554. }
  2555. }
  2556. return (res);
  2557. }
  2558. static int
  2559. validate_sliceop(node *tree)
  2560. {
  2561. int nch = NCH(tree);
  2562. int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
  2563. && validate_ntype(tree, sliceop);
  2564. if (!res && !PyErr_Occurred()) {
  2565. res = validate_numnodes(tree, 1, "sliceop");
  2566. }
  2567. if (res)
  2568. res = validate_colon(CHILD(tree, 0));
  2569. if (res && (nch == 2))
  2570. res = validate_test(CHILD(tree, 1));
  2571. return (res);
  2572. }
  2573. static int
  2574. validate_exprlist(node *tree)
  2575. {
  2576. return (validate_repeating_list(tree, exprlist,
  2577. validate_expr, "exprlist"));
  2578. }
  2579. static int
  2580. validate_dictmaker(node *tree)
  2581. {
  2582. int nch = NCH(tree);
  2583. int res = (validate_ntype(tree, dictmaker)
  2584. && (nch >= 3)
  2585. && validate_test(CHILD(tree, 0))
  2586. && validate_colon(CHILD(tree, 1))
  2587. && validate_test(CHILD(tree, 2)));
  2588. if (res && ((nch % 4) == 0))
  2589. res = validate_comma(CHILD(tree, --nch));
  2590. else if (res)
  2591. res = ((nch % 4) == 3);
  2592. if (res && (nch > 3)) {
  2593. int pos = 3;
  2594. /* ( ',' test ':' test )* */
  2595. while (res && (pos < nch)) {
  2596. res = (validate_comma(CHILD(tree, pos))
  2597. && validate_test(CHILD(tree, pos + 1))
  2598. && validate_colon(CHILD(tree, pos + 2))
  2599. && validate_test(CHILD(tree, pos + 3)));
  2600. pos += 4;
  2601. }
  2602. }
  2603. return (res);
  2604. }
  2605. static int
  2606. validate_eval_input(node *tree)
  2607. {
  2608. int pos;
  2609. int nch = NCH(tree);
  2610. int res = (validate_ntype(tree, eval_input)
  2611. && (nch >= 2)
  2612. && validate_testlist(CHILD(tree, 0))
  2613. && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
  2614. for (pos = 1; res && (pos < (nch - 1)); ++pos)
  2615. res = validate_ntype(CHILD(tree, pos), NEWLINE);
  2616. return (res);
  2617. }
  2618. static int
  2619. validate_node(node *tree)
  2620. {
  2621. int nch = 0; /* num. children on current node */
  2622. int res = 1; /* result value */
  2623. node* next = 0; /* node to process after this one */
  2624. while (res && (tree != 0)) {
  2625. nch = NCH(tree);
  2626. next = 0;
  2627. switch (TYPE(tree)) {
  2628. /*
  2629. * Definition nodes.
  2630. */
  2631. case funcdef:
  2632. res = validate_funcdef(tree);
  2633. break;
  2634. case with_stmt:
  2635. res = validate_with_stmt(tree);
  2636. break;
  2637. case classdef:
  2638. res = validate_class(tree);
  2639. break;
  2640. case decorated:
  2641. res = validate_decorated(tree);
  2642. break;
  2643. /*
  2644. * "Trivial" parse tree nodes.
  2645. * (Why did I call these trivial?)
  2646. */
  2647. case stmt:
  2648. res = validate_stmt(tree);
  2649. break;
  2650. case small_stmt:
  2651. /*
  2652. * expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
  2653. * | import_stmt | global_stmt | exec_stmt | assert_stmt
  2654. */
  2655. res = validate_small_stmt(tree);
  2656. break;
  2657. case flow_stmt:
  2658. res = (validate_numnodes(tree, 1, "flow_stmt")
  2659. && ((TYPE(CHILD(tree, 0)) == break_stmt)
  2660. || (TYPE(CHILD(tree, 0)) == continue_stmt)
  2661. || (TYPE(CHILD(tree, 0)) == yield_stmt)
  2662. || (TYPE(CHILD(tree, 0)) == return_stmt)
  2663. || (TYPE(CHILD(tree, 0)) == raise_stmt)));
  2664. if (res)
  2665. next = CHILD(tree, 0);
  2666. else if (nch == 1)
  2667. err_string("illegal flow_stmt type");
  2668. break;
  2669. case yield_stmt:
  2670. res = validate_yield_stmt(tree);
  2671. break;
  2672. /*
  2673. * Compound statements.
  2674. */
  2675. case simple_stmt:
  2676. res = validate_simple_stmt(tree);
  2677. break;
  2678. case compound_stmt:
  2679. res = validate_compound_stmt(tree);
  2680. break;
  2681. /*
  2682. * Fundamental statements.
  2683. */
  2684. case expr_stmt:
  2685. res = validate_expr_stmt(tree);
  2686. break;
  2687. case print_stmt:
  2688. res = validate_print_stmt(tree);
  2689. break;
  2690. case del_stmt:
  2691. res = validate_del_stmt(tree);
  2692. break;
  2693. case pass_stmt:
  2694. res = (validate_numnodes(tree, 1, "pass")
  2695. && validate_name(CHILD(tree, 0), "pass"));
  2696. break;
  2697. case break_stmt:
  2698. res = (validate_numnodes(tree, 1, "break")
  2699. && validate_name(CHILD(tree, 0), "break"));
  2700. break;
  2701. case continue_stmt:
  2702. res = (validate_numnodes(tree, 1, "continue")
  2703. && validate_name(CHILD(tree, 0), "continue"));
  2704. break;
  2705. case return_stmt:
  2706. res = validate_return_stmt(tree);
  2707. break;
  2708. case raise_stmt:
  2709. res = validate_raise_stmt(tree);
  2710. break;
  2711. case import_stmt:
  2712. res = validate_import_stmt(tree);
  2713. break;
  2714. case import_name:
  2715. res = validate_import_name(tree);
  2716. break;
  2717. case import_from:
  2718. res = validate_import_from(tree);
  2719. break;
  2720. case global_stmt:
  2721. res = validate_global_stmt(tree);
  2722. break;
  2723. case exec_stmt:
  2724. res = validate_exec_stmt(tree);
  2725. break;
  2726. case assert_stmt:
  2727. res = validate_assert_stmt(tree);
  2728. break;
  2729. case if_stmt:
  2730. res = validate_if(tree);
  2731. break;
  2732. case while_stmt:
  2733. res = validate_while(tree);
  2734. break;
  2735. case for_stmt:
  2736. res = validate_for(tree);
  2737. break;
  2738. case try_stmt:
  2739. res = validate_try(tree);
  2740. break;
  2741. case suite:
  2742. res = validate_suite(tree);
  2743. break;
  2744. /*
  2745. * Expression nodes.
  2746. */
  2747. case testlist:
  2748. res = validate_testlist(tree);
  2749. break;
  2750. case yield_expr:
  2751. res = validate_yield_expr(tree);
  2752. break;
  2753. case testlist1:
  2754. res = validate_testlist1(tree);
  2755. break;
  2756. case test:
  2757. res = validate_test(tree);
  2758. break;
  2759. case and_test:
  2760. res = validate_and_test(tree);
  2761. break;
  2762. case not_test:
  2763. res = validate_not_test(tree);
  2764. break;
  2765. case comparison:
  2766. res = validate_comparison(tree);
  2767. break;
  2768. case exprlist:
  2769. res = validate_exprlist(tree);
  2770. break;
  2771. case comp_op:
  2772. res = validate_comp_op(tree);
  2773. break;
  2774. case expr:
  2775. res = validate_expr(tree);
  2776. break;
  2777. case xor_expr:
  2778. res = validate_xor_expr(tree);
  2779. break;
  2780. case and_expr:
  2781. res = validate_and_expr(tree);
  2782. break;
  2783. case shift_expr:
  2784. res = validate_shift_expr(tree);
  2785. break;
  2786. case arith_expr:
  2787. res = validate_arith_expr(tree);
  2788. break;
  2789. case term:
  2790. res = validate_term(tree);
  2791. break;
  2792. case factor:
  2793. res = validate_factor(tree);
  2794. break;
  2795. case power:
  2796. res = validate_power(tree);
  2797. break;
  2798. case atom:
  2799. res = validate_atom(tree);
  2800. break;
  2801. default:
  2802. /* Hopefully never reached! */
  2803. err_string("unrecognized node type");
  2804. res = 0;
  2805. break;
  2806. }
  2807. tree = next;
  2808. }
  2809. return (res);
  2810. }
  2811. static int
  2812. validate_expr_tree(node *tree)
  2813. {
  2814. int res = validate_eval_input(tree);
  2815. if (!res && !PyErr_Occurred())
  2816. err_string("could not validate expression tuple");
  2817. return (res);
  2818. }
  2819. /* file_input:
  2820. * (NEWLINE | stmt)* ENDMARKER
  2821. */
  2822. static int
  2823. validate_file_input(node *tree)
  2824. {
  2825. int j;
  2826. int nch = NCH(tree) - 1;
  2827. int res = ((nch >= 0)
  2828. && validate_ntype(CHILD(tree, nch), ENDMARKER));
  2829. for (j = 0; res && (j < nch); ++j) {
  2830. if (TYPE(CHILD(tree, j)) == stmt)
  2831. res = validate_stmt(CHILD(tree, j));
  2832. else
  2833. res = validate_newline(CHILD(tree, j));
  2834. }
  2835. /* This stays in to prevent any internal failures from getting to the
  2836. * user. Hopefully, this won't be needed. If a user reports getting
  2837. * this, we have some debugging to do.
  2838. */
  2839. if (!res && !PyErr_Occurred())
  2840. err_string("VALIDATION FAILURE: report this to the maintainer!");
  2841. return (res);
  2842. }
  2843. static int
  2844. validate_encoding_decl(node *tree)
  2845. {
  2846. int nch = NCH(tree);
  2847. int res = ((nch == 1)
  2848. && validate_file_input(CHILD(tree, 0)));
  2849. if (!res && !PyErr_Occurred())
  2850. err_string("Error Parsing encoding_decl");
  2851. return res;
  2852. }
  2853. static PyObject*
  2854. pickle_constructor = NULL;
  2855. static PyObject*
  2856. parser__pickler(PyObject *self, PyObject *args)
  2857. {
  2858. NOTE(ARGUNUSED(self))
  2859. PyObject *result = NULL;
  2860. PyObject *st = NULL;
  2861. PyObject *empty_dict = NULL;
  2862. if (PyArg_ParseTuple(args, "O!:_pickler", &PyST_Type, &st)) {
  2863. PyObject *newargs;
  2864. PyObject *tuple;
  2865. if ((empty_dict = PyDict_New()) == NULL)
  2866. goto finally;
  2867. if ((newargs = Py_BuildValue("Oi", st, 1)) == NULL)
  2868. goto finally;
  2869. tuple = parser_st2tuple((PyST_Object*)NULL, newargs, empty_dict);
  2870. if (tuple != NULL) {
  2871. result = Py_BuildValue("O(O)", pickle_constructor, tuple);
  2872. Py_DECREF(tuple);
  2873. }
  2874. Py_DECREF(empty_dict);
  2875. Py_DECREF(newargs);
  2876. }
  2877. finally:
  2878. Py_XDECREF(empty_dict);
  2879. return (result);
  2880. }
  2881. /* Functions exported by this module. Most of this should probably
  2882. * be converted into an ST object with methods, but that is better
  2883. * done directly in Python, allowing subclasses to be created directly.
  2884. * We'd really have to write a wrapper around it all anyway to allow
  2885. * inheritance.
  2886. */
  2887. static PyMethodDef parser_functions[] = {
  2888. {"ast2tuple", (PyCFunction)parser_ast2tuple, PUBLIC_METHOD_TYPE,
  2889. PyDoc_STR("Creates a tuple-tree representation of an ST.")},
  2890. {"ast2list", (PyCFunction)parser_ast2list, PUBLIC_METHOD_TYPE,
  2891. PyDoc_STR("Creates a list-tree representation of an ST.")},
  2892. {"compileast", (PyCFunction)parser_compileast,PUBLIC_METHOD_TYPE,
  2893. PyDoc_STR("Compiles an ST object into a code object.")},
  2894. {"compilest", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,
  2895. PyDoc_STR("Compiles an ST object into a code object.")},
  2896. {"expr", (PyCFunction)parser_expr, PUBLIC_METHOD_TYPE,
  2897. PyDoc_STR("Creates an ST object from an expression.")},
  2898. {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,
  2899. PyDoc_STR("Determines if an ST object was created from an expression.")},
  2900. {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,
  2901. PyDoc_STR("Determines if an ST object was created from a suite.")},
  2902. {"suite", (PyCFunction)parser_suite, PUBLIC_METHOD_TYPE,
  2903. PyDoc_STR("Creates an ST object from a suite.")},
  2904. {"sequence2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
  2905. PyDoc_STR("Creates an ST object from a tree representation.")},
  2906. {"sequence2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
  2907. PyDoc_STR("Creates an ST object from a tree representation.")},
  2908. {"st2tuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,
  2909. PyDoc_STR("Creates a tuple-tree representation of an ST.")},
  2910. {"st2list", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,
  2911. PyDoc_STR("Creates a list-tree representation of an ST.")},
  2912. {"tuple2ast", (PyCFunction)parser_tuple2ast, PUBLIC_METHOD_TYPE,
  2913. PyDoc_STR("Creates an ST object from a tree representation.")},
  2914. {"tuple2st", (PyCFunction)parser_tuple2st, PUBLIC_METHOD_TYPE,
  2915. PyDoc_STR("Creates an ST object from a tree representation.")},
  2916. /* private stuff: support pickle module */
  2917. {"_pickler", (PyCFunction)parser__pickler, METH_VARARGS,
  2918. PyDoc_STR("Returns the pickle magic to allow ST objects to be pickled.")},
  2919. {NULL, NULL, 0, NULL}
  2920. };
  2921. PyMODINIT_FUNC initparser(void); /* supply a prototype */
  2922. PyMODINIT_FUNC
  2923. initparser(void)
  2924. {
  2925. PyObject *module, *copyreg;
  2926. Py_TYPE(&PyST_Type) = &PyType_Type;
  2927. module = Py_InitModule("parser", parser_functions);
  2928. if (module == NULL)
  2929. return;
  2930. if (parser_error == 0)
  2931. parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
  2932. if (parser_error == 0)
  2933. /* caller will check PyErr_Occurred() */
  2934. return;
  2935. /* CAUTION: The code next used to skip bumping the refcount on
  2936. * parser_error. That's a disaster if initparser() gets called more
  2937. * than once. By incref'ing, we ensure that each module dict that
  2938. * gets created owns its reference to the shared parser_error object,
  2939. * and the file static parser_error vrbl owns a reference too.
  2940. */
  2941. Py_INCREF(parser_error);
  2942. if (PyModule_AddObject(module, "ParserError", parser_error) != 0)
  2943. return;
  2944. Py_INCREF(&PyST_Type);
  2945. PyModule_AddObject(module, "ASTType", (PyObject*)&PyST_Type);
  2946. Py_INCREF(&PyST_Type);
  2947. PyModule_AddObject(module, "STType", (PyObject*)&PyST_Type);
  2948. PyModule_AddStringConstant(module, "__copyright__",
  2949. parser_copyright_string);
  2950. PyModule_AddStringConstant(module, "__doc__",
  2951. parser_doc_string);
  2952. PyModule_AddStringConstant(module, "__version__",
  2953. parser_version_string);
  2954. /* Register to support pickling.
  2955. * If this fails, the import of this module will fail because an
  2956. * exception will be raised here; should we clear the exception?
  2957. */
  2958. copyreg = PyImport_ImportModuleNoBlock("copy_reg");
  2959. if (copyreg != NULL) {
  2960. PyObject *func, *pickler;
  2961. func = PyObject_GetAttrString(copyreg, "pickle");
  2962. pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
  2963. pickler = PyObject_GetAttrString(module, "_pickler");
  2964. Py_XINCREF(pickle_constructor);
  2965. if ((func != NULL) && (pickle_constructor != NULL)
  2966. && (pickler != NULL)) {
  2967. PyObject *res;
  2968. res = PyObject_CallFunctionObjArgs(func, &PyST_Type, pickler,
  2969. pickle_constructor, NULL);
  2970. Py_XDECREF(res);
  2971. }
  2972. Py_XDECREF(func);
  2973. Py_XDECREF(pickle_constructor);
  2974. Py_XDECREF(pickler);
  2975. Py_DECREF(copyreg);
  2976. }
  2977. }