]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/lexicon/lexiconMarshal.ml
Huge reorganization of matita and ocaml.
[helm.git] / helm / ocaml / lexicon / lexiconMarshal.ml
1 (* Copyright (C) 2005, 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 exception Checksum_failure of string
27 exception Corrupt_lexicon of string
28 exception Version_mismatch of string
29
30 type lexicon = LexiconAst.command list
31
32 let marshal_flags = []
33
34 let rehash_cmd_uris =
35   let rehash_uri uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
36   function
37   | LexiconAst.Interpretation (loc, dsc, args, cic_appl_pattern) ->
38       let rec aux =
39         function
40         | CicNotationPt.UriPattern uri ->
41             CicNotationPt.UriPattern (rehash_uri uri)
42         | CicNotationPt.ApplPattern args ->
43             CicNotationPt.ApplPattern (List.map aux args)
44         | CicNotationPt.VarPattern _
45         | CicNotationPt.ImplicitPattern as pat -> pat
46       in
47       let appl_pattern = aux cic_appl_pattern in
48       LexiconAst.Interpretation (loc, dsc, args, appl_pattern)
49   | LexiconAst.Notation _
50   | LexiconAst.Alias _ as cmd -> cmd
51   | cmd ->
52       prerr_endline "Found a command not expected in a .lexicon:";
53       prerr_endline (LexiconAstPp.pp_command cmd);
54       assert false
55
56 (** .lexicon file format
57  * - an integer -- magic number -- denoting the version of the dumped data
58  *   structure. Different magic numbers stand for incompatible data structures
59  * - an integer -- checksum -- denoting the hash value (computed with
60  *   Hashtbl.hash) of the string representation of the dumped data structur
61  * - marshalled data: list of LexiconAst.command
62  *)
63
64 let save_lexicon ~fname lexicon =
65  let ensure_path_exists path =
66    let dir = Filename.dirname path in
67    HExtlib.mkdir dir
68  in
69  ensure_path_exists fname;
70  let oc = open_out fname in
71  let marshalled = Marshal.to_string (List.rev lexicon) marshal_flags in
72  let checksum = Hashtbl.hash marshalled in
73  output_binary_int oc LexiconAst.magic;
74  output_binary_int oc checksum;
75  output_string oc marshalled;
76  close_out oc
77
78 let load_lexicon ~fname =
79   let ic = open_in fname in
80   HExtlib.finally
81     (fun () -> close_in ic)
82     (fun () ->
83       try
84         let lexicon_magic = input_binary_int ic in
85         if lexicon_magic <> LexiconAst.magic then raise (Version_mismatch fname);
86         let lexicon_checksum = input_binary_int ic in
87         let marshalled = HExtlib.input_all ic in
88         let checksum = Hashtbl.hash marshalled in
89         if checksum <> lexicon_checksum then raise (Checksum_failure fname);
90         let (lexicon:lexicon) =
91           Marshal.from_string marshalled 0
92         in
93         List.map rehash_cmd_uris lexicon
94       with End_of_file -> raise (Corrupt_lexicon fname))
95     ()
96