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/12/03 21:09:30 UTC
[commons-jexl] branch master updated: JEXL-307: fixed lexical
feature handling of variable scope in for units (take 3) Task #JEXL-307 -
Variable redeclaration option
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 0d51930 JEXL-307: fixed lexical feature handling of variable scope in for units (take 3) Task #JEXL-307 - Variable redeclaration option
0d51930 is described below
commit 0d51930407eed691e11dc2767f34ef5df6cbf4b8
Author: henrib <he...@apache.org>
AuthorDate: Tue Dec 3 22:08:55 2019 +0100
JEXL-307: fixed lexical feature handling of variable scope in for units (take 3)
Task #JEXL-307 - Variable redeclaration option
---
.../commons/jexl3/parser/ASTForeachStatement.java | 62 ++++++++++++++++++++++
.../org/apache/commons/jexl3/parser/Parser.jjt | 5 +-
.../java/org/apache/commons/jexl3/LexicalTest.java | 42 +++++++++++++++
3 files changed, 107 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/commons/jexl3/parser/ASTForeachStatement.java b/src/main/java/org/apache/commons/jexl3/parser/ASTForeachStatement.java
new file mode 100644
index 0000000..1ad61c0
--- /dev/null
+++ b/src/main/java/org/apache/commons/jexl3/parser/ASTForeachStatement.java
@@ -0,0 +1,62 @@
+/*
+ * 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.jexl3.parser;
+
+import org.apache.commons.jexl3.internal.LexicalScope;
+
+/**
+ * Declares a local variable.
+ */
+public class ASTForeachStatement extends JexlNode implements JexlParser.LexicalUnit {
+ private LexicalScope locals = null;
+
+ public ASTForeachStatement(int id) {
+ super(id);
+ }
+
+ public ASTForeachStatement(Parser p, int id) {
+ super(p, id);
+ }
+
+ @Override
+ public Object jjtAccept(ParserVisitor visitor, Object data) {
+ return visitor.visit(this, data);
+ }
+
+ @Override
+ public boolean declareSymbol(int symbol) {
+ if (locals == null) {
+ locals = new LexicalScope(null);
+ }
+ return locals.declareSymbol(symbol);
+ }
+
+ @Override
+ public int getSymbolCount() {
+ return locals == null? 0 : locals.getSymbolCount();
+ }
+
+ @Override
+ public boolean hasSymbol(int symbol) {
+ return locals == null? false : locals.hasSymbol(symbol);
+ }
+
+ @Override
+ public void clearUnit() {
+ locals = null;
+ }
+}
\ No newline at end of file
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 a750261..01a2857 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
+++ b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
@@ -399,10 +399,11 @@ void Break() #Break : {
void ForeachStatement() : {}
{
+ { pushUnit(jjtThis); }
<FOR> <LPAREN> ForEachVar() <COLON> Expression() <RPAREN>
- { loopCount += 1; }
+ { loopCount += 1; }
(LOOKAHEAD(1) Block() | Statement())
- { loopCount -= 1; }
+ { loopCount -= 1; popUnit(jjtThis); }
}
void ForEachVar() #Reference : {}
diff --git a/src/test/java/org/apache/commons/jexl3/LexicalTest.java b/src/test/java/org/apache/commons/jexl3/LexicalTest.java
index 64cfe6b..00bb2ed 100644
--- a/src/test/java/org/apache/commons/jexl3/LexicalTest.java
+++ b/src/test/java/org/apache/commons/jexl3/LexicalTest.java
@@ -494,4 +494,46 @@ public class LexicalTest {
JexlEngine jexl = new JexlBuilder().strict(true).lexical(true).create();
JexlScript script = jexl.createScript("var x = 32; (()->{ for(var x : null) { var c = 0; {return x; }} })();");
}
+
+ @Test
+ public void testForVariable0() throws Exception {
+ JexlFeatures f = new JexlFeatures();
+ f.lexical(true);
+ JexlEngine jexl = new JexlBuilder().strict(true).features(f).create();
+ try {
+ JexlScript script = jexl.createScript("for(var x : 1..3) { var c = 0}; return x");
+ Assert.fail("Should not have been parsed");
+ } catch (JexlException ex) {
+ // OK
+ }
+ }
+
+
+ @Test
+ public void testForVariable1() throws Exception {
+ JexlFeatures f = new JexlFeatures();
+ f.lexical(true);
+ JexlEngine jexl = new JexlBuilder().strict(true).features(f).create();
+ try {
+ JexlScript script = jexl.createScript("for(var x : 1..3) { var c = 0} for(var x : 1..3) { var c = 0}; return x");
+ Assert.fail("Should not have been parsed");
+ } catch (JexlException ex) {
+ // OK
+ Assert.assertTrue(ex instanceof JexlException);
+ }
+ }
+
+ @Test
+ public void testUndeclaredVariable() throws Exception {
+ JexlFeatures f = new JexlFeatures();
+ f.lexical(true);
+ JexlEngine jexl = new JexlBuilder().strict(true).features(f).create();
+ try {
+ JexlScript script = jexl.createScript("{var x = 0}; return x");
+ Assert.fail("Should not have been parsed");
+ } catch (Exception ex) {
+ // OK
+ Assert.assertTrue(ex instanceof JexlException);
+ }
+ }
}