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