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