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