SF.net SVN: jython:[7218] trunk/jython

[email protected] Sat, 12 Mar 2011 19:36:23 +0000
Newsgroups gmane.comp.lang.jython.cvs
Message-ID <[email protected]>
Revision: 7218
          http://jython.svn.sourceforge.net/jython/?rev=7218&view=rev
Author:   fwierzbicki
Date:     2011-03-12 19:36:22 +0000 (Sat, 12 Mar 2011)

Log Message:
-----------
First cut at Python.g grammar + associated fixes.

The with statement is on by default.
Passes test_grammar from Python 2.6.

Modified Paths:
--------------
    trunk/jython/Lib/test/test_func_syntax_jy.py
    trunk/jython/grammar/Python.g
    trunk/jython/src/org/python/Version.java
    trunk/jython/src/org/python/antlr/GrammarActions.java
    trunk/jython/src/org/python/core/Options.java
    trunk/jython/src/org/python/core/Py.java
    trunk/jython/src/org/python/core/PySystemState.java
    trunk/jython/src/org/python/core/exceptions.java
    trunk/jython/src/org/python/indexer/Builtins.java

Modified: trunk/jython/Lib/test/test_func_syntax_jy.py
===================================================================
--- trunk/jython/Lib/test/test_func_syntax_jy.py	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/Lib/test/test_func_syntax_jy.py	2011-03-12 19:36:22 UTC (rev 7218)
@@ -10,7 +10,7 @@
                 "parrot(voltage=.5, \'dead\')")
 
     def test_dup_keywords(self):
-        self.assertRaises(TypeError, eval,
+        self.assertRaises(SyntaxError, eval,
                 "complex(imag=4, imag=2)")
 
 def test_main():

Modified: trunk/jython/grammar/Python.g
===================================================================
--- trunk/jython/grammar/Python.g	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/grammar/Python.g	2011-03-12 19:36:22 UTC (rev 7218)
@@ -991,12 +991,13 @@
     : (AS | NAME) expr[expr_contextType.Store]
       {
           $etype = actions.castExpr($expr.tree);
+          actions.checkAssign($etype);
       }
     ;
 
-//except_clause: 'except' [test [',' test]]
+//except_clause: 'except' [test [('as' | ',') test]]
 except_clause
-    : EXCEPT (t1=test[expr_contextType.Load] (COMMA t2=test[expr_contextType.Store])?)? COLON suite[!$suite.isEmpty() && $suite::continueIllegal]
+    : EXCEPT (t1=test[expr_contextType.Load] ((COMMA | AS) t2=test[expr_contextType.Store])?)? COLON suite[!$suite.isEmpty() && $suite::continueIllegal]
    -> ^(EXCEPT<ExceptHandler>[$EXCEPT, actions.castExpr($t1.tree), actions.castExpr($t2.tree),
           actions.castStmts($suite.stypes)])
     ;
@@ -1639,7 +1640,9 @@
       }
     ;
 
-//arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
+//arglist: (argument ',')* (argument [',']
+//                         |'*' test (',' argument)* [',' '**' test] 
+//                         |'**' test)
 arglist
     returns [List args, List keywords, expr starargs, expr kwargs]
 @init {
@@ -1647,9 +1650,9 @@
     List kws = new ArrayList();
     List gens = new ArrayList();
 }
-    : argument[arguments, kws, gens, true] (COMMA argument[arguments, kws, gens, false])*
+    : argument[arguments, kws, gens, true, false] (COMMA argument[arguments, kws, gens, false, false])*
           (COMMA
-              ( STAR s=test[expr_contextType.Load] (COMMA DOUBLESTAR k=test[expr_contextType.Load])?
+              ( STAR s=test[expr_contextType.Load] (COMMA argument[arguments, kws, gens, false, true])* (COMMA DOUBLESTAR k=test[expr_contextType.Load])?
               | DOUBLESTAR k=test[expr_contextType.Load]
               )?
           )?
@@ -1662,25 +1665,37 @@
           $starargs=actions.castExpr($s.tree);
           $kwargs=actions.castExpr($k.tree);
       }
