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 2020/01/28 10:29:23 UTC

[commons-jexl] 02/02: JEXL-324: refining parsing error reporting Task #JEXL-324 - JexlEngine.createExpression("new()").getParsedText() throws NPE

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

commit 1a304ae037c278ce2204989ecfdddf9ac449638c
Author: henrib <he...@apache.org>
AuthorDate: Tue Jan 28 11:26:59 2020 +0100

    JEXL-324: refining parsing error reporting
    Task #JEXL-324 - JexlEngine.createExpression("new()").getParsedText() throws NPE
---
 .../java/org/apache/commons/jexl3/parser/JexlParser.java   | 14 ++++++++++++++
 src/main/java/org/apache/commons/jexl3/parser/Parser.jjt   |  2 +-
 2 files changed, 15 insertions(+), 1 deletion(-)

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 42fc47f..bab6c63 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
@@ -585,4 +585,18 @@ public abstract class JexlParser extends StringParser {
         // unlikely but safe
         throw xparse != null ? xparse : new JexlException.Parsing(xinfo, msg);
     }
+    
+    /**
+     * Pick the most significant token for error reporting.
+     * @param tokens the tokens to choose from
+     * @return the token
+     */
+    protected static Token errorToken(Token... tokens) {
+        for (Token token : tokens) {
+            if (token != null && token.image != null && !token.image.isEmpty()) {
+                return token;
+            }
+        }
+        return null;
+    }
 }
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 5cbb9a6..fb5ae37 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
+++ b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
@@ -70,7 +70,7 @@ public final class Parser extends JexlParser
         } catch (TokenMgrError xtme) {
             throw new JexlException.Tokenization(info, xtme).clean();
         } catch (ParseException xparse) {
-            Token errortok = jj_lastpos != null? jj_lastpos : jj_scanpos != null ? jj_scanpos : token;
+            Token errortok = errorToken(jj_lastpos, jj_scanpos, token.next, token);
             throw new JexlException.Parsing(info.at(errortok.beginLine, errortok.beginColumn), errortok.image).clean();
         } finally {
             token_source.defaultLexState = DEFAULT;