]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/toplevel/top.ml
579f5e6585211f665c3119548ebca5a44d79d10b
[helm.git] / helm / software / lambda-delta / 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 F  = Filename
13 module P  = Printf
14 module U  = NUri
15 module C  = Cps
16 module L  = Log
17 module Y  = Time
18 module G  = Options
19 module H  = Hierarchy
20 module O  = Output
21 module E  = Entity
22 module S  = Status
23 module TD = CrgTxt
24 module AA = AutProcess
25 module AO = AutOutput
26 module AD = CrgAut
27 module DO = CrgOutput
28 module XL = XmlLibrary
29 module XD = XmlCrg
30 module BD = BrgCrg
31 module BO = BrgOutput
32 module BR = BrgReduction
33 module BU = BrgUntrusted
34 module ZO = BagOutput
35 module ZT = BagType
36 module ZU = BagUntrusted
37
38 module MA = MetaAut
39 module MO = MetaOutput
40 module MB = MetaBrg
41 module MZ = MetaBag
42
43 type status = {
44    kst: S.status;
45    tst: TD.status;
46    pst: AA.status;
47    ast: AD.status;
48    ac : AO.counters;
49    dc : DO.counters;
50    bc : BO.counters;
51    zc : ZO.counters;
52    mst: MA.status;
53    mc : MO.counters;
54 }
55
56 let flush_all () = L.flush 0; L.flush_err ()
57
58 let bag_error s msg =
59    L.error ZO.specs (L.Warn s :: L.Loc :: msg); flush_all () 
60
61 let brg_error s msg =
62    L.error BR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
63
64 let initial_status () = {
65    kst = S.initial_status ();
66    tst = TD.initial_status ();
67    pst = AA.initial_status ();
68    ast = AD.initial_status ();
69    ac  = AO.initial_counters;
70    dc  = DO.initial_counters;
71    bc  = BO.initial_counters;
72    zc  = ZO.initial_counters;
73    mst = MA.initial_status ();
74    mc  = MO.initial_counters;
75 }
76
77 let refresh_status st = {st with
78    kst = S.refresh_status st.kst;
79    tst = TD.refresh_status st.tst;
80    ast = AD.refresh_status st.ast;
81    mst = MA.refresh_status st.mst;
82 }
83
84 (* kernel related ***********************************************************)
85
86 type kernel_entity = BrgEntity  of Brg.entity
87                    | BagEntity  of Bag.entity
88                    | CrgEntity  of Crg.entity
89                    | MetaEntity of Meta.entity
90
91 let print_counters st = function
92    | G.Crg -> DO.print_counters C.start st.dc
93    | G.Brg -> BO.print_counters C.start st.bc
94    | G.Bag -> ZO.print_counters C.start st.zc
95
96 let xlate_entity entity = match !G.kernel, entity with
97    | G.Brg, CrgEntity e  -> 
98       let f e = (BrgEntity e) in E.xlate f BD.brg_of_crg e
99    | G.Brg, MetaEntity e -> 
100       let f e = (BrgEntity e) in E.xlate f MB.brg_of_meta e
101    | G.Bag, MetaEntity e -> 
102       let f e = (BagEntity e) in E.xlate f MZ.bag_of_meta e  
103    | _, entity         -> entity
104
105 let pp_progress e =
106    let f a u =
107       let s = U.string_of_uri u in
108       let err () = L.warn (P.sprintf "%s" s) in
109       let f i = L.warn (P.sprintf "[%u] %s" i s) in
110       E.mark err f a
111    in
112    match e with
113       | CrgEntity e  -> E.common f e
114       | BrgEntity e  -> E.common f e
115       | BagEntity e  -> E.common f e      
116       | MetaEntity e -> E.common f e
117
118 let count_entity st = function
119    | BrgEntity e  -> {st with bc = BO.count_entity C.start st.bc e}
120    | BagEntity e  -> {st with zc = ZO.count_entity C.start st.zc e}
121    | CrgEntity e  -> {st with dc = DO.count_entity C.start st.dc e}
122    | MetaEntity e -> {st with mc = MO.count_entity C.start st.mc e} 
123
124 let export_entity = function
125    | CrgEntity e  -> XL.export_entity XD.export_term e
126    | BrgEntity e  -> XL.export_entity BO.export_term e
127    | MetaEntity _
128    | BagEntity _  -> ()
129
130 let type_check st k =
131    let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
132    let ok _ _ = st in
133    match k with
134       | BrgEntity entity -> BU.type_check brg_err ok st.kst entity
135       | BagEntity entity -> ZU.type_check ok st.kst entity
136       | CrgEntity _
137       | MetaEntity _     -> st
138
139 (* extended lexer ***********************************************************)
140
141 type 'token lexer = {
142    parse : Lexing.lexbuf -> 'token;
143    mutable tokbuf: 'token option;
144    mutable unget : bool
145 }
146
147 let initial_lexer parse = {
148    parse = parse; tokbuf = None; unget = false
149 }
150
151 let token xl lexbuf = match xl.tokbuf with
152    | Some token when xl.unget ->   
153       xl.unget <- false; token
154    | _                        ->
155       let token = xl.parse lexbuf in
156       xl.tokbuf <- Some token; token
157
158 (* input related ************************************************************)
159
160 type input = Text | Automath
161
162 type input_entity = TxtEntity of Txt.command
163                   | AutEntity of Aut.command
164                   | NoEntity
165
166 let type_of_input name =
167    if F.check_suffix name ".hln" then Text 
168    else if F.check_suffix name ".aut" then 
169       let _ = H.set_sorts 0 ["Set"; "Prop"] in
170       assert (H.set_graph "Z2");
171       Automath
172    else begin
173       L.warn (P.sprintf "Unknown file type: %s" name); exit 2
174    end
175
176 let txt_xl = initial_lexer TxtLexer.token 
177
178 let aut_xl = initial_lexer AutLexer.token 
179
180 let parbuf = ref [] (* parser buffer *)
181
182 let gen_text command = 
183    parbuf := TxtEntity command :: !parbuf
184
185 let entity_of_input lexbuf i = match i, !parbuf with
186    | Automath, _    -> 
187       begin match AutParser.entry (token aut_xl) lexbuf with
188          | Some e -> aut_xl.unget <- true; AutEntity e
189          | None   -> NoEntity
190       end     
191    | Text, []       -> 
192       begin match TxtParser.entry (token txt_xl) lexbuf with
193          | Some e -> txt_xl.unget <- true; TxtEntity e
194          | None   -> NoEntity
195       end
196    | Text, hd :: tl ->
197       parbuf := tl; hd
198
199 let process_input f st = function 
200    | AutEntity e     ->
201       let f pst e = f {st with pst = pst} (AutEntity e) in
202       AA.process_command f st.pst e
203    | xe              -> f st xe
204
205 let count_input st = function
206    | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
207    | xe          -> st
208
209 (****************************************************************************)
210
211 let stage = ref 3
212 let progress = ref false
213 let preprocess = ref false
214 let root = ref ""
215 let export = ref false
216 let old = ref false
217 let st = ref (initial_status ())
218 let streaming = ref false (* parsing style (temporary) *)
219
220 let process_2 st entity =
221    let st = if !L.level > 2 then count_entity st entity else st in
222    if !export then export_entity entity;
223    if !stage > 2 then type_check st entity else st
224             
225 let process_1 st entity = 
226    if !progress then pp_progress entity;
227    let st = if !L.level > 2 then count_entity st entity else st in
228    if !export && !stage = 1 then export_entity entity;
229    if !stage > 1 then process_2 st (xlate_entity entity) else st 
230
231 let process_0 st entity = 
232    let f st entity =
233       if !stage = 0 then st else
234       match entity, !old with
235          | AutEntity e, true  ->
236             let frr mst = {st with mst = mst} in
237             let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
238             MA.meta_of_aut frr h st.mst e 
239          | AutEntity e, false -> 
240             let err ast = {st with ast = ast} in
241             let g ast e = process_1 {st with ast = ast} (CrgEntity e) in
242             AD.crg_of_aut err g st.ast e
243          | TxtEntity e, _     -> 
244             let crr tst = {st with tst = tst} in
245             let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
246             TD.crg_of_txt crr d gen_text st.tst e
247          | NoEntity, _        -> assert false
248    in
249    let st = if !L.level > 2 then count_input st entity else st in 
250    if !preprocess then process_input f st entity else f st entity
251
252 let process_nostreaming st lexbuf input =
253    let id x = x in
254    let rec aux1 book = match entity_of_input lexbuf input with
255       | NoEntity -> List.rev book
256       | e        -> aux1 (id e :: book)   
257    in
258    let rec aux2 st = function
259       | []           -> st
260       | entity :: tl -> aux2 (process_0 st entity) tl
261    in
262    aux2 st (aux1 [])
263
264 let process_streaming st lexbuf input =
265    let rec aux st = match entity_of_input lexbuf input with
266       | NoEntity -> st
267       | e        -> aux (process_0 st e)
268    in
269    aux st
270
271 (****************************************************************************)
272
273 let process st name =
274    let process = if !streaming then process_streaming else process_nostreaming in
275    let input = type_of_input name in
276    let ich = open_in name in
277    let lexbuf = Lexing.from_channel ich in 
278    let st = process st lexbuf input in
279    close_in ich; 
280    if !stage > 2 && !G.cc && !G.si then XL.export_csys st.kst.S.cc; 
281    st, input
282
283 let main =
284 try 
285    let version_string = "Helena 0.8.1 M - October 2010" in
286    let print_version () = L.warn (version_string ^ "\n"); exit 0 in
287    let set_hierarchy s = 
288       if H.set_graph s then () else 
289          L.warn (P.sprintf "Unknown type hierarchy: %s" s)
290    in
291    let set_kernel = function
292       | "brg" -> G.kernel := G.Brg
293       | "bag" -> G.kernel := G.Bag
294       | s     -> L.warn (P.sprintf "Unknown kernel version: %s" s)
295    in
296    let set_summary i = L.level := i in
297    let set_stage i = stage := i in
298    let set_xdir s = G.xdir := s in
299    let set_root s = root := s in
300    let clear_options () =
301       progress := false; old := false; export := false; preprocess := false;
302       stage := 3; root := "";
303       st := initial_status ();
304       L.clear (); G.clear (); H.clear (); O.clear_reductions ();
305       streaming := false;
306    in
307    let process_file name =
308       if !L.level > 0 then Y.gmtime version_string;      
309       if !L.level > 1 then
310          L.warn (P.sprintf "Processing file: %s" name);
311       if !L.level > 0 then Y.utime_stamp "started";
312       let base_name = Filename.chop_extension (Filename.basename name) in
313       let cover = F.concat !root base_name in
314       if !stage < 2 then G.kernel := G.Crg;
315       G.cover := cover;
316       let sst, input = process (refresh_status !st) name in
317       st := sst;
318       if !L.level > 0 then Y.utime_stamp "processed";
319       if !L.level > 2 then begin
320          AO.print_counters C.start !st.ac;
321          if !preprocess then AO.print_process_counters C.start !st.pst;
322          if !stage > 0 then 
323             if !old then MO.print_counters C.start !st.mc
324             else print_counters !st G.Crg;
325          if !stage > 1 then print_counters !st !G.kernel;
326          if !stage > 2 then O.print_reductions ()
327       end
328    in
329    let exit () =
330       if !L.level > 0 then Y.utime_stamp "at exit";
331       flush_all ()
332    in
333    let help = 
334       "Usage: helena [ -LPVXcgijopqux1 | -Ss <number> | -O <dir> | -hkr <string> ]* [ <file> ]*\n\n" ^
335       "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
336        3 data information, 4 typing information, 5 reduction information\n\n" ^
337        "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
338    in
339    let help_L = " show lexer debug information" in 
340    let help_O = "<dir>  set location of output directory (XML) to <dir> (default: current directory)" in
341    let help_P = " show parser debug information" in 
342    let help_S = "<number>  set summary level (see above)" in
343    let help_V = " show version information" in
344    let help_X = " clear options" in
345    
346    let help_c = " read/write conversion constraints" in
347    let help_g = " always expand global definitions" in
348    let help_h = "<string>  set type hierarchy (default: \"Z1\")" in
349    let help_i = " show local references by index" in
350    let help_j = " show URI of processed kernel objects" in
351    let help_k = "<string>  set kernel version (default: \"brg\")" in
352    let help_o = " use old abstract language instead of crg" in   
353    let help_p = " preprocess source" in
354    let help_q = " disable quotation of identifiers" in
355    let help_r = "<string>  set initial segment of URI hierarchy (default: empty)" in
356    let help_s = "<number>  set translation stage (see above)" in
357    let help_u = " activate sort inclusion" in
358    let help_x = " export kernel entities (XML)" in
359    
360    let help_1 = " parse files with streaming policy" in
361    L.box 0; L.box_err ();
362    at_exit exit;
363    Arg.parse [
364       ("-L", Arg.Set G.debug_lexer, help_L); 
365       ("-O", Arg.String set_xdir, help_O);       
366       ("-P", Arg.Set G.debug_parser, help_P); 
367       ("-S", Arg.Int set_summary, help_S);
368       ("-V", Arg.Unit print_version, help_V);
369       ("-X", Arg.Unit clear_options, help_X);
370       ("-c", Arg.Set G.cc, help_c);
371       ("-g", Arg.Set G.expand, help_g);
372       ("-h", Arg.String set_hierarchy, help_h);
373       ("-i", Arg.Set G.indexes, help_i);
374       ("-j", Arg.Set progress, help_j);      
375       ("-k", Arg.String set_kernel, help_k);
376       ("-o", Arg.Set old, help_o);      
377       ("-p", Arg.Set preprocess, help_p);
378       ("-q", Arg.Set G.unquote, help_q);      
379       ("-r", Arg.String set_root, help_r);
380       ("-s", Arg.Int set_stage, help_s);
381       ("-u", Arg.Set G.si, help_u);
382       ("-x", Arg.Set export, help_x);
383       ("-1", Arg.Set streaming, help_1);      
384    ] process_file help;
385 with ZT.TypeError msg -> bag_error "Type Error" msg