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