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 2022/12/19 15:28:18 UTC

[commons-jexl] 01/01: JEXL-390: modified grammar to remove pragma declarations from statements and allow them in scripts/blocks;

This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to branch JEXL-390
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git

commit fe97d7c4e58a538544f43a1259d22647df093055
Author: henrib <he...@apache.org>
AuthorDate: Mon Dec 19 16:28:11 2022 +0100

    JEXL-390: modified grammar to remove pragma declarations from statements and allow them in scripts/blocks;
---
 .../org/apache/commons/jexl3/parser/Parser.jjt     |  7 +++----
 .../org/apache/commons/jexl3/Issues300Test.java    | 22 ++++++++++++++++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
index 05a17285..cd6cc868 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
+++ b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
@@ -324,7 +324,7 @@ ASTJexlScript JexlScript(Scope frame) : {
    {
         pushUnit(jjtThis);
    }
-        (Statement())* <EOF>
+        ( LOOKAHEAD(<PRAGMA>) Pragma() | Statement() )* <EOF>
    {
         popUnit(jjtThis);
         return jjtThis.script();
@@ -338,7 +338,7 @@ ASTJexlScript JexlExpression(Scope frame) #JexlScript : {
    {
         pushUnit(jjtThis);
    }
-   ( Expression() )? <EOF>
+   ( Pragma() )* ( Expression() )? <EOF>
    {
         popUnit(jjtThis);
         return jjtThis.script();
@@ -370,7 +370,6 @@ void Statement() #void : {}
 void StatementNoVar() #void : {}
 {
     <SEMICOL>
-    | LOOKAHEAD(<PRAGMA>) Pragma()
     | LOOKAHEAD(<ANNOTATION>) AnnotatedStatement()
     | LOOKAHEAD(<IF>) IfStatement()
     | LOOKAHEAD(<FOR>) ForeachStatement()
@@ -386,7 +385,7 @@ void StatementNoVar() #void : {}
 
 void Block() #Block : {}
 {
-    <LCURLY> { pushUnit(jjtThis); } ( Statement() )* { popUnit(jjtThis); } <RCURLY>
+    <LCURLY> { pushUnit(jjtThis); } ( LOOKAHEAD(<PRAGMA>)  Pragma() | Statement() )* { popUnit(jjtThis); } <RCURLY>
 }
 
 void FunctionStatement() #JexlLambda : {}
diff --git a/src/test/java/org/apache/commons/jexl3/Issues300Test.java b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
index 720bafaf..e742d4e4 100644
--- a/src/test/java/org/apache/commons/jexl3/Issues300Test.java
+++ b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
@@ -34,6 +34,7 @@ import java.util.Map;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import static org.apache.commons.jexl3.internal.Util.debuggerCheck;
 import static org.junit.Assert.assertEquals;
 
 /**
@@ -1150,4 +1151,25 @@ public class Issues300Test {
             Assert.assertEquals("ABC", s1.execute(ctxt, null));
         }
     }
+    @Test
+    public void test390() throws Exception {
+        final JexlEngine jexl = new JexlBuilder()
+                .safe(false)
+                .strict(true)
+                .debug(true)
+                .create();
+        JexlScript script = null;
+        String src;
+        src = "if (true) #pragma one 42";
+        try {
+            script = jexl.createScript(src);
+            Assert.fail("should have failed parsing");
+        } catch(JexlException.Parsing xparse) {
+            Assert.assertTrue(xparse.getDetail().contains("pragma"));
+        }
+        src = "if (true) { #pragma one 42 }";
+        script = jexl.createScript(src);
+        Object result = script.execute(null);
+        debuggerCheck(jexl);
+    }
 }