PROX: Fehlermeldung in KoralQuery schreiben funktioniert.
Change-Id: I4227d6d0899c08017f859d9c044201352d4d4214
diff --git a/src/main/antlr/cosmas/c2ps_opPROX.g b/src/main/antlr/cosmas/c2ps_opPROX.g
index f6cdd5a..eda044e 100644
--- a/src/main/antlr/cosmas/c2ps_opPROX.g
+++ b/src/main/antlr/cosmas/c2ps_opPROX.g
@@ -95,24 +95,12 @@
proxDist
@init{ int countM=0;}
-@rulecatch
- {
- catch (RecognitionException re)
- {
- if( re instanceOf C2RecognitionException )
- System.err.printf("Debug: catched in proxDist: C2RecognitionException!\n");
- else
- System.err.printf("Debug: catched in proxDist: RecognitionException!\n");
-
- reportError(re);
- }
- }
:
//((m=proxMeasure {countM++;})|d=proxDirection|v=proxDistValue)+ {countM == 1}?
- ((m=proxMeasure)|d=proxDirection|v=proxDistValue)+
+ ((m=proxMeasure {countM++;})|d=proxDirection|v=proxDistValue)+
- -> {c2ps_opPROX.encodeDIST(DIST, DIR, $d.tree, $m.tree, $v.tree, $proxDist.text)};
+ -> {c2ps_opPROX.encodeDIST(DIST, DIR, $d.tree, $m.tree, $v.tree, $proxDist.text, countM)};
// -> {c2ps_opPROX.checkDIST($proxDist.text) == true } ? {c2ps_opPROX.encodeDIST(DIST, DIR, $d.tree, $m.tree, $v.tree, $proxDist.text)};
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 b7c73fa..efa5a41 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
@@ -14,38 +14,53 @@
{
+ /* duplicateOptions:
+ * detect if any prox option appears twice or more.
+ * Return true of false.
+ * 19.12.23/FB
+ */
+
+ private static boolean duplicateOptions(String text)
+
+ {
+
+ return false;
+ }
+
/* encodeDIST():
- * - returns a CommonTree built of out Direction/Measure/Distance value.
+ * - returns a CommonTree built from the Direction/Measure/Distance value.
* - accepts options in any order.
* - creates CommonTree in that order: Direction .. Distance value .. Measure.
* - sets default direction to BOTH if not set yet.
* 28.11.23/FB
*/
- public static Object encodeDIST(int typeDIST, int typeDIR, Object ctDir, Object ctMeas, Object ctVal, String text)
- throws C2RecognitionException
+ public static Object encodeDIST(int typeDIST, int typeDIR, Object ctDir, Object ctMeas, Object ctVal, String text, int countM)
+ //throws C2RecognitionException
{
boolean multiple = false;
CommonTree tree1 = (CommonTree)ctDir;
CommonTree tree2 = (CommonTree)ctMeas;
CommonTree tree3 = (CommonTree)ctVal;
- //addError(StatusCodes.MALFORMED_QUERY,
- // "Could not parse query. Please make sure it is well-formed.");
+ System.err.printf("Debug: encodeDIST: scanned input='%s' countM=%d.\n", text, countM);
+
- System.err.printf("Debug: encodeDIST: scanned input='%s'.\n", text);
-
- if( multiple == true )
+ if( duplicateOptions(text) == true )
{
- CommonTree
- errorTree = new CommonTree(new CommonToken(typeDIST, "DIST")),
- errorNode = new CommonTree(new CommonToken(1, "ERROR")),
- errorPos = new CommonTree(new CommonToken(1, "15")),
- errorArg = new CommonTree(new CommonToken(1, text));
+ CommonTree errorTree, errorNode, errorPos, errorArg, errorMes;
+ errorTree = new CommonTree(new CommonToken(typeDIST, "DIST"));
+ errorNode = new CommonTree(new CommonToken(1, "ERROR"));
+ errorPos = new CommonTree(new CommonToken(1, "15"));
+ errorArg = new CommonTree(new CommonToken(1, text));
+ errorMes = new CommonTree(new CommonToken(1, "Operator PROX/Abstand darf nur höchsten 1 Abstandswert, Abstandstyp und" +
+ " Abstandsrichtung enthalten!"));
+
errorTree.addChild(errorNode);
errorNode.addChild(errorPos);
errorNode.addChild(errorArg);
+ errorNode.addChild(errorMes);
System.err.printf("Debug: encodeDIST: parse error found, returning error tree: %s.\n",
errorTree.toStringTree());
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 4c36a1d..8d42718 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
@@ -131,6 +131,56 @@
public static Pattern wildcardPlusPattern = Pattern.compile("([+])");
public static Pattern wildcardQuestionPattern = Pattern.compile("([?])");
+ /**
+ * searchforError
+ * - returns true if an error node is found in the tree referenced by 'node'.
+ * - adds the corresponding error message to the error list.
+ * @param node
+ * @return: true: error node was found,
+ * false; no error node found.
+ * 19.12.23/FB
+ */
+
+ private boolean searchforError(Tree node)
+
+ {
+ //System.err.printf("Debug: searchforError: '%s' has %d children.\n",
+ // node.toStringTree(), node.getChildCount());
+
+ if( node.getType() == 1 && node.getText().compareTo("ERROR") == 0 )
+ {
+ // error node found:
+ //System.err.printf("Debug: searchforError: error node found: %s.\n", node.toStringTree());
+ 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() : "");
+
+ addError(345, message);
+ return true;
+ }
+
+ for(int i=0; i<node.getChildCount(); i++)
+ {
+ Tree
+ son = node.getChild(i);
+
+ /* System.err.printf(" node: text='%s' type=%d start=%d end=%d.\n",
+ son.getText(),
+ son.getType(),
+ son.getTokenStartIndex(),
+ son.getTokenStopIndex());
+ */
+
+ if( searchforError(son) )
+ return true; // error found, stop here.
+ }
+
+ // no error node:
+ return false;
+ }
+
/**
* @param tree
* The syntax tree as returned by ANTLR
@@ -165,10 +215,12 @@
public void process (String query) {
Tree tree = null;
tree = parseCosmasQuery(query);
- if (DEBUG) {
+ if (DEBUG)
+ {
System.out.printf("\nProcessing COSMAS II query: %s.\n\n", query);
log.debug("Processing CosmasII query: " + query);
- }
+ }
+
if (tree != null)
{
if (verbose) {
@@ -731,10 +783,19 @@
@SuppressWarnings("unchecked")
private void processOPPROX (Tree node) {
+
// collect info
Tree prox_opts = node.getChild(0);
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
Map<String, Object> group =
KoralObjectGenerator.makeGroup(KoralOperation.SEQUENCE);
@@ -1762,8 +1823,8 @@
new org.antlr.runtime.CommonTokenStream(lex); // v3
System.out.printf("parseCosmasQuery: tokens = %d\n", tokens.size());
- System.out.printf("parseCosmasQuery: tokens = %s\n", tokens.toString());
System.out.printf("parseCosmasQuery: tokens[] = ");
+
for(int i=0; i<tokens.size(); i++)
System.out.printf("%s ", tokens.get(i).toString());