diff Modules/parsermodule.c @ 56237:7ee140b7ef93

fix an ambiguity in the grammar from the implementation of extended unpacking (one which was strangely "resolved" by pgen) This also kills the unused testlist1 rule and fixes parse tree validation of extended unpacking.
author Benjamin Peterson <benjamin@python.org>
date Sun, 27 Sep 2009 02:43:28 +0000
parents 287ce30d08b5
children cab14be0ada1
line wrap: on
line diff
--- a/Modules/parsermodule.c	Sat Sep 26 20:59:11 2009 +0000
+++ b/Modules/parsermodule.c	Sun Sep 27 02:43:28 2009 +0000
@@ -943,7 +943,7 @@
 VALIDATER(import_stmt);         VALIDATER(import_stmt);
 VALIDATER(import_name);         VALIDATER(yield_stmt);
 VALIDATER(global_stmt);         VALIDATER(assert_stmt);
-VALIDATER(compound_stmt);
+VALIDATER(compound_stmt);       VALIDATER(test_or_star_expr);
 VALIDATER(while);               VALIDATER(for);
 VALIDATER(try);                 VALIDATER(except_clause);
 VALIDATER(test);                VALIDATER(and_test);
@@ -958,10 +958,10 @@
 VALIDATER(subscriptlist);       VALIDATER(sliceop);
 VALIDATER(exprlist);            VALIDATER(dictorsetmaker);
 VALIDATER(arglist);             VALIDATER(argument);
-VALIDATER(testlist1);           VALIDATER(comp_for);
+VALIDATER(comp_for);
 VALIDATER(comp_iter);           VALIDATER(comp_if);
 VALIDATER(testlist_comp);       VALIDATER(yield_expr);
-VALIDATER(yield_or_testlist);   VALIDATER(or_test);
+VALIDATER(or_test);
 VALIDATER(test_nocond);         VALIDATER(lambdef_nocond);
 
 #undef VALIDATER
@@ -1183,12 +1183,11 @@
                                     validate_test, "testlist"));
 }
 
-
 static int
-validate_testlist1(node *tree)
+validate_testlist_star_expr(node *tl)
 {
-    return (validate_repeating_list(tree, testlist1,
-                                    validate_test, "testlist1"));
+    return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
+                                    "testlist"));
 }
 
 
@@ -1516,12 +1515,17 @@
 }
 
 static int
-validate_yield_or_testlist(node *tree)
+validate_yield_or_testlist(node *tree, int tse)
 {
-	if (TYPE(tree) == yield_expr)
-		return validate_yield_expr(tree);
-	else
-		return validate_testlist(tree);
+    if (TYPE(tree) == yield_expr) {
+        return validate_yield_expr(tree);
+    }
+    else {
+        if (tse)
+            return validate_testlist_star_expr(tree);
+        else
+            return validate_testlist(tree);
+    }
 }
 
 static int
@@ -1531,12 +1535,12 @@
     int nch = NCH(tree);
     int res = (validate_ntype(tree, expr_stmt)
                && is_odd(nch)
-               && validate_testlist(CHILD(tree, 0)));
+               && validate_testlist_star_expr(CHILD(tree, 0)));
 
     if (res && nch == 3
         && TYPE(CHILD(tree, 1)) == augassign) {
         res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
-		&& validate_yield_or_testlist(CHILD(tree, 2));
+            && validate_yield_or_testlist(CHILD(tree, 2), 0);
 
         if (res) {
             char *s = STR(CHILD(CHILD(tree, 1), 0));
@@ -1560,7 +1564,7 @@
     else {
         for (j = 1; res && (j < nch); j += 2)
             res = validate_equal(CHILD(tree, j))
-                   && validate_yield_or_testlist(CHILD(tree, j + 1));
+                && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
     }
     return (res);
 }
@@ -2077,11 +2081,11 @@
     int nch = NCH(tree);
     int res = (validate_ntype(tree, comparison)
                && is_odd(nch)
-               && validate_star_expr(CHILD(tree, 0)));
+               && validate_expr(CHILD(tree, 0)));
 
     for (pos = 1; res && (pos < nch); pos += 2)
         res = (validate_comp_op(CHILD(tree, pos))
-               && validate_star_expr(CHILD(tree, pos + 1)));
+               && validate_expr(CHILD(tree, pos + 1)));
 
     return (res);
 }
@@ -2143,12 +2147,10 @@
 {
     int res = validate_ntype(tree, star_expr);
     if (!res) return res;
-    if (NCH(tree) == 2) {
-        return validate_ntype(CHILD(tree, 0), STAR) && \
-               validate_expr(CHILD(tree, 1));
-    } else {
-        return validate_expr(CHILD(tree, 0));
-    }
+    if (!validate_numnodes(tree, 2, "star_expr"))
+        return 0;
+    return validate_ntype(CHILD(tree, 0), STAR) &&      \
+        validate_expr(CHILD(tree, 1));
 }
 
 
@@ -2379,7 +2381,7 @@
     if (nch == 0)
         err_string("missing child nodes of testlist_comp");
     else {
-        ok = validate_test(CHILD(tree, 0));
+        ok = validate_test_or_star_expr(CHILD(tree, 0));
     }
 
     /*
@@ -2392,7 +2394,7 @@
         int i = 1;
         while (ok && nch - i >= 2) {
             ok = (validate_comma(CHILD(tree, i))
-                  && validate_test(CHILD(tree, i+1)));
+                  && validate_test_or_star_expr(CHILD(tree, i+1)));
             i += 2;
         }
         if (ok && i == nch-1)
@@ -2786,10 +2788,27 @@
 
 
 static int
+validate_test_or_star_expr(node *n)
+{
+    if (TYPE(n) == test)
+        return validate_test(n);
+    return validate_star_expr(n);
+}
+
+static int
+validate_expr_or_star_expr(node *n)
+{
+    if (TYPE(n) == expr)
+        return validate_expr(n);
+    return validate_star_expr(n);
+}
+
+
+static int
 validate_exprlist(node *tree)
 {
     return (validate_repeating_list(tree, exprlist,
-                                    validate_star_expr, "exprlist"));
+                                    validate_expr_or_star_expr, "exprlist"));
 }
 
 
@@ -2970,9 +2989,6 @@
           case yield_expr:
             res = validate_yield_expr(tree);
             break;
-          case testlist1:
-            res = validate_testlist1(tree);
-            break;
           case test:
             res = validate_test(tree);
             break;