-    | STAR s=test[expr_contextType.Load] (COMMA DOUBLESTAR k=test[expr_contextType.Load])?
+    | STAR s=test[expr_contextType.Load] (COMMA argument[arguments, kws, gens, false, true])* (COMMA DOUBLESTAR k=test[expr_contextType.Load])?
       {
           $starargs=actions.castExpr($s.tree);
-            $kwargs=actions.castExpr($k.tree);
+          $keywords=kws;
+          $kwargs=actions.castExpr($k.tree);
       }
     | DOUBLESTAR k=test[expr_contextType.Load]
       {
-            $kwargs=actions.castExpr($k.tree);
+          $kwargs=actions.castExpr($k.tree);
       }
     ;
 
 //argument: test [gen_for] | test '=' test  # Really [keyword '='] test
 argument
-    [List arguments, List kws, List gens, boolean first] returns [boolean genarg]
+    [List arguments, List kws, List gens, boolean first, boolean afterStar] returns [boolean genarg]
     : t1=test[expr_contextType.Load]
         ((ASSIGN t2=test[expr_contextType.Load])
           {
+              expr newkey = actions.castExpr($t1.tree);
+              //Loop through all current keys and fail on duplicate.
+              for(Object o: $kws) {
+                  List list = (List)o;
+                  Object oldkey = list.get(0);
+                  if (oldkey instanceof Name && newkey instanceof Name) {
+                      if (((Name)oldkey).getId().equals(((Name)newkey).getId())) {
+                          errorHandler.error("keyword arguments repeated", $t1.tree);
+                      }
+                  }
+              }
               List<expr> exprs = new ArrayList<expr>();
-              exprs.add(actions.castExpr($t1.tree));
+              exprs.add(newkey);
               exprs.add(actions.castExpr($t2.tree));
               $kws.add(exprs);
           }
@@ -1698,6 +1713,8 @@
           {
               if (kws.size() > 0) {
                   errorHandler.error("non-keyword arg after keyword arg", $t1.tree);
+              } else if (afterStar) {
+                  errorHandler.error("only named arguments may follow *expression", $t1.tree);
               }
               $arguments.add($t1.tree);
           }
@@ -1905,9 +1922,13 @@
 INT :   // Hex
         '0' ('x' | 'X') ( '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' )+
     |   // Octal
-        '0'  ( '0' .. '7' )*
-    |   '1'..'9' DIGITS*
-    ;
+        '0' ('o' | 'O') ( '0' .. '7' )*
+    |   '0'  ( '0' .. '7' )*
+    |   // Binary
+        '0' ('b' | 'B') ( '0' .. '1' )*
+    |   // Decimal
+        '1'..'9' DIGITS*
+;
 
 COMPLEX
     :   DIGITS+ ('j'|'J')
@@ -1925,7 +1946,7 @@
  *  should make us exit loop not continue.
  */
 STRING
-    :   ('r'|'u'|'ur'|'R'|'U'|'UR'|'uR'|'Ur')?
+    :   ('r'|'u'|'b'|'ur'|'R'|'U'|'B'|'UR'|'uR'|'Ur')?
         (   '\'\'\'' (options {greedy=false;}:TRIAPOS)* '\'\'\''
         |   '"""' (options {greedy=false;}:TRIQUOTE)* '"""'
         |   '"' (ESC|~('\\'|'\n'|'"'))* '"'

Modified: trunk/jython/src/org/python/Version.java
===================================================================
--- trunk/jython/src/org/python/Version.java	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/src/org/python/Version.java	2011-03-12 19:36:22 UTC (rev 7218)
@@ -44,7 +44,7 @@
 
     /** The flags that are set by default in a code object. */
     private static final Collection<CodeFlag> defaultCodeFlags = Arrays.asList(
-            CodeFlag.CO_NESTED, CodeFlag.CO_GENERATOR_ALLOWED);
+            CodeFlag.CO_NESTED, CodeFlag.CO_GENERATOR_ALLOWED, CodeFlag.CO_FUTURE_WITH_STATEMENT);
 
     private static final String headURL =
             "$HeadURL$";

Modified: trunk/jython/src/org/python/antlr/GrammarActions.java
===================================================================
--- trunk/jython/src/org/python/antlr/GrammarActions.java	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/src/org/python/antlr/GrammarActions.java	2011-03-12 19:36:22 UTC (rev 7218)
@@ -347,20 +347,22 @@
     }
 
     List<keyword> makeKeywords(List args) {
-        List<keyword> k = new ArrayList<keyword>();
+        List<keyword> keywords = new ArrayList<keyword>();
         if (args != null) {
-            for(int i=0;i<args.size();i++) {
-                List e = (List)args.get(i);
-                checkAssign(castExpr(e.get(0)));
-                if (e.get(0) instanceof Name) {
-                    Name arg = (Name)e.get(0);
-                    k.add(new keyword(arg, arg.getInternalId(), castExpr(e.get(1))));
+            for (Object o : args) {
+                List e = (List)o;
+                Object k = e.get(0);
+                Object v = e.get(1);
+                checkAssign(castExpr(k));
+                if (k instanceof Name) {
+                    Name arg = (Name)k;
+                    keywords.add(new keyword(arg, arg.getInternalId(), castExpr(v)));
                 } else {
-                    errorHandler.error("keyword must be a name", (PythonTree)e.get(0));
+                    errorHandler.error("keyword must be a name", (PythonTree)k);
                 }
             }
         }
-        return k;
+        return keywords;
     }
 
     Object makeFloat(Token t) {
@@ -373,12 +375,21 @@
         return Py.newImaginary(Double.valueOf(s));
     }
 
+    //XXX: needs to handle NumberFormatException (on input like 0b2) and needs
+    //     a better long guard than ndigits > 11 (this is much to short for
+    //     binary for example)
     Object makeInt(Token t) {
         String s = t.getText();
         int radix = 10;
         if (s.startsWith("0x") || s.startsWith("0X")) {
             radix = 16;
             s = s.substring(2, s.length());
+        } else if (s.startsWith("0o") || s.startsWith("0O")) {
+            radix = 8;
+            s = s.substring(2, s.length());
+        } else if (s.startsWith("0b") || s.startsWith("0B")) {
+            radix = 2;
+            s = s.substring(2, s.length());
         } else if (s.startsWith("0")) {
             radix = 8;
         }

Modified: trunk/jython/src/org/python/core/Options.java
===================================================================
--- trunk/jython/src/org/python/core/Options.java	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/src/org/python/core/Options.java	2011-03-12 19:36:22 UTC (rev 7218)
@@ -75,7 +75,11 @@
     /** Force stdin, stdout and stderr to be unbuffered, and opened in
      * binary mode */
     public static boolean unbuffered = false;
+    
+    //XXX: place holder
+    public static int bytes_warning = 0;
 
+
     /**
      * Enable division warning. The value maps to the registry values of
      * <ul>

Modified: trunk/jython/src/org/python/core/Py.java
===================================================================
--- trunk/jython/src/org/python/core/Py.java	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/src/org/python/core/Py.java	2011-03-12 19:36:22 UTC (rev 7218)
@@ -402,6 +402,11 @@
     public static void UnicodeWarning(String message) {
         warning(UnicodeWarning, message);
     }
+    
+    public static PyObject BytesWarning;
+    public static void BytesWarning(String message) {
+        warning(BytesWarning, message);
+    }
 
     private static PyObject warnings_mod;
 
@@ -775,6 +780,7 @@
         FutureWarning = initExc("FutureWarning", exc, dict);
         ImportWarning = initExc("ImportWarning", exc, dict);
         UnicodeWarning = initExc("UnicodeWarning", exc, dict);
+        BytesWarning = initExc("BytesWarning", exc, dict);
 
         // Pre-initialize the PyJavaClass for OutOfMemoryError so when we need
         // it it creating the pieces for it won't cause an additional out of

Modified: trunk/jython/src/org/python/core/PySystemState.java
===================================================================
--- trunk/jython/src/org/python/core/PySystemState.java	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/src/org/python/core/PySystemState.java	2011-03-12 19:36:22 UTC (rev 7218)
@@ -65,6 +65,16 @@
     public static PyTuple version_info;
 
     public final static int maxunicode = 1114111;
+
+    //XXX: we should someday make this Long.MAX_VALUE, but see test_index.py
+    //     for tests that would need to pass but today would not.
+    public final static int maxsize = Integer.MAX_VALUE;
+
+    //XXX: place holder
+    public final static boolean py3kwarning = false;
+
+    public final static Class flags = Options.class;
+    
     public static PyTuple subversion;
     /**
      * The copyright notice for this release.

Modified: trunk/jython/src/org/python/core/exceptions.java
===================================================================
--- trunk/jython/src/org/python/core/exceptions.java	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/src/org/python/core/exceptions.java	2011-03-12 19:36:22 UTC (rev 7218)
@@ -166,6 +166,10 @@
         buildClass(dict, "UnicodeWarning", "Warning",
                    "Base class for warnings about Unicode related problems, mostly\n"
                    + "related to conversion problems.");
+            
+        buildClass(dict, "BytesWarning", "Warning",
+                   "Base class for warnings about bytes and buffer related problems, mostly\n"
+                   + "related to conversion from str or comparing to str."); 
 
         // Initialize ZipImportError here, where it's safe to; it's
         // needed immediately

Modified: trunk/jython/src/org/python/indexer/Builtins.java
===================================================================
--- trunk/jython/src/org/python/indexer/Builtins.java	2011-03-12 19:30:30 UTC (rev 7217)
+++ trunk/jython/src/org/python/indexer/Builtins.java	2011-03-12 19:36:22 UTC (rev 7218)
@@ -99,8 +99,8 @@
     Scope moduleTable;
 
     String[] builtin_exception_types = {
-        "ArithmeticError", "AssertionError", "AttributeError",
-        "BaseException", "Exception", "DeprecationWarning", "EOFError",
+        "ArithmeticError", "AssertionError", "AttributeError", "BaseException",
+        "BytesWarning", "Exception", "DeprecationWarning", "EOFError",
         "EnvironmentError", "FloatingPointError", "FutureWarning",
         "GeneratorExit", "IOError", "ImportError", "ImportWarning",
         "IndentationError", "IndexError", "KeyError", "KeyboardInterrupt",
@@ -108,11 +108,10 @@
         "NotImplementedError", "OSError", "OverflowError",
         "PendingDeprecationWarning", "ReferenceError", "RuntimeError",
         "RuntimeWarning", "StandardError", "StopIteration", "SyntaxError",
-        "SyntaxWarning", "SystemError", "SystemExit", "TabError",
-        "TypeError", "UnboundLocalError", "UnicodeDecodeError",
-        "UnicodeEncodeError", "UnicodeError", "UnicodeTranslateError",
-        "UnicodeWarning", "UserWarning", "ValueError", "Warning",
-        "ZeroDivisionError"
+        "SyntaxWarning", "SystemError", "SystemExit", "TabError", "TypeError",
+        "UnboundLocalError", "UnicodeDecodeError", "UnicodeEncodeError",
+        "UnicodeError", "UnicodeTranslateError", "UnicodeWarning",
+        "UserWarning", "ValueError", "Warning", "ZeroDivisionError"
     };
 
     Set<NType> nativeTypes = new HashSet<NType>();


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d