commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r1069299 - in /commons/proper/jexl/trunk/src: main/java/org/apache/commons/jexl2/ main/java/org/apache/commons/jexl2/parser/ site/xdoc/ test/java/org/apache/commons/jexl2/
Date Thu, 10 Feb 2011 09:22:13 GMT
Author: henrib
Date: Thu Feb 10 09:22:12 2011
New Revision: 1069299

URL: http://svn.apache.org/viewvc?rev=1069299&view=rev
Log:
More generic fix related to JEXL-107 to allow (expr)[index] expressions

Added:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTReferenceExpression.java
  (with props)
Modified:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/ExpressionImpl.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.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/test/java/org/apache/commons/jexl2/IssuesTest.java

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java?rev=1069299&r1=1069298&r2=1069299&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java Thu Feb
10 09:22:12 2011
@@ -571,6 +571,21 @@ final class Debugger implements ParserVi
     }
 
     /** {@inheritDoc} */
+    public Object visit(ASTReferenceExpression node, Object data) {
+        JexlNode first = node.jjtGetChild(0);
+            builder.append('(');
+        accept(first, data);
+            builder.append(')');
+        int num = node.jjtGetNumChildren();
+        for (int i = 1; i < num; ++i) {
+            builder.append("[");
+            accept(node.jjtGetChild(i), data);
+            builder.append("]");
+        }
+        return data;
+    }
+
+    /** {@inheritDoc} */
     public Object visit(ASTSizeFunction node, Object data) {
         builder.append("size(");
         accept(node.jjtGetChild(0), data);
@@ -639,11 +654,4 @@ final class Debugger implements ParserVi
     public Object visit(ASTAmbiguous node, Object data) {
         throw new UnsupportedOperationException("unexpected type of node");
     }
-
-    public Object visit(ASTReferenceExpression node, Object data) {
-        builder.append("(");
-        accept(node.jjtGetChild(0), data);
-        builder.append(")");
-        return data;
-    }
 }
\ No newline at end of file

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/ExpressionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/ExpressionImpl.java?rev=1069299&r1=1069298&r2=1069299&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/ExpressionImpl.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/ExpressionImpl.java Thu
Feb 10 09:22:12 2011
@@ -69,7 +69,8 @@ public class ExpressionImpl implements E
      */
     public String dump() {
         Debugger debug = new Debugger();
-        return debug.debug(script)? debug.toString() : "/*?*/";
+        boolean d = debug.debug(script);
+        return debug.data() + (d? " /*" + debug.start() + ":" + debug.end() + "*/" : "/*?:?*/
");
     }
 
     /**

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java?rev=1069299&r1=1069298&r2=1069299&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java Thu
Feb 10 09:22:12 2011
@@ -25,7 +25,6 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
-import org.apache.commons.jexl2.parser.ASTReferenceExpression;
 
 import org.apache.commons.jexl2.parser.SimpleNode;
 import org.apache.commons.logging.Log;
@@ -71,6 +70,7 @@ import org.apache.commons.jexl2.parser.A
 import org.apache.commons.jexl2.parser.ASTNullLiteral;
 import org.apache.commons.jexl2.parser.ASTOrNode;
 import org.apache.commons.jexl2.parser.ASTReference;
+import org.apache.commons.jexl2.parser.ASTReferenceExpression;
 import org.apache.commons.jexl2.parser.ASTSizeFunction;
 import org.apache.commons.jexl2.parser.ASTSizeMethod;
 import org.apache.commons.jexl2.parser.ASTStringLiteral;
@@ -1068,7 +1068,8 @@ public class Interpreter implements Pars
     }
 
     public Object visit(ASTReferenceExpression node, Object data) {
-        return node.jjtGetChild(0).jjtAccept(this, data);
+        ASTArrayAccess upper = (ASTArrayAccess) node;
+        return visit(upper, data);
     }
     
     /**

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java?rev=1069299&r1=1069298&r2=1069299&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlEngine.java Thu Feb
10 09:22:12 2011
@@ -825,9 +825,9 @@ public class JexlEngine {    
                     cache.put(expr, tree);
                 }
             } catch (TokenMgrError xtme) {
-                throw new JexlException(info, "tokenization failed", xtme);
+                throw new JexlException(info, "!!! " +expression+ " !!!" + ", tokenization
failed", xtme);
             } catch (ParseException xparse) {
-                throw new JexlException(info, "parsing failed", xparse);
+                throw new JexlException(info, "!!! " +expression+ " !!!" + ", parsing failed",
xparse);
             }
         }
         return tree;

Added: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTReferenceExpression.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTReferenceExpression.java?rev=1069299&view=auto
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTReferenceExpression.java
(added)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTReferenceExpression.java
Thu Feb 10 09:22:12 2011
@@ -0,0 +1,32 @@
+/*
+ * 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.jexl2.parser;
+
+public final class ASTReferenceExpression extends ASTArrayAccess {
+    public ASTReferenceExpression(int id) {
+        super(id);
+    }
+
+    public ASTReferenceExpression(Parser p, int id) {
+        super(p, id);
+    }
+
+    /** Accept the visitor. **/
+    public Object jjtAccept(ParserVisitor visitor, Object data) {
+        return visitor.visit(this, data);
+    }
+}

