[Python-checkins] python/dist/src/Modules parsermodule.c,2.81,2.82

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed May 19 04:20:47 EDT 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9770/Modules

Modified Files:
	parsermodule.c 
Log Message:
SF patch #872326:  Generator expression implementation
(Code contributed by Jiwon Seo.)

The documentation portion of the patch is being re-worked and will be
checked-in soon.  Likewise, PEP 289 will be updated to reflect Guido's
rationale for the design decisions on binding behavior (as described in
in his patch comments and in discussions on python-dev).

The test file, test_genexps.py, is written in doctest format and is
meant to exercise all aspects of the the patch.  Further additions are
welcome from everyone.  Please stress test this new feature as much as
possible before the alpha release.



Index: parsermodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/parsermodule.c,v
retrieving revision 2.81
retrieving revision 2.82
diff -C2 -d -r2.81 -r2.82
*** parsermodule.c	20 Nov 2003 01:44:58 -0000	2.81
--- parsermodule.c	19 May 2004 08:20:13 -0000	2.82
***************
*** 856,860 ****
  VALIDATER(arglist);             VALIDATER(argument);
  VALIDATER(listmaker);           VALIDATER(yield_stmt);
! VALIDATER(testlist1);
  
  #undef VALIDATER
--- 856,862 ----
  VALIDATER(arglist);             VALIDATER(argument);
  VALIDATER(listmaker);           VALIDATER(yield_stmt);
! VALIDATER(testlist1);           VALIDATER(gen_for);
! VALIDATER(gen_iter);            VALIDATER(gen_if);
! VALIDATER(testlist_gexp);
  
  #undef VALIDATER
***************
*** 1247,1250 ****
--- 1249,1267 ----
  }
  
+ /*  gen_iter:  gen_for | gen_if
+  */
+ static int
+ validate_gen_iter(node *tree)
+ {
+     int res = (validate_ntype(tree, gen_iter)
+                && validate_numnodes(tree, 1, "gen_iter"));
+     if (res && TYPE(CHILD(tree, 0)) == gen_for)
+         res = validate_gen_for(CHILD(tree, 0));
+     else
+         res = validate_gen_if(CHILD(tree, 0));
+ 
+     return res;
+ }
+ 
  /*  list_for:  'for' exprlist 'in' testlist [list_iter]
   */
***************
*** 1269,1272 ****
--- 1286,1311 ----
  }
  
+ /*  gen_for:  'for' exprlist 'in' test [gen_iter]
+  */
+ static int
+ validate_gen_for(node *tree)
+ {
+     int nch = NCH(tree);
+     int res;
+ 
+     if (nch == 5)
+         res = validate_gen_iter(CHILD(tree, 4));
+     else
+         res = validate_numnodes(tree, 4, "gen_for");
+ 
+     if (res)
+         res = (validate_name(CHILD(tree, 0), "for")
+                && validate_exprlist(CHILD(tree, 1))
+                && validate_name(CHILD(tree, 2), "in")
+                && validate_test(CHILD(tree, 3)));
+ 
+     return res;
+ }
+ 
  /*  list_if:  'if' test [list_iter]
   */
***************
*** 1289,1292 ****
--- 1328,1350 ----
  }
  
+ /*  gen_if:  'if' test [gen_iter]
+  */
+ static int
+ validate_gen_if(node *tree)
+ {
+     int nch = NCH(tree);
+     int res;
+ 
+     if (nch == 3)
+         res = validate_gen_iter(CHILD(tree, 2));
+     else
+         res = validate_numnodes(tree, 2, "gen_if");
+     
+     if (res)
+         res = (validate_name(CHILD(tree, 0), "if")
+                && validate_test(CHILD(tree, 1)));
+ 
+     return res;
+ }
  
  /*  validate_fpdef()
***************
*** 2188,2192 ****
  
              if (res && (nch == 3))
!                 res = validate_testlist(CHILD(tree, 1));
              break;
            case LSQB:
--- 2246,2250 ----
  
              if (res && (nch == 3))
!                 res = validate_testlist_gexp(CHILD(tree, 1));
              break;
            case LSQB:
***************
*** 2245,2249 ****
  
      /*
!      *  list_iter | (',' test)* [',']
       */
      if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
--- 2303,2307 ----
  
      /*
!      *  list_for | (',' test)* [',']
       */
      if (nch == 2 && TYPE(CHILD(tree, 1)) == list_for)
***************
*** 2267,2270 ****
--- 2325,2365 ----
  }
  
+ /*  testlist_gexp:
+  *    test ( gen_for | (',' test)* [','] )
+  */
+ static int
+ validate_testlist_gexp(node *tree)
+ {
+     int nch = NCH(tree);
+     int ok = nch;
+ 
+     if (nch == 0)
+         err_string("missing child nodes of testlist_gexp");
+     else {
+         ok = validate_test(CHILD(tree, 0));
+     }
+ 
+     /*
+      *  gen_for | (',' test)* [',']
+      */
+     if (nch == 2 && TYPE(CHILD(tree, 1)) == gen_for)
+         ok = validate_gen_for(CHILD(tree, 1));
+     else {
+         /*  (',' test)* [',']  */
+         int i = 1;
+         while (ok && nch - i >= 2) {
+             ok = (validate_comma(CHILD(tree, i))
+                   && validate_test(CHILD(tree, i+1)));
+             i += 2;
+         }
+         if (ok && i == nch-1)
+             ok = validate_comma(CHILD(tree, i));
+         else if (i != nch) {
+             ok = 0;
+             err_string("illegal trailing nodes for testlist_gexp");
+         }
+     }
+     return ok;
+ }
  
  /*  funcdef:
***************
*** 2319,2322 ****
--- 2414,2429 ----
          return validate_numnodes(tree, nch + 1, "arglist");
  
+     if (nch > 1) {
+         for (i=0; i<nch; i++) {
+             if (TYPE(CHILD(tree, i)) == argument) {
+                 node *ch = CHILD(tree, i);
+                 if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == gen_for) {
+                     err_string("need '(', ')' for generator expression");
+                     return 0;
+                 }
+             }
+         }
+     }
+ 
      while (ok && nch-i >= 2) {
          /* skip leading (argument ',') */
***************
*** 2378,2382 ****
  /*  argument:
   *
!  *  [test '='] test
   */
  static int
--- 2485,2489 ----
  /*  argument:
   *
!  *  [test '='] test [gen_for]
   */
  static int
***************
*** 2385,2392 ****
      int nch = NCH(tree);
      int res = (validate_ntype(tree, argument)
!                && ((nch == 1) || (nch == 3))
                 && validate_test(CHILD(tree, 0)));
  
!     if (res && (nch == 3))
          res = (validate_equal(CHILD(tree, 1))
                 && validate_test(CHILD(tree, 2)));
--- 2492,2501 ----
      int nch = NCH(tree);
      int res = (validate_ntype(tree, argument)
!                && ((nch == 1) || (nch == 2) || (nch == 3))
                 && validate_test(CHILD(tree, 0)));
  
!     if (res && (nch == 2))
!         res = validate_gen_for(CHILD(tree, 1));
!     else if (res && (nch == 3))
          res = (validate_equal(CHILD(tree, 1))
                 && validate_test(CHILD(tree, 2)));




More information about the Python-checkins mailing list