Author: henrib
Date: Thu Dec 10 09:26:32 2009
New Revision: 889154

URL: http://svn.apache.org/viewvc?rev=889154&view=rev
Log:
changed parser so that 'in' is only a keyword in a 'foreach' expression; made 
(principal) keywords explicit in parser; added test

Modified:
    
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt
    
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ForEachTest.java

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=889154&r1=889153&r2=889154&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 Dec 10 09:26:32 2009
@@ -81,7 +81,7 @@
  *     Skip & Number literal tokens
  ***************************************/
 
-SKIP : /* WHITE SPACE */
+<DEFAULT, FOR_EACH_IN> SKIP : /* WHITE SPACE */
 {
   <"##" (~["\n","\r"])* ("\n" | "\r" | "\r\n")? >
 | <"/*" (~["*"])* "*" ("*" | ~["*","/"] (~["*"])* "*")* "/">
@@ -116,6 +116,15 @@
     | < FALSE : "false" >
 }
 
+<DEFAULT, FOR_EACH_IN> TOKEN : {
+      < LPAREN : "(" >
+    | < RPAREN : ")" >
+    | < LCURLY : "{" >
+    | < RCURLY : "}" >
+    | < SEMICOL : ";" >
+    | < COLON : ":" >
+}
+
 <FOR_EACH_IN> TOKEN : /* foreach in */
 {
     < IN : "in" > : DEFAULT
@@ -134,7 +143,7 @@
 
 void Statement() #void : {}
 {
-    ";"
+    <SEMICOL>
     | LOOKAHEAD(3) Block()
     | IfStatement()
     | ForeachStatement()
@@ -144,33 +153,33 @@
 
 void Block() #Block : {}
 {
-    "{" ( Statement() )* "}"
+    <LCURLY> ( Statement() )* <RCURLY>
 }
 
 
 void ExpressionStatement() #void : {}
 {
-    Expression() (LOOKAHEAD(1) Expression() #Ambiguous())* (LOOKAHEAD(2) ";")?
+    Expression() (LOOKAHEAD(1) Expression() #Ambiguous())* (LOOKAHEAD(2) 
<SEMICOL>)?
 }
 
 
 void IfStatement() : {}
 {
-    <IF> "(" Expression() ")" Statement() ( LOOKAHEAD(1) <ELSE> Statement() )?
+    <IF> <LPAREN> Expression() <RPAREN> Statement() ( LOOKAHEAD(1) <ELSE> 
Statement() )?
 }
 
 
 void WhileStatement() : {}
 {
-    <WHILE> "(" Expression() ")" Statement()
+    <WHILE> <LPAREN> Expression() <RPAREN> Statement()
 }
 
 
 void ForeachStatement() : {}
 {
-    <FOR> "(" Reference() ":"  Expression() ")" Statement()
+    <FOR> <LPAREN> Reference() <COLON>  Expression() <RPAREN> Statement()
 |
-    <FOREACH> "(" Reference() <IN>  Expression() ")" Statement()
+    <FOREACH> <LPAREN> Reference() <IN>  Expression() <RPAREN> Statement()
 }
 
 
@@ -529,7 +538,7 @@
  *     Identifier & String tokens
  ***************************************/
 
-TOKEN : /* IDENTIFIERS */
+<DEFAULT, FOR_EACH_IN> TOKEN : /* IDENTIFIERS */
 {
   < IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* >
 |

Modified: 
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ForEachTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ForEachTest.java?rev=889154&r1=889153&r2=889154&view=diff
==============================================================================
--- 
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ForEachTest.java
 (original)
+++ 
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ForEachTest.java
 Thu Dec 10 09:26:32 2009
@@ -94,13 +94,17 @@
     }
 
     public void testForEachWithBlock() throws Exception {
-        Expression e = JEXL.createExpression("for(item : list) { x = x + item; 
}");
+        Expression exs0 = JEXL.createExpression("for(in : list) { x = x + in; 
}");
+        Expression exs1 = JEXL.createExpression("foreach(item in list) { x = x 
+ item; }");
+        Expression []exs = { exs0, exs1 };
         JexlContext jc = new MapContext();
-        jc.set("list", new Object[] {"1", "1"});
-        jc.set("x", new Integer(0));
-        Object o = e.evaluate(jc);
-        assertEquals("Result is wrong", new Integer(2), o);
-        assertEquals("x is wrong", new Integer(2), jc.get("x"));
+        jc.set("list", new Object[] {"2", "3"});
+        for(int ex = 0; ex < exs.length; ++ex) {
+            jc.set("x", new Integer(1));
+            Object o = exs[ex].evaluate(jc);
+            assertEquals("Result is wrong", new Integer(6), o);
+            assertEquals("x is wrong", new Integer(6), jc.get("x"));
+        }
     }
 
     public void testForEachWithListExpression() throws Exception {


Reply via email to