Propchange: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTReferenceExpression.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1069299&r1=1069298&r2=1069299&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 Thu
Feb 10 09:22:12 2011
@@ -257,7 +257,7 @@ void ForeachStatement() : {}
 
 void Expression() #void : {}
 {
-    ConditionalExpression() ( <assign> Expression() #Assignment(2) )?
+    ConditionalExpression() (LOOKAHEAD(1) <assign> Expression() #Assignment(2) )?
 }
 
 void Assignment() #Assignment(2) : {}
@@ -525,7 +525,7 @@ void Constructor() #ConstructorNode() : 
 
 void PrimaryExpression() #void : {}
 {
-  LOOKAHEAD(3)  Reference()
+  LOOKAHEAD(2) Reference()
 |
   LOOKAHEAD( <EMPTY> ) EmptyFunction()
 |
@@ -542,7 +542,7 @@ void PrimaryExpression() #void : {}
 
 void ArrayAccess() : {}
 {
-    Identifier() (LOOKAHEAD(2) <LBRACKET> Expression() <RBRACKET>)+
+    Identifier() (LOOKAHEAD(1) <LBRACKET> Expression() <RBRACKET>)+
 }
 
 void DotReference() #void : {}
@@ -551,14 +551,14 @@ void DotReference() #void : {}
      ( LOOKAHEAD(Identifier() <LBRACKET> )
          ArrayAccess()
        |
-       ( LOOKAHEAD(3)
+       ( LOOKAHEAD(2)
             AnyMethod()
           |
             Identifier()
           |
             IntegerLiteral()
-        )
-      )
+       )
+     )
   )*
 }
 
@@ -585,8 +585,11 @@ void Reference() : {}
   )  DotReference()
 }
 
+/**
+ * ReferenceExpression is a subclass of ArrayAccess
+ */
 void ReferenceExpression() : {}
 {
-    <LPAREN> Expression() <RPAREN>
+    <LPAREN> Expression() <RPAREN> (LOOKAHEAD(1) <LBRACKET> Expression()
<RBRACKET>)*
 }
 

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=1069299&r1=1069298&r2=1069299&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/jexl/trunk/src/site/xdoc/changes.xml Thu Feb 10 09:22:12 2011
@@ -26,6 +26,9 @@
   </properties>
   <body>
     <release version="2.0.2" date="unreleased">
+        <action dev="henrib" type="add" issue="JEXL-107" due-to="henrib">
+            Literals and parenthesized expressions can not be used as references
+        </action>
         <action dev="henrib" type="add" issue="JEXL-106" due-to="Michal Sabol">
             When divide two BigDecimal values in an expression it results in java.lang.ArithmeticException:
Non-terminating decimal expansion; no exact representable decimal result.
         </action>

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=1069299&r1=1069298&r2=1069299&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 Thu Feb
10 09:22:12 2011
@@ -23,6 +23,7 @@ import org.apache.commons.jexl2.internal
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.commons.jexl2.introspection.UberspectImpl;
+import org.apache.commons.jexl2.parser.JexlNode;
 
 /**
  * Test cases for reported issues
@@ -454,31 +455,28 @@ public class IssuesTest extends JexlTest
     }
 
     public void test107() throws Exception {
+        String[] exprs = {
+            "'Q4'.toLowerCase()", "q4",
+            "(Q4).toLowerCase()", "q4",
+            "(4).toString()", "4",
+            "(1 + 3).toString()", "4",
+            "({ 'q' : 'Q4'}).get('q').toLowerCase()", "q4",
+            "{ 'q' : 'Q4'}.get('q').toLowerCase()", "q4",
+            "({ 'q' : 'Q4'})['q'].toLowerCase()", "q4",
+            "(['Q4'])[0].toLowerCase()", "q4"
+        };
+
         JexlContext context = new MapContext();
         context.set("Q4", "Q4");
         JexlEngine jexl = new JexlEngine();
-        Expression expr;
-        Object value;
-        expr = jexl.createExpression("'Q4'.toLowerCase()");
-        value = expr.evaluate(context);
-        assertEquals("q4", value);
-        expr = jexl.createExpression("(Q4).toLowerCase()");
-        value = expr.evaluate(context);
-        assertEquals("q4", value);
-        expr = jexl.createExpression("(4).toString()");
-        value = expr.evaluate(context);
-        assertEquals("4", value);
-        value = jexl.createExpression("(1 + 3).toString()");
-        value = expr.evaluate(context);
-        assertEquals("4", value);
-        expr = jexl.createExpression("({ 'Q4' : 'Q4'}).get('Q4').toLowerCase()");
-        value = expr.evaluate(context);
-        assertEquals("q4", value);
-        expr = jexl.createExpression("{ 'Q4' : 'Q4'}.get('Q4').toLowerCase()");
-        value = expr.evaluate(context);
-        assertEquals("q4", value);
-        expr = jexl.createExpression("({ 'Q4' : 'Q4'}).get('Q4').toLowerCase()");
-        value = expr.evaluate(context);
-        assertEquals("q4", value);
+        for(int e = 0; e < exprs.length; e += 2) {
+            Expression expr = jexl.createExpression(exprs[e]);
+            Object expected = exprs[e + 1];
+            Object value = expr.evaluate(context);
+            assertEquals(expected, value);
+            expr = jexl.createExpression(expr.dump());
+            value = expr.evaluate(context);
+            assertEquals(expected, value);
+        }
     }
 }



Mime
View raw message