[Jython-checkins] jython: Complete multi-context with statement.

frank.wierzbicki jython-checkins at python.org
Wed Mar 7 05:24:07 CET 2012


http://hg.python.org/jython/rev/427e560506e3
changeset:   6311:427e560506e3
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Tue Mar 06 14:23:04 2012 -0800
summary:
  Complete multi-context with statement.

files:
  grammar/Python.g                         |  35 +++++------
  src/org/python/antlr/GrammarActions.java |  14 +++-
  2 files changed, 28 insertions(+), 21 deletions(-)


diff --git a/grammar/Python.g b/grammar/Python.g
--- a/grammar/Python.g
+++ b/grammar/Python.g
@@ -1163,7 +1163,7 @@
       )
       ;
 
-//with_stmt: 'with' test [ with_var ] ':' suite
+//with_stmt: 'with' with_item (',' with_item)*  ':' suite
 with_stmt
 @init {
     stmt stype = null;
@@ -1171,30 +1171,29 @@
 @after {
    $with_stmt.tree = stype;
 }
-    : WITH with_item COLON suite[false]
+    : WITH w+=with_item (options {greedy=true;}:COMMA w+=with_item)* COLON suite[false]
       {
-          stype = new With($WITH, $with_item.item, $with_item.var,
-              actions.castStmts($suite.stypes));
+          stype = actions.makeWith($WITH, $w, $suite.stypes);
       }
     ;
 
 //with_item: test ['as' expr]
 with_item
-    returns [expr item, expr var]
-    : test[expr_contextType.Load] (with_var)?
+ at init {
+    stmt stype = null;
+}
+ at after {
+   $with_item.tree = stype;
+}
+    : test[expr_contextType.Load] (AS expr[expr_contextType.Store])?
       {
-          $item = actions.castExpr($test.tree);
-          $var = $with_var.etype;
-      }
-    ;
-
-//with_var: 'as' expr
-with_var
-    returns [expr etype]
-    : AS expr[expr_contextType.Store]
-      {
-          $etype = actions.castExpr($expr.tree);
-          actions.checkAssign($etype);
+          expr item = actions.castExpr($test.tree);
+          expr var = null;
+          if ($expr.start != null) {
+              var = actions.castExpr($expr.tree);
+              actions.checkAssign(var);
+          }
+          stype = new With($test.start, item, var, null);
       }
     ;
 
diff --git a/src/org/python/antlr/GrammarActions.java b/src/org/python/antlr/GrammarActions.java
--- a/src/org/python/antlr/GrammarActions.java
+++ b/src/org/python/antlr/GrammarActions.java
@@ -241,9 +241,17 @@
         return new While(t, test, b, o);
     }
 
-    stmt makeWith(Token t, List<stmt> withs, List<stmt> body) {
-        With w = (With)withs.get(0);
-        With result = new With(t, w.getInternalContext_expr(), w.getInternalOptional_vars(), w.getInternalBody());
+    stmt makeWith(Token t, List<With> items, List<stmt> body) {
+        int last = items.size() - 1;
+        With result = null;
+        for (int i = last; i>=0; i--) {
+            With current = items.get(i);
+            if (i != last) {
+                body = new ArrayList<stmt>();
+                body.add(result);
+            }
+            result = new With(current.getToken(), current.getInternalContext_expr(), current.getInternalOptional_vars(), body);
+        }
         return result;
     }
 

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list