opPROX: correcting order of Prox options: WIP.

Change-Id: I7976484475b2f9116c7df7feed43b777b05bc713
diff --git a/src/main/antlr/cosmas/c2ps_opPROX.g b/src/main/antlr/cosmas/c2ps_opPROX.g
index 257ee95..e54ad5e 100644
--- a/src/main/antlr/cosmas/c2ps_opPROX.g
+++ b/src/main/antlr/cosmas/c2ps_opPROX.g
@@ -16,6 +16,7 @@
 	  MEAS; // measure
 	  DIR; PLUS; MINUS; BOTH;
 	  GRP; MIN; MAX; }
+	  
 @header {package de.ids_mannheim.korap.query.parse.cosmas;}
 @lexer::header {package de.ids_mannheim.korap.query.parse.cosmas;}
 
@@ -39,25 +40,53 @@
 		
 		-> ^(PROX_OPTS {$proxTyp.tree} ^(DIST_LIST proxDist+) {$proxGroup.tree});
 	
-proxTyp	:	  '/' -> ^(TYP PROX)	// klassischer Abstand.
-		| '%' -> ^(TYP EXCL);	// ausschließender Abstand.
+proxTyp	:  '/' -> ^(TYP PROX)	// klassischer Abstand.
+		|  '%' -> ^(TYP EXCL);	// ausschließender Abstand.
 
 // proxDist: e.g. +5w or -s0 or /w2:4 etc.
 // kein proxDirection? hier, weil der Default erst innerhalb von Regel proxDirection erzeugt werden kann.
+/* incomplete original version:
 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});
+*/
 
+// new version: accepts any order (28.11.23/FB):
+
+/*
+proxDist:	('s'|'w'|'p'|'t'|'+'|'-'|DISTVALUE)+ 
+
+		-> {c2ps_opPROX.encode($proxDist.text, DIST)};
+*/
+
+// new version: just use the grammar another way (28.11.23/FB):
+
+proxDist:	(d=proxDirection|v=proxDistValue|m=proxMeasure)+
+		//-> {$v.tree != null && $m.tree != null} ? ^(DIST DIST); //{c2ps_opPROX.encodeDIST(DIR, $d.tree, $m.tree, $v.tree)} );
+		->  {c2ps_opPROX.encodeDIST(DIST, $d.tree, $m.tree, $v.tree)};
+
+/* old rule for optional direction with default setting:
 proxDirection:
 		(p='+'|m='-')?	-> {$p != null}? ^(DIR PLUS)
 						-> {$m != null}? ^(DIR MINUS)
 						->               ^(DIR BOTH) ;
+*/
+
+// new rule with default setting. Default tree for direction set in c2ps_opPROX.encode():
+// 28.11.23/FB
+
+proxDirection
+		: '+'	-> ^(DIR PLUS)
+		| '-'	-> ^(DIR MINUS);
+
 /*
 proxDistValue	// proxDistMin ( ':' proxDistMax)? ;
 	:	(m1=proxDistMin -> ^(DIST_RANGE VAL0 $m1)) (':' m2=proxDistMax -> ^(DIST_RANGE $m1 $m2))? ;
 */
-proxDistValue	// proxDistMin ( ':' proxDistMax)? ;
+
+// proxDistMin ( ':' proxDistMax)? ;
+proxDistValue	
 	:	(m1=proxDistMin ) (':' m2=proxDistMax)? 
 	
 		-> {$m2.text != null}? ^(RANGE $m1  $m2)
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 2a5b163..af6fbdf 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,7 +11,73 @@
 
 {
 
-    public static Tree check (String input, int index) {
+	/* 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():
+	 * - return a tree containing the default Prox Direction when there is no
+	 *   direction indication in the input query.
+	 * 28.11.23/FB
+	 */
+	
+	public static Object encodeDIST(int type, Object ctDir, Object ctMeas, Object ctVal)
+	{
+		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");
+
+		if( ctDir == null )
+			{
+				
+			}
+		CommonTree 
+			tree = new CommonTree(new CommonToken(type, "DIST"));
+		
+		tree.addChild(tree1);
+		tree.addChild(tree3); // tree3 before tree2.
+		tree.addChild(tree2);
+		
+		return tree;
+	} // encodeDIST
+	
+	public static Tree check (String input, int index) {
         ANTLRStringStream ss = new ANTLRStringStream(input);
         c2ps_opPROXLexer lex = new c2ps_opPROXLexer(ss);
         CommonTokenStream tokens = new CommonTokenStream(lex);