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 2019/02/12 17:12:27 UTC

[commons-jexl] branch master updated: JEXL-280: found new case when lambda created within loop of a lambda, fixed loop counter stack management Task #JEXL-280 - break/continue statements outside of the loop

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 31eec80  JEXL-280: found new case when lambda created within loop of a lambda, fixed loop counter stack management Task #JEXL-280 - break/continue statements outside of the loop
31eec80 is described below

commit 31eec803d83bab7fe157f40f932bab63aaee22aa
Author: henrib <he...@apache.org>
AuthorDate: Tue Feb 12 18:11:42 2019 +0100

    JEXL-280: found new case when lambda created within loop of a lambda, fixed loop counter stack management
    Task #JEXL-280 - break/continue statements outside of the loop
---
 .../apache/commons/jexl3/parser/JexlParser.java    | 31 ++++++++++++++--------
 .../org/apache/commons/jexl3/parser/Parser.jjt     | 10 +++----
 .../java/org/apache/commons/jexl3/DoWhileTest.java |  7 ++++-
 3 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
index 39c2cee..17e1937 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
@@ -26,14 +26,13 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.StringReader;
 import java.lang.reflect.Constructor;
+import java.util.ArrayDeque;
 import java.util.Arrays;
+import java.util.Deque;
 import java.util.HashSet;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Queue;
 import java.util.Set;
-import java.util.Stack;
 import java.util.TreeMap;
 
 
@@ -62,7 +61,7 @@ public abstract class JexlParser extends StringParser {
     /**
      * When parsing inner functions/lambda, need to stack the scope (sic).
      */
-    protected Stack<Scope> frames = new Stack<Scope>();
+    protected Deque<Scope> frames = new ArrayDeque<Scope>();
     /**
      * The list of pragma declarations.
      */
@@ -74,10 +73,23 @@ public abstract class JexlParser extends StringParser {
     /**
      * Stack of parsing loop counts.
      */
-    protected Queue<Integer> loopCounts = null;
+    protected Deque<Integer> loopCounts = new ArrayDeque<Integer>();
 
 
     /**
+     * Cleanup.
+     * @param features the feature set to restore if any
+     */
+    protected void cleanup(JexlFeatures features) {
+        info = null;
+        source = null;
+        frame = null;
+        frames.clear();
+        pragmas = null;
+        loopCounts.clear();
+        loopCount = 0;
+    }
+    /**
      * Utility function to create '.' separated string from a list of string.
      * @param lstr the list of strings
      * @return the dotted version
@@ -167,10 +179,7 @@ public abstract class JexlParser extends StringParser {
             frames.push(frame);
         }
         frame = new Scope(frame, (String[]) null);
-        if (loopCounts == null) {
-            loopCounts = new LinkedList<Integer>();
-        }
-        loopCounts.add(loopCount);
+        loopCounts.push(loopCount);
         loopCount = 0;
     }
 
@@ -183,8 +192,8 @@ public abstract class JexlParser extends StringParser {
         } else {
             frame = null;
         }
-        if (loopCounts != null && !loopCounts.isEmpty()) {
-            loopCount = loopCounts.remove();
+        if (!loopCounts.isEmpty()) {
+            loopCount = loopCounts.pop();
         }
     }
 
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 dcb0f87..3565ba0 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
+++ b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
@@ -63,18 +63,14 @@ public final class Parser extends JexlParser
             script.setPragmas(pragmas != null
                              ? Collections.<String,Object>unmodifiableMap(pragmas)
                              : Collections.<String,Object>emptyMap());
-            pragmas = null;
             return script;
         } catch (TokenMgrError xtme) {
             throw new JexlException.Tokenization(info, xtme).clean();
         } catch (ParseException xparse) {
             throw new JexlException.Parsing(info, xparse).clean();
         } finally {
-            info = null;
-            source = null;
-            frame = null;
             token_source.defaultLexState = DEFAULT;
-            setFeatures(previous);
+            cleanup(previous);
         }
     }
 }
@@ -306,9 +302,9 @@ void Annotation() #Annotation :
 }
 
 void AnnotatedStatement() #AnnotatedStatement() : {}
-{
+ {
     (LOOKAHEAD(<ANNOTATION>) Annotation())+ (LOOKAHEAD(<VAR>) Var() | LOOKAHEAD(1) Block() | Expression())
-}
+ }
 
 void Statement() #void : {}
 {
diff --git a/src/test/java/org/apache/commons/jexl3/DoWhileTest.java b/src/test/java/org/apache/commons/jexl3/DoWhileTest.java
index 7260363..c29b853 100644
--- a/src/test/java/org/apache/commons/jexl3/DoWhileTest.java
+++ b/src/test/java/org/apache/commons/jexl3/DoWhileTest.java
@@ -16,7 +16,6 @@
  */
 package org.apache.commons.jexl3;
 
-import org.apache.commons.jexl3.internal.Debugger;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -100,4 +99,10 @@ public class DoWhileTest extends JexlTestCase {
             Assert.assertTrue(str.contains("continue"));
         }
     }
+            
+    @Test
+    public void testForEachLambda() throws Exception {
+        JexlScript e = JEXL.createScript("(x)->{ for (i : 1..2) {  continue; var y = function() { 42; } break; } }");
+        Assert.assertNotNull(e);
+    }
 }