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