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

3493 lines
101 KiB

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