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_______________________________________________________________ *)
23 module AP = AutProcess
28 module MO = MetaOutput
29 module ML = MetaLibrary
33 module BrgO = BrgOutput
34 module BrgR = BrgReduction
35 module BrgU = BrgUntrusted
37 module BagO = BagOutput
39 module BagU = BagUntrusted
53 let flush_all () = L.flush 0; L.flush_err ()
56 L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all ()
59 L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all ()
61 let initial_status mk_uri cover g expand si = {
62 ac = AO.initial_counters;
63 mc = MO.initial_counters;
64 brgc = BrgO.initial_counters;
65 bagc = BagO.initial_counters;
66 mst = MA.initial_status ~cover ();
67 dst = DA.initial_status (mk_uri si cover);
68 tst = DT.initial_status (mk_uri si cover);
69 ast = AP.initial_status;
70 kst = Y.initial_status g expand si
73 (* kernel related ***********************************************************)
75 type kernel = Brg | Bag
77 type kernel_entity = BrgEntity of Brg.entity
78 | BagEntity of Bag.entity
79 | CrgEntity of Crg.entity
80 | MetaEntity of Meta.entity
84 let print_counters st = match !kernel with
85 | Brg -> BrgO.print_counters C.start st.brgc
86 | Bag -> BagO.print_counters C.start st.bagc
88 let xlate_entity entity = match !kernel, entity with
90 let f e = (BrgEntity e) in Y.xlate f DBrg.brg_of_crg e
91 | Brg, MetaEntity e ->
92 let f e = (BrgEntity e) in Y.xlate f MBrg.brg_of_meta e
93 | Bag, MetaEntity e ->
94 let f e = (BagEntity e) in Y.xlate f MBag.bag_of_meta e
99 let s = U.string_of_uri u in
100 let err () = L.warn (P.sprintf "%s" s) in
101 let f i = L.warn (P.sprintf "[%u] %s" i s) in
105 | CrgEntity e -> Y.common f e
106 | BrgEntity e -> Y.common f e
107 | BagEntity e -> Y.common f e
108 | MetaEntity e -> Y.common f e
110 let count_entity st = function
111 | MetaEntity e -> {st with mc = MO.count_entity C.start st.mc e}
112 | BrgEntity e -> {st with brgc = BrgO.count_entity C.start st.brgc e}
113 | BagEntity e -> {st with bagc = BagO.count_entity C.start st.bagc e}
116 let export_entity si g moch = function
117 | CrgEntity e -> X.export_entity DO.export_term si g e
118 | BrgEntity e -> X.export_entity BrgO.export_term si g e
120 begin match moch with
122 | Some och -> ML.write_entity C.start och e
126 let type_check st k =
127 let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
130 | BrgEntity entity -> BrgU.type_check brg_err ok st.kst entity
131 | BagEntity entity -> BagU.type_check ok st.kst entity
135 (* extended lexer ***********************************************************)
137 type 'token lexer = {
138 parse : Lexing.lexbuf -> 'token;
139 mutable tokbuf: 'token option;
143 let initial_lexer parse = {
144 parse = parse; tokbuf = None; unget = false
147 let token xl lexbuf = match xl.tokbuf with
148 | Some token when xl.unget ->
149 xl.unget <- false; token
151 let token = xl.parse lexbuf in
152 xl.tokbuf <- Some token; token
154 (* input related ************************************************************)
156 type input = Text | Automath
158 type input_entity = TxtEntity of Txt.entity
159 | AutEntity of Aut.entity
161 let type_of_input name =
162 if F.check_suffix name ".hln" then Text
163 else if F.check_suffix name ".aut" then Automath
165 L.warn (P.sprintf "Unknown file type: %s" name); exit 2
168 let txt_xl = initial_lexer TxtLexer.token
170 let aut_xl = initial_lexer AutLexer.token
172 let entity_of_input lexbuf = function
174 begin match TxtParser.entry (token txt_xl) lexbuf with
175 | Some e, unget -> txt_xl.unget <- unget; Some (TxtEntity e)
179 begin match AutParser.entry (token aut_xl) lexbuf with
180 | Some e, unget -> aut_xl.unget <- unget; Some (AutEntity e)
184 let process_input f st = function
186 let f ast e = f {st with ast = ast} (AutEntity e) in
187 AP.process_entity f st.ast e
190 let count_input st = function
191 | AutEntity e -> {st with ac = AO.count_entity C.start st.ac e}
194 (****************************************************************************)
199 let progress = ref false
200 let preprocess = ref false
201 let use_cover = ref true
203 let expand = ref false
205 let export = ref false
206 let graph = ref (H.graph_of_string C.err C.start "Z2")
209 let process_2 st entity =
210 let st = if !L.level > 2 then count_entity st entity else st in
211 if !export then export_entity !si !graph !moch entity;
212 if !stage > 2 then type_check st entity else st
214 let process_1 st entity =
215 if !progress then pp_progress entity;
216 let st = if !L.level > 2 then count_entity st entity else st in
217 if !export && !stage = 1 then export_entity !si !graph !moch entity;
218 if !stage > 1 then process_2 st (xlate_entity entity) else st
220 let process_0 st entity =
222 if !stage = 0 then st else
223 let frr mst = {st with mst = mst} in
224 let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
225 let err dst = {st with dst = dst} in
226 let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
227 let crr tst = {st with tst = tst} in
228 let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
229 match entity, !old with
230 | AutEntity e, true -> MA.meta_of_aut frr h st.mst e
231 | AutEntity e, false -> DA.crg_of_aut err g st.dst e
232 | TxtEntity e, false -> DT.crg_of_txt crr d st.tst e
235 let st = if !L.level > 2 then count_input st entity else st in
236 if !preprocess then process_input f st entity else f st entity
238 let process st name =
239 let input = type_of_input name in
240 let ich = open_in name in
241 let lexbuf = Lexing.from_channel ich in
242 let rec aux st = match entity_of_input lexbuf input with
243 | None -> close_in ich; st, input
244 | Some e -> aux (process_0 st e)
248 (****************************************************************************)
252 let version_string = "Helena 0.8.1 M - February 2010" in
253 let set_hierarchy s =
254 let err () = L.warn (P.sprintf "Unknown type hierarchy: %s" s) in
255 let f g = graph := g in
256 H.graph_of_string err f s
258 let set_kernel = function
259 | "brg" -> kernel := Brg
260 | "bag" -> kernel := Bag
261 | s -> L.warn (P.sprintf "Unknown kernel version: %s" s)
263 let set_summary i = L.level := i in
264 let print_version () = L.warn (version_string ^ "\n"); exit 0 in
265 let set_stage i = stage := i in
266 let set_meta_file name =
267 let f och = moch := Some och in
275 | Some och -> ML.close_out C.start och
277 let process_file name =
278 if !L.level > 0 then T.gmtime version_string;
280 L.warn (P.sprintf "Processing file: %s" name);
281 if !L.level > 0 then T.utime_stamp "started";
282 let base_name = Filename.chop_extension (Filename.basename name) in
283 if !meta then set_meta_file base_name;
284 O.clear_reductions ();
286 if !stage < 2 then Crg.mk_uri else
291 let cover = if !use_cover then base_name else "" in
292 let st = initial_status mk_uri cover !graph !expand !si in
293 let st, input = process st name in
294 if !L.level > 0 then T.utime_stamp "processed";
295 if !L.level > 2 then begin
296 AO.print_counters C.start st.ac;
297 if !preprocess then AO.print_process_counters C.start st.ast;
298 if !stage > 0 then MO.print_counters C.start st.mc;
299 if !stage > 1 then print_counters st;
300 if !stage > 2 then O.print_reductions ()
305 if !L.level > 0 then T.utime_stamp "at exit";
309 "Usage: helena [ -Vcgijmopqrux | -Ss <number> | -hk <string> ] <file> ...\n\n" ^
310 "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
311 3 data information, 4 typing information, 5 reduction information\n\n" ^
312 "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
314 let help_S = "<number> set summary level (see above)" in
315 let help_V = " show version information" in
317 let help_c = " output conversion constraints" in
318 let help_g = " always expand global definitions" in
319 let help_h = "<string> set type hierarchy (default: Z2)" in
320 let help_i = " show local references by index" in
321 let help_j = " show URI of processed kernel objects" in
322 let help_k = "<string> set kernel version (default: brg)" in
323 let help_m = " output intermediate representation (HAL)" in
324 let help_o = " use old abstract language instead of crg" in
325 let help_p = " preprocess Automath source" in
326 let help_q = " disable quotation of identifiers" in
327 let help_r = " disable initial segment of URI hierarchy" in
328 let help_s = "<number> set translation stage (see above)" in
329 let help_u = " activate sort inclusion" in
330 let help_x = " export kernel entities (XML)" in
331 L.box 0; L.box_err ();
332 let _ = H.set_sorts ["Set"; "Prop"] 0 in
335 ("-S", Arg.Int set_summary, help_S);
336 ("-V", Arg.Unit print_version, help_V);
337 ("-c", Arg.Set cc, help_c);
338 ("-g", Arg.Set expand, help_g);
339 ("-h", Arg.String set_hierarchy, help_h);
340 ("-i", Arg.Set O.indexes, help_i);
341 ("-j", Arg.Set progress, help_j);
342 ("-k", Arg.String set_kernel, help_k);
343 ("-m", Arg.Set meta, help_m);
344 ("-o", Arg.Set old, help_o);
345 ("-p", Arg.Set preprocess, help_p);
346 ("-q", Arg.Unit unquote, help_q);
347 ("-r", Arg.Clear use_cover, help_r);
348 ("-s", Arg.Int set_stage, help_s);
349 ("-u", Arg.Set si, help_u);
350 ("-x", Arg.Set export, help_x)
352 with BagT.TypeError msg -> bag_error "Type Error" msg