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 2011/10/21 18:45:12 UTC

svn commit: r1187460 - in /commons/proper/jexl/trunk/src: main/java/org/apache/commons/jexl2/ main/java/org/apache/commons/jexl2/parser/ test/java/org/apache/commons/jexl2/

Author: henrib
Date: Fri Oct 21 16:45:11 2011
New Revision: 1187460

URL: http://svn.apache.org/viewvc?rev=1187460&view=rev
Log:
JEXL-120:
Made simpler version of ParseException that does not need to go through System.getProperty;
Made error messages a bit easier to understand;

Added:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ParseException.java   (with props)
Modified:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlException.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/SimpleNode.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/TokenMgrError.java
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ParseFailuresTest.java

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlException.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlException.java?rev=1187460&r1=1187459&r2=1187460&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlException.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/JexlException.java Fri Oct 21 16:45:11 2011
@@ -19,6 +19,8 @@ package org.apache.commons.jexl2;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.UndeclaredThrowableException;
 import org.apache.commons.jexl2.parser.JexlNode;
+import org.apache.commons.jexl2.parser.ParseException;
+import org.apache.commons.jexl2.parser.TokenMgrError;
 
 /**
  * Wraps any error that might occur during interpretation of a script or expression.
@@ -112,8 +114,25 @@ public class JexlException extends Runti
          * @param expr the expression
          * @param cause the javacc cause
          */
