Prox: reporting exact error messages: works.
Change-Id: I5bd0ea053dc08bfb792b8ea0c12a346b095fcaa2
diff --git a/src/main/antlr/cosmas/c2ps_opPROX.g b/src/main/antlr/cosmas/c2ps_opPROX.g
index ab0f4cb..9193de0 100644
--- a/src/main/antlr/cosmas/c2ps_opPROX.g
+++ b/src/main/antlr/cosmas/c2ps_opPROX.g
@@ -23,39 +23,6 @@
@lexer::header {package de.ids_mannheim.korap.query.parse.cosmas;}
-@members {
- public void displayRecognitionError(String[] tokenNames,
- RecognitionException e) {
- String hdr = getErrorHeader(e);
- String msg = getErrorMessage(e, tokenNames);
- System.err.println("Debug: displayRecognitionError: hdr = " + hdr + ".");
- System.err.println("Debug: displayRecognitionError: msg='" + msg + "'.");
- System.err.println("Debug: displayRecognitionError: e = " + e.toString() + ".");
-
- if( e instanceof C2RecognitionException )
- {
- C2RecognitionException c2e = (C2RecognitionException) e;
- String c2msg = hdr + ": PROX options mismatch at '" + c2e.getMismatchedToken() + "'...";
-
- emitErrorMessage(c2msg);
- }
- else
- emitErrorMessage(hdr + " prox options mismatch...");
-
- // Now do something with hdr and msg...
- }
-}
-
-@rulecatch {
- catch (C2RecognitionException c2e) {
- //Custom handling of an exception. Any java code is allowed.
- System.err.printf("Debug: overall rulecatch for c2ps_opPROX: c2RecognitionException.\n");
- //reportError(c2e);
- //recover(c2e.input, (RecognitionException) c2e);
- //throw (RecognitionException)c2e;
- //System.err.printf("Debug: overall rulecatch: back from reportError(c2e).\n");
- }
-} // rulecatch
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//
diff --git a/src/main/java/de/ids_mannheim/korap/query/parse/cosmas/c2ps_opPROX.java b/src/main/java/de/ids_mannheim/korap/query/parse/cosmas/c2ps_opPROX.java
index 82bc9b9..7987168 100644
--- a/src/main/java/de/ids_mannheim/korap/query/parse/cosmas/c2ps_opPROX.java
+++ b/src/main/java/de/ids_mannheim/korap/query/parse/cosmas/c2ps_opPROX.java
@@ -15,14 +15,17 @@
public class c2ps_opPROX
{
- final static int typeERROR = 1; // type of an Error CommonToken.
- final static int ERR_MEAS_NULL = 300;
- final static int ERR_MEAS_TOOGREAT = 301;
- final static int ERR_VAL_NULL = 302;
- final static int ERR_VAL_TOOGREAT = 303;
- final static int ERR_DIR_TOOGREAT = 304;
+ // type of an Error CommonToken:
+ final static int typeERROR = 1;
+ // error codes returned to client:
+ final public static int ERR_PROX_UNKNOWN = 300;
+ final static int ERR_MEAS_NULL = 301;
+ final static int ERR_MEAS_TOOGREAT = 302;
+ final static int ERR_VAL_NULL = 303;
+ final static int ERR_VAL_TOOGREAT = 304;
+ final static int ERR_DIR_TOOGREAT = 305;
- private static CommonTree buildErrorTree(String text, int errCode, int typeDIST, int pos)
+ private static CommonTree buildErrorTree(String text, int errCode, int typeDIST, int pos) throws RecognitionException
{
CommonTree
@@ -30,9 +33,9 @@
CommonTree
errorNode = new CommonTree(new CommonToken(typeERROR, "ERROR"));
CommonTree
- errorPos = new CommonTree(new CommonToken(typeERROR, Integer.toString(errCode)));
+ errorPos = new CommonTree(new CommonToken(typeERROR, String.valueOf(pos)));
CommonTree
- errorArg = new CommonTree(new CommonToken(1, text));
+ errorCode = new CommonTree(new CommonToken(typeERROR, String.valueOf(errCode)));
CommonTree
errorMes;
String
@@ -46,7 +49,7 @@
break;
case ERR_MEAS_TOOGREAT:
mess = String.format("Abstandsoperator an der Stelle '%s': Bitte nur 1 der folgenden Angaben einsetzen: w,s,p! " +
- "Falls Mehrfachangabe erwünscht, dann durch Kommata trennen (z.B.: /+w2,s0).", text);
+ "Falls Mehrfachangabe erwünscht, müssen diese durch Kommata getrennt werden (z.B.: /+w2,s0).", text);
errorMes = new CommonTree(new CommonToken(typeERROR, mess));
break;
case ERR_VAL_NULL:
@@ -69,12 +72,9 @@
errorTree.addChild(errorNode);
errorNode.addChild(errorPos);
- errorNode.addChild(errorArg);
+ errorNode.addChild(errorCode);
errorNode.addChild(errorMes);
- // test - 09.01.24/FB
- //addError("ab");
-
return errorTree;
}
@@ -97,6 +97,7 @@
public static Object encodeDIST(int typeDIST, int typeDIR, Object ctDir, Object ctMeas, Object ctVal, String text,
int countD, int countM, int countV, int pos)
+ throws RecognitionException
{
CommonTree tree1 = (CommonTree)ctDir;
@@ -128,7 +129,7 @@
}
else if( countD > 1 )
return buildErrorTree(text, ERR_DIR_TOOGREAT, typeDIST, pos);
-
+
// create DIST tree:
CommonTree
tree = new CommonTree(new CommonToken(typeDIST, "DIST"));
@@ -148,7 +149,7 @@
return true;
}
- public static Tree check (String input, int pos)
+ public static Tree check (String input, int pos) throws RecognitionException
{
ANTLRStringStream ss = new ANTLRStringStream(input);
c2ps_opPROXLexer lex = new c2ps_opPROXLexer(ss);
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/Cosmas2QueryProcessor.java b/src/main/java/de/ids_mannheim/korap/query/serialize/Cosmas2QueryProcessor.java
index 47eb907..525bcb6 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/Cosmas2QueryProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/Cosmas2QueryProcessor.java
@@ -1,5 +1,6 @@
package de.ids_mannheim.korap.query.serialize;
+import de.ids_mannheim.korap.query.parse.cosmas.c2ps_opPROX; // error codes.
import de.ids_mannheim.korap.query.object.ClassRefCheck;
import de.ids_mannheim.korap.query.object.ClassRefOp;
import de.ids_mannheim.korap.query.object.CosmasPosition;
@@ -133,32 +134,60 @@
public static Pattern wildcardQuestionPattern = Pattern.compile("([?])");
/**
- * searchforError
+ * reportErrorsinTree:
+ * - traverse the AST tree and search for nodes of type ERROR, they contain
+ * the errCode, the error message and the error char position.
* - returns true if an error node is found in the tree referenced by 'node'.
- * - adds the corresponding error message to the error list.
+ * - adds error code, error position and error message to the error list.
+ * Arguments:
+ * node : might be null if it has been reseted previously by another error handler.
* @param node
* @return: true: error node was found,
* false; no error node found.
* 19.12.23/FB
*/
- private boolean searchforError(Tree node)
+ private boolean reportErrorsinTree(Tree node)
{
- //System.err.printf("Debug: searchforError: '%s' has %d children.\n",
- // node.toStringTree(), node.getChildCount());
+ final String func = "reportErrorsinTree";
+
+ //System.err.printf("Debug: %s: '%s' has %d children.\n",
+ // func, node.getText(), node.getChildCount());
+ if( node == null )
+ {
+ //
+ System.err.printf("Warning: %s: node == null: no action requested.\n", func);
+ return false;
+ }
if( node.getType() == 1 && node.getText().compareTo("ERROR") == 0 )
{
// error node found:
- //System.err.printf("Debug: searchforError: error node found: %s.\n", node.toStringTree());
+ // child[0] : error pos.
+ // child[1] : error code.
+ // child[2] : error message, containing offending string.
+ /*
+ System.err.printf("Debug: %s: child[0]='%s' child[1]='%s' child[2]='%s'.\n", func,
+ node.getChild(0) != null ? node.getChild(0).getText() : "???",
+ node.getChild(1) != null ? node.getChild(1).getText() : "???",
+ node.getChild(2) != null ? node.getChild(2).getText() : "???");
+ */
+ // TODO: StatusCode.MALFORMED_QUERY etc. einsetzen!
+ int
+ errPos = node.getChild(0) != null ? Integer.parseInt(node.getChild(0).getText()) : 0;
+ int
+ errCode = node.getChild(1) != null ? Integer.parseInt(node.getChild(1).getText()) : c2ps_opPROX.ERR_PROX_UNKNOWN;
String
- message = String.format("Fehler %s gefunden bei '%s': %s.",
- node.getChild(0) != null ? node.getChild(0).getText() : "",
- node.getChild(1) != null ? node.getChild(1).getText() : "",
- node.getChild(2) != null ? node.getChild(2).getText() : "");
+ errMess = node.getChild(2) != null ? node.getChild(2).getText() : "Genaue Fehlermeldung nicht auffindbar.";
- addError(345, message);
+ ArrayList<Object>
+ errorSpecs = new ArrayList<Object>();
+
+ errorSpecs.add(errCode);
+ errorSpecs.add(errMess);
+ errorSpecs.add(errPos);
+ addError(errorSpecs);
return true;
}
@@ -173,14 +202,14 @@
son.getTokenStartIndex(),
son.getTokenStopIndex());
*/
-
- if( searchforError(son) )
+ // return the first error found only:
+ if( reportErrorsinTree(son) )
return true; // error found, stop here.
}
// no error node:
return false;
- }
+ } // reportErrorsinTree
/**
* @param tree
@@ -790,14 +819,7 @@
Tree typ = prox_opts.getChild(0);
Tree dist_list = prox_opts.getChild(1);
- // search for an error and if there is one, add it to the list:
- if( searchforError(prox_opts) )
- {
- return;
- }
-
-
- // Step I: create group
+ // Step I: create group
Map<String, Object> group =
KoralObjectGenerator.makeGroup(KoralOperation.SEQUENCE);
@@ -1828,13 +1850,18 @@
// Use custom error reporters
lex.setErrorReporter(errorListener);
((c2psParser) parser).setErrorReporter(errorListener);
-
+
c2psParser.c2ps_query_return c2Return =
((c2psParser) parser).c2ps_query(); // statt t().
// AST Tree anzeigen:
tree = (Tree) c2Return.getTree();
- if (DEBUG) log.debug(tree.toStringTree());
+
+ if (DEBUG)
+ {
+ System.out.printf("Debug: parseCosmasQuery: tree = '%s'.\n", tree.toStringTree());
+ log.debug(tree.toStringTree());
+ }
}
catch (FailedPredicateException fe)
{
@@ -1843,15 +1870,14 @@
"failed predicate on prox something.");
}
catch (RecognitionException e) {
- System.out.printf("parseCosmasQuery: RecognitionException!\n");
- log.error(
+ System.out.printf("Debug: out: parseCosmasQuery: RecognitionException!\n");
+ log.error(
"Could not parse query. Please make sure it is well-formed.");
addError(StatusCodes.MALFORMED_QUERY,
"Could not parse query. Please make sure it is well-formed.");
}
String treestring = tree.toStringTree();
- System.err.printf("Debug: parseCosmasQuery: tree = '%s'.\n", treestring);
boolean erroneous = false;
if (parser.failed() || parser.getNumberOfSyntaxErrors() > 0) {
@@ -1859,21 +1885,26 @@
tree = null;
}
- System.err.printf("Debug: parseCosmasQuery: failed=%b no. of synt. err = %d.\n",
- parser.failed(), parser.getNumberOfSyntaxErrors());
-
if (erroneous || treestring.contains("<mismatched token")
|| treestring.contains("<error")
|| treestring.contains("<unexpected"))
{
- System.err.printf("Debug: parseCosmasQuery: tree: '%s'.\n", treestring);
- System.err.printf("Debug: parseCosmasQuery: FullErrorMsg: '%s'.\n", errorListener.generateFullErrorMsg().toString());
+ //System.err.printf("Debug: parseCosmasQuery: tree: '%s'.\n", treestring);
+ //System.err.printf("Debug: parseCosmasQuery: FullErrorMsg: '%s'.\n", errorListener.generateFullErrorMsg().toString());
log.error(errorListener.generateFullErrorMsg().toString());
addError(errorListener.generateFullErrorMsg());
}
-
- addError(1234, "Test-Message");
-
+
+ // collect and report errors found by other functions than the lexer/parser:
+ // tree might already be null if another error was reported above.
+ if( reportErrorsinTree(tree) == true )
+ {
+ System.out.printf("Debug: parseCosmasQuery: reportErrorsinTree at least 1 error message found. Setting tree = null.\n");
+ return null;
+ }
+ else
+ System.out.printf("Debug: parseCosmasQuery: reportErrorsinTree has found no error messages.\n");
+
return tree;
- }
+ } // parseCosmasQuery
}