You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2011/06/14 22:03:13 UTC

svn commit: r1135771 - in /commons/proper/jexl/trunk/src: main/java/org/apache/commons/jexl2/parser/ site/xdoc/ site/xdoc/reference/ test/java/org/apache/commons/jexl2/

Author: henrib
Date: Tue Jun 14 20:03:13 2011
New Revision: 1135771

URL: http://svn.apache.org/viewvc?rev=1135771&view=rev
Log:
Related to JEXL-24, JEXL-112:
Added notation for octal and hexadecimal natural literals (ie int as default, long as 'l' or 'L', big-integer as 'b' or 'B' as in big); mod Parser.jjt / ASTNumberLiteral.java
Added notation for exponents for real literals (ie float as default, double as 'D' or 'd', big-decimal as 'h' or 'H' as in huge); mod Parser.jjt / ASTNumberLiteral.java
Added specific tests;
Initial documentation update;
Updated changes.xml to reflect bug fixes

Modified:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt
    commons/proper/jexl/trunk/src/site/xdoc/changes.xml
    commons/proper/jexl/trunk/src/site/xdoc/reference/syntax.xml
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java?rev=1135771&r1=1135770&r2=1135771&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTNumberLiteral.java Tue Jun 14 20:03:13 2011
@@ -32,7 +32,7 @@ public class ASTNumberLiteral extends Je
     public ASTNumberLiteral(Parser p, int id) {
         super(p, id);
     }
-    
+
     /**
      * Gets the literal value.
      * @return the number literal
@@ -40,78 +40,109 @@ public class ASTNumberLiteral extends Je
     public Number getLiteral() {
         return literal;
     }
-    
+
     /** {@inheritDoc} */
     @Override
     public Object jjtAccept(ParserVisitor visitor, Object data) {
         return visitor.visit(this, data);
     }
-    
+
     public Class<?> getLiteralClass() {
         return clazz;
     }
-    
+
     public boolean isInteger() {
         return Integer.class.equals(clazz);
     }
-        
-    public boolean isDouble() {
-        return Double.class.equals(clazz);
-    }
-    
+
+    /**
+     * Sets this node as a natural literal.
+     * Originally from OGNL.
+     * @param s the natural as string
+     */
     public void setNatural(String s) {
         Number result;
         Class<?> rclass;
-        int last = s.length() - 1;
+        // determine the base
+        final int base;
+        if (s.charAt(0) == '0') {
+            if ((s.length() > 1 && (s.charAt(1) == 'x' || s.charAt(1) == 'X'))) {
+                base = 16;
+                s = s.substring(2); // Trim the 0x off the front
+            } else {
+                base = 8;
+            }
+        } else {
+            base = 10;
+        }
+        final int last = s.length() - 1;
         switch (s.charAt(last)) {
             case 'l':
-            case 'L':
-                result = Long.valueOf(s.substring(0, last));
+            case 'L': {
                 rclass = Long.class;
+                result = Long.valueOf(s.substring(0, last), base);
                 break;
+            }
             case 'h':
-            case 'H':
-                result = new BigInteger(s.substring(0, last));
+            case 'H': {
                 rclass = BigInteger.class;
+                result = new BigInteger(s.substring(0, last), base);
                 break;
-            default:
+            }
+            default: {
                 rclass = Integer.class;
                 try {
-                    result = Integer.valueOf(s);
-                } catch(NumberFormatException xnumber) {
-                    result = Long.valueOf(s);
+                    result = Integer.valueOf(s, base);
+                } catch (NumberFormatException take2) {
+                    try {
+                        result = Long.valueOf(s, base);
+                    } catch (NumberFormatException take3) {
+                        result = new BigInteger(s, base);
+                    }
                 }
-                break;
+            }
         }
         literal = result;
         clazz = rclass;
     }
 
