Changeset 388 for python/vendor/current/Modules/parsermodule.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Modules/parsermodule.c
r2 r388 170 170 171 171 static void parser_free(PyST_Object *st); 172 static PyObject* parser_sizeof(PyST_Object *, void *); 172 173 static int parser_compare(PyST_Object *left, PyST_Object *right); 173 174 static PyObject *parser_getattr(PyObject *self, char *name); 174 175 static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *); 176 static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *); 177 static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *); 178 static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *); 179 static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *); 180 181 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS) 182 183 static PyMethodDef 184 parser_methods[] = { 185 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE, 186 PyDoc_STR("Compile this ST object into a code object.")}, 187 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE, 188 PyDoc_STR("Determines if this ST object was created from an expression.")}, 189 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE, 190 PyDoc_STR("Determines if this ST object was created from a suite.")}, 191 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE, 192 PyDoc_STR("Creates a list-tree representation of this ST.")}, 193 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE, 194 PyDoc_STR("Creates a tuple-tree representation of this ST.")}, 195 {"__sizeof__", (PyCFunction)parser_sizeof, METH_NOARGS, 196 PyDoc_STR("Returns size in memory, in bytes.")}, 197 {NULL, NULL, 0, NULL} 198 }; 175 199 176 200 static … … 201 225 202 226 /* __doc__ */ 203 "Intermediate representation of a Python parse tree." 227 "Intermediate representation of a Python parse tree.", 228 0, /* tp_traverse */ 229 0, /* tp_clear */ 230 0, /* tp_richcompare */ 231 0, /* tp_weaklistoffset */ 232 0, /* tp_iter */ 233 0, /* tp_iternext */ 234 parser_methods, /* tp_methods */ 204 235 }; /* PyST_Type */ 205 236 … … 320 351 int col_offset = 0; 321 352 if (line_option != NULL) { 322 lineno = (PyObject_IsTrue(line_option) != 0) ? 1 : 0; 353 lineno = PyObject_IsTrue(line_option); 354 if (lineno < 0) 355 return NULL; 323 356 } 324 357 if (col_option != NULL) { 325 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0; 358 col_offset = PyObject_IsTrue(col_option); 359 if (col_offset < 0) 360 return NULL; 326 361 } 327 362 /* … … 371 406 int col_offset = 0; 372 407 if (line_option != 0) { 373 lineno = PyObject_IsTrue(line_option) ? 1 : 0; 374 } 375 if (col_option != NULL) { 376 col_offset = (PyObject_IsTrue(col_option) != 0) ? 1 : 0; 408 lineno = PyObject_IsTrue(line_option); 409 if (lineno < 0) 410 return NULL; 411 } 412 if (col_option != 0) { 413 col_offset = PyObject_IsTrue(col_option); 414 if (col_offset < 0) 415 return NULL; 377 416 } 378 417 /* … … 495 534 496 535 497 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)498 499 static PyMethodDef500 parser_methods[] = {501 {"compile", (PyCFunction)parser_compilest, PUBLIC_METHOD_TYPE,502 PyDoc_STR("Compile this ST object into a code object.")},503 {"isexpr", (PyCFunction)parser_isexpr, PUBLIC_METHOD_TYPE,504 PyDoc_STR("Determines if this ST object was created from an expression.")},505 {"issuite", (PyCFunction)parser_issuite, PUBLIC_METHOD_TYPE,506 PyDoc_STR("Determines if this ST object was created from a suite.")},507 {"tolist", (PyCFunction)parser_st2list, PUBLIC_METHOD_TYPE,508 PyDoc_STR("Creates a list-tree representation of this ST.")},509 {"totuple", (PyCFunction)parser_st2tuple, PUBLIC_METHOD_TYPE,510 PyDoc_STR("Creates a tuple-tree representation of this ST.")},511 512 {NULL, NULL, 0, NULL}513 };514 515 516 536 static PyObject* 517 537 parser_getattr(PyObject *self, char *name) … … 556 576 &err, &flags); 557 577 558 559 578 if (n) { 579 res = parser_newstobject(n, type); 560 580 if (res) 561 581 ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK; … … 679 699 } 680 700 } 681 /* Make sure we throwan exception on all errors. We should never701 /* Make sure we raise an exception on all errors. We should never 682 702 * get this, but we'd do well to be sure something is done. 683 703 */ … … 694 714 return NULL; 695 715 return parser_tuple2st(self, args, kw); 716 } 717 718 static PyObject * 719 parser_sizeof(PyST_Object *st, void *unused) 720 { 721 Py_ssize_t res; 722 723 res = sizeof(PyST_Object) + _PyNode_SizeOf(st->st_node); 724 return PyLong_FromSsize_t(res); 696 725 } 697 726 … … 768 797 "third item in terminal node must be an" 769 798 " integer, found %s", 770 799 Py_TYPE(temp)->tp_name); 771 800 Py_DECREF(o); 772 801 Py_DECREF(temp); … … 785 814 /* 786 815 * It has to be one or the other; this is an error. 787 * Throwan exception.816 * Raise an exception. 788 817 */ 789 818 PyObject *err = Py_BuildValue("os", elem, "unknown node type."); … … 835 864 /* 836 865 * The tuple is simple, but it doesn't start with a start symbol. 837 * Throwan exception now and be done with it.866 * Raise an exception now and be done with it. 838 867 */ 839 868 tuple = Py_BuildValue("os", tuple, … … 936 965 VALIDATER(trailer); VALIDATER(subscript); 937 966 VALIDATER(subscriptlist); VALIDATER(sliceop); 938 VALIDATER(exprlist); VALIDATER(dict maker);967 VALIDATER(exprlist); VALIDATER(dictorsetmaker); 939 968 VALIDATER(arglist); VALIDATER(argument); 940 969 VALIDATER(listmaker); VALIDATER(yield_stmt); 941 VALIDATER(testlist1); VALIDATER( gen_for);942 VALIDATER( gen_iter); VALIDATER(gen_if);943 VALIDATER(testlist_ gexp);VALIDATER(yield_expr);944 VALIDATER(yield_or_testlist); 945 VALIDATER(old_test); 970 VALIDATER(testlist1); VALIDATER(comp_for); 971 VALIDATER(comp_iter); VALIDATER(comp_if); 972 VALIDATER(testlist_comp); VALIDATER(yield_expr); 973 VALIDATER(yield_or_testlist); VALIDATER(or_test); 974 VALIDATER(old_test); VALIDATER(old_lambdef); 946 975 947 976 #undef VALIDATER … … 1032 1061 int nch = NCH(tree); 1033 1062 int res = (validate_ntype(tree, classdef) && 1034 1063 ((nch == 4) || (nch == 6) || (nch == 7))); 1035 1064 1036 1065 if (res) { … … 1043 1072 (void) validate_numnodes(tree, 4, "class"); 1044 1073 } 1045 1074 1046 1075 if (res) { 1047 1048 1049 1050 1051 1052 1053 1054 1055 1076 if (nch == 7) { 1077 res = ((validate_lparen(CHILD(tree, 2)) && 1078 validate_testlist(CHILD(tree, 3)) && 1079 validate_rparen(CHILD(tree, 4)))); 1080 } 1081 else if (nch == 6) { 1082 res = (validate_lparen(CHILD(tree,2)) && 1083 validate_rparen(CHILD(tree,3))); 1084 } 1056 1085 } 1057 1086 return (res); … … 1343 1372 } 1344 1373 1345 /* gen_iter: gen_for | gen_if1346 */ 1347 static int 1348 validate_ gen_iter(node *tree)1349 { 1350 int res = (validate_ntype(tree, gen_iter)1351 && validate_numnodes(tree, 1, " gen_iter"));1352 if (res && TYPE(CHILD(tree, 0)) == gen_for)1353 res = validate_ gen_for(CHILD(tree, 0));1374 /* comp_iter: comp_for | comp_if 1375 */ 1376 static int 1377 validate_comp_iter(node *tree) 1378 { 1379 int res = (validate_ntype(tree, comp_iter) 1380 && validate_numnodes(tree, 1, "comp_iter")); 1381 if (res && TYPE(CHILD(tree, 0)) == comp_for) 1382 res = validate_comp_for(CHILD(tree, 0)); 1354 1383 else 1355 res = validate_ gen_if(CHILD(tree, 0));1384 res = validate_comp_if(CHILD(tree, 0)); 1356 1385 1357 1386 return res; … … 1380 1409 } 1381 1410 1382 /* gen_for: 'for' exprlist 'in' test [gen_iter]1383 */ 1384 static int 1385 validate_ gen_for(node *tree)1411 /* comp_for: 'for' exprlist 'in' test [comp_iter] 1412 */ 1413 static int 1414 validate_comp_for(node *tree) 1386 1415 { 1387 1416 int nch = NCH(tree); … … 1389 1418 1390 1419 if (nch == 5) 1391 res = validate_ gen_iter(CHILD(tree, 4));1420 res = validate_comp_iter(CHILD(tree, 4)); 1392 1421 else 1393 res = validate_numnodes(tree, 4, " gen_for");1422 res = validate_numnodes(tree, 4, "comp_for"); 1394 1423 1395 1424 if (res) … … 1422 1451 } 1423 1452 1424 /* gen_if: 'if' old_test [gen_iter]1425 */ 1426 static int 1427 validate_ gen_if(node *tree)1453 /* comp_if: 'if' old_test [comp_iter] 1454 */ 1455 static int 1456 validate_comp_if(node *tree) 1428 1457 { 1429 1458 int nch = NCH(tree); … … 1431 1460 1432 1461 if (nch == 3) 1433 res = validate_ gen_iter(CHILD(tree, 2));1462 res = validate_comp_iter(CHILD(tree, 2)); 1434 1463 else 1435 res = validate_numnodes(tree, 2, " gen_if");1436 1464 res = validate_numnodes(tree, 2, "comp_if"); 1465 1437 1466 if (res) 1438 1467 res = (validate_name(CHILD(tree, 0), "if") … … 1594 1623 validate_yield_or_testlist(node *tree) 1595 1624 { 1596 if (TYPE(tree) == yield_expr) 1597 1598 1599 1625 if (TYPE(tree) == yield_expr) 1626 return validate_yield_expr(tree); 1627 else 1628 return validate_testlist(tree); 1600 1629 } 1601 1630 … … 1612 1641 && TYPE(CHILD(tree, 1)) == augassign) { 1613 1642 res = validate_numnodes(CHILD(tree, 1), 1, "augassign") 1614 1643 && validate_yield_or_testlist(CHILD(tree, 2)); 1615 1644 1616 1645 if (res) { … … 1630 1659 || strcmp(s, "**=") == 0); 1631 1660 if (!res) 1632 err_string("illegal augm mented assignment operator");1661 err_string("illegal augmented assignment operator"); 1633 1662 } 1634 1663 } … … 1838 1867 validate_dotted_as_names(node *tree) 1839 1868 { 1840 1841 1842 1843 1844 1845 1846 1847 1869 int nch = NCH(tree); 1870 int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0)); 1871 int i; 1872 1873 for (i = 1; res && (i < nch); i += 2) 1874 res = (validate_comma(CHILD(tree, i)) 1875 && validate_dotted_as_name(CHILD(tree, i + 1))); 1876 return (res); 1848 1877 } 1849 1878 … … 1858 1887 1859 1888 for (i = 1; res && (i + 1 < nch); i += 2) 1860 1861 1889 res = (validate_comma(CHILD(tree, i)) 1890 && validate_import_as_name(CHILD(tree, i + 1))); 1862 1891 return (res); 1863 1892 } … … 1868 1897 validate_import_name(node *tree) 1869 1898 { 1870 1871 1872 1873 1874 } 1875 1876 /* Helper function to count the number of leading dots in 1899 return (validate_ntype(tree, import_name) 1900 && validate_numnodes(tree, 2, "import_name") 1901 && validate_name(CHILD(tree, 0), "import") 1902 && validate_dotted_as_names(CHILD(tree, 1))); 1903 } 1904 1905 /* Helper function to count the number of leading dots in 1877 1906 * 'from ...module import name' 1878 1907 */ … … 1882 1911 int i; 1883 1912 for (i = 1; i < NCH(tree); i++) 1884 1885 1913 if (TYPE(CHILD(tree, i)) != DOT) 1914 break; 1886 1915 return i-1; 1887 1916 } 1888 1917 1889 /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |1890 * import_as_names1918 /* import_from: ('from' ('.'* dotted_name | '.'+) 1919 * 'import' ('*' | '(' import_as_names ')' | import_as_names)) 1891 1920 */ 1892 1921 static int 1893 1922 validate_import_from(node *tree) 1894 1923 { 1895 int nch = NCH(tree); 1896 int ndots = count_from_dots(tree); 1897 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name); 1898 int offset = ndots + havename; 1899 int res = validate_ntype(tree, import_from) 1900 && (nch >= 4 + ndots) 1901 && validate_name(CHILD(tree, 0), "from") 1902 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1))) 1903 && validate_name(CHILD(tree, offset + 1), "import"); 1904 1905 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR) 1906 res = ((nch == offset + 5) 1907 && validate_lparen(CHILD(tree, offset + 2)) 1908 && validate_import_as_names(CHILD(tree, offset + 3)) 1909 && validate_rparen(CHILD(tree, offset + 4))); 1910 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR) 1911 res = validate_import_as_names(CHILD(tree, offset + 2)); 1912 return (res); 1924 int nch = NCH(tree); 1925 int ndots = count_from_dots(tree); 1926 int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name); 1927 int offset = ndots + havename; 1928 int res = validate_ntype(tree, import_from) 1929 && (offset >= 1) 1930 && (nch >= 3 + offset) 1931 && validate_name(CHILD(tree, 0), "from") 1932 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1))) 1933 && validate_name(CHILD(tree, offset + 1), "import"); 1934 1935 if (res && TYPE(CHILD(tree, offset + 2)) == LPAR) 1936 res = ((nch == offset + 5) 1937 && validate_lparen(CHILD(tree, offset + 2)) 1938 && validate_import_as_names(CHILD(tree, offset + 3)) 1939 && validate_rparen(CHILD(tree, offset + 4))); 1940 else if (res && TYPE(CHILD(tree, offset + 2)) != STAR) 1941 res = validate_import_as_names(CHILD(tree, offset + 2)); 1942 return (res); 1913 1943 } 1914 1944 … … 1922 1952 1923 1953 if (res) { 1924 1925 1926 1954 int ntype = TYPE(CHILD(tree, 0)); 1955 1956 if (ntype == import_name || ntype == import_from) 1927 1957 res = validate_node(CHILD(tree, 0)); 1928 1958 else { … … 2093 2123 } 2094 2124 /* try/except statement: skip past except_clause sections */ 2095 while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {2125 while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) { 2096 2126 res = (validate_except_clause(CHILD(tree, pos)) 2097 2127 && validate_colon(CHILD(tree, pos + 1)) … … 2100 2130 } 2101 2131 /* skip else clause */ 2102 if (res && (TYPE(CHILD(tree, pos)) == NAME) &&2132 if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) && 2103 2133 (strcmp(STR(CHILD(tree, pos)), "else") == 0)) { 2104 2134 res = (validate_colon(CHILD(tree, pos + 1)) … … 2127 2157 if (res && (nch > 1)) 2128 2158 res = validate_test(CHILD(tree, 1)); 2129 if (res && (nch == 4)) 2130 res = (validate_comma(CHILD(tree, 2)) 2131 && validate_test(CHILD(tree, 3))); 2132 2159 if (res && (nch == 4)) { 2160 if (TYPE(CHILD(tree, 2)) == NAME) 2161 res = validate_name(CHILD(tree, 2), "as"); 2162 else 2163 res = validate_comma(CHILD(tree, 2)); 2164 res = res && validate_test(CHILD(tree, 3)); 2165 } 2133 2166 return (res); 2134 2167 } … … 2457 2490 2458 2491 if (res && (nch == 3)) { 2459 2460 2461 2462 res = validate_testlist_gexp(CHILD(tree, 1));2463 2492 if (TYPE(CHILD(tree, 1))==yield_expr) 2493 res = validate_yield_expr(CHILD(tree, 1)); 2494 else 2495 res = validate_testlist_comp(CHILD(tree, 1)); 2496 } 2464 2497 break; 2465 2498 case LSQB: … … 2479 2512 2480 2513 if (res && (nch == 3)) 2481 res = validate_dict maker(CHILD(tree, 1));2514 res = validate_dictorsetmaker(CHILD(tree, 1)); 2482 2515 break; 2483 2516 case BACKQUOTE: … … 2540 2573 } 2541 2574 2542 /* testlist_ gexp:2543 * test ( gen_for | (',' test)* [','] )2544 */ 2545 static int 2546 validate_testlist_ gexp(node *tree)2575 /* testlist_comp: 2576 * test ( comp_for | (',' test)* [','] ) 2577 */ 2578 static int 2579 validate_testlist_comp(node *tree) 2547 2580 { 2548 2581 int nch = NCH(tree); … … 2550 2583 2551 2584 if (nch == 0) 2552 err_string("missing child nodes of testlist_ gexp");2585 err_string("missing child nodes of testlist_comp"); 2553 2586 else { 2554 2587 ok = validate_test(CHILD(tree, 0)); … … 2556 2589 2557 2590 /* 2558 * gen_for | (',' test)* [',']2591 * comp_for | (',' test)* [','] 2559 2592 */ 2560 if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)2561 ok = validate_ gen_for(CHILD(tree, 1));2593 if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for) 2594 ok = validate_comp_for(CHILD(tree, 1)); 2562 2595 else { 2563 2596 /* (',' test)* [','] */ … … 2572 2605 else if (i != nch) { 2573 2606 ok = 0; 2574 err_string("illegal trailing nodes for testlist_ gexp");2607 err_string("illegal trailing nodes for testlist_comp"); 2575 2608 } 2576 2609 } … … 2587 2620 int nch = NCH(tree); 2588 2621 ok = (validate_ntype(tree, decorator) && 2589 2590 2591 2592 2622 (nch == 3 || nch == 5 || nch == 6) && 2623 validate_at(CHILD(tree, 0)) && 2624 validate_dotted_name(CHILD(tree, 1)) && 2625 validate_newline(RCHILD(tree, -1))); 2593 2626 2594 2627 if (ok && nch != 3) { 2595 2596 2597 2598 2599 2628 ok = (validate_lparen(CHILD(tree, 2)) && 2629 validate_rparen(RCHILD(tree, -2))); 2630 2631 if (ok && nch == 6) 2632 ok = validate_arglist(CHILD(tree, 3)); 2600 2633 } 2601 2634 … … 2609 2642 validate_decorators(node *tree) 2610 2643 { 2611 int i, nch, ok; 2644 int i, nch, ok; 2612 2645 nch = NCH(tree); 2613 2646 ok = validate_ntype(tree, decorators) && nch >= 1; 2614 2647 2615 2648 for (i = 0; ok && i < nch; ++i) 2616 2649 ok = validate_decorator(CHILD(tree, i)); 2617 2650 2618 2651 return ok; 2619 2652 } 2620 2653 2621 /* with_var 2622 with_var: 'as' expr 2623 */ 2624 static int 2625 validate_with_var(node *tree) 2626 { 2627 int nch = NCH(tree); 2628 int ok = (validate_ntype(tree, with_var) 2629 && (nch == 2) 2630 && validate_name(CHILD(tree, 0), "as") 2631 && validate_expr(CHILD(tree, 1))); 2632 return ok; 2633 } 2634 2635 /* with_stmt 2636 * 0 1 2 -2 -1 2637 with_stmt: 'with' test [ with_var ] ':' suite 2654 /* with_item: 2655 * test ['as' expr] 2656 */ 2657 static int 2658 validate_with_item(node *tree) 2659 { 2660 int nch = NCH(tree); 2661 int ok = (validate_ntype(tree, with_item) 2662 && (nch == 1 || nch == 3) 2663 && validate_test(CHILD(tree, 0))); 2664 if (ok && nch == 3) 2665 ok = (validate_name(CHILD(tree, 1), "as") 2666 && validate_expr(CHILD(tree, 2))); 2667 return ok; 2668 } 2669 2670 /* with_stmt: 2671 * 0 1 ... -2 -1 2672 * 'with' with_item (',' with_item)* ':' suite 2638 2673 */ 2639 2674 static int 2640 2675 validate_with_stmt(node *tree) 2641 2676 { 2677 int i; 2642 2678 int nch = NCH(tree); 2643 2679 int ok = (validate_ntype(tree, with_stmt) 2644 && ( (nch == 4) || (nch == 5))2680 && (nch % 2 == 0) 2645 2681 && validate_name(CHILD(tree, 0), "with") 2646 && validate_test(CHILD(tree, 1))2647 && (nch == 4 || validate_with_var(CHILD(tree, 2)))2648 2682 && validate_colon(RCHILD(tree, -2)) 2649 2683 && validate_suite(RCHILD(tree, -1))); 2650 return ok; 2684 for (i = 1; ok && i < nch - 2; i += 2) 2685 ok = validate_with_item(CHILD(tree, i)); 2686 return ok; 2651 2687 } 2652 2688 2653 2689 /* funcdef: 2654 * 2690 * 2655 2691 * -5 -4 -3 -2 -1 2656 2692 * 'def' NAME parameters ':' suite … … 2661 2697 int nch = NCH(tree); 2662 2698 int ok = (validate_ntype(tree, funcdef) 2663 2664 2665 2666 2667 2668 2699 && (nch == 5) 2700 && validate_name(RCHILD(tree, -5), "def") 2701 && validate_ntype(RCHILD(tree, -4), NAME) 2702 && validate_colon(RCHILD(tree, -2)) 2703 && validate_parameters(RCHILD(tree, -3)) 2704 && validate_suite(RCHILD(tree, -1))); 2669 2705 return ok; 2670 2706 } … … 2677 2713 validate_decorated(node *tree) 2678 2714 { 2679 int nch = NCH(tree); 2680 int ok = (validate_ntype(tree, decorated) 2681 && (nch == 2) 2682 && validate_decorators(RCHILD(tree, -2)) 2683 && (validate_funcdef(RCHILD(tree, -1)) 2684 || validate_class(RCHILD(tree, -1))) 2685 ); 2686 return ok; 2715 int nch = NCH(tree); 2716 int ok = (validate_ntype(tree, decorated) 2717 && (nch == 2) 2718 && validate_decorators(RCHILD(tree, -2))); 2719 if (TYPE(RCHILD(tree, -1)) == funcdef) 2720 ok = ok && validate_funcdef(RCHILD(tree, -1)); 2721 else 2722 ok = ok && validate_class(RCHILD(tree, -1)); 2723 return ok; 2687 2724 } 2688 2725 … … 2744 2781 if (TYPE(CHILD(tree, i)) == argument) { 2745 2782 node *ch = CHILD(tree, i); 2746 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {2783 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) { 2747 2784 err_string("need '(', ')' for generator expression"); 2748 2785 return 0; … … 2811 2848 /* argument: 2812 2849 * 2813 * [test '='] test [ gen_for]2850 * [test '='] test [comp_for] 2814 2851 */ 2815 2852 static int … … 2822 2859 2823 2860 if (res && (nch == 2)) 2824 res = validate_ gen_for(CHILD(tree, 1));2861 res = validate_comp_for(CHILD(tree, 1)); 2825 2862 else if (res && (nch == 3)) 2826 2863 res = (validate_equal(CHILD(tree, 1)) … … 2963 3000 2964 3001 2965 static int 2966 validate_dictmaker(node *tree) 2967 { 2968 int nch = NCH(tree); 2969 int res = (validate_ntype(tree, dictmaker) 2970 && (nch >= 3) 2971 && validate_test(CHILD(tree, 0)) 2972 && validate_colon(CHILD(tree, 1)) 2973 && validate_test(CHILD(tree, 2))); 2974 2975 if (res && ((nch % 4) == 0)) 2976 res = validate_comma(CHILD(tree, --nch)); 2977 else if (res) 2978 res = ((nch % 4) == 3); 2979 2980 if (res && (nch > 3)) { 2981 int pos = 3; 2982 /* ( ',' test ':' test )* */ 2983 while (res && (pos < nch)) { 2984 res = (validate_comma(CHILD(tree, pos)) 2985 && validate_test(CHILD(tree, pos + 1)) 2986 && validate_colon(CHILD(tree, pos + 2)) 2987 && validate_test(CHILD(tree, pos + 3))); 2988 pos += 4; 2989 } 2990 } 2991 return (res); 3002 /* 3003 * dictorsetmaker: 3004 * 3005 * (test ':' test (comp_for | (',' test ':' test)* [','])) | 3006 * (test (comp_for | (',' test)* [','])) 3007 */ 3008 static int 3009 validate_dictorsetmaker(node *tree) 3010 { 3011 int nch = NCH(tree); 3012 int ok = validate_ntype(tree, dictorsetmaker); 3013 int i = 0; 3014 int check_trailing_comma = 0; 3015 3016 assert(nch > 0); 3017 3018 if (ok && (nch == 1 || TYPE(CHILD(tree, 1)) == COMMA)) { 3019 /* We got a set: 3020 * test (',' test)* [','] 3021 */ 3022 ok = validate_test(CHILD(tree, i++)); 3023 while (ok && nch - i >= 2) { 3024 ok = (validate_comma(CHILD(tree, i)) 3025 && validate_test(CHILD(tree, i+1))); 3026 i += 2; 3027 } 3028 check_trailing_comma = 1; 3029 } 3030 else if (ok && TYPE(CHILD(tree, 1)) == comp_for) { 3031 /* We got a set comprehension: 3032 * test comp_for 3033 */ 3034 ok = (validate_test(CHILD(tree, 0)) 3035 && validate_comp_for(CHILD(tree, 1))); 3036 } 3037 else if (ok && NCH(tree) > 3 && TYPE(CHILD(tree, 3)) == comp_for) { 3038 /* We got a dict comprehension: 3039 * test ':' test comp_for 3040 */ 3041 ok = (validate_test(CHILD(tree, 0)) 3042 && validate_colon(CHILD(tree, 1)) 3043 && validate_test(CHILD(tree, 2)) 3044 && validate_comp_for(CHILD(tree, 3))); 3045 } 3046 else if (ok) { 3047 /* We got a dict: 3048 * test ':' test (',' test ':' test)* [','] 3049 */ 3050 if (nch >= 3) { 3051 ok = (validate_test(CHILD(tree, i)) 3052 && validate_colon(CHILD(tree, i+1)) 3053 && validate_test(CHILD(tree, i+2))); 3054 i += 3; 3055 } 3056 else { 3057 ok = 0; 3058 err_string("illegal number of nodes for dictorsetmaker"); 3059 } 3060 3061 while (ok && nch - i >= 4) { 3062 ok = (validate_comma(CHILD(tree, i)) 3063 && validate_test(CHILD(tree, i+1)) 3064 && validate_colon(CHILD(tree, i+2)) 3065 && validate_test(CHILD(tree, i+3))); 3066 i += 4; 3067 } 3068 check_trailing_comma = 1; 3069 } 3070 if (ok && check_trailing_comma) { 3071 if (i == nch-1) 3072 ok = validate_comma(CHILD(tree, i)); 3073 else if (i != nch) { 3074 ok = 0; 3075 err_string("illegal trailing nodes for dictorsetmaker"); 3076 } 3077 } 3078 3079 return ok; 2992 3080 } 2993 3081 … … 3033 3121 res = validate_class(tree); 3034 3122 break; 3035 3036 3037 3123 case decorated: 3124 res = validate_decorated(tree); 3125 break; 3038 3126 /* 3039 3127 * "Trivial" parse tree nodes. … … 3107 3195 res = validate_import_stmt(tree); 3108 3196 break; 3109 3110 3111 3112 3113 3114 3197 case import_name: 3198 res = validate_import_name(tree); 3199 break; 3200 case import_from: 3201 res = validate_import_from(tree); 3202 break; 3115 3203 case global_stmt: 3116 3204 res = validate_global_stmt(tree); … … 3348 3436 module = Py_InitModule("parser", parser_functions); 3349 3437 if (module == NULL) 3350 3438 return; 3351 3439 3352 3440 if (parser_error == 0)
Note:
See TracChangeset
for help on using the changeset viewer.