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 1d2be49 JEXL-278: refined logic to better capture ambiguous statements
1d2be49 is described below
commit 1d2be49e6fc0fd8bf70a98cf633991c970657136
Author: henrib <[email protected]>
AuthorDate: Sun Oct 28 17:56:23 2018 +0100
JEXL-278: refined logic to better capture ambiguous statements
---
.../org/apache/commons/jexl3/JexlException.java | 2 +-
.../apache/commons/jexl3/parser/JexlParser.java | 54 ++++++++++------------
.../org/apache/commons/jexl3/parser/Parser.jjt | 2 +-
.../org/apache/commons/jexl3/Issues200Test.java | 10 ++--
4 files changed, 32 insertions(+), 36 deletions(-)
diff --git a/src/main/java/org/apache/commons/jexl3/JexlException.java
b/src/main/java/org/apache/commons/jexl3/JexlException.java
index f82d9cf..f9862d8 100644
--- a/src/main/java/org/apache/commons/jexl3/JexlException.java
+++ b/src/main/java/org/apache/commons/jexl3/JexlException.java
@@ -381,7 +381,7 @@ public class JexlException extends RuntimeException {
buffer.append(line.substring(0, fromc - 1));
}
if (cl == tol) {
- buffer.append(line.substring(toc));
+ buffer.append(line.substring(toc + 1));
}
} // else ignore line
cl += 1;
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 5c02a70..61756ea 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
@@ -33,8 +33,7 @@ import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
-import static org.apache.commons.jexl3.parser.ParserConstants.EOF;
-import static org.apache.commons.jexl3.parser.ParserConstants.SEMICOL;
+
/**
* The base class for parsing, manages the parameter/local variable frame.
@@ -271,34 +270,6 @@ public abstract class JexlParser extends StringParser {
* @return the next token on the stack
*/
protected abstract Token getNextToken();
-
- /**
- * Throws Ambiguous exception.
- * <p>It seeks a ';' (or EOF) to recover.
- * @param begin the first token in ambiguous expression
- */
- protected void throwAmbiguousException(Token begin) {
- Token pt = null, end = null;
- while((end = getNextToken()) != null) {
- if (end.kind == EOF || end.kind == SEMICOL) {
- if (pt != null) {
- end = pt;
- }
- break;
- }
- pt = end;
- }
- JexlInfo infob = info.at(begin.beginLine, begin.beginColumn);
- JexlInfo infoe = end != null? info.at(end.endLine, end.endColumn) :
null;
- String msg = readSourceLine(source, begin.beginLine);
- throw new JexlException.Ambiguous(infob, infoe, msg);
- }
-
- protected void jjtreeOpenNodeScope(JexlNode node) {
- if (node instanceof ASTAmbiguous) {
- throwAmbiguousException(getToken(1));
- }
- }
/**
* The set of assignment operators as classes.
@@ -318,6 +289,14 @@ public abstract class JexlParser extends StringParser {
);
/**
+ * Called by parser at beginning of node construction.
+ * @param node the node
+ */
+ protected void jjtreeOpenNodeScope(JexlNode node) {
+ // nothing
+ }
+
+ /**
* Called by parser at end of node construction.
* <p>
* Detects "Ambiguous statement" and 'non-left value assignment'.</p>
@@ -325,6 +304,9 @@ public abstract class JexlParser extends StringParser {
* @throws ParseException
*/
protected void jjtreeCloseNodeScope(JexlNode node) throws ParseException {
+ if (node instanceof ASTAmbiguous) {
+ throwAmbiguousException(node);
+ }
if (node instanceof ASTJexlScript) {
if (node instanceof ASTJexlLambda &&
!getFeatures().supportsLambda()) {
throwFeatureException(JexlFeatures.LAMBDA, node.jexlInfo());
@@ -345,6 +327,18 @@ public abstract class JexlParser extends StringParser {
featureController.controlNode(node);
}
+ /**
+ * Throws Ambiguous exception.
+ * <p>Seeks the end of the ambiguous statement to recover.
+ * @param node the first token in ambiguous expression
+ */
+ protected void throwAmbiguousException(JexlNode node) {
+ JexlInfo begin = node.jexlInfo();
+ Token t = getToken(0);
+ JexlInfo end = info.at(t.beginLine, t.endColumn);
+ String msg = readSourceLine(source, end.getLine());
+ throw new JexlException.Ambiguous(begin, end, msg);
+ }
/**
* Throws a feature exception.
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 1ec783c..590b575 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
+++ b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
@@ -338,7 +338,7 @@ void Block() #Block : {}
void ExpressionStatement() #void : {}
{
- Expression() (LOOKAHEAD(1) Expression() #Ambiguous())* (LOOKAHEAD(1)
<SEMICOL>)?
+ Expression() (LOOKAHEAD(1) Expression() #Ambiguous(1))* (LOOKAHEAD(1)
<SEMICOL>)?
}
diff --git a/src/test/java/org/apache/commons/jexl3/Issues200Test.java
b/src/test/java/org/apache/commons/jexl3/Issues200Test.java
index 63a33bf..b949da8 100644
--- a/src/test/java/org/apache/commons/jexl3/Issues200Test.java
+++ b/src/test/java/org/apache/commons/jexl3/Issues200Test.java
@@ -601,10 +601,10 @@ public class Issues200Test extends JexlTestCase {
@Test
public void test278() throws Exception {
String[] srcs = new String[]{
- "return union x('arg',5,6) ",
- "return union 143('arg',5,6) ;",
- "return union\n 143('arg',5,6) ;",
- "var f =()->{ return union 143; } foo[0]"
+ "return union x143('arg',5,6) ",
+ "return union y143('arg',5,6) ;",
+ "return union\n z143('arg',5,6) ;",
+ "var f =()->{ return union 143 } foo[0]"
};
Object[] ctls = new Object[]{
"42","42","42", 42
@@ -622,6 +622,8 @@ public class Issues200Test extends JexlTestCase {
jc = jexl.createScript(src);
Assert.fail("should have failed, " + (jc != null));
} catch(JexlException.Ambiguous xa) {
+ String str = xa.toString();
+ Assert.assertTrue(str.contains("143"));
src = xa.tryCleanSource(src);
}
jc = jexl.createScript(src);