Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Modules/parsermodule.c

    r2 r388  
    170170
    171171static void parser_free(PyST_Object *st);
     172static PyObject* parser_sizeof(PyST_Object *, void *);
    172173static int parser_compare(PyST_Object *left, PyST_Object *right);
    173174static PyObject *parser_getattr(PyObject *self, char *name);
    174 
     175static PyObject* parser_compilest(PyST_Object *, PyObject *, PyObject *);
     176static PyObject* parser_isexpr(PyST_Object *, PyObject *, PyObject *);
     177static PyObject* parser_issuite(PyST_Object *, PyObject *, PyObject *);
     178static PyObject* parser_st2list(PyST_Object *, PyObject *, PyObject *);
     179static PyObject* parser_st2tuple(PyST_Object *, PyObject *, PyObject *);
     180
     181#define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
     182
     183static PyMethodDef
     184parser_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};
    175199
    176200static
     
    201225
    202226    /* __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 */
    204235};  /* PyST_Type */
    205236
     
    320351        int col_offset = 0;
    321352        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;
    323356        }
    324357        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;
    326361        }
    327362        /*
     
    371406        int col_offset = 0;
    372407        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;
    377416        }
    378417        /*
     
    495534
    496535
    497 #define PUBLIC_METHOD_TYPE (METH_VARARGS|METH_KEYWORDS)
    498 
    499 static PyMethodDef
    500 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 
    516536static PyObject*
    517537parser_getattr(PyObject *self, char *name)
     
    556576                                                      &err, &flags);
    557577
    558         if (n) {
    559             res = parser_newstobject(n, type);
     578        if (n) {
     579            res = parser_newstobject(n, type);
    560580            if (res)
    561581                ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK;
     
    679699        }
    680700    }
    681     /*  Make sure we throw an exception on all errors.  We should never
     701    /*  Make sure we raise an exception on all errors.  We should never
    682702     *  get this, but we'd do well to be sure something is done.
    683703     */
     
    694714        return NULL;
    695715    return parser_tuple2st(self, args, kw);
     716}
     717
     718static PyObject *
     719parser_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);
    696725}
    697726
     
    768797                                     "third item in terminal node must be an"
    769798                                     " integer, found %s",
    770                                      Py_TYPE(temp)->tp_name);
     799                                     Py_TYPE(temp)->tp_name);
    771800                        Py_DECREF(o);
    772801                        Py_DECREF(temp);
     
    785814            /*
    786815             *  It has to be one or the other; this is an error.
    787              *  Throw an exception.
     816             *  Raise an exception.
    788817             */
    789818            PyObject *err = Py_BuildValue("os", elem, "unknown node type.");
     
    835864        /*
    836865         *  The tuple is simple, but it doesn't start with a start symbol.
    837          *  Throw an exception now and be done with it.
     866         *  Raise an exception now and be done with it.
    838867         */
    839868        tuple = Py_BuildValue("os", tuple,
     
    936965VALIDATER(trailer);             VALIDATER(subscript);
    937966VALIDATER(subscriptlist);       VALIDATER(sliceop);
    938 VALIDATER(exprlist);            VALIDATER(dictmaker);
     967VALIDATER(exprlist);            VALIDATER(dictorsetmaker);
    939968VALIDATER(arglist);             VALIDATER(argument);
    940969VALIDATER(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);   VALIDATER(or_test);
    945 VALIDATER(old_test);            VALIDATER(old_lambdef);
     970VALIDATER(testlist1);           VALIDATER(comp_for);
     971VALIDATER(comp_iter);           VALIDATER(comp_if);
     972VALIDATER(testlist_comp);       VALIDATER(yield_expr);
     973VALIDATER(yield_or_testlist);   VALIDATER(or_test);
     974VALIDATER(old_test);            VALIDATER(old_lambdef);
    946975
    947976#undef VALIDATER
     
    10321061    int nch = NCH(tree);
    10331062    int res = (validate_ntype(tree, classdef) &&
    1034                 ((nch == 4) || (nch == 6) || (nch == 7)));
     1063                ((nch == 4) || (nch == 6) || (nch == 7)));
    10351064
    10361065    if (res) {
     
    10431072        (void) validate_numnodes(tree, 4, "class");
    10441073    }
    1045        
     1074
    10461075    if (res) {
    1047         if (nch == 7) {
    1048                 res = ((validate_lparen(CHILD(tree, 2)) &&
    1049                         validate_testlist(CHILD(tree, 3)) &&
    1050                         validate_rparen(CHILD(tree, 4))));
    1051         }
    1052         else if (nch == 6) {
    1053                 res = (validate_lparen(CHILD(tree,2)) &&
    1054                         validate_rparen(CHILD(tree,3)));
    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        }
    10561085    }
    10571086    return (res);
     
    13431372}
    13441373
    1345 /*  gen_iter:  gen_for | gen_if
    1346  */
    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 */
     1376static int
     1377validate_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));
    13541383    else
    1355         res = validate_gen_if(CHILD(tree, 0));
     1384        res = validate_comp_if(CHILD(tree, 0));
    13561385
    13571386    return res;
     
    13801409}
    13811410
    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 */
     1413static int
     1414validate_comp_for(node *tree)
    13861415{
    13871416    int nch = NCH(tree);
     
    13891418
    13901419    if (nch == 5)
    1391         res = validate_gen_iter(CHILD(tree, 4));
     1420        res = validate_comp_iter(CHILD(tree, 4));
    13921421    else
    1393         res = validate_numnodes(tree, 4, "gen_for");
     1422        res = validate_numnodes(tree, 4, "comp_for");
    13941423
    13951424    if (res)
     
    14221451}
    14231452
    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 */
     1455static int
     1456validate_comp_if(node *tree)
    14281457{
    14291458    int nch = NCH(tree);
     
    14311460
    14321461    if (nch == 3)
    1433         res = validate_gen_iter(CHILD(tree, 2));
     1462        res = validate_comp_iter(CHILD(tree, 2));
    14341463    else
    1435         res = validate_numnodes(tree, 2, "gen_if");
    1436    
     1464        res = validate_numnodes(tree, 2, "comp_if");
     1465
    14371466    if (res)
    14381467        res = (validate_name(CHILD(tree, 0), "if")
     
    15941623validate_yield_or_testlist(node *tree)
    15951624{
    1596         if (TYPE(tree) == yield_expr)
    1597                 return validate_yield_expr(tree);
    1598         else
    1599                 return validate_testlist(tree);
     1625        if (TYPE(tree) == yield_expr)
     1626                return validate_yield_expr(tree);
     1627        else
     1628                return validate_testlist(tree);
    16001629}
    16011630
     
    16121641        && TYPE(CHILD(tree, 1)) == augassign) {
    16131642        res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
    1614                 && validate_yield_or_testlist(CHILD(tree, 2));
     1643                && validate_yield_or_testlist(CHILD(tree, 2));
    16151644
    16161645        if (res) {
     
    16301659                   || strcmp(s, "**=") == 0);
    16311660            if (!res)
    1632                 err_string("illegal augmmented assignment operator");
     1661                err_string("illegal augmented assignment operator");
    16331662        }
    16341663    }
     
    18381867validate_dotted_as_names(node *tree)
    18391868{
    1840         int nch = NCH(tree);
    1841         int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
    1842         int i;
    1843 
    1844         for (i = 1; res && (i < nch); i += 2)
    1845             res = (validate_comma(CHILD(tree, i))
    1846                    && validate_dotted_as_name(CHILD(tree, i + 1)));
    1847         return (res);
     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);
    18481877}
    18491878
     
    18581887
    18591888    for (i = 1; res && (i + 1 < nch); i += 2)
    1860         res = (validate_comma(CHILD(tree, i))
    1861                && validate_import_as_name(CHILD(tree, i + 1)));
     1889        res = (validate_comma(CHILD(tree, i))
     1890               && validate_import_as_name(CHILD(tree, i + 1)));
    18621891    return (res);
    18631892}
     
    18681897validate_import_name(node *tree)
    18691898{
    1870         return (validate_ntype(tree, import_name)
    1871                 && validate_numnodes(tree, 2, "import_name")
    1872                 && validate_name(CHILD(tree, 0), "import")
    1873                 && validate_dotted_as_names(CHILD(tree, 1)));
    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
    18771906 * 'from ...module import name'
    18781907 */
     
    18821911        int i;
    18831912        for (i = 1; i < NCH(tree); i++)
    1884                 if (TYPE(CHILD(tree, i)) != DOT)
    1885                         break;
     1913                if (TYPE(CHILD(tree, i)) != DOT)
     1914                        break;
    18861915        return i-1;
    18871916}
    18881917
    1889 /* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
    1890  *     import_as_names
     1918/* import_from: ('from' ('.'* dotted_name | '.'+)
     1919 *               'import' ('*' | '(' import_as_names ')' | import_as_names))
    18911920 */
    18921921static int
    18931922validate_import_from(node *tree)
    18941923{
    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);
    19131943}
    19141944
     
    19221952
    19231953    if (res) {
    1924         int ntype = TYPE(CHILD(tree, 0));
    1925 
    1926         if (ntype == import_name || ntype == import_from)
     1954        int ntype = TYPE(CHILD(tree, 0));
     1955
     1956        if (ntype == import_name || ntype == import_from)
    19271957            res = validate_node(CHILD(tree, 0));
    19281958        else {
     
    20932123    }
    20942124    /* 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)) {
    20962126        res = (validate_except_clause(CHILD(tree, pos))
    20972127               && validate_colon(CHILD(tree, pos + 1))
     
    21002130    }
    21012131    /* skip else clause */
    2102     if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
     2132    if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
    21032133        (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
    21042134        res = (validate_colon(CHILD(tree, pos + 1))
     
    21272157    if (res && (nch > 1))
    21282158        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    }
    21332166    return (res);
    21342167}
     
    24572490
    24582491            if (res && (nch == 3)) {
    2459                 if (TYPE(CHILD(tree, 1))==yield_expr)
    2460                         res = validate_yield_expr(CHILD(tree, 1));
    2461                 else
    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            }
    24642497            break;
    24652498          case LSQB:
     
    24792512
    24802513            if (res && (nch == 3))
    2481                 res = validate_dictmaker(CHILD(tree, 1));
     2514                res = validate_dictorsetmaker(CHILD(tree, 1));
    24822515            break;
    24832516          case BACKQUOTE:
     
    25402573}
    25412574
    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 */
     2578static int
     2579validate_testlist_comp(node *tree)
    25472580{
    25482581    int nch = NCH(tree);
     
    25502583
    25512584    if (nch == 0)
    2552         err_string("missing child nodes of testlist_gexp");
     2585        err_string("missing child nodes of testlist_comp");
    25532586    else {
    25542587        ok = validate_test(CHILD(tree, 0));
     
    25562589
    25572590    /*
    2558      *  gen_for | (',' test)* [',']
     2591     *  comp_for | (',' test)* [',']
    25592592     */
    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));
    25622595    else {
    25632596        /*  (',' test)* [',']  */
     
    25722605        else if (i != nch) {
    25732606            ok = 0;
    2574             err_string("illegal trailing nodes for testlist_gexp");
     2607            err_string("illegal trailing nodes for testlist_comp");
    25752608        }
    25762609    }
     
    25872620    int nch = NCH(tree);
    25882621    ok = (validate_ntype(tree, decorator) &&
    2589           (nch == 3 || nch == 5 || nch == 6) &&
    2590           validate_at(CHILD(tree, 0)) &&
    2591           validate_dotted_name(CHILD(tree, 1)) &&
    2592           validate_newline(RCHILD(tree, -1)));
     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)));
    25932626
    25942627    if (ok && nch != 3) {
    2595         ok = (validate_lparen(CHILD(tree, 2)) &&
    2596               validate_rparen(RCHILD(tree, -2)));
    2597 
    2598         if (ok && nch == 6)
    2599             ok = validate_arglist(CHILD(tree, 3));
     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));
    26002633    }
    26012634
     
    26092642validate_decorators(node *tree)
    26102643{
    2611     int i, nch, ok; 
     2644    int i, nch, ok;
    26122645    nch = NCH(tree);
    26132646    ok = validate_ntype(tree, decorators) && nch >= 1;
    26142647
    26152648    for (i = 0; ok && i < nch; ++i)
    2616         ok = validate_decorator(CHILD(tree, i));
     2649        ok = validate_decorator(CHILD(tree, i));
    26172650
    26182651    return ok;
    26192652}
    26202653
    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 */
     2657static int
     2658validate_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
    26382673 */
    26392674static int
    26402675validate_with_stmt(node *tree)
    26412676{
     2677    int i;
    26422678    int nch = NCH(tree);
    26432679    int ok = (validate_ntype(tree, with_stmt)
    2644         && ((nch == 4) || (nch == 5))
     2680        && (nch % 2 == 0)
    26452681        && validate_name(CHILD(tree, 0), "with")
    2646         && validate_test(CHILD(tree, 1))
    2647         && (nch == 4 || validate_with_var(CHILD(tree, 2)))
    26482682        && validate_colon(RCHILD(tree, -2))
    26492683        && 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;
    26512687}
    26522688
    26532689/*  funcdef:
    2654  *     
     2690 *
    26552691 *     -5   -4         -3  -2    -1
    26562692 *  'def' NAME parameters ':' suite
     
    26612697    int nch = NCH(tree);
    26622698    int ok = (validate_ntype(tree, funcdef)
    2663                && (nch == 5)
    2664                && validate_name(RCHILD(tree, -5), "def")
    2665                && validate_ntype(RCHILD(tree, -4), NAME)
    2666                && validate_colon(RCHILD(tree, -2))
    2667                && validate_parameters(RCHILD(tree, -3))
    2668                && validate_suite(RCHILD(tree, -1)));
     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)));
    26692705    return ok;
    26702706}
     
    26772713validate_decorated(node *tree)
    26782714{
    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;
    26872724}
    26882725
     
    27442781            if (TYPE(CHILD(tree, i)) == argument) {
    27452782                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) {
    27472784                    err_string("need '(', ')' for generator expression");
    27482785                    return 0;
     
    28112848/*  argument:
    28122849 *
    2813  *  [test '='] test [gen_for]
     2850 *  [test '='] test [comp_for]
    28142851 */
    28152852static int
     
    28222859
    28232860    if (res && (nch == 2))
    2824         res = validate_gen_for(CHILD(tree, 1));
     2861        res = validate_comp_for(CHILD(tree, 1));
    28252862    else if (res && (nch == 3))
    28262863        res = (validate_equal(CHILD(tree, 1))
     
    29633000
    29643001
    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 */
     3008static int
     3009validate_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;
    29923080}
    29933081
     
    30333121            res = validate_class(tree);
    30343122            break;
    3035           case decorated:
    3036             res = validate_decorated(tree);
    3037             break;
     3123          case decorated:
     3124            res = validate_decorated(tree);
     3125            break;
    30383126            /*
    30393127             *  "Trivial" parse tree nodes.
     
    31073195            res = validate_import_stmt(tree);
    31083196            break;
    3109           case import_name:
    3110             res = validate_import_name(tree);
    3111             break;
    3112           case import_from:
    3113             res = validate_import_from(tree);
    3114             break;
     3197          case import_name:
     3198            res = validate_import_name(tree);
     3199            break;
     3200          case import_from:
     3201            res = validate_import_from(tree);
     3202            break;
    31153203          case global_stmt:
    31163204            res = validate_global_stmt(tree);
     
    33483436    module = Py_InitModule("parser", parser_functions);
    33493437    if (module == NULL)
    3350         return;
     3438        return;
    33513439
    33523440    if (parser_error == 0)
Note: See TracChangeset for help on using the changeset viewer.