You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2009/08/06 13:43:49 UTC

svn commit: r801610 - in /commons/proper/jexl/branches/2.0: src/main/java/org/apache/commons/jexl/ src/main/java/org/apache/commons/jexl/parser/ src/test/java/org/apache/commons/jexl/ src/test/java/org/apache/commons/jexl/scripting/ xdocs/reference/

Author: sebb
Date: Thu Aug  6 11:43:48 2009
New Revision: 801610

URL: http://svn.apache.org/viewvc?rev=801610&view=rev
Log:
JEXL-75 Array literal syntax is not supported

Added:
    commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/ArrayLiteralTest.java   (with props)
Modified:
    commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Debugger.java
    commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java
    commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/Parser.jjt
    commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/VisitorAdapter.java
    commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java
    commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java
    commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml

Modified: commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Debugger.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Debugger.java?rev=801610&r1=801609&r2=801610&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Debugger.java (original)
+++ commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Debugger.java Thu Aug  6 11:43:48 2009
@@ -19,6 +19,7 @@
 import org.apache.commons.jexl.parser.ASTAddNode;
 import org.apache.commons.jexl.parser.ASTAndNode;
 import org.apache.commons.jexl.parser.ASTArrayAccess;
+import org.apache.commons.jexl.parser.ASTArrayLiteral;
 import org.apache.commons.jexl.parser.ASTAssignment;
 import org.apache.commons.jexl.parser.ASTBitwiseAndNode;
 import org.apache.commons.jexl.parser.ASTBitwiseComplNode;
@@ -220,6 +221,19 @@
     }
 
     /** {@inheritDoc} */
+    public Object visit(ASTArrayLiteral node, Object data) {
+        int num = node.jjtGetNumChildren();
+        builder.append("[ ");
+        accept(node.jjtGetChild(0), data);
+        for (int i = 1; i < num; ++i) {
+            builder.append(", ");
+            accept(node.jjtGetChild(i), data);
+        }
+        builder.append(" ]");
+        return data;
+    }
+    
+    /** {@inheritDoc} */
     public Object visit(ASTAssignment node, Object data) {
         return infixChildren(node, " = ", data);
     }
@@ -379,13 +393,13 @@
     /** {@inheritDoc} */
     public Object visit(ASTMapLiteral node, Object data) {
         int num = node.jjtGetNumChildren();
-        builder.append("[");
+        builder.append("{ ");
         accept(node.jjtGetChild(0), data);
         for (int i = 1; i < num; ++i) {
             builder.append(", ");
             accept(node.jjtGetChild(i), data);
         }
-        builder.append("]");
+        builder.append(" }");
         return data;
     }
 

Modified: commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java?rev=801610&r1=801609&r2=801610&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java (original)
+++ commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java Thu Aug  6 11:43:48 2009
@@ -33,6 +33,7 @@
 import org.apache.commons.jexl.parser.ASTAddNode;
 import org.apache.commons.jexl.parser.ASTAndNode;
 import org.apache.commons.jexl.parser.ASTArrayAccess;
+import org.apache.commons.jexl.parser.ASTArrayLiteral;
 import org.apache.commons.jexl.parser.ASTAssignment;
 import org.apache.commons.jexl.parser.ASTBitwiseAndNode;
 import org.apache.commons.jexl.parser.ASTBitwiseComplNode;
@@ -263,6 +264,17 @@
     }
 
     /** {@inheritDoc} */
