typo test.ml + Why the old asta is taken for building Compil.trans qu ?
[tatoo.git] / src / test.ml
index 8b69fd8..42dc011 100644 (file)
 (***********************************************************************)
 
 
-(** use: xml_file "XPath querie"
-    or : xml_file -f XPath_querie_file
-    only the first line of XPath_querie_file is read 
+(** use: [./test xml_file "XPath querie"]
+    or : [./test xml_file -f XPath_querie_file]
+    only the first line of [XPath_querie_file] is read 
 *)
 
+open Format
 
-let doc =
+let doc () =
   let fd = open_in Sys.argv.(1) in
   let d = Tree.load_xml_file fd in
-  close_in fd; d
-
+  close_in fd;
+  fprintf err_formatter "Parse Tree OK ! ";
+  d
 
 
-let query = 
+let query () 
   let arg2 = Sys.argv.(2) in
   if arg2 = "-f"
   then  let fq = open_in Sys.argv.(3) in
-       let q = XPath.parse_file fq in
-       close_in fq; q
-  else XPath.parse_string arg2
+        let q = XPath.parse_file fq in
+        close_in fq;
+        fprintf err_formatter "Parse query OK ! ";
+        q
+  else let q = XPath.parse_string arg2 in
+       fprintf err_formatter "Parse query OK ! ";
+       q
 
-open Format
+let build_asta query =
+  let asta = Compil.trans query in
+  fprintf err_formatter "Compil OK ! ";
+  asta
 
-let asta = Compil.trad query
+let compute_run doc query = 
+  let run = Run.compute doc query in
+  fprintf err_formatter "Run OK ! \n";
+  run
 
 let () =
-  fprintf err_formatter "Query: %a\n%!" XPath.Ast.print query;
-  fprintf err_formatter "Asta: %a\n%!" Asta.print asta;
-  fprintf err_formatter "Document:\n%!";
+  let doc = doc () in
+  let query = query () in
+  let asta = build_asta query in
+  let run = compute_run doc asta in
+  let selected_nodes = Run.selected_nodes doc asta in
+  Format.pp_set_margin err_formatter 80;
+  fprintf err_formatter "@[<v 0>##### Query #####@.  %a@]\n"
+    XPath.Ast.print query;
+  output_string stderr "\n##### Doc #####\n";
   Tree.print_xml stderr doc (Tree.root doc);
+  output_string stderr "\n";
+  Asta.print err_formatter asta;
+  Run.print err_formatter run;
+  output_string stderr "\n  # Doc with positions: \n";
+  Tree.print_xml_preorder stderr doc (Tree.root doc);
+  let rec print_selec fmt l = match l with
+    | [x] -> fprintf fmt "%s" (string_of_int x)
+    | x :: tl -> fprintf fmt "%s" ((string_of_int x)^"; ");print_selec fmt tl
+    | [] -> fprintf fmt "%s" "ΓΈ" in
+  fprintf err_formatter "@.@.  # Selected nodes: {%a}@."
+    print_selec selected_nodes;
   exit 0
-