Commit a382b251 authored by gmantele's avatar gmantele
Browse files

[ADQL] Throwing a ParseException instead of an Error

when an incorrect character that can not be interpreted by
the JavaCC Token Manager is encountered.

Actually, the TokenMgrError thrown by JavaCC is caught by all
ADQLParser.parseQuery(...) functions, wrapped inside a ParseException
which is finally thrown instead of the TokenMgrError. In this way,
ADQL-Lib users just have to care about a single Throwable:
ParseException.

Besides the error message has been slightly modified from:

> Lexical error at line 1, column 10.  Encountered: "\u00e9" (233), after : \"\"

to:

> Incorrect character encountered at l.1, c.10: \"\\u00e9\" ('é'), after : \"\"

Thus, the error is more user-friendly, more easy to understand by users.
Additionally, the incorrect character is displayed, as before, in its unicode
expression, but also in its character form (instead of an integer value that
nobody can really understand).

This commit fixes the GitHub issue #17
parent 0057e594
Loading
Loading
Loading
Loading
+92 −80
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ import adql.translator.TranslationException;
* @see ADQLQueryFactory
*
* @author Grégory Mantelet (CDS;ARI) - gmantele@ari.uni-heidelberg.de
* @version 1.4 (04/2017)
* @version 1.4 (09/2017)
*/
public class ADQLParser implements ADQLParserConstants {

@@ -295,7 +295,11 @@ public class ADQLParser implements ADQLParserConstants {
	public final ADQLQuery parseQuery() throws ParseException{
		stackQuery.clear();
		query = null;
		try{
			return Query();
		}catch(TokenMgrError tme){
			throw new ParseException(tme.getMessage(), new TextPosition(tme.getErrorLine(), tme.getErrorColumn()));
		}
	}

	/**
@@ -313,7 +317,11 @@ public class ADQLParser implements ADQLParserConstants {
		stackQuery.clear();
		query = null;
		ReInit(new java.io.ByteArrayInputStream(q.getBytes()));
		try{
			return Query();
		}catch(TokenMgrError tme){
			throw new ParseException(tme.getMessage(), new TextPosition(tme.getErrorLine(), tme.getErrorColumn()));
		}
	}

	/**
@@ -331,7 +339,11 @@ public class ADQLParser implements ADQLParserConstants {
		stackQuery.clear();
		query = null;
		ReInit(stream);
		try{
			return Query();
		}catch(TokenMgrError tme){
			throw new ParseException(tme.getMessage(), new TextPosition(tme.getErrorLine(), tme.getErrorColumn()));
		}
	}

	public final void setDebug(boolean debug){
@@ -3912,82 +3924,6 @@ public class ADQLParser implements ADQLParserConstants {
		}
	}

	private boolean jj_3R_59(){
		if (jj_3R_24())
			return true;
		return false;
	}

	private boolean jj_3R_58(){
		if (jj_scan_token(LEFT_PAR))
			return true;
		if (jj_3R_42())
			return true;
		if (jj_scan_token(RIGHT_PAR))
			return true;
		return false;
	}

	private boolean jj_3R_57(){
		if (jj_3R_27())
			return true;
		return false;
	}

	private boolean jj_3R_124(){
		if (jj_scan_token(CIRCLE))
			return true;
		if (jj_scan_token(LEFT_PAR))
			return true;
		if (jj_3R_134())
			return true;
		if (jj_scan_token(COMMA))
			return true;
		if (jj_3R_135())
			return true;
		if (jj_scan_token(COMMA))
			return true;
		if (jj_3R_101())
			return true;
		if (jj_scan_token(RIGHT_PAR))
			return true;
		return false;
	}

	private boolean jj_3R_44(){
		if (jj_scan_token(SELECT))
			return true;
		return false;
	}

	private boolean jj_3R_56(){
		if (jj_3R_101())
			return true;
		return false;
	}

	private boolean jj_3R_123(){
		if (jj_scan_token(CENTROID))
			return true;
		if (jj_scan_token(LEFT_PAR))
			return true;
		if (jj_3R_113())
			return true;
		if (jj_scan_token(RIGHT_PAR))
			return true;
		return false;
	}

	private boolean jj_3R_117(){
		if (jj_scan_token(LEFT_PAR))
			return true;
		if (jj_3R_27())
			return true;
		if (jj_scan_token(RIGHT_PAR))
			return true;
		return false;
	}

	private boolean jj_3R_16(){
		if (jj_scan_token(LEFT_PAR))
			return true;
@@ -5612,6 +5548,82 @@ public class ADQLParser implements ADQLParserConstants {
		return false;
	}

	private boolean jj_3R_59(){
		if (jj_3R_24())
			return true;
		return false;
	}

	private boolean jj_3R_58(){
		if (jj_scan_token(LEFT_PAR))
			return true;
		if (jj_3R_42())
			return true;
		if (jj_scan_token(RIGHT_PAR))
			return true;
		return false;
	}

	private boolean jj_3R_57(){
		if (jj_3R_27())
			return true;
		return false;
	}

	private boolean jj_3R_124(){
		if (jj_scan_token(CIRCLE))
			return true;
		if (jj_scan_token(LEFT_PAR))
			return true;
		if (jj_3R_134())
			return true;
		if (jj_scan_token(COMMA))
			return true;
		if (jj_3R_135())
			return true;
		if (jj_scan_token(COMMA))
			return true;
		if (jj_3R_101())
			return true;
		if (jj_scan_token(RIGHT_PAR))
			return true;
		return false;
	}

	private boolean jj_3R_44(){
		if (jj_scan_token(SELECT))
			return true;
		return false;
	}

	private boolean jj_3R_56(){
		if (jj_3R_101())
			return true;
		return false;
	}

	private boolean jj_3R_123(){
		if (jj_scan_token(CENTROID))
			return true;
		if (jj_scan_token(LEFT_PAR))
			return true;
		if (jj_3R_113())
			return true;
		if (jj_scan_token(RIGHT_PAR))
			return true;
		return false;
	}

	private boolean jj_3R_117(){
		if (jj_scan_token(LEFT_PAR))
			return true;
		if (jj_3R_27())
			return true;
		if (jj_scan_token(RIGHT_PAR))
			return true;
		return false;
	}

	/** Generated Token Manager. */
	public ADQLParserTokenManager token_source;
	SimpleCharStream jj_input_stream;
+7 −6
Original line number Diff line number Diff line
@@ -2,12 +2,13 @@
/* JavaCCOptions:KEEP_LINE_COL=null
 * 
 * Modified by Grégory Mantelet (CDS), on March 2017
 * Modification: several small modifications.
 * Modifications:
 *     - several small modifications.
 * 
 * /!\ DO NOT RE-GENERATE THIS FILE /!\
 *               In case of re-generation, replace it by
 *               ParseException.java.backup (but maybe after a diff
 *               in case of significant modifications have been done
 *               by a new version of JavaCC).
 * In case of re-generation, replace it by ParseException.java.backup (but maybe
 * after a diff in case of significant modifications have been done by a new
 * version of JavaCC).
 */
package adql.parser;

+7 −6
Original line number Diff line number Diff line
@@ -2,12 +2,13 @@
/* JavaCCOptions:KEEP_LINE_COL=null
 * 
 * Modified by Grégory Mantelet (CDS), on March 2017
 * Modification: several small modifications.
 * Modifications:
 *     - several small modifications.
 * 
 * /!\ DO NOT RE-GENERATE THIS FILE /!\
 *               In case of re-generation, replace it by
 *               ParseException.java.backup (but maybe after a diff
 *               in case of significant modifications have been done
 *               by a new version of JavaCC).
 * In case of re-generation, replace it by ParseException.java.backup (but maybe
 * after a diff in case of significant modifications have been done by a new
 * version of JavaCC).
 */
package adql.parser;

+12 −8
Original line number Diff line number Diff line
/* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 6.0 */
/* JavaCCOptions:
 *
 * Modified by Grégory Mantelet (CDS), on April 2017
 * Modification: addition of position (line and column) in the original text.
 * Modified by Grégory Mantelet (CDS), on Sept. 2017
 * Modifications:
 *     - addition of position (line and column) in the original text
 *     - adapt the error message so that being more explicit for humans
 *       and display the incorrect character as a character instead of an
 *       integer value
 * 
 * /!\ DO NOT RE-GENERATE THIS FILE /!\
 *               In case of re-generation, replace it by
 *               TokenMgrError.java.backup (but maybe after a diff
 *               in case of significant modifications have been done
 *               by a new version of JavaCC).
 * In case of re-generation, replace it by TokenMgrError.java.backup (but maybe
 * after a diff in case of significant modifications have been done by a new
 * version of JavaCC).
 */
package adql.parser;

@@ -118,7 +122,7 @@ public class TokenMgrError extends Error {
	 * Note: You can customize the lexical error message by modifying this method.
	 */
	protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar){
		return ("Lexical error at line " + errorLine + ", column " + errorColumn + ".  Encountered: " + (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") + "after : \"" + addEscapes(errorAfter) + "\"");
		return ("Incorrect character encountered at l." + errorLine + ", c." + errorColumn + ": " + (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " ('" + curChar + "'), ") + "after : \"" + addEscapes(errorAfter) + "\"");
	}

	/**
+10 −8
Original line number Diff line number Diff line
/* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 6.0 */
/* JavaCCOptions:
 *
 * Modified by Gr&eacute;gory Mantelet (CDS), on April 2017
 * Modification: addition of position (line and column) in the original text.
 * Modified by Gr&eacute;gory Mantelet (CDS), on Sept. 2017
 * Modifications:
 *     - addition of position (line and column) in the original text
 *     - display the incorrect character as a character instead of an integer
 * 
 * /!\ DO NOT RE-GENERATE THIS FILE /!\
 *               In case of re-generation, replace it by
 *               TokenMgrError.java.backup (but maybe after a diff
 *               in case of significant modifications have been done
 *               by a new version of JavaCC).
 * In case of re-generation, replace it by TokenMgrError.java.backup (but maybe
 * after a diff in case of significant modifications have been done by a new
 * version of JavaCC).
 */
package adql.parser;

@@ -118,7 +120,7 @@ public class TokenMgrError extends Error {
	 * Note: You can customize the lexical error message by modifying this method.
	 */
	protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar){
		return ("Lexical error at line " + errorLine + ", column " + errorColumn + ".  Encountered: " + (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") + "after : \"" + addEscapes(errorAfter) + "\"");
		return ("Incorrect character encountered at l." + errorLine + ", c." + errorColumn + ": " + (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " ('" + curChar + "'), ") + "after : \"" + addEscapes(errorAfter) + "\"");
	}

	/**
Loading