Prox: beliebige Reihenfolge der Optionen: OK.

Change-Id: I53309d91b506c1f7108fa3fd596c69e9f214be6a
diff --git a/src/main/antlr/cosmas/c2ps.g b/src/main/antlr/cosmas/c2ps.g
index 2a63bb7..f0eba80 100644
--- a/src/main/antlr/cosmas/c2ps.g
+++ b/src/main/antlr/cosmas/c2ps.g
@@ -74,10 +74,25 @@
 
 fragment DISTVALUE
 	:	 ('0' .. '9')+ (':' ('0'..'9')+)? ;
-		
+
+fragment DISTTYPE // 30.11.23/FB
+	: 	('w'|'s'|'p'|'t');
+
+fragment DISTDIR // 30.11.23/FB
+	: 	('+'|'-');
+	
+/* old version (before 30.11.23/FB)
 fragment DIST
 	:	('+'|'-')? (DISTVALUE ('w'|'s'|'p'|'t') | ('w'|'s'|'p'|'t') DISTVALUE);
-	
+*/
+
+// accept these 3 options in any order.
+// afterwards, we will have to check if any of them is missing.
+// 30.11.23/FB
+
+fragment DIST // 30.11.23/FB
+	:	(DISTDIR | DISTTYPE | DISTVALUE )+;
+
 fragment GROUP
 	:	('min' | 'max');
 
diff --git a/src/main/antlr/cosmas/c2ps_opPROX.g b/src/main/antlr/cosmas/c2ps_opPROX.g
index df7eab0..3c23cf1 100644
--- a/src/main/antlr/cosmas/c2ps_opPROX.g
+++ b/src/main/antlr/cosmas/c2ps_opPROX.g
@@ -1,9 +1,10 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//												//
-// 	lokale Grammatik der COSMAS II zeilenorientierten Suchanfragesprache (= c2ps)		//
-//	für den Abstandsoperator /w... und %w...						//
-//	v-1.0 - 07.12.12/FB									//
-//												//
+//												
+// 	lokale Grammatik der COSMAS II zeilenorientierten Suchanfragesprache (= c2ps)		
+//	für den Abstandsoperator /w... und %w...	
+//	v-1.0 - 07.12.12/FB							
+//  v-1.1 - 30.11.23/FB opPROX accepts any order of direction, measure and value.
+//												
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
 grammar c2ps_opPROX;
@@ -29,9 +30,6 @@
 DISTVALUE
 	:	('0' .. '9')+ ;
 	
-DISTOPTS
-	:	('s'|'p'|'w'|'t'|'+'|'-')+;
-	
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 //
 // 						PROX-Parser
@@ -52,68 +50,30 @@
 proxDist:	proxDirection (v1=proxDistValue m1=proxMeasure | m2=proxMeasure v2=proxDistValue)
 
 		-> {$v1.tree != null}? ^(DIST {$proxDirection.tree} {$v1.tree} {$m1.tree})
-		-> 		       ^(DIST {$proxDirection.tree} {$v2.tree} {$m2.tree});
+		->		 		       ^(DIST {$proxDirection.tree} {$v2.tree} {$m2.tree});
 */
 
