]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite/grafiteMarshal.ml
abstracted pretty printers over inner pretty printing units (terms, lazy terms, and...
[helm.git] / helm / ocaml / grafite / grafiteMarshal.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_moo of string
28 exception Version_mismatch of string
29
30 type ast_command = Cic.obj GrafiteAst.command
31 type moo = ast_command list
32
33 let marshal_flags = []
34
35 let rehash_cmd_uris =
36   let rehash_uri uri = UriManager.uri_of_string (UriManager.string_of_uri uri) in
37   function
38   | GrafiteAst.Default (loc, name, uris) ->
39       let uris = List.map rehash_uri uris in
40       GrafiteAst.Default (loc, name, uris)
41   | GrafiteAst.Interpretation (loc, dsc, args, cic_appl_pattern) ->
42       let rec aux =
43         function
44         | CicNotationPt.UriPattern uri ->
45             CicNotationPt.UriPattern (rehash_uri uri)
46         | CicNotationPt.ApplPattern args ->
47             CicNotationPt.ApplPattern (List.map aux args)
48         | CicNotationPt.VarPattern _
49         | CicNotationPt.ImplicitPattern as pat -> pat
50       in
51       let appl_pattern = aux cic_appl_pattern in
52       GrafiteAst.Interpretation (loc, dsc, args, appl_pattern)
53   | GrafiteAst.Coercion (loc, uri, close) ->
54       GrafiteAst.Coercion (loc, rehash_uri uri, close)
55   | GrafiteAst.Notation _
56   | GrafiteAst.Alias _ as cmd -> cmd
57   | cmd ->
58       prerr_endline "Found a command not expected in a .moo:";
59       let obj_pp _ = assert false in
60       prerr_endline (GrafiteAstPp.pp_command ~obj_pp cmd);
61       assert false
62
63 (** .moo file format
64  * - an integer -- magic number -- denoting the version of the dumped data
65  *   structure. Different magic numbers stand for incompatible data structures
66  * - an integer -- checksum -- denoting the hash value (computed with
67  *   Hashtbl.hash) of the string representation of the dumped data structur
68  * - marshalled data: list of GrafiteAst.command
69  *)
70
71 let save_moo ~fname moo =
72  let oc = open_out fname in
73  let marshalled = Marshal.to_string (List.rev moo) marshal_flags in
74  let checksum = Hashtbl.hash marshalled in
75  output_binary_int oc GrafiteAst.magic;
76  output_binary_int oc checksum;
77  output_string oc marshalled;
78  close_out oc
79
80 let load_moo ~fname =
81   let ic = open_in fname in
82   HExtlib.finally
83     (fun () -> close_in ic)
84     (fun () ->
85       try
86         let moo_magic = input_binary_int ic in
87         if moo_magic <> GrafiteAst.magic then raise (Version_mismatch fname);
88         let moo_checksum = input_binary_int ic in
89         let marshalled = HExtlib.input_all ic in
90         let checksum = Hashtbl.hash marshalled in
91         if checksum <> moo_checksum then raise (Checksum_failure fname);
92         let (moo:moo) =
93           Marshal.from_string marshalled 0
94         in
95         List.map rehash_cmd_uris moo
96       with End_of_file -> raise (Corrupt_moo fname))
97     ()
98