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 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 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 moch = function
117 | CrgEntity e -> X.export_entity DO.export_term si e
118 | BrgEntity e -> X.export_entity BrgO.export_term si 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.command
159 | AutEntity of Aut.command
161 let type_of_input name =
162 if F.check_suffix name ".hln" then Text
163 else if F.check_suffix name ".aut" then
164 let _ = H.set_sorts 0 ["Set"; "Prop"] in
165 assert (H.set_graph "Z2");
168 L.warn (P.sprintf "Unknown file type: %s" name); exit 2
171 let txt_xl = initial_lexer TxtLexer.token
173 let aut_xl = initial_lexer AutLexer.token
175 let entity_of_input lexbuf = function
177 begin match TxtParser.entry (token txt_xl) lexbuf with
178 | Some e, unget -> txt_xl.unget <- unget; Some (TxtEntity e)
182 begin match AutParser.entry (token aut_xl) lexbuf with
183 | Some e, unget -> aut_xl.unget <- unget; Some (AutEntity e)
187 let process_input f st = function
189 let f ast e = f {st with ast = ast} (AutEntity e) in
190 AP.process_command f st.ast e
193 let count_input st = function
194 | AutEntity e -> {st with ac = AO.count_command C.start st.ac e}
197 (****************************************************************************)
202 let progress = ref false
203 let preprocess = ref false
206 let expand = ref false
208 let export = ref false
211 let process_2 st entity =
212 let st = if !L.level > 2 then count_entity st entity else st in
213 if !export then export_entity !si !moch entity;
214 if !stage > 2 then type_check st entity else st
216 let process_1 st entity =
217 if !progress then pp_progress entity;
218 let st = if !L.level > 2 then count_entity st entity else st in
219 if !export && !stage = 1 then export_entity !si !moch entity;
220 if !stage > 1 then process_2 st (xlate_entity entity) else st
222 let process_0 st entity =
224 if !stage = 0 then st else
225 let frr mst = {st with mst = mst} in
226 let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
227 let err dst = {st with dst = dst} in
228 let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
229 let crr tst = {st with tst = tst} in
230 let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
231 match entity, !old with
232 | AutEntity e, true -> MA.meta_of_aut frr h st.mst e
233 | AutEntity e, false -> DA.crg_of_aut err g st.dst e
234 | TxtEntity e, false -> DT.crg_of_txt crr d st.tst e
237 let st = if !L.level > 2 then count_input st entity else st in
238 if !preprocess then process_input f st entity else f st entity
240 let process st name =
241 let input = type_of_input name in
242 let ich = open_in name in
243 let lexbuf = Lexing.from_channel ich in
244 let rec aux st = match entity_of_input lexbuf input with
245 | None -> close_in ich; st, input
246 | Some e -> aux (process_0 st e)
250 (****************************************************************************)
254 let version_string = "Helena 0.8.1 M - February 2010" in
255 let print_version () = L.warn (version_string ^ "\n"); exit 0 in
256 let set_hierarchy s =
257 if H.set_graph s then () else
258 L.warn (P.sprintf "Unknown type hierarchy: %s" s)
260 let set_kernel = function
261 | "brg" -> kernel := Brg
262 | "bag" -> kernel := Bag
263 | s -> L.warn (P.sprintf "Unknown kernel version: %s" s)
265 let set_summary i = L.level := i in
266 let set_stage i = stage := i in
267 let set_meta_file name =
268 let f och = moch := Some och in
271 let set_root s = root := s in
274 | Some och -> ML.close_out C.start och
276 let process_file name =
277 if !L.level > 0 then T.gmtime version_string;
279 L.warn (P.sprintf "Processing file: %s" name);
280 if !L.level > 0 then T.utime_stamp "started";
281 let base_name = Filename.chop_extension (Filename.basename name) in
282 if !meta then set_meta_file base_name;
283 O.clear_reductions ();
285 if !stage < 2 then Crg.mk_uri else
290 let cover = F.concat !root base_name in
291 let st = initial_status mk_uri cover !expand !si in
292 let st, input = process st name in
293 if !L.level > 0 then T.utime_stamp "processed";
294 if !L.level > 2 then begin
295 AO.print_counters C.start st.ac;
296 if !preprocess then AO.print_process_counters C.start st.ast;
297 if !stage > 0 then MO.print_counters C.start st.mc;
298 if !stage > 1 then print_counters st;
299 if !stage > 2 then O.print_reductions ()
304 if !L.level > 0 then T.utime_stamp "at exit";
308 "Usage: helena [ -Vcgijmopqux | -Ss <number> | -hkr <string> ] <file> ...\n\n" ^
309 "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
310 3 data information, 4 typing information, 5 reduction information\n\n" ^
311 "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
313 let help_S = "<number> set summary level (see above)" in
314 let help_V = " show version information" in
316 let help_c = " output conversion constraints" in
317 let help_g = " always expand global definitions" in
318 let help_h = "<string> set type hierarchy (default: Z1)" in
319 let help_i = " show local references by index" in
320 let help_j = " show URI of processed kernel objects" in
321 let help_k = "<string> set kernel version (default: brg)" in
322 let help_m = " output intermediate representation (HAL)" in
323 let help_o = " use old abstract language instead of crg" in
324 let help_p = " preprocess Automath source" in
325 let help_q = " disable quotation of identifiers" in
326 let help_r = "<string> set initial segment of URI hierarchy" in
327 let help_s = "<number> set translation stage (see above)" in
328 let help_u = " activate sort inclusion" in
329 let help_x = " export kernel entities (XML)" in
330 L.box 0; L.box_err ();
333 ("-S", Arg.Int set_summary, help_S);
334 ("-V", Arg.Unit print_version, help_V);
335 ("-c", Arg.Set cc, help_c);
336 ("-g", Arg.Set expand, help_g);
337 ("-h", Arg.String set_hierarchy, help_h);
338 ("-i", Arg.Set O.indexes, help_i);
339 ("-j", Arg.Set progress, help_j);
340 ("-k", Arg.String set_kernel, help_k);
341 ("-m", Arg.Set meta, help_m);
342 ("-o", Arg.Set old, help_o);
343 ("-p", Arg.Set preprocess, help_p);
344 ("-q", Arg.Set AL.unquote, help_q);
345 ("-r", Arg.String set_root, help_r);
346 ("-s", Arg.Int set_stage, help_s);
347 ("-u", Arg.Set si, help_u);
348 ("-x", Arg.Set export, help_x)
350 with BagT.TypeError msg -> bag_error "Type Error" msg