-// +++ new version 29.11.23 by including all possible option ordering +++ //
-/*
-proxDist:	
-		proxDirection v1=proxDistValue m1=proxMeasure 
-			-> ^(DIST {$proxDirection.tree} {$v1.tree} {$m1.tree})
-	|	proxDirection m1=proxMeasure v1=proxDistValue
-			-> ^(DIST {$proxDirection.tree} {$v1.tree} {$m1.tree})
-	|	m1=proxMeasure d1=proxDirection v1=proxDistValue
-			-> ^(DIST {$d1.tree} {$v1.tree} {$m1.tree})
-	|	m1=proxMeasure v1=proxDistValue d1=proxDirection 
-			-> ^(DIST {$d1.tree} {$v1.tree} {$m1.tree})
-	|	v1=proxDistValue m1=proxMeasure d1=proxDirection 
-			-> ^(DIST {$d1.tree} {$v1.tree} {$m1.tree})
-	|	v1=proxDistValue d1=proxDirection m1=proxMeasure 
-			-> ^(DIST {$d1.tree} {$v1.tree} {$m1.tree});
-*/			
-
-// new version: accepts any order (28.11.23/FB):
-
-proxDist:	(DISTOPTS|DISTVALUE)+ 
-
-		-> {c2ps_opPROX.encode($proxDist.text, DIST)};
-
-
-//+++++++++ new version: accepts options in any order (28.11.23/FB): +++++++ //
-	// -> {$v.tree != null && $m.tree != null} ? ^(DIST DIST); //{c2ps_opPROX.encodeDIST(DIR, $d.tree, $m.tree, $v.tree)} );
-/*
-proxDist:	(m=proxMeasure|d=proxDirection|v=proxDistValue)+
-		      
-		->  {c2ps_opPROX.encodeDIST(DIST, $m.tree, $m.tree, $m.tree)};
-*/
-/* old rule for optional direction with default setting:
-proxDirection:
-		(p='+'|m='-')?	-> {$p != null}? ^(DIR PLUS)
-						-> {$m != null}? ^(DIR MINUS)
-						->               ^(DIR BOTH) ;
-*/
-
-// new rule accepts + and -. Default tree for direction is set in c2ps_opPROX.encode():
+// new rule: accepts options in any order:
+// but how to handle multiple values for those options?
 // 28.11.23/FB
-/*
+
+proxDist:	(m+=proxMeasure|d=proxDirection|v=proxDistValue)+
+		      
+		->  {c2ps_opPROX.encodeDIST(DIST, DIR, $d.tree, $m.tree, $v.tree, $proxDist.text)};
+
+// new rule accepts only '+' and '-'; default tree for direction is 
+// set in c2ps_opPROX.encodeDIST() now.
+// 28.11.23/FB
+
 proxDirection
 		: '+'	-> ^(DIR PLUS)
 		| '-'	-> ^(DIR MINUS);
-*/
-/*
-proxDistValue	// proxDistMin ( ':' proxDistMax)? ;
-	:	(m1=proxDistMin -> ^(DIST_RANGE VAL0 $m1)) (':' m2=proxDistMax -> ^(DIST_RANGE $m1 $m2))? ;
-*/
 
