]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/make_table.ml
first moogle template checkin
[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 let debug = false
31 let debug_print s = if debug then prerr_endline s
32
33   (* source files for tables xml parsing (if unmarshall=false) *)
34 let xml_tables = [
35 (*
36   `Entities, "/usr/share/gtkmathview/entities-table.xml";
37   `Dictionary, "/usr/share/editex/dictionary-tex.xml"
38 *)
39   `Entities, "macros/entities-table.xml";
40   `Dictionary, "macros/dictionary-tex.xml";
41   `Entities, "macros/extra-entities.xml";
42 ]
43
44
45 let rec find_first_tag pull_parser =
46   match pull_parser () with
47   | Some (E_start_tag _ as e) -> e
48   | None -> assert false
49   | _ -> find_first_tag pull_parser
50
51 let iter_entities_file f pull_parser =
52   ignore (find_first_tag pull_parser); (* <entities-table> *)
53   let rec aux () =
54     match pull_parser () with
55     | Some (E_start_tag ("entity", attrs, _)) ->
56         (try
57           let name = List.assoc "name" attrs in
58           let value = List.assoc "value" attrs in
59           f name value
60         with Not_found -> ());
61         aux ()
62     | None -> ()
63     | _ -> aux ()
64   in
65   aux ()
66
67 let iter_dictionary_file f pull_parser =
68   ignore (find_first_tag pull_parser); (* <dictionary> *)
69   let rec aux () =
70     match pull_parser () with
71     | Some (E_start_tag ("entry", attrs, _)) ->
72         (try
73           let name = List.assoc "name" attrs in
74           let value = List.assoc "val" attrs in
75           f name value
76         with Not_found -> ());
77         aux ()
78     | None -> ()
79     | _ -> aux ()
80   in
81   aux ()
82
83 let parse_from_xml () =
84   let (macro2utf8, utf82macro) = (Hashtbl.create 2000, Hashtbl.create 2000) in
85   let add_macro macro utf8 =
86     debug_print (sprintf "Adding macro %s = '%s'" macro utf8);
87     Hashtbl.add macro2utf8 macro utf8;
88 (*     Hashtbl.add utf82macro utf8 macro *)
89   in
90   let fill_table () =
91     List.iter
92       (fun (typ, fname) ->
93         let entry = `Entry_document [ `Extend_dtd_fully; `Parse_xml_decl ] in
94         let config = PxpHelmConf.pxp_config in
95         let entity_manager =
96           create_entity_manager ~is_document:true config (from_file fname)
97         in
98         let pull_parser = create_pull_parser config entry entity_manager in
99         match typ with
100         | `Entities -> iter_entities_file add_macro pull_parser
101         | `Dictionary -> iter_dictionary_file add_macro pull_parser)
102       xml_tables
103   in
104   fill_table ();
105   macro2utf8
106
107 let main () =
108   let oc = open_out Sys.argv.(1) in
109   output_string oc "(* GENERATED by make_table: DO NOT EDIT! *)\n";
110   output_string oc "let macro2utf8 = Hashtbl.create 2000\n";
111   let macro2utf8 = parse_from_xml () in
112   Hashtbl.iter
113     (fun macro utf8 ->
114       fprintf oc "let _ = Hashtbl.add macro2utf8 \"%s\" \"%s\"\n"
115         macro (String.escaped utf8))
116     macro2utf8;
117   close_out oc
118
119 let _ = main ()
120