]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/matitaWiki.ml
huge commit regarding the grafite_status:
[helm.git] / helm / software / matita / matitaWiki.ml
1 (* Copyright (C) 2004-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 (* $Id: matitacLib.ml 7090 2006-12-12 14:04:59Z fguidi $ *)
27
28 open Printf
29
30 open GrafiteTypes
31
32 exception AttemptToInsertAnAlias
33
34
35 (** {2 Initialization} *)
36
37 let grafite_status = (ref [] : GrafiteTypes.status list ref)
38
39 let run_script is eval_function  =
40   let grafite_status' = 
41     match !grafite_status with
42     | s::_ -> s
43     | _ -> assert false
44   in
45   let cb = fun _ _ -> () in
46   let matita_debug = Helm_registry.get_bool "matita.debug" in
47   try
48    match eval_function grafite_status' is cb with
49       [] -> raise End_of_file
50     | (grafite_status'',None)::_ ->
51        grafite_status := grafite_status''::!grafite_status
52     | (s,Some _)::_ -> raise AttemptToInsertAnAlias
53   with
54   | GrafiteEngine.Drop  
55   | End_of_file
56   | CicNotationParser.Parse_error _
57   | HExtlib.Localized _ as exn -> raise exn
58   | exn -> 
59       if not matita_debug then
60        HLog.error (snd (MatitaExcPp.to_string exn)) ;
61       raise exn
62
63 let clean_exit n =
64   match !grafite_status with
65      [] -> exit n
66    | grafite_status::_ ->
67        let baseuri = GrafiteTypes.get_baseuri grafite_status in
68        LibraryClean.clean_baseuris ~verbose:false [baseuri];
69        exit n
70
71 let terminate n =
72  let terminator = String.make 1 (Char.chr 249) in
73  let prompt =
74   (match n with
75      None -> "-1"
76    | Some n -> string_of_int n) ^ terminator
77  in
78   print_endline prompt;
79   prerr_endline prompt
80 ;;
81
82 let outer_syntax_parser () =
83  let text = ref "" in
84  let tag = ref `Do in
85  let callbacks =
86   { XmlPushParser.default_callbacks with
87     XmlPushParser.start_element =
88      (Some
89        (fun name attrs ->
90          match name with
91             "pgip" -> ()
92           | "doitem" -> tag := `Do
93           | "undoitem" -> tag := `Undo
94           | _ -> assert false)) ;
95     XmlPushParser.character_data =
96      (Some (fun s -> text := !text ^ s)) ;
97     XmlPushParser.end_element =
98      (Some
99        (function
100            "pgip" -> raise (XmlPushParser.Parse_error "EOC")
101          | "doitem"
102          | "undoitem" -> ()
103          | _ -> assert false))
104   }
105  in
106   let parse = XmlPushParser.create_parser callbacks in
107     try
108      XmlPushParser.parse parse (`Channel stdin) ;
109      raise End_of_file
110     with
111        XmlPushParser.Parse_error "no element found" -> raise End_of_file
112      | XmlPushParser.Parse_error "EOC" ->
113         match !tag with
114            `Do -> `Do !text
115          | `Undo ->
116              try
117               `Undo (int_of_string !text)
118              with
119               Failure _ -> assert false
120 ;;
121
122 let include_paths =
123  lazy (Helm_registry.get_list Helm_registry.string "matita.includes")
124 ;;
125   
126 let rec interactive_loop () = 
127   (* every loop is terminated by a terminator both on stdout and stderr *)
128   let interactive_loop n = terminate n; interactive_loop () in
129   try
130    match outer_syntax_parser () with
131       `Undo n ->
132         let rec drop =
133          function
134             0,l -> l
135           | n,_::l -> drop (n-1,l)
136           | _,[] -> assert false
137         in
138          let to_be_dropped = List.length !grafite_status - n in
139          let safe_hd = function [] -> assert false | he::_ -> he in
140          let cur_grafite_status = safe_hd !grafite_status in
141           grafite_status := drop (to_be_dropped, !grafite_status) ;
142           let grafite_status = safe_hd !grafite_status in
143            LexiconSync.time_travel
144             ~present:(GrafiteTypes.get_lexicon cur_grafite_status)
145             ~past:(GrafiteTypes.get_lexicon grafite_status);
146            GrafiteSync.time_travel
147             ~present:cur_grafite_status ~past:grafite_status;
148            interactive_loop (Some n)
149     | `Do command ->
150         let str = Ulexing.from_utf8_string command in
151         let watch_statuses grafite_status =
152          match grafite_status.GrafiteTypes.proof_status with
153             GrafiteTypes.Incomplete_proof
154              {GrafiteTypes.proof = uri,metasenv,_subst,bo,ty,attrs ;
155               GrafiteTypes.stack = stack } ->
156               let open_goals = Continuationals.Stack.open_goals stack in
157               print_endline
158                (String.concat "\n"
159                  (List.map
160                    (fun i ->
161                      ApplyTransformation.txt_of_cic_sequent 80 metasenv
162                       ~map_unicode_to_tex:(Helm_registry.get_bool
163                         "matita.paste_unicode_as_tex")
164                       (List.find (fun (j,_,_) -> j=i) metasenv)
165                    ) open_goals))
166           | _ -> ()
167         in
168          run_script str 
169            (MatitaEngine.eval_from_stream ~first_statement_only:true 
170            ~include_paths:(Lazy.force include_paths) ~watch_statuses) ;
171          interactive_loop (Some (List.length !grafite_status))
172   with 
173    | GrafiteEngine.Macro (floc,_) ->
174       let x, y = HExtlib.loc_of_floc floc in
175        HLog.error
176         (sprintf "A macro has been found in a script at %d-%d" x y);
177        interactive_loop None
178    | Sys.Break -> HLog.error "user break!"; interactive_loop None
179    | GrafiteTypes.Command_error _ -> interactive_loop None
180    | HExtlib.Localized (floc,CicNotationParser.Parse_error err) ->
181       let x, y = HExtlib.loc_of_floc floc in
182        HLog.error (sprintf "Parse error at %d-%d: %s" x y err);
183        interactive_loop None
184    | End_of_file as exn -> raise exn
185    | exn -> HLog.error (Printexc.to_string exn); interactive_loop None
186
187
188 let main () = 
189   MatitaInit.initialize_all ();
190   HLog.set_log_callback
191    (fun tag msg ->
192      let s =
193       match tag with
194          `Debug -> "<div style='color:blue'>Debug: " ^ msg ^ "</div><br/>\n"
195        | `Message -> "<div style='color:green'>Info: " ^ msg ^ "</div><br/>\n"
196        | `Warning -> "<div style='color:yellow'>Warn: " ^ msg ^ "</div><br/>\n"
197        | `Error -> "<div style='color:red'>Error: " ^ msg ^ "</div><br/>\n"
198      in
199       output_string stderr s;
200       flush stderr
201    );
202   (* must be called after init since args are set by cmdline parsing *)
203   let system_mode =  Helm_registry.get_bool "matita.system" in
204   let include_paths =
205    Helm_registry.get_list Helm_registry.string "matita.includes" in
206   grafite_status := [GrafiteSync.init (CicNotation2.load_notation ~include_paths
207      BuildTimeConf.core_notation_script) "cic:/matita/tests/"];
208   Sys.catch_break true;
209   let origcb = HLog.get_log_callback () in
210   let origcb t s = origcb t ((if system_mode then "[S] " else "") ^ s) in
211   let newcb tag s =
212     match tag with
213     | `Debug -> ()
214     | `Message | `Warning | `Error -> origcb tag s
215   in
216   HLog.set_log_callback newcb;
217   let matita_debug = Helm_registry.get_bool "matita.debug" in
218   try
219     (try
220       interactive_loop ()
221      with
222       | End_of_file -> ()
223       | GrafiteEngine.Drop -> clean_exit 1
224     );
225     let proof_status,moo_content_rev,lexicon_content_rev = 
226       match !grafite_status with
227       |  s::_ ->
228          s.proof_status, s.moo_content_rev,
229           (GrafiteTypes.get_lexicon s).LexiconEngine.lexicon_content_rev
230       | _ -> assert false
231     in
232     if proof_status <> GrafiteTypes.No_proof then
233      begin
234       HLog.error
235        "there are still incomplete proofs at the end of the script";
236       clean_exit 2
237      end
238     else
239      begin
240        let baseuri =
241         GrafiteTypes.get_baseuri 
242            (match !grafite_status with
243              [] -> assert false
244            | s::_ -> s)
245        in
246        let moo_fname = 
247          LibraryMisc.obj_file_of_baseuri 
248            ~must_exist:false ~baseuri ~writable:true 
249        in
250        let lexicon_fname= 
251          LibraryMisc.lexicon_file_of_baseuri 
252           ~must_exist:false ~baseuri ~writable:true 
253        in
254        GrafiteMarshal.save_moo moo_fname moo_content_rev;
255        LexiconMarshal.save_lexicon lexicon_fname lexicon_content_rev;
256        exit 0
257      end
258   with 
259   | exn ->
260       if matita_debug then raise exn;
261       clean_exit 3