Tune the Remakefile to re-run tests when the binary changes.
[tatoo.git] / src / tatoo.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
9 (*  Recherche Scientifique. All rights reserved.  This file is         *)
10 (*  distributed under the terms of the GNU Lesser General Public       *)
11 (*  License, with the special exception on linking described in file   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 (*
17   Time-stamp: <Last modified on 2013-04-22 18:48:48 CEST by Kim Nguyen>
18 *)
19
20 open Format
21
22 let time f arg msg =
23   let t1 = Unix.gettimeofday () in
24   let r = f arg in
25   let t2 = Unix.gettimeofday () in
26   let time = (t2 -. t1) *. 1000. in
27   if !Options.stats then fprintf err_formatter "@[STATS: %s: %fms@]@." msg time;
28   r
29
30
31 let main () =
32   let () = Options.parse () in
33   let doc =
34     let fd = open_in !Options.input_file in
35     let d = time Naive_tree.load_xml_file fd "parsing xml document" in
36     close_in fd; d
37   in
38   let query =
39     time
40       Xpath.Parser.parse
41       (Ulexing.from_latin1_string !Options.query)
42       "parsing XPath query"
43   in
44   let auto =
45     time Xpath.Compile.path query "compiling XPath query"
46   in
47   let output =
48     match !Options.output_file with
49     | None | Some "-" | Some "/dev/stdout" -> stdout
50     | Some f -> open_out f
51   in
52   if !Options.stats then begin
53     fprintf err_formatter "@[STATS: Query: %a @]@." Xpath.Ast.print_path query;
54     fprintf err_formatter "@[STATS: @[Automaton: @\n";
55     Ata.print err_formatter auto;
56     fprintf err_formatter "@]@]@.";
57   end;
58
59   let module Naive = Eval.Make(Naive_tree) in
60   let results =
61     time (Naive.eval auto doc) (Naive_tree.root doc) "evaluating query"
62   in
63   time (fun () ->
64   output_string output "<xml_result>\n";
65   if !Options.count then begin
66     output_string output (string_of_int (List.length results));
67     output_char output '\n';
68   end else
69     List.iter (fun n ->
70       Naive_tree.print_xml output doc n;
71       output_char output '\n'
72     ) results;
73   output_string output "</xml_result>\n";
74   flush output;
75   if output != stdout then close_out output
76  
77 ) () "serializing results"
78
79
80 let () =
81   try
82     main ()
83   with
84     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
85   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
86   | Tree.Parse_error msg -> eprintf "Error: file %s, %s\n%!" !Options.input_file msg; exit 3
87   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
88   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128