+    /**
+     * Sets this node as a real literal.
+     * Originally from OGNL.
+     * @param s the real as string
+     */
     public void setReal(String s) {
         Number result;
         Class<?> rclass;
-        int last = s.length() - 1;
+        final int last = s.length() - 1;
         switch (s.charAt(last)) {
             case 'b':
-            case 'B':
+            case 'B': {
                 result = new BigDecimal(s.substring(0, last));
                 rclass = BigDecimal.class;
                 break;
+            }
             case 'd':
-            case 'D':
-                result = Double.valueOf(s);
+            case 'D': {
                 rclass = Double.class;
+                result = Double.valueOf(s);
                 break;
+            }
             case 'f':
             case 'F':
-            default:
+            default: {
                 rclass = Float.class;
                 try {
                     result = Float.valueOf(s);
-                } catch(NumberFormatException xnumber) {
-                    result = Double.valueOf(s);
+                } catch (NumberFormatException take2) {
+                    try {
+                        result = Double.valueOf(s);
+                    } catch (NumberFormatException take3) {
+                        result = new BigDecimal(s);
+                    }
                 }
                 break;
+            }
         }
         literal = result;
         clazz = rclass;

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt?rev=1135771&r1=1135770&r2=1135771&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt Tue Jun 14 20:03:13 2011
@@ -170,7 +170,7 @@ PARSER_END(Parser)
         (["l","L","h","H"])?
     >
  |
-  < FLOAT_LITERAL: (<DIGIT>)+ "."(<DIGIT>)+ (["d","D","f","F","b","B"])? >
+  < FLOAT_LITERAL: (<DIGIT>)+ "." (<DIGIT>)+ ((["e","E"])(["+","-"])?(<DIGIT>)+)? (["d","D","f","F","b","B"])? >
 }
 
 <*> TOKEN :

Modified: commons/proper/jexl/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/site/xdoc/changes.xml?rev=1135771&r1=1135770&r2=1135771&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/jexl/trunk/src/site/xdoc/changes.xml Tue Jun 14 20:03:13 2011
@@ -26,6 +26,12 @@
   </properties>
   <body>
     <release version="2.0.2" date="unreleased">
+        <action dev="henrib" type="fix" issue="JEXL-112" due-to="sebb">
+            Cannot parse Integer.MIN_VALUE.
+        </action>
+        <action dev="henrib" type="fix" issue="JEXL-24" due-to="freish">
+            Support Long for integer literal instead of Integers.
+        </action>
         <action dev="henrib" type="add">
             Added ObjectContext that wraps an object as JexlContext and added JexlContext as source to solve
             top-level namespace functions.

Modified: commons/proper/jexl/trunk/src/site/xdoc/reference/syntax.xml
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/site/xdoc/reference/syntax.xml?rev=1135771&r1=1135770&r2=1135771&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/site/xdoc/reference/syntax.xml (original)
+++ commons/proper/jexl/trunk/src/site/xdoc/reference/syntax.xml Tue Jun 14 20:03:13 2011
@@ -130,39 +130,45 @@
         <tr><th width="15%">Item</th><th>Description</th></tr>
         <tr>
           <td>Integer Literals</td>
-          <td>1 or more digits from <code>0</code> to <code>9</code></td>
+          <td>1 or more digits from <code>0</code> to <code>9</code>, eg <code>42</code>.
+          </td>
         </tr>
         <tr>
-          <td>Long Literals</td>
-          <td>1 or more digits from <code>0</code> to <code>9</code>, followed by <code>l</code> or <code>L</code></td>
+          <td>Float Literals</td>
+          <td>
+            1 or more digits from <code>0</code> to <code>9</code>, followed
+            by a decimal point and then one or more digits from
+            <code>0</code> to <code>9</code>, eg <code>42.0</code>.
+          </td>
         </tr>
         <tr>
