[Jython-checkins] jython: Grammar changes needed to support print_function.

frank.wierzbicki jython-checkins at python.org
Tue May 17 04:32:56 CEST 2011


http://hg.python.org/jython/rev/1aed2fedd01b
changeset:   6210:1aed2fedd01b
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Mon May 16 19:32:48 2011 -0700
summary:
  Grammar changes needed to support print_function.

files:
  grammar/Python.g |  31 ++++++++++++++++++++++++++-----
  1 files changed, 26 insertions(+), 5 deletions(-)


diff --git a/grammar/Python.g b/grammar/Python.g
--- a/grammar/Python.g
+++ b/grammar/Python.g
@@ -165,6 +165,8 @@
 
     private String encoding;
 
+    private boolean printStatement = true;
+
     public void setErrorHandler(ErrorHandler eh) {
         this.errorHandler = eh;
         actions.setErrorHandler(eh);
@@ -374,6 +376,19 @@
       )
     ;
 
+//not in CPython's Grammar file
+// This is used to allow PRINT as a NAME for the __future__ print_function.
+name_or_print
+    returns [Token tok]
+    : NAME {
+        $tok = $name_or_print.start;
+    }
+    | {!printStatement}? => PRINT {
+        $tok = $name_or_print.start;
+    }
+    ;
+
+//not in CPython's Grammar file
 //attr is here for Java  compatibility.  A Java foo.getIf() can be called from Jython as foo.if
 //     so we need to support any keyword as an attribute.
 
@@ -456,13 +471,13 @@
 @after {
     $funcdef.tree = stype;
 }
-    : decorators? DEF NAME parameters COLON suite[false]
+    : decorators? DEF name_or_print parameters COLON suite[false]
     {
         Token t = $DEF;
         if ($decorators.start != null) {
             t = $decorators.start;
         }
-        stype = actions.makeFuncdef(t, $NAME, $parameters.args, $suite.stypes, $decorators.etypes);
+        stype = actions.makeFuncdef(t, $name_or_print.start, $parameters.args, $suite.stypes, $decorators.etypes);
     }
     ;
 
@@ -584,7 +599,6 @@
 //small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
 //             import_stmt | global_stmt | exec_stmt | assert_stmt)
 small_stmt : expr_stmt
-           | print_stmt
            | del_stmt
            | pass_stmt
            | flow_stmt
@@ -592,6 +606,7 @@
            | global_stmt
            | exec_stmt
            | assert_stmt
+           | {printStatement}? => print_stmt
            ;
 
 //expr_stmt: testlist (augassign (yield_expr|testlist) |
@@ -916,6 +931,12 @@
          }
         | i1=import_as_names
          {
+             String dottedText = $dotted_name.text;
+             String importText = $i1.text;
+             if (dottedText != null && dottedText.equals("__future__") &&
+                 importText != null && importText.equals("print_function")) {
+                 printStatement = false;
+             }
              stype = new ImportFrom($FROM, actions.makeFromText($d, $dotted_name.names),
                  actions.makeModuleNameNode($d, $dotted_name.names),
                  actions.makeAliases($i1.atypes), actions.makeLevel($d));
@@ -1691,9 +1712,9 @@
        {
            etype = new Repr($lb, actions.castExpr($testlist.tree));
        }
-     | NAME
+     | name_or_print
        {
-           etype = new Name($NAME, $NAME.text, $expr::ctype);
+           etype = new Name($name_or_print.start, $name_or_print.text, $expr::ctype);
        }
      | INT
        {

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


More information about the Jython-checkins mailing list