+    public Object visit(ASTArrayLiteral node, Object data) {
+        int childCount = node.jjtGetNumChildren();
+        Object[] array = new Object[childCount];
+        for (int i = 0; i < childCount; i++) {
+            Object entry = node.jjtGetChild(i).jjtAccept(this, data);
+            array[i] = entry;
+        }
+        return array;
+    }
+    
+    /** {@inheritDoc} */
     public Object visit(ASTAssignment node, Object data) {
         // left contains the reference to assign to
         JexlNode left = node.jjtGetChild(0);
@@ -971,8 +983,8 @@
         Map<String, ?> vars = context.getVars();
         boolean isVariable = true;
         int v = 0;
-        for (int i = 0; i < numChildren; i++) {
-            JexlNode theNode = node.jjtGetChild(i);
+        for (int c = 0; c < numChildren; c++) {
+            JexlNode theNode = node.jjtGetChild(c);
             isVariable &= (theNode instanceof ASTIdentifier);
             result = theNode.jjtAccept(this, result);
             // if we get null back a result, check for an ant variable
@@ -981,7 +993,7 @@
                     variableName = new StringBuilder(node.jjtGetChild(0).image);
                     v = 1;
                 }
-                for(; v <= i; ++v) {
+                for(; v <= c; ++v) {
                     variableName.append('.');
                     variableName.append(node.jjtGetChild(v).image);
                 }

Modified: commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/Parser.jjt
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/Parser.jjt?rev=801610&r1=801609&r2=801610&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/Parser.jjt (original)
+++ commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/Parser.jjt Thu Aug  6 11:43:48 2009
@@ -109,12 +109,12 @@
 void EmptyFunction() : {}
 {
     "empty"
-    ( Reference() | "(" Reference() ")" )
+    ( Reference() | "(" Expression() ")" )
 }
 
 void SizeFunction() : {}
 {
-    "size" "(" Reference() ")"
+    "size" "(" Expression() ")"
 }
 
 void Identifier() :
@@ -281,13 +281,19 @@
   LOOKAHEAD(3)
   Reference()
 |
-  "(" Expression() ")"
+  LOOKAHEAD("(") "(" Expression() ")"
 |
-  EmptyFunction()
+  LOOKAHEAD("empty") EmptyFunction()
 |
-  SizeFunction()
+  LOOKAHEAD("size") SizeFunction()
 |
-  MapLiteral()
+  LOOKAHEAD("new" "(") Constructor()
+|
+  LOOKAHEAD( "{" MapEntry() ( "," MapEntry() )* "}" )
+    MapLiteral()
+|
+  LOOKAHEAD( "[" Parameter() ( "," Parameter() )* "]" )
+    ArrayLiteral()
 }
 
 
@@ -412,19 +418,24 @@
   "foreach" "(" Reference() "in"  Reference() ")" Statement()
 }
 
+void ArrayLiteral() : {}
+{
+  "[" Parameter() ( "," Parameter() )* "]"
+}
+
 void MapLiteral() : {}
 {
-  "["  MapEntry() ( "," MapEntry() )* "]"
+    "{"  MapEntry() ( "," MapEntry() )* "}"
 }
 
 void MapEntry() : {}
 {
-    Parameter() "=>" Parameter()
+    Parameter() ":" Parameter()
 }
 
 void Method() #MethodNode: {}
 {
-   Identifier() "("[ Parameter() ( "," Parameter() )* ] ")"  
+   Identifier() "("[ Parameter() ( "," Parameter() )* ] ")"
 }
 
 void Function() #FunctionNode: {}
@@ -495,4 +506,4 @@
      |
         ("\'" ( ~["\'","\n","\r"] | "\\" ["n","t","b","r","f","\\","\'"])* "\'" )
     >
-}
\ No newline at end of file
+}

Modified: commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/VisitorAdapter.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/VisitorAdapter.java?rev=801610&r1=801609&r2=801610&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/VisitorAdapter.java (original)
+++ commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/parser/VisitorAdapter.java Thu Aug  6 11:43:48 2009
@@ -325,4 +325,8 @@
         return node.childrenAccept(this, data);
     }
 
+    public Object visit(ASTArrayLiteral node, Object data) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
 }
\ No newline at end of file

