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.
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_______________________________________________________________ *)
24 module AP = AutProcess
29 module MO = MetaOutput
30 module ML = MetaLibrary
34 module BrgO = BrgOutput
35 module BrgR = BrgReduction
36 module BrgU = BrgUntrusted
38 module BagO = BagOutput
40 module BagU = BagUntrusted
54 let flush_all () = L.flush 0; L.flush_err ()
57 L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all ()
60 L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all ()
62 let initial_status () = {
63 ac = AO.initial_counters;
64 mc = MO.initial_counters;
65 brgc = BrgO.initial_counters;
66 bagc = BagO.initial_counters;
67 mst = MA.initial_status ();
68 dst = DA.initial_status ();
69 tst = DT.initial_status ();
70 ast = AP.initial_status ();
71 kst = Y.initial_status ()
74 let refresh_status st = {st with
75 mst = MA.refresh_status st.mst;
76 dst = DA.refresh_status st.dst;
77 tst = DT.refresh_status st.tst;
78 kst = Y.refresh_status st.kst
81 (* kernel related ***********************************************************)
83 type kernel = Brg | Bag
85 type kernel_entity = BrgEntity of Brg.entity
86 | BagEntity of Bag.entity
87 | CrgEntity of Crg.entity
88 | MetaEntity of Meta.entity
92 let print_counters st = match !kernel with
93 | Brg -> BrgO.print_counters C.start st.brgc
94 | Bag -> BagO.print_counters C.start st.bagc
96 let xlate_entity entity = match !kernel, entity with
98 let f e = (BrgEntity e) in Y.xlate f DBrg.brg_of_crg e
99 | Brg, MetaEntity e ->
100 let f e = (BrgEntity e) in Y.xlate f MBrg.brg_of_meta e
101 | Bag, MetaEntity e ->
102 let f e = (BagEntity e) in Y.xlate f MBag.bag_of_meta e
103 | _, entity -> entity
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
113 | CrgEntity e -> Y.common f e
114 | BrgEntity e -> Y.common f e
115 | BagEntity e -> Y.common f e
116 | MetaEntity e -> Y.common f e
118 let count_entity st = function
119 | MetaEntity e -> {st with mc = MO.count_entity C.start st.mc e}
120 | BrgEntity e -> {st with brgc = BrgO.count_entity C.start st.brgc e}
121 | BagEntity e -> {st with bagc = BagO.count_entity C.start st.bagc e}
124 let export_entity si xdir moch = function
125 | CrgEntity e -> X.export_entity DO.export_term si xdir e
126 | BrgEntity e -> X.export_entity BrgO.export_term si xdir e
128 begin match moch with
130 | Some och -> ML.write_entity C.start och e
134 let type_check st k =
135 let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
138 | BrgEntity entity -> BrgU.type_check brg_err ok st.kst entity
139 | BagEntity entity -> BagU.type_check ok st.kst entity
143 (* extended lexer ***********************************************************)
145 type 'token lexer = {
146 parse : Lexing.lexbuf -> 'token;
147 mutable tokbuf: 'token option;
151 let initial_lexer parse = {
152 parse = parse; tokbuf = None; unget = false
155 let token xl lexbuf = match xl.tokbuf with
156 | Some token when xl.unget ->
157 xl.unget <- false; token
159 let token = xl.parse lexbuf in
160 xl.tokbuf <- Some token; token
162 (* input related ************************************************************)
164 type input = Text | Automath
166 type input_entity = TxtEntity of Txt.command
167 | AutEntity of Aut.command
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");
176 L.warn (P.sprintf "Unknown file type: %s" name); exit 2
179 let txt_xl = initial_lexer TxtLexer.token
181 let aut_xl = initial_lexer AutLexer.token
183 let entity_of_input lexbuf = function
185 begin match TxtParser.entry (token txt_xl) lexbuf with
186 | Some e, unget -> txt_xl.unget <- unget; Some (TxtEntity e)
190 begin match AutParser.entry (token aut_xl) lexbuf with
191 | Some e, unget -> aut_xl.unget <- unget; Some (AutEntity e)
195 let process_input f st = function
197 let f ast e = f {st with ast = ast} (AutEntity e) in
198 AP.process_command f st.ast e
201 let count_input st = function
202 | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
205 (****************************************************************************)
210 let progress = ref false
211 let preprocess = ref false
216 let st = ref (initial_status ())
218 let process_2 st entity =
219 let st = if !L.level > 2 then count_entity st entity else st in
220 if !export <> "" then export_entity !O.si !export !moch entity;
221 if !stage > 2 then type_check st entity else st
223 let process_1 st entity =
224 if !progress then pp_progress entity;
225 let st = if !L.level > 2 then count_entity st entity else st in
226 if !export <> "" && !stage = 1 then export_entity !O.si !export !moch entity;
227 if !stage > 1 then process_2 st (xlate_entity entity) else st
229 let process_0 st entity =
231 if !stage = 0 then st else
232 let frr mst = {st with mst = mst} in
233 let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
234 let err dst = {st with dst = dst} in
235 let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
236 let crr tst = {st with tst = tst} in
237 let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
238 match entity, !old with
239 | AutEntity e, true -> MA.meta_of_aut frr h st.mst e
240 | AutEntity e, false -> DA.crg_of_aut err g st.dst e
241 | TxtEntity e, false -> DT.crg_of_txt crr d st.tst e
244 let st = if !L.level > 2 then count_input st entity else st in
245 if !preprocess then process_input f st entity else f st entity
247 let process st name =
248 let input = type_of_input name in
249 let ich = open_in name in
250 let lexbuf = Lexing.from_channel ich in
251 let rec aux st = match entity_of_input lexbuf input with
252 | None -> close_in ich; st, input
253 | Some e -> aux (process_0 st e)
257 (****************************************************************************)
261 let version_string = "Helena 0.8.1 M - February 2010" in
262 let print_version () = L.warn (version_string ^ "\n"); exit 0 in
263 let set_hierarchy s =
264 if H.set_graph s then () else
265 L.warn (P.sprintf "Unknown type hierarchy: %s" s)
267 let set_kernel = function
268 | "brg" -> kernel := Brg
269 | "bag" -> kernel := Bag
270 | s -> L.warn (P.sprintf "Unknown kernel version: %s" s)
272 let set_summary i = L.level := i in
273 let set_stage i = stage := i in
274 let set_meta_file name =
275 let f och = moch := Some och in
278 let set_xdir s = export := s in
279 let set_root s = root := s in
282 | Some och -> ML.close_out C.start och
284 let clear_options () =
285 stage := 3; moch := None; meta := false; progress := false;
286 preprocess := false; root := ""; cc := false; export := "";
287 old := false; kernel := Brg; st := initial_status ();
288 L.clear (); O.clear (); H.clear (); Op.clear_reductions ()
290 let process_file name =
291 if !L.level > 0 then T.gmtime version_string;
293 L.warn (P.sprintf "Processing file: %s" name);
294 if !L.level > 0 then T.utime_stamp "started";
295 let base_name = Filename.chop_extension (Filename.basename name) in
296 if !meta then set_meta_file base_name;
298 if !stage < 2 then Crg.mk_uri else
303 let cover = F.concat !root base_name in
304 O.mk_uri := mk_uri; O.cover := cover;
305 let sst, input = process (refresh_status !st) name in
307 if !L.level > 0 then T.utime_stamp "processed";
308 if !L.level > 2 then begin
309 AO.print_counters C.start !st.ac;
310 if !preprocess then AO.print_process_counters C.start !st.ast;
311 if !stage > 0 then MO.print_counters C.start !st.mc;
312 if !stage > 1 then print_counters !st;
313 if !stage > 2 then Op.print_reductions ()
318 if !L.level > 0 then T.utime_stamp "at exit";
322 "Usage: helena [ -VXcgijmopqu | -Ss <number> | -x <dir> | -hkr <string> ]* [ <file> ]*\n\n" ^
323 "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
324 3 data information, 4 typing information, 5 reduction information\n\n" ^
325 "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
327 let help_S = "<number> set summary level (see above)" in
328 let help_V = " show version information" in
329 let help_X = " clear options" in
331 let help_c = " output conversion constraints" in
332 let help_g = " always expand global definitions" in
333 let help_h = "<string> set type hierarchy (default: Z1)" in
334 let help_i = " show local references by index" in
335 let help_j = " show URI of processed kernel objects" in
336 let help_k = "<string> set kernel version (default: brg)" in
337 let help_m = " output intermediate representation (HAL)" in
338 let help_o = " use old abstract language instead of crg" in
339 let help_p = " preprocess source" in
340 let help_q = " disable quotation of identifiers" in
341 let help_r = "<string> set initial segment of URI hierarchy" in
342 let help_s = "<number> set translation stage (see above)" in
343 let help_u = " activate sort inclusion" in
344 let help_x = "<dir> export kernel entities (XML) to <dir>" in
345 L.box 0; L.box_err ();
348 ("-S", Arg.Int set_summary, help_S);
349 ("-V", Arg.Unit print_version, help_V);
350 ("-X", Arg.Unit clear_options, help_X);
351 ("-c", Arg.Set cc, help_c);
352 ("-g", Arg.Set O.expand, help_g);
353 ("-h", Arg.String set_hierarchy, help_h);
354 ("-i", Arg.Set O.indexes, help_i);
355 ("-j", Arg.Set progress, help_j);
356 ("-k", Arg.String set_kernel, help_k);
357 ("-m", Arg.Set meta, help_m);
358 ("-o", Arg.Set old, help_o);
359 ("-p", Arg.Set preprocess, help_p);
360 ("-q", Arg.Set O.unquote, help_q);
361 ("-r", Arg.String set_root, help_r);
362 ("-s", Arg.Int set_stage, help_s);
363 ("-u", Arg.Set O.si, help_u);
364 ("-x", Arg.String set_xdir, help_x)
366 with BagT.TypeError msg -> bag_error "Type Error" msg