You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Narendran Chakkaravarthy <na...@gmail.com> on 2012/02/16 02:22:30 UTC

[Jexl] Return statement

Hello Jexl Users,

Is 'return' statement supported? The
documentation<http://commons.apache.org/jexl/reference/syntax.html#Functions>claims
to, but I get the error - "Ambiguous statement... missing ; ".
Without 'return' or 'break' it seems difficult to write even a simple
routine. Curious about how others are managing this.
Here is the use case:

Return true if my name is one of the element in the list

for(x : items) {
 if(x.equals(myname)) {
   return true;
 }
}
return false;

items and mynames are variables in the context.

Thanks for any pointers.

-- 
~Naren

Re: [Jexl] Return statement

Posted by henrib <he...@apache.org>.
Hello Naren;
Indeed, the 'return' keyword was introduced in 2.1 (JEXL-114, fixed
2011-07-17).
I guess your snapshot is older than that...
Cheers,
Henrib

PS: the stack overflow case is 2010 so this is likely the same culprit...

--
View this message in context: http://apache-commons.680414.n4.nabble.com/Jexl-Return-statement-tp4392635p4406152.html
Sent from the Commons - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [Jexl] Return statement

Posted by Narendran Chakkaravarthy <na...@gmail.com>.
Hello Henrib,

Thanks for trying this and sorry about the delay in response.

I am using commons-jexl-2.0.2-SNAPSHOT.jar (built ourselves from a
snapshot), so there could be difference there.

I used your same test case (with changes to use old api)

  public void test130() throws Exception {
    JexlEngine jexl = new JexlEngine();
    JexlContext ctxt = new MapContext();
    String[] items = { "foo" , "bar", "quux" };
    ctxt.set("items", items);
    ctxt.set("x", "bar");
    ctxt.set("y", "bar");
    // Script s = jexl.createScript("if(x.equals(y)) { return true; } ");
    Script s = jexl.createScript("for(a : items) { if(a.equals(y)) { return
true; } }");

    Object r = s.execute(ctxt);
    assertEquals(true, r);
    ctxt.set("y", "froboz");
    r = s.execute(ctxt);
    assertNull(r);
}

And I get the following error:

org.apache.commons.jexl2.JexlException: MyScriptTest.test130@855 !!! for(a
: items) { if(a.equals(y)) { return true; } } !!!, parsing failed
at org.apache.commons.jexl2.JexlEngine.parse(JexlEngine.java:830)
at org.apache.commons.jexl2.JexlEngine.createScript(JexlEngine.java:418)
at org.apache.commons.jexl2.JexlEngine.createScript(JexlEngine.java:401)
at MyScriptTest.test130(JexlScriptIntegrationTest.java:855)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.commons.jexl2.parser.ParseException: Ambiguous
statement @1:43, missing ';' between expressions
at
org.apache.commons.jexl2.parser.Parser.jjtreeCloseNodeScope(Parser.java:41)
at
org.apache.commons.jexl2.parser.Parser.ExpressionStatement(Parser.java:280)
at org.apache.commons.jexl2.parser.Parser.Statement(Parser.java:154)
at org.apache.commons.jexl2.parser.Parser.Block(Parser.java:204)
at org.apache.commons.jexl2.parser.Parser.Statement(Parser.java:124)
at org.apache.commons.jexl2.parser.Parser.IfStatement(Parser.java:302)
at org.apache.commons.jexl2.parser.Parser.Statement(Parser.java:128)
at org.apache.commons.jexl2.parser.Parser.Block(Parser.java:204)
at org.apache.commons.jexl2.parser.Parser.Statement(Parser.java:124)
at org.apache.commons.jexl2.parser.Parser.ForeachStatement(Parser.java:383)
at org.apache.commons.jexl2.parser.Parser.Statement(Parser.java:132)
at org.apache.commons.jexl2.parser.Parser.JexlScript(Parser.java:86)
at org.apache.commons.jexl2.parser.Parser.parse(Parser.java:24)
at org.apache.commons.jexl2.JexlEngine.parse(JexlEngine.java:823)
... 19 more

Looks like others are running into this problem as well --
http://stackoverflow.com/questions/3209439/jexl-program-have-exception

Thanks,
Naren



On Thu, Feb 16, 2012 at 5:16 AM, henrib <he...@apache.org> wrote:

> Hi Naren,
> I've tried to reproduce your problem and failed so I must be missing
> something.
> Here is the code I tried on 2.1 trunk (added to IssuesTest.java):
>
>    public void test130() throws Exception {
>        JexlEngine jexl = new JexlEngine();
>        String[] items = { "foo" , "bar", "quux" };
>        Script s = jexl.createScript("for(var x : items) { if(x.equals(y)) {
> return true; } }", "items", "y");
>        Object r = s.execute(null, items, "bar");
>        assertEquals(true, r);
>        r = s.execute(null, items, "froboz");
>        assertNull(r);
>        JexlContext ctxt = new MapContext();
>        ctxt.set("items", items);
>        ctxt.set("y", "bar");
>        s = jexl.createScript("for(x : items) { if(x.equals(y)) { return
> true; } }");
>        r = s.execute(ctxt);
>        assertEquals(true, r);
>        ctxt.set("y", "froboz");
>        r = s.execute(ctxt);
>        assertNull(r);
>    }
>
> I've also tried with out the 'var x' and a MapContext  and with an added
> 'return false' at the end but couldn't make it fail.
> Can you post the actual code that fails on your end?
> Thanks,
> Regards
> Henrib
>
> --
> View this message in context:
> http://apache-commons.680414.n4.nabble.com/Jexl-Return-statement-tp4392635p4393882.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>


-- 
~Naren

Re: [Jexl] Return statement

Posted by henrib <he...@apache.org>.
Hi Naren,
I've tried to reproduce your problem and failed so I must be missing
something.
Here is the code I tried on 2.1 trunk (added to IssuesTest.java):
    
    public void test130() throws Exception {
        JexlEngine jexl = new JexlEngine();
        String[] items = { "foo" , "bar", "quux" };
        Script s = jexl.createScript("for(var x : items) { if(x.equals(y)) {
return true; } }", "items", "y"); 
        Object r = s.execute(null, items, "bar");
        assertEquals(true, r);
        r = s.execute(null, items, "froboz");
        assertNull(r);
        JexlContext ctxt = new MapContext();
        ctxt.set("items", items);
        ctxt.set("y", "bar");
        s = jexl.createScript("for(x : items) { if(x.equals(y)) { return
true; } }");
        r = s.execute(ctxt);
        assertEquals(true, r);
        ctxt.set("y", "froboz");
        r = s.execute(ctxt);
        assertNull(r);
    }

I've also tried with out the 'var x' and a MapContext  and with an added
'return false' at the end but couldn't make it fail.
Can you post the actual code that fails on your end?
Thanks,
Regards
Henrib

--
View this message in context: http://apache-commons.680414.n4.nabble.com/Jexl-Return-statement-tp4392635p4393882.html
Sent from the Commons - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org