Added: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/ArrayLiteralTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/ArrayLiteralTest.java?rev=801610&view=auto
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/ArrayLiteralTest.java (added)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/ArrayLiteralTest.java Thu Aug  6 11:43:48 2009
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jexl;
+
+import java.util.Arrays;
+
+/**
+ * Tests for map literals
+ * @since 2.0
+ */
+public class ArrayLiteralTest extends JexlTestCase {
+
+    public void testLiteralWithStrings() throws Exception {
+        Expression e = JEXL.createExpression( "[ 'foo' , 'bar' ]" );
+        JexlContext jc = JexlHelper.createContext();
+
+        Object o = e.evaluate( jc );
+        Object[] check = { "foo", "bar" };
+        assertTrue( Arrays.equals(check, (Object[])o) );
+    }
+
+    public void testLiteralWithOneEntry() throws Exception {
+        Expression e = JEXL.createExpression( "[ 'foo' ]" );
+        JexlContext jc = JexlHelper.createContext();
+
+        Object o = e.evaluate( jc );
+        Object[] check = { "foo" };
+        assertTrue( Arrays.equals(check, (Object[])o) );
+    }
+
+    public void testLiteralWithNumbers() throws Exception {
+        Expression e = JEXL.createExpression( "[ 5 , 10 ]" );
+        JexlContext jc = JexlHelper.createContext();
+
+        Object o = e.evaluate( jc );
+        Object[] check = { 5, 10 };
+        assertTrue( Arrays.equals(check, (Object[])o) );
+    }
+
+    public void testSizeOfSimpleArrayLiteral() throws Exception {
+        Expression e = JEXL.createExpression( "size([ 'foo' , 'bar' ])" );
+        JexlContext jc = JexlHelper.createContext();
+
+        Object o = e.evaluate( jc );
+        assertEquals( new Integer( 2 ), o );
+    }
+
+    public void notestCallingMethodsOnNewMapLiteral() throws Exception {
+        Expression e = JEXL.createExpression( "size({ 'foo' : 'bar' }.values())" );
+        JexlContext jc = JexlHelper.createContext();
+
+        Object o = e.evaluate( jc );
+        assertEquals( new Integer( 1 ), o );
+    }
+
+    public void testNotEmptySimpleArrayLiteral() throws Exception {
+        Expression e = JEXL.createExpression( "empty([ 'foo' , 'bar' ])" );
+        JexlContext jc = JexlHelper.createContext();
+
+        Object o = e.evaluate( jc );
+        assertFalse( ( (Boolean) o ).booleanValue() );
+    }
+
+}