-// proxDistMin ( ':' proxDistMax)? ;
-/*
-proxDistValue	
-	:	(m1=proxDistMin ) (':' m2=proxDistMax)? 
+proxDistValue	:	(m1=proxDistMin ) (':' m2=proxDistMax)? 
 	
 		-> {$m2.text != null}? ^(RANGE $m1  $m2)
-		->		       ^(RANGE VAL0 $m1);
-*/
-/*
+		->				       ^(RANGE VAL0 $m1);
+
 proxMeasure
 	:	(m='w'|m='s'|m='p'|m='t') -> ^(MEAS $m);
 
@@ -122,7 +82,7 @@
 	
 proxDistMax
 	:	DISTVALUE;
-*/	
+	
 proxGroup
 	:	'min' -> ^(GRP MIN)
 	|	'max' -> ^(GRP MAX);
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 779902e..66daf0d 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
@@ -11,41 +11,7 @@
 
 {
 
-	/* encode():
-	 * - encodes Distance type, Direction and Distance value
-	 *   which are written in any order.
-	 * 28.11.23/FB
-	 */
-	
-	public static Tree encode(String input, int type)
-	{
-		StringBuffer sb = new StringBuffer("(DIST (DIR MINUS) (RANGE VAL0 0) (MEAS w))");
-		System.err.printf("Debug: encode: input = '%s' output = >>%s<<.\n", input, sb.toString());
-		CommonTree ctree = new CommonTree(new CommonToken(type, sb.toString()));
-		//CommonTree treeType = new CommonTree(new CommonToken(1, ""))
-		//CommonToken ct = ct.
-		System.err.printf("Debug: encode: CommonTree : '%s'.\n", ctree.toStringTree());
-		//return new CommonTree(new CommonToken(type, sb.toString()));
-		return ctree;
-	} // encode
-	
-	/* encodeDefaultDir():
-	 * - return a tree containing the default Prox Direction when there is no
-	 *   direction indication in the input query.
-	 * 28.11.23/FB
-	 */
-	
-	public static Tree encodeDefautDir(String input, int type)
-	{
-		StringBuffer sb = new StringBuffer("BOTH");
-		CommonTree tree = new CommonTree(new CommonToken(type, sb.toString()));
-		
-		System.err.printf("Debug: encodeDefaultDir: CommonTree : '%s'.\n", tree.toStringTree());
-
-		return tree;
-	} // encode
-		
-	/* encodeDefaultDir():
+	/* encodeDIST():
 	 * - returns a CommonTree built of out Direction/Measure/Distance value.
 	 * - accepts options in any order.
 	 * - creates CommonTree in that order: Direction .. Distance value .. Measure.
@@ -53,29 +19,46 @@
 	 * 28.11.23/FB
 	 */
 	
-	public static Object encodeDIST(int type, Object ctDir, Object ctMeas, Object ctVal)
+	public static Object encodeDIST(int typeDIST, int typeDIR, Object ctDir, Object ctMeas, Object ctVal, String text)
 	{
-		StringBuffer sb = new StringBuffer("BOTH");
 		CommonTree tree1 = (CommonTree)ctDir;
 		CommonTree tree2 = (CommonTree)ctMeas;
 		CommonTree tree3 = (CommonTree)ctVal;
 		
-		System.err.printf("Debug: encodeDIST: ctDir='%s' ctMeas='%s' ctVal='%s'.\n",
-				tree1 != null ? tree1.toStringTree() : "null",
-				tree2 != null ? tree2.toStringTree() : "null",
-				tree3 != null ? tree3.toStringTree() : "null");
 
+		System.err.printf("Debug: encodeDIST: scanned input='%s'.\n", text);
+		
+		System.err.printf("Debug: encodeDIST: ctDir='%s': %d ctMeas='%s': %d ctVal='%s': %d.\n",
+				tree1 != null ? tree1.toStringTree() : "null",
+				tree1 != null ? tree1.getChildCount() : 0,
+				tree2 != null ? tree2.toStringTree() : "null",
+				tree2 != null ? tree2.getChildCount() : 0,
+				tree3 != null ? tree3.toStringTree() : "null",
+				tree3 != null ? tree3.getChildCount() : 0);
+
+		// if direction is not specified, return default = BOTH:
 		if( ctDir == null )
 			{
-				
+			CommonTree treeDIR = new CommonTree(new CommonToken(typeDIR, (String)"DIR"));
+			//CommonToken tok = new CommonToken(typeDIR, "BOTH");
+			CommonTree treeBOTH = new CommonTree(new CommonToken(typeDIR, "BOTH"));
+			treeDIR.addChild(treeBOTH);
+			
+			System.err.printf("Debug: encodeDIST: tree for DIR: '%s'.\n", 
+					treeDIR.toStringTree());
+			tree1 = treeDIR;
 			}
+		
 		CommonTree 
-			tree = new CommonTree(new CommonToken(type, "DIST"));
+			tree = new CommonTree(new CommonToken(typeDIST, "DIST"));
 		
 		tree.addChild(tree1);
-		tree.addChild(tree3); // tree3 before tree2.
+		tree.addChild(tree3); // tree3 before tree2 expected by serialization.
 		tree.addChild(tree2);
 		
+		System.err.printf("Debug: encodeDIST: returning '%s'.\n", 
+				tree.toStringTree());
+		
 		return tree;
 	} // encodeDIST