-          <td>BigInteger Literals</td>
-          <td>1 or more digits from <code>0</code> to <code>9</code>, followed by <code>h</code> or <code>H</code>.</td>
+          <td>Long Literals</td>
+          <td>1 or more digits from <code>0</code> to <code>9</code> suffixed with <code>l</code> or <code>L</code>
+          , eg <code>42.0l</code>.
+          </td>
         </tr>
         <tr>
-          <td>BigDecimal Literals</td>
+          <td>Double Literals</td>
           <td>
             1 or more digits from <code>0</code> to <code>9</code>, followed
             by a decimal point and then one or more digits from
-            <code>0</code> to <code>9</code>, followed by <code>b</code> or <code>B</code>.
+            <code>0</code> to <code>9</code> suffixed with <code>d</code> or <code>D</code>
+            , eg <code>42.0d</code>..
           </td>
         </tr>
         <tr>
-          <td>Floating point Literals</td>
-          <td>
-            1 or more digits from <code>0</code> to <code>9</code>, followed
-            by a decimal point and then one or more digits from
-            <code>0</code> to <code>9</code>,
-            optionally followed by <code>f</code> or <code>F</code>.
+          <td>Big Integer Literals</td>
+          <td>1 or more digits from <code>0</code> to <code>9</code> suffixed with <code>b</code> or <code>B</code>
+          , eg <code>42B</code>.
           </td>
         </tr>
         <tr>
-          <td>Double Literals</td>
+          <td>Big Decimal Literals</td>
           <td>
             1 or more digits from <code>0</code> to <code>9</code>, followed
             by a decimal point and then one or more digits from
-            <code>0</code> to <code>9</code>, followed by <code>d</code> or <code>D</code>.
+            <code>0</code> to <code>9</code> suffixed with <code>h</code> or <code>H</code> (for Huge ala OGNL))
+           , eg <code>42.0H</code>.
           </td>
         </tr>
         <tr>

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java?rev=1135771&r1=1135770&r2=1135771&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java Tue Jun 14 20:03:13 2011
@@ -64,7 +64,32 @@ public class IssuesTest extends JexlTest
         assertEquals(new BigInteger("10"), vars.get("b"));
         assertEquals(new BigDecimal("42.0"), vars.get("c"));
         assertEquals(new BigDecimal("42.0"), vars.get("d"));
+    }
+    
+    // JEXL-24: big decimals with exponent
+    public void test24C() throws Exception {
+        Map<String, Object> vars = new HashMap<String, Object>();
+        JexlContext ctxt = new MapContext(vars);
+        String stmt = "{a = 42.0e1B; b = 42.0E+2B; c = 42.0e-1B; d = 42.0E-2b;}";
+        Script expr = JEXL.createScript(stmt);
+        /* Object value = */ expr.execute(ctxt);
+        assertEquals(new BigDecimal("42.0e+1"), vars.get("a"));
+        assertEquals(new BigDecimal("42.0e+2"), vars.get("b"));
+        assertEquals(new BigDecimal("42.0e-1"), vars.get("c"));
+        assertEquals(new BigDecimal("42.0e-2"), vars.get("d"));
+    }
         
+    // JEXL-24: doubles with exponent
+    public void test24D() throws Exception {
+        Map<String, Object> vars = new HashMap<String, Object>();
+        JexlContext ctxt = new MapContext(vars);
+        String stmt = "{a = 42.0e1D; b = 42.0E+2D; c = 42.0e-1d; d = 42.0E-2d;}";
+        Script expr = JEXL.createScript(stmt);
+        /* Object value = */ expr.execute(ctxt);
+        assertEquals(Double.valueOf("42.0e+1"), vars.get("a"));
+        assertEquals(Double.valueOf("42.0e+2"), vars.get("b"));
+        assertEquals(Double.valueOf("42.0e-1"), vars.get("c"));
+        assertEquals(Double.valueOf("42.0e-2"), vars.get("d"));
     }
 
     // JEXL-49: blocks not parsed (fixed)