]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/xml.ml
Initial revision
[helm.git] / helm / interface / xml.ml
1 (******************************************************************************)
2 (*                                                                            *)
3 (*                               PROJECT HELM                                 *)
4 (*                                                                            *)
5 (*                     A tactic to print Coq objects in XML                   *)
6 (*                                                                            *)
7 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
8 (*                                 18/10/2000                                 *)
9 (*                                                                            *)
10 (* This module defines a pretty-printer and the stream of commands to the pp  *)
11 (*                                                                            *)
12 (******************************************************************************)
13
14
15 (* the type token for XML cdata, empty elements and not-empty elements *)
16 (* Usage:                                                                *)
17 (*  Str cdata                                                            *)
18 (*  Empty (element_name, [attrname1, value1 ; ... ; attrnamen, valuen]   *)
19 (*  NEmpty (element_name, [attrname1, value2 ; ... ; attrnamen, valuen], *)
20 (*          content                                                      *)
21 type token = Str of string
22            | Empty of string * (string * string) list
23            | NEmpty of string * (string * string) list * token Stream.t
24 ;;
25
26 (* currified versions of the constructors make the code more readable *)
27 let xml_empty name attrs = [< 'Empty(name,attrs) >]
28 let xml_nempty name attrs content = [< 'NEmpty(name,attrs,content) >]
29 let xml_cdata str = [< 'Str str >]
30
31 (* Usage:                                                                   *)
32 (*  pp tokens None     pretty prints the output on stdout                   *)
33 (*  pp tokens (Some filename) pretty prints the output on the file filename *)
34 let pp strm fn =
35  let channel = ref stdout in
36  let rec pp_r m =
37   parser
38     [< 'Str a ; s >] ->
39       print_spaces m ;
40       fprint_string (a ^ "\n") ;
41       pp_r m s
42   | [< 'Empty(n,l) ; s >] ->
43       print_spaces m ;
44       fprint_string ("<" ^ n) ;
45       List.iter (function (n,v) -> fprint_string (" " ^ n ^ "=\"" ^ v ^ "\"")) l;
46       fprint_string "/>\n" ;
47       pp_r m s
48   | [< 'NEmpty(n,l,c) ; s >] ->
49       print_spaces m ;
50       fprint_string ("<" ^ n) ;
51       List.iter (function (n,v) -> fprint_string (" " ^ n ^ "=\"" ^ v ^ "\"")) l;
52       fprint_string ">\n" ;
53       pp_r (m+1) c ;
54       print_spaces m ;
55       fprint_string ("</" ^ n ^ ">\n") ;
56       pp_r m s
57   | [< >] -> ()
58  and print_spaces m =
59   for i = 1 to m do fprint_string "  " done
60  and fprint_string str =
61   output_string !channel str
62  in
63   match fn with
64      Some filename ->
65        channel := open_out filename ;
66        pp_r 0 strm ;
67        close_out !channel ;
68        print_string ("\nWriting on file \"" ^ filename ^ "\" was succesfull\n");
69        flush stdout
70    | None ->
71        pp_r 0 strm
72 ;;