]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/make_table.ml
still a working copy, now towards a cleaner implementation ...
[helm.git] / helm / ocaml / cic_disambiguation / make_table.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27 open Pxp_types
28 open Pxp_ev_parser
29
30 (* Usage: make_table <hashtbls_dump_file> *)
31
32 let debug = false
33 let debug_print s = if debug then prerr_endline s
34
35 let tables = [
36 (*
37   `Entities, "/usr/share/gtkmathview/entities-table.xml";
38   `Dictionary, "/usr/share/editex/dictionary-tex.xml"
39 *)
40   `Entities, "macros/entities-table.xml";
41   `Dictionary, "macros/dictionary-tex.xml";
42   `Entities, "macros/extra-entities.xml";
43 ]
44
45 let macro2utf8 = Hashtbl.create 2000
46 let utf82macro = Hashtbl.create 2000
47
48 let add_macro macro utf8 =
49   debug_print (sprintf "Adding macro %s = '%s'" macro utf8);
50   Hashtbl.add macro2utf8 macro utf8;
51   Hashtbl.add utf82macro utf8 macro
52
53 let rec find_first_tag pull_parser =
54   match pull_parser () with
55   | Some (E_start_tag _ as e) -> e
56   | None -> assert false
57   | _ -> find_first_tag pull_parser
58
59 let iter_entities_file f pull_parser =
60   ignore (find_first_tag pull_parser); (* <entities-table> *)
61   let rec aux () =
62     match pull_parser () with
63     | Some (E_start_tag ("entity", attrs, _)) ->
64         (try
65           let name = List.assoc "name" attrs in
66           let value = List.assoc "value" attrs in
67           f name value
68         with Not_found -> ());
69         aux ()
70     | None -> ()
71     | _ -> aux ()
72   in
73   aux ()
74
75 let iter_dictionary_file f pull_parser =
76   ignore (find_first_tag pull_parser); (* <dictionary> *)
77   let rec aux () =
78     match pull_parser () with
79     | Some (E_start_tag ("entry", attrs, _)) ->
80         (try
81           let name = List.assoc "name" attrs in
82           let value = List.assoc "val" attrs in
83           f name value
84         with Not_found -> ());
85         aux ()
86     | None -> ()
87     | _ -> aux ()
88   in
89   aux ()
90
91 let fill_table () =
92   List.iter
93     (fun (typ, fname) ->
94       let entry = `Entry_document [ `Extend_dtd_fully; `Parse_xml_decl ] in
95       let config = { default_config with encoding = `Enc_utf8 } in
96       let entity_manager =
97         create_entity_manager ~is_document:true config (from_file fname)
98       in
99       let pull_parser = create_pull_parser config entry entity_manager in
100       match typ with
101       | `Entities -> iter_entities_file add_macro pull_parser
102       | `Dictionary -> iter_dictionary_file add_macro pull_parser)
103     tables
104
105 let main () =
106   let oc = open_out Sys.argv.(1) in
107   fill_table ();
108   Marshal.to_channel oc (macro2utf8, utf82macro) [];
109   close_out oc
110
111 let _ = main ()
112