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 parbuf = ref [] (* parser buffer *)
185 let gen_text command =
186 parbuf := TxtEntity command :: !parbuf
188 let entity_of_input lexbuf i = match i, !parbuf with
190 begin match TxtParser.entry (token txt_xl) lexbuf with
191 | Some e, unget -> txt_xl.unget <- unget; Some (TxtEntity e)
195 parbuf := tl; Some hd
197 begin match AutParser.entry (token aut_xl) lexbuf with
198 | Some e, unget -> aut_xl.unget <- unget; Some (AutEntity e)
202 let process_input f st = function
204 let f ast e = f {st with ast = ast} (AutEntity e) in
205 AP.process_command f st.ast e
208 let count_input st = function
209 | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
212 (****************************************************************************)
217 let progress = ref false
218 let preprocess = ref false
223 let st = ref (initial_status ())
225 let process_2 st entity =
226 let st = if !L.level > 2 then count_entity st entity else st in
227 if !export <> "" then export_entity !O.si !export !moch entity;
228 if !stage > 2 then type_check st entity else st
230 let process_1 st entity =
231 if !progress then pp_progress entity;
232 let st = if !L.level > 2 then count_entity st entity else st in
233 if !export <> "" && !stage = 1 then export_entity !O.si !export !moch entity;
234 if !stage > 1 then process_2 st (xlate_entity entity) else st
236 let process_0 st entity =
238 if !stage = 0 then st else
239 let frr mst = {st with mst = mst} in
240 let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
241 let err dst = {st with dst = dst} in
242 let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
243 let crr tst = {st with tst = tst} in
244 let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
245 match entity, !old with
246 | AutEntity e, true -> MA.meta_of_aut frr h st.mst e
247 | AutEntity e, false -> DA.crg_of_aut err g st.dst e
248 | TxtEntity e, false -> DT.crg_of_txt crr d gen_text st.tst e
251 let st = if !L.level > 2 then count_input st entity else st in
252 if !preprocess then process_input f st entity else f st entity
254 let process st name =
255 let input = type_of_input name in
256 let ich = open_in name in
257 let lexbuf = Lexing.from_channel ich in
258 let rec aux st = match entity_of_input lexbuf input with
259 | None -> close_in ich; st, input
260 | Some e -> aux (process_0 st e)
264 (****************************************************************************)
268 let version_string = "Helena 0.8.1 M - February 2010" in
269 let print_version () = L.warn (version_string ^ "\n"); exit 0 in
270 let set_hierarchy s =
271 if H.set_graph s then () else
272 L.warn (P.sprintf "Unknown type hierarchy: %s" s)
274 let set_kernel = function
275 | "brg" -> kernel := Brg
276 | "bag" -> kernel := Bag
277 | s -> L.warn (P.sprintf "Unknown kernel version: %s" s)
279 let set_summary i = L.level := i in
280 let set_stage i = stage := i in
281 let set_meta_file name =
282 let f och = moch := Some och in
285 let set_xdir s = export := s in
286 let set_root s = root := s in
289 | Some och -> ML.close_out C.start och
291 let clear_options () =
292 stage := 3; moch := None; meta := false; progress := false;
293 preprocess := false; root := ""; cc := false; export := "";
294 old := false; kernel := Brg; st := initial_status ();
295 L.clear (); O.clear (); H.clear (); Op.clear_reductions ()
297 let process_file name =
298 if !L.level > 0 then T.gmtime version_string;
300 L.warn (P.sprintf "Processing file: %s" name);
301 if !L.level > 0 then T.utime_stamp "started";
302 let base_name = Filename.chop_extension (Filename.basename name) in
303 if !meta then set_meta_file base_name;
305 if !stage < 2 then Crg.mk_uri else
310 let cover = F.concat !root base_name in
311 O.mk_uri := mk_uri; O.cover := cover;
312 let sst, input = process (refresh_status !st) name in
314 if !L.level > 0 then T.utime_stamp "processed";
315 if !L.level > 2 then begin
316 AO.print_counters C.start !st.ac;
317 if !preprocess then AO.print_process_counters C.start !st.ast;
318 if !stage > 0 then MO.print_counters C.start !st.mc;
319 if !stage > 1 then print_counters !st;
320 if !stage > 2 then Op.print_reductions ()
325 if !L.level > 0 then T.utime_stamp "at exit";
329 "Usage: helena [ -VXcgijmopqu | -Ss <number> | -x <dir> | -hkr <string> ]* [ <file> ]*\n\n" ^
330 "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
331 3 data information, 4 typing information, 5 reduction information\n\n" ^
332 "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
334 let help_S = "<number> set summary level (see above)" in
335 let help_V = " show version information" in
336 let help_X = " clear options" in
338 let help_c = " output conversion constraints" in
339 let help_g = " always expand global definitions" in
340 let help_h = "<string> set type hierarchy (default: Z1)" in
341 let help_i = " show local references by index" in
342 let help_j = " show URI of processed kernel objects" in
343 let help_k = "<string> set kernel version (default: brg)" in
344 let help_m = " output intermediate representation (HAL)" in
345 let help_o = " use old abstract language instead of crg" in
346 let help_p = " preprocess source" in
347 let help_q = " disable quotation of identifiers" in
348 let help_r = "<string> set initial segment of URI hierarchy" in
349 let help_s = "<number> set translation stage (see above)" in
350 let help_u = " activate sort inclusion" in
351 let help_x = "<dir> export kernel entities (XML) to <dir>" in
352 L.box 0; L.box_err ();
355 ("-S", Arg.Int set_summary, help_S);
356 ("-V", Arg.Unit print_version, help_V);
357 ("-X", Arg.Unit clear_options, help_X);
358 ("-c", Arg.Set cc, help_c);
359 ("-g", Arg.Set O.expand, help_g);
360 ("-h", Arg.String set_hierarchy, help_h);
361 ("-i", Arg.Set O.indexes, help_i);
362 ("-j", Arg.Set progress, help_j);
363 ("-k", Arg.String set_kernel, help_k);
364 ("-m", Arg.Set meta, help_m);
365 ("-o", Arg.Set old, help_o);
366 ("-p", Arg.Set preprocess, help_p);
367 ("-q", Arg.Set O.unquote, help_q);
368 ("-r", Arg.String set_root, help_r);
369 ("-s", Arg.Int set_stage, help_s);
370 ("-u", Arg.Set O.si, help_u);
371 ("-x", Arg.String set_xdir, help_x)
373 with BagT.TypeError msg -> bag_error "Type Error" msg