-        public Tokenization(JexlInfo node, CharSequence expr, Throwable cause) {
-            super(node, expr.toString(), cause);
+        public Tokenization(JexlInfo node, CharSequence expr, TokenMgrError cause) {
+            super(merge(node, cause), expr.toString(), cause);
+        }
+                
+        /**
+         * Merge the node info and the cause info to obtain best possible location.
+         * @param node the node
+         * @param cause the cause
+         * @return the info to use
+         */
+        private static DebugInfo merge(JexlInfo node, TokenMgrError cause) {
+            DebugInfo dbgn = node != null? node.debugInfo() : null;
+            if (cause == null) {
+                return dbgn;
+            } else if (dbgn == null) {
+                return new DebugInfo("", cause.getLine(), cause.getColumn());
+            } else {
+                return new DebugInfo(dbgn.getName(), cause.getLine(), cause.getColumn());
+            }
         }
                             
         /**
@@ -122,10 +141,23 @@ public class JexlException extends Runti
         public String getExpression() {
             return super.detailedMessage();
         }
-        
+                
         @Override
         protected String detailedMessage() {
-            return "!!! " + getExpression() + " !!!" + ", tokenization failed";
+            int begin = info.debugInfo().getColumn();
+            int end = begin + 5;
+            begin -= 5;
+            if (begin < 0) {
+                end += 5;
+                begin = 0;
+            }
+            int length = getExpression().length();
+            if (length < 10) {
+                return "parsing error in '" + getExpression() + "'";
+            } else {
+                return "parsing error near '... "
+                       + getExpression().substring(begin, end > length? length : end) + " ...'";
+            }
         }
     } 
             
@@ -136,11 +168,28 @@ public class JexlException extends Runti
         /**
          * Creates a new Variable exception instance.
          * @param node the offending ASTnode
-         * @param expr the unknown variable
+         * @param expr the offending source
          * @param cause the javacc cause
          */
-        public Parsing(JexlInfo node, CharSequence expr, Throwable cause) {
-            super(node, expr.toString(), cause);
+        public Parsing(JexlInfo node, CharSequence expr, ParseException cause) {
+            super(merge(node, cause), expr.toString(), cause);
+        }
+                
+        /**
+         * Merge the node info and the cause info to obtain best possible location.
+         * @param node the node
+         * @param cause the cause
+         * @return the info to use
+         */
+        private static DebugInfo merge(JexlInfo node, ParseException cause) {
+            DebugInfo dbgn = node != null? node.debugInfo() : null;
+            if (cause == null) {
+                return dbgn;
+            } else if (dbgn == null) {
+                return new DebugInfo("", cause.getLine(), cause.getColumn());
+            } else {
+                return new DebugInfo(dbgn.getName(), cause.getLine(), cause.getColumn());
+            }
         }
                     
         /**
@@ -152,7 +201,20 @@ public class JexlException extends Runti
         
         @Override
         protected String detailedMessage() {
-            return "!!! " + getExpression() + " !!!" + ", parsing failed";
+            int begin = info.debugInfo().getColumn();
+            int end = begin + 5;
+            begin -= 5;
+            if (begin < 0) {
+                end += 5;
+                begin = 0;
+            }
+            int length = getExpression().length();
+            if (length < 10) {
+                return "parsing error in '" + getExpression() + "'";
+            } else {
+                return "parsing error near '... "
+                       + getExpression().substring(begin, end > length? length : end) + " ...'";
+            }
         }
     }
     

Added: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ParseException.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ParseException.java?rev=1187460&view=auto
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ParseException.java (added)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ParseException.java Fri Oct 21 16:45:11 2011
@@ -0,0 +1,99 @@
+
+/*
+ * 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.jexl2.parser;
+
+/**
+ * This exception is thrown when parse errors are encountered.
+ */
+public class ParseException extends Exception {
+    /**
+     * The version identifier.
+     */
+    private static final long serialVersionUID = 1L;
+    /**
+     * Last correct input before error occurs.
+     */
+    private String after = "";
+    /**
+     * Error line.
+     */
+    private int line = -1;
+    /**
+     * Error column.
+     */
+    private int column = -1;
+    
+    /**
+     * Gets the line number.
+     * @return line number.
+     */
+    public int getLine() {
+        return line;
+    }
+
+    /**
+     * Gets the column number.
+     * @return the column.
+     */
+    public int getColumn() {
+        return column;
+    }
+    
+    /**
+     * Gets the last correct input.
+     * @return the string after which the error occured
+     */
+    public String getAfter() {
+        return after;
+    }
+    
+    /**
+     * This constructor is used by the method "generateParseException"
+     * in the generated parser.  Calling this constructor generates
+     * a new object of this type with the fields "currentToken",
+     * "expectedTokenSequences", and "tokenImage" set.
+     * @param cuurentToken This is the last token that has been consumed successfully.  If
+     * this object has been created due to a parse error, the token
+     * followng this token will (therefore) be the first error token.
+     * @param expectedTokenSequences Each entry in this array is an array of integers.  Each array
+     * of integers represents a sequence of tokens (by their ordinal
+     * values) that is expected at this point of the parse.
+     * @param tokenImage This is a reference to the "tokenImage" array of the generated
+     * parser within which the parse error occurred.  This array is
+     * defined in the generated ...Constants interface.
+     */
+    public ParseException(Token currentToken, int[][] expectedTokenSequences, String[] tokenImage) {
+        super("parse error");
+        Token tok = currentToken.next;
+        after = tok.image;
+        line = tok.beginLine;
+        column = tok.beginColumn;
+    }
+
+    /**
+     * Default ctor.
+     */
+    public ParseException() {
+        super();
+    }
+
+    /** Constructor with message. */
+    public ParseException(String message) {
+        super(message);
+    }
+}

Propchange: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ParseException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/SimpleNode.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/SimpleNode.java?rev=1187460&r1=1187459&r2=1187460&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/SimpleNode.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/SimpleNode.java Fri Oct 21 16:45:11 2011
@@ -20,7 +20,7 @@ package org.apache.commons.jexl2.parser;
  * A class originally generated by JJTree with the following JavaCCOptions:
  * MULTI=true,NODE_USES_PARSER=true,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=
  *
- * Worksaround issue https://javacc.dev.java.net/issues/show_bug.cgi?id=227
+ * Works around issue https://javacc.dev.java.net/issues/show_bug.cgi?id=227
  * As soon as this issue if fixed and the maven plugin uses the correct version of Javacc, this
  * class can go away.
  *

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/TokenMgrError.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/TokenMgrError.java?rev=1187460&r1=1187459&r2=1187460&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/TokenMgrError.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/TokenMgrError.java Fri Oct 21 16:45:11 2011
@@ -104,7 +104,7 @@ public class TokenMgrError extends Error
      * Gets the last correct input.
      * @return the string after which the error occured
      */
-    public String getAfer() {
+    public String getAfter() {
         return after;
     }
  

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ParseFailuresTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ParseFailuresTest.java?rev=1187460&r1=1187459&r2=1187460&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ParseFailuresTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ParseFailuresTest.java Fri Oct 21 16:45:11 2011
@@ -45,6 +45,7 @@ public class ParseFailuresTest extends J
                 + "\" should result in a JexlException");
         } catch (JexlException pe) {
             // expected
+            JEXL.logger.error(pe);
         }
     }
 
@@ -57,6 +58,7 @@ public class ParseFailuresTest extends J
                 + "\" should result in a JexlException");
         } catch (JexlException pe) {
             // expected
+            JEXL.logger.error(pe);
         }
     }
 
@@ -69,6 +71,7 @@ public class ParseFailuresTest extends J
                 + "\" should result in a JexlException");
         } catch (JexlException pe) {
             // expected
+            JEXL.logger.error(pe);
         }
     }
 
@@ -82,6 +85,7 @@ public class ParseFailuresTest extends J
                 + "\" should result in a JexlException");
         } catch (JexlException pe) {
             // expected
+            JEXL.logger.error(pe);
         }
     }
 
@@ -94,6 +98,7 @@ public class ParseFailuresTest extends J
                 + "\" should result in a JexlException");
         } catch (JexlException pe) {
             // expected
+            JEXL.logger.error(pe);
         }
     }