]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_disambiguation/make_table.ml
new experimental cic textual parser: checkin
[helm.git] / helm / ocaml / cic_disambiguation / make_table.ml
diff --git a/helm/ocaml/cic_disambiguation/make_table.ml b/helm/ocaml/cic_disambiguation/make_table.ml
new file mode 100644 (file)
index 0000000..770fe2b
--- /dev/null
@@ -0,0 +1,88 @@
+
+open Printf
+open Pxp_types
+open Pxp_ev_parser
+
+(* Usage: make_table <hashtbls_dump_file> *)
+
+let debug = false
+let debug_print s = if debug then prerr_endline s
+
+let tables = [
+(*
+  `Entities, "/usr/share/gtkmathview/entities-table.xml";
+  `Dictionary, "/usr/share/editex/dictionary-tex.xml"
+*)
+  `Entities, "macros/entities-table.xml";
+  `Dictionary, "macros/dictionary-tex.xml";
+  `Entities, "macros/extra-entities.xml";
+]
+
+let macro2utf8 = Hashtbl.create 2000
+let utf82macro = Hashtbl.create 2000
+
+let add_macro macro utf8 =
+  debug_print (sprintf "Adding macro %s = '%s'" macro utf8);
+  Hashtbl.add macro2utf8 macro utf8;
+  Hashtbl.add utf82macro utf8 macro
+
+let rec find_first_tag pull_parser =
+  match pull_parser () with
+  | Some (E_start_tag _ as e) -> e
+  | None -> assert false
+  | _ -> find_first_tag pull_parser
+
+let iter_entities_file f pull_parser =
+  ignore (find_first_tag pull_parser); (* <entities-table> *)
+  let rec aux () =
+    match pull_parser () with
+    | Some (E_start_tag ("entity", attrs, _)) ->
+       (try
+         let name = List.assoc "name" attrs in
+         let value = List.assoc "value" attrs in
+         f name value
+       with Not_found -> ());
+       aux ()
+    | None -> ()
+    | _ -> aux ()
+  in
+  aux ()
+
+let iter_dictionary_file f pull_parser =
+  ignore (find_first_tag pull_parser); (* <dictionary> *)
+  let rec aux () =
+    match pull_parser () with
+    | Some (E_start_tag ("entry", attrs, _)) ->
+       (try
+         let name = List.assoc "name" attrs in
+         let value = List.assoc "val" attrs in
+         f name value
+       with Not_found -> ());
+       aux ()
+    | None -> ()
+    | _ -> aux ()
+  in
+  aux ()
+
+let fill_table () =
+  List.iter
+    (fun (typ, fname) ->
+      let entry = `Entry_document [ `Extend_dtd_fully; `Parse_xml_decl ] in
+      let config = { default_config with encoding = `Enc_utf8 } in
+      let entity_manager =
+       create_entity_manager ~is_document:true config (from_file fname)
+      in
+      let pull_parser = create_pull_parser config entry entity_manager in
+      match typ with
+      | `Entities -> iter_entities_file add_macro pull_parser
+      | `Dictionary -> iter_dictionary_file add_macro pull_parser)
+    tables
+
+let main () =
+  let oc = open_out Sys.argv.(1) in
+  fill_table ();
+  Marshal.to_channel oc (macro2utf8, utf82macro) [];
+  close_out oc
+
+let _ = main ()
+