[Jython-checkins] jython (merge default -> default): Multiple context managers.
frank.wierzbicki
jython-checkins at python.org
Wed Mar 7 05:24:07 CET 2012
http://hg.python.org/jython/rev/587310b11b26
changeset: 6312:587310b11b26
parent: 6309:01869de23bfd
parent: 6311:427e560506e3
user: Frank Wierzbicki <fwierzbicki at gmail.com>
date: Tue Mar 06 20:23:59 2012 -0800
summary:
Multiple context managers.
files:
grammar/Python.g | 29 +++++++----
src/org/python/antlr/GrammarActions.java | 15 ++++++
2 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/grammar/Python.g b/grammar/Python.g
--- a/grammar/Python.g
+++ b/grammar/Python.g
@@ -1169,7 +1169,7 @@
)
;
-//with_stmt: 'with' test [ with_var ] ':' suite
+//with_stmt: 'with' with_item (',' with_item)* ':' suite
with_stmt
@init {
stmt stype = null;
@@ -1177,20 +1177,29 @@
@after {
$with_stmt.tree = stype;
}
- : WITH test[expr_contextType.Load] (with_var)? COLON suite[false]
+ : WITH w+=with_item (options {greedy=true;}:COMMA w+=with_item)* COLON suite[false]
{
- stype = new With($WITH, actions.castExpr($test.tree), $with_var.etype,
- actions.castStmts($suite.stypes));
+ stype = actions.makeWith($WITH, $w, $suite.stypes);
}
;
-//with_var: ('as' | NAME) expr
-with_var
- returns [expr etype]
- : (AS | NAME) expr[expr_contextType.Store]
+//with_item: test ['as' expr]
+with_item
+ at init {
+ stmt stype = null;
+}
+ at after {
+ $with_item.tree = stype;
+}
+ : test[expr_contextType.Load] (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
@@ -41,6 +41,7 @@
import org.python.antlr.ast.Str;
import org.python.antlr.ast.UnaryOp;
import org.python.antlr.ast.While;
+import org.python.antlr.ast.With;
import org.python.antlr.ast.Yield;
import org.python.antlr.base.excepthandler;
import org.python.antlr.base.expr;
@@ -240,6 +241,20 @@
return new While(t, test, b, o);
}
+ 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;
+ }
+
stmt makeFor(Token t, expr target, expr iter, List body, List orelse) {
if (target == null || iter == null) {
return errorHandler.errorStmt(new PythonTree(t));
--
Repository URL: http://hg.python.org/jython
More information about the Jython-checkins
mailing list