Propchange: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/ArrayLiteralTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/ArrayLiteralTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java?rev=801610&r1=801609&r2=801610&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java (original)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java Thu Aug  6 11:43:48 2009
@@ -29,7 +29,7 @@
 public class MapLiteralTest extends JexlTestCase {
 
     public void testLiteralWithStrings() throws Exception {
-        Expression e = JEXL.createExpression( "[ 'foo' => 'bar' ]" );
+        Expression e = JEXL.createExpression( "{ 'foo' : 'bar' }" );
         JexlContext jc = JexlHelper.createContext();
 
         Object o = e.evaluate( jc );
@@ -37,7 +37,7 @@
     }
 
     public void testLiteralWithMultipleEntries() throws Exception {
-        Expression e = JEXL.createExpression( "[ 'foo' => 'bar', 'eat' => 'food' ]" );
+        Expression e = JEXL.createExpression( "{ 'foo' : 'bar', 'eat' : 'food' }" );
         JexlContext jc = JexlHelper.createContext();
 
         Map<String, String> expected = new HashMap<String, String>();
@@ -49,13 +49,13 @@
     }
 
     public void testLiteralWithNumbers() throws Exception {
-        Expression e = JEXL.createExpression( "[ 5 => 10 ]" );
+        Expression e = JEXL.createExpression( "{ 5 : 10 }" );
         JexlContext jc = JexlHelper.createContext();
 
         Object o = e.evaluate( jc );
         assertEquals( Collections.singletonMap( new Integer( 5 ), new Integer( 10 ) ), o );
 
-        e = JEXL.createExpression("m = [ 3 => 30, 4 => 40, 5 => 'fifty', '7' => 'seven', 7 => 'SEVEN' ]");
+        e = JEXL.createExpression("m = { 3 : 30, 4 : 40, 5 : 'fifty', '7' : 'seven', 7 : 'SEVEN' }");
         e.evaluate(jc);
 
         e = JEXL.createExpression("m.3");
@@ -87,7 +87,7 @@
     }
 
     public void testSizeOfSimpleMapLiteral() throws Exception {
-        Expression e = JEXL.createExpression( "size([ 'foo' => 'bar' ])" );
+        Expression e = JEXL.createExpression( "size({ 'foo' : 'bar' })" );
         JexlContext jc = JexlHelper.createContext();
 
         Object o = e.evaluate( jc );
@@ -95,7 +95,7 @@
     }
 
     public void testCallingMethodsOnNewMapLiteral() throws Exception {
-        Expression e = JEXL.createExpression( "size([ 'foo' => 'bar' ].values())" );
+        Expression e = JEXL.createExpression( "size({ 'foo' : 'bar' }.values())" );
         JexlContext jc = JexlHelper.createContext();
 
         Object o = e.evaluate( jc );
@@ -103,7 +103,7 @@
     }
 
     public void testNotEmptySimpleMapLiteral() throws Exception {
-        Expression e = JEXL.createExpression( "empty([ 'foo' => 'bar' ])" );
+        Expression e = JEXL.createExpression( "empty({ 'foo' : 'bar' })" );
         JexlContext jc = JexlHelper.createContext();
 
         Object o = e.evaluate( jc );
@@ -111,7 +111,7 @@
     }
 
     public void testMapMapLiteral() throws Exception {
-        Expression e = JEXL.createExpression( "['foo' => [ 'inner' => 'bar' ]]" );
+        Expression e = JEXL.createExpression( "{'foo' : { 'inner' : 'bar' }}" );
         JexlContext jc = JexlHelper.createContext();
         Object o = e.evaluate( jc );
         assertNotNull(o);
@@ -121,5 +121,17 @@
         o = e.evaluate( jc );
         assertEquals( "bar", o );
     }
-    
+
+    public void testMapArrayLiteral() throws Exception {
+        Expression e = JEXL.createExpression( "{'foo' : [ 'inner' , 'bar' ]}" );
+        JexlContext jc = JexlHelper.createContext();
+        Object o = e.evaluate( jc );
+        assertNotNull(o);
+
+        jc.getVars().put("outer", o);
+        e = JEXL.createExpression("outer.foo.1");
+        o = e.evaluate( jc );
+        assertEquals( "bar", o );
+    }
+
 }

Modified: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java?rev=801610&r1=801609&r2=801610&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java (original)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java Thu Aug  6 11:43:48 2009
@@ -119,7 +119,7 @@
         engine.eval("this.is.a.test=null");
         assertNull(engine.get("this.is.a.test"));
         assertEquals(Boolean.TRUE, engine.eval("empty(this.is.a.test)"));
-        final Object mymap = engine.eval("testmap=[ 'key1' => 'value1', 'key2' => 'value2' ]");
+        final Object mymap = engine.eval("testmap={ 'key1' : 'value1', 'key2' : 'value2' }");
         assertTrue(mymap instanceof Map<?, ?>);
         assertEquals(2,((Map<?, ?>)mymap).size());
     }

Modified: commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml?rev=801610&r1=801609&r2=801610&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml (original)
+++ commons/proper/jexl/branches/2.0/xdocs/reference/syntax.xml Thu Aug  6 11:43:48 2009
@@ -163,6 +163,24 @@
             <source>val1 == null</source>
           </td>
         </tr>
+        <tr>
+          <td>Array literal</td>
+          <td>
+            A <code>[</code> followed by one or more expressions separated by <code>,</code> and ending
+            with <code>]</code>, e.g.
+            <source>[ 1, 2, "three" ]</source>
+            <p>This syntax creates an <code>Object[]</code>.</p>
+          </td>
+        </tr>
+        <tr>
+          <td>Map literal</td>
+          <td>
+            A <code>{</code> followed by one or more sets of <code>key : value</code> pairs separated by <code>,</code> and ending
+            with <code>}</code>, e.g.
+            <source>{ "one" : 1, "two" : 2, "three" : 3, "more": "many more" }</source>
+            <p>This syntax creates a <code>HashMap&lt;Object,Object&gt;</code>.</p>
+          </td>
+        </tr>
       </table>
     </section>
     <section name="Functions">