]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/toplevel/top.ml
- bugfix is refreshed state of AutCrg: now we return a fresh state
[helm.git] / helm / software / helena / src / toplevel / top.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.              
9      \ /   This software is distributed as is, NO WARRANTY.              
10       V_______________________________________________________________ *)
11
12 module KF = Filename
13 module KP = Printf
14
15 module U  = NUri
16 module C  = Cps
17 module L  = Log
18 module Y  = Time
19 module P  = Marks
20 module G  = Options
21 module H  = Hierarchy
22 module N  = Layer
23 module E  = Entity
24 module O  = Output
25 module DO = CrgOutput
26 module TD = TxtCrg
27 module AA = AutProcess
28 module AO = AutOutput
29 module AD = AutCrg
30 module XL = XmlLibrary
31 module XD = XmlCrg
32 module BD = BrgCrg
33 module BO = BrgOutput
34 module BR = BrgReduction
35 module BU = BrgUntrusted
36 module BG = BrgGrafite
37 module ZD = BagCrg
38 module ZO = BagOutput
39 module ZT = BagType
40 module ZU = BagUntrusted
41
42 type status = {
43    kst: N.status;
44    tst: TD.status;
45    pst: AA.status;
46    ast: AD.status;
47    ac : AO.counters;
48    dc : DO.counters;
49    bc : BO.counters;
50    zc : ZO.counters;
51    och: out_channel option;
52 }
53
54 let level = 0
55
56 let bag_error st s msg =
57    L.error st.kst ZO.specs (L.Warn s :: msg) 
58
59 let brg_error st s msg =
60    L.error st.kst BR.specs (L.Warn s :: msg)
61
62 let initial_status () = {
63    kst = N.initial_status ();
64    tst = TD.initial_status ();
65    pst = AA.initial_status ();
66    ast = AD.initial_status ();
67    ac  = AO.initial_counters;
68    dc  = DO.initial_counters;
69    bc  = BO.initial_counters;
70    zc  = ZO.initial_counters;
71    och = None;
72 }
73
74 let refresh_status st = {st with
75    kst = N.refresh_status st.kst;
76    tst = TD.refresh_status st.tst;
77    ast = AD.refresh_status st.ast;
78 }
79
80 (* kernel related ***********************************************************)
81
82 type kernel_entity = BrgEntity of Brg.entity
83                    | BagEntity of Bag.entity
84                    | CrgEntity of Crg.entity
85
86 let print_counters st = function
87    | G.V4 -> DO.print_counters C.start st.dc
88    | G.V3 -> BO.print_counters C.start st.bc
89    | G.V0 -> ZO.print_counters C.start st.zc
90
91 let xlate_entity st entity = match !G.kernel, entity with
92    | G.V3, CrgEntity e -> 
93       let f e = (BrgEntity e) in E.xlate f BD.brg_of_crg e
94    | G.V0, CrgEntity e -> 
95       let f e = (BagEntity e) in E.xlate f (ZD.bag_of_crg st.kst) e
96    | _, entity          -> entity
97
98 let pp_progress e =
99    let f _ na u =
100       let s = U.string_of_uri u in
101       L.warn 2 (KP.sprintf "[%u] <%s>" na.E.n_apix s);
102    in
103    Y.utime_stamp "intermediate";
104    match e with
105       | CrgEntity e -> E.common f e
106       | BrgEntity e -> E.common f e
107       | BagEntity e -> E.common f e      
108
109 let count_entity st = function
110    | BrgEntity e -> {st with bc = BO.count_entity C.start st.bc e}
111    | BagEntity e -> {st with zc = ZO.count_entity C.start st.zc e}
112    | CrgEntity e -> {st with dc = DO.count_entity C.start st.dc e}
113
114 let export_entity st = function
115    | CrgEntity e -> XL.export_entity (XD.export_term st.kst) e
116    | BrgEntity e -> XL.export_entity (BO.export_term st.kst) e
117    | BagEntity e -> XL.export_entity (ZO.export_term st.kst) e
118
119 let type_check st k =
120    let brg_err msg = brg_error st "Type Error" msg; failwith "Interrupted" in
121    let bag_err msg = bag_error st "Type Error" msg; failwith "Interrupted" in
122    let ok _ _ = st in
123    match k with
124       | BrgEntity entity -> BU.type_check brg_err ok st.kst entity
125       | BagEntity entity -> ZU.type_check bag_err ok st.kst entity
126       | CrgEntity _      -> st
127
128 let validate st k =
129    let brg_err msg = brg_error st "Validation Error" msg; failwith "Interrupted" in
130    let ok _ = st in
131    match k with
132       | BrgEntity entity -> BU.validate brg_err ok st.kst entity
133       | BagEntity _      -> st
134       | CrgEntity _      -> st
135
136 let grafite st och = function
137    | BrgEntity entity -> 
138       if BG.output_entity st.kst och entity then st else
139       begin L.warn level "Matita exportation stopped"; {st with och = None} end
140    | BagEntity _      -> st
141    | CrgEntity _      -> st
142
143 (* extended lexer ***********************************************************)
144
145 type 'token lexer = {
146    parse : Lexing.lexbuf -> 'token;
147    mutable tokbuf: 'token option;
148    mutable unget : bool
149 }
150
151 let initial_lexer parse = {
152    parse = parse; tokbuf = None; unget = false
153 }
154
155 let token xl lexbuf = match xl.tokbuf with
156    | Some token when xl.unget ->   
157       xl.unget <- false; token
158    | _                        ->
159       let token = xl.parse lexbuf in
160       xl.tokbuf <- Some token; token
161
162 (* input related ************************************************************)
163
164 type input = Text | Automath
165
166 type input_entity = TxtEntity of Txt.command
167                   | AutEntity of Aut.command
168                   | NoEntity
169
170 let type_of_input name =
171    if KF.check_suffix name ".hln" then Text 
172    else if KF.check_suffix name ".aut" then 
173       let _ = H.set_sorts 0 ["Set"; "Prop"] in
174       assert (H.set_graph "Z2");
175       Automath
176    else begin
177       L.warn level (KP.sprintf "Unknown file type: %s" name); exit 2
178    end
179
180 let txt_xl = initial_lexer TxtLexer.token 
181
182 let aut_xl = initial_lexer AutLexer.token 
183
184 let parbuf = ref [] (* parser buffer *)
185
186 let gen_text command = 
187    parbuf := TxtEntity command :: !parbuf
188
189 let entity_of_input lexbuf i = match i, !parbuf with
190    | Automath, _    -> 
191       begin match AutParser.entry (token aut_xl) lexbuf with
192          | Some e -> aut_xl.unget <- true; AutEntity e
193          | None   -> NoEntity
194       end     
195    | Text, []       -> 
196       begin match TxtParser.entry (token txt_xl) lexbuf with
197          | Some e -> txt_xl.unget <- true; TxtEntity e
198          | None   -> NoEntity
199       end
200    | Text, hd :: tl ->
201       parbuf := tl; hd
202
203 let process_input f st = function 
204    | AutEntity e     ->
205       let f pst e = f {st with pst = pst} (AutEntity e) in
206       AA.process_command f st.pst e
207    | xe              -> f st xe
208
209 let count_input st = function
210    | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
211    | xe          -> st
212
213 (****************************************************************************)
214
215 let version = ref true
216 let preprocess = ref false
217 let root = ref ""
218 let export = ref false
219 let st = ref (initial_status ())
220 let streaming = ref false (* parsing style (temporary) *)
221
222 let process_2 st entity =
223    let st = if !G.summary then count_entity st entity else st in
224    let st = 
225       if !G.stage >= 3 then 
226          let f = if !version then validate else type_check in f st entity
227       else st
228    in
229    if !export then export_entity st entity;
230    match st.och with
231      | None     -> st
232      | Some och -> grafite st och entity
233
234 let process_1 st entity = 
235    if !G.trace >= 3 then pp_progress entity;
236    let st = if !G.summary then count_entity st entity else st in
237    if !export && !G.stage = 1 then export_entity st entity;
238    if !G.stage >= 2 then process_2 st (xlate_entity st entity) else st 
239
240 let process_0 st entity = 
241    let f st entity =
242       if !G.stage = 0 then st else
243       match entity with
244          | AutEntity e -> 
245             let err ast = {st with ast = ast} in
246             let g ast e = process_1 {st with ast = ast} (CrgEntity e) in
247             AD.crg_of_aut err g st.kst st.ast e
248          | TxtEntity e -> 
249             let crr tst = {st with tst = tst} in
250             let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
251             TD.crg_of_txt crr d gen_text st.tst e
252          | NoEntity    -> assert false
253    in
254    let st = if !G.summary then count_input st entity else st in 
255    if !preprocess then process_input f st entity else f st entity
256
257 let process_nostreaming st lexbuf input =
258    let id x = x in
259    let rec aux1 book = match entity_of_input lexbuf input with
260       | NoEntity -> List.rev book
261       | e        -> aux1 (id e :: book)   
262    in
263    let rec aux2 st = function
264       | []           -> st
265       | entity :: tl -> aux2 (process_0 st entity) tl
266    in
267    aux2 st (aux1 [])
268
269 let process_streaming st lexbuf input =
270    let rec aux st = match entity_of_input lexbuf input with
271       | NoEntity -> st
272       | e        -> aux (process_0 st e)
273    in
274    aux st
275
276 (****************************************************************************)
277
278 let process st name =
279    let process = if !streaming then process_streaming else process_nostreaming in
280    let input = type_of_input name in
281    let ich = open_in name in
282    let lexbuf = Lexing.from_channel ich in 
283    let st = process st lexbuf input in
284    close_in ich;
285    st, input
286
287 let main = 
288    let print_version () = L.warn level (G.version_string ^ "\n"); exit 0 in
289    let set_hierarchy s = 
290       if H.set_graph s then () else 
291          L.warn level (KP.sprintf "Unknown type hierarchy: %s" s)
292    in
293    let set_kernel = function
294       | "V3" -> G.kernel := G.V3
295       | "V0" -> G.kernel := G.V0
296       | s    -> L.warn level (KP.sprintf "Unknown kernel version: %s" s)
297    in
298    let set_trace i = 
299       if !G.trace = 0 && i > 0 then Y.gmtime G.version_string;
300       if !G.trace > 0 && i = 0 then Y.utime_stamp "at exit";
301       G.trace := i;
302       if i <= 1 then G.summary := false;
303       if i <= 1 then preprocess := false
304    in
305    let set_summary () = 
306       if !G.trace >= 2 then G.summary := true
307    in 
308    let set_preprocess () = 
309       if !G.trace >= 2 then begin preprocess := true; G.summary := true end 
310    in 
311    let clear_options () =
312       export := false; preprocess := false;
313       root := "";
314       G.clear (); H.clear (); O.clear_reductions ();
315       streaming := false;
316       version := true
317    in
318    let process_file name =
319       if !G.trace >= 2 then L.warn 1 (KP.sprintf "Processing file: %s" name);
320       if !G.trace >= 2 then Y.utime_stamp "started";
321       let base_name = Filename.chop_extension (Filename.basename name) in
322       let cover = KF.concat !root base_name in
323       if !G.stage <= 1 then G.kernel := G.V4;
324       G.cover := cover;
325       if !G.ma_preamble <> "" then st := {!st with och = Some (BG.open_out base_name)};
326       P.clear_marks ();
327       let sst, input = process (refresh_status !st) name in
328       st := begin match sst.och with 
329          | None     -> sst
330          | Some och -> BG.close_out och; {sst with och = None}
331       end;
332       if !G.trace >= 2 then Y.utime_stamp "processed";
333       if !G.summary then begin
334          AO.print_counters C.start !st.ac;
335          if !preprocess then AO.print_process_counters C.start !st.pst;
336          if !G.stage >= 1 then print_counters !st G.V4;
337          if !G.stage >= 2 then print_counters !st !G.kernel;
338          if !G.stage >= 3 then O.print_reductions ()
339       end
340    in
341    let exit () =
342       if !G.trace >= 1 then Y.utime_stamp "at exit"
343    in
344    let help = 
345       "Usage: helena [ -LPVXdgilopqtx1 | -Ts <number> | -MO <dir> | -m <file> | -ahkr <string> ]* [ <file> ]*\n\n" ^
346       "Trace levels: 0 just errors (default), 1 time stamps, 2 processed files, 3 processed objects,\n" ^
347       "              4 typing information, 5 conversion information, 6 reduction information,\n" ^
348       "              7 level disambiguation\n\n" ^
349       "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
350    in
351    let help_L = " show lexer debug information" in 
352    let help_M = "<dir>  set location of output directory (Grafite) to <dir> (default: current directory)" in
353    let help_O = "<dir>  set location of output directory (XML) to <dir> (default: current directory)" in
354    let help_P = " show parser debug information" in 
355    let help_T = "<number>  set trace level (see above)" in
356    let help_V = " show version information" in
357    let help_X = " clear options" in
358    
359    let help_a = "<string>  set prefix of numeric identifiers (default: empty)" in
360    let help_d = " show summary information (requires trace >= 2)" in
361    let help_g = " always expand global definitions" in
362    let help_h = "<string>  set type hierarchy (default: \"Z1\")" in
363    let help_i = " show local references by index" in
364    let help_k = "<string>  set kernel version (default: \"V3\")" in
365    let help_l = " disambiguate binders level (Automath)" in
366    let help_m = "<file>  export kernel entities (Grafite) setting location of preamble to <file> (default: empty)" in   
367    let help_o = " activate sort inclusion (default: false)" in
368    let help_p = " preprocess source (Automath)" in
369    let help_q = " disable quotation of identifiers" in
370    let help_r = "<string>  set initial segment of URI hierarchy (default: empty)" in
371    let help_s = "<number>  set translation stage (see above)" in
372    let help_t = " type check [version 1]" in
373    let help_x = " export kernel entities (XML)" in
374    
375    let help_1 = " parse files with streaming policy" in
376    at_exit exit;
377    Arg.parse [
378       ("-L", Arg.Set G.debug_lexer, help_L);
379       ("-M", Arg.String ((:=) G.ma_dir), help_M);
380       ("-O", Arg.String ((:=) G.xdir), help_O);
381       ("-P", Arg.Set G.debug_parser, help_P);
382       ("-T", Arg.Int set_trace, help_T);
383       ("-V", Arg.Unit print_version, help_V);
384       ("-X", Arg.Unit clear_options, help_X);
385       ("-a", Arg.String ((:=) G.alpha), help_a);
386       ("-d", Arg.Unit set_summary, help_d);
387       ("-g", Arg.Set G.expand, help_g);
388       ("-h", Arg.String set_hierarchy, help_h);
389       ("-i", Arg.Set G.indexes, help_i);
390       ("-k", Arg.String set_kernel, help_k);
391       ("-l", Arg.Set G.cc, help_l);
392       ("-m", Arg.String ((:=) G.ma_preamble), help_m);      
393       ("-o", Arg.Set G.si, help_o);
394       ("-p", Arg.Unit set_preprocess, help_p);
395       ("-q", Arg.Set G.unquote, help_q);      
396       ("-r", Arg.String ((:=) root), help_r);
397       ("-s", Arg.Int ((:=) G.stage), help_s);
398       ("-t", Arg.Clear version, help_t);      
399       ("-x", Arg.Set export, help_x);
400       ("-1", Arg.Set streaming, help_1);      
401    ] process_file help