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_______________________________________________________________ *)
22 module XL = XmlLibrary
24 module AA = AutProcess
29 module MO = MetaOutput
30 module ML = MetaLibrary
34 module BR = BrgReduction
35 module BU = BrgUntrusted
39 module ZU = BagUntrusted
53 let flush_all () = L.flush 0; L.flush_err ()
56 L.error ZO.specs (L.Warn s :: L.Loc :: msg); flush_all ()
59 L.error BR.specs (L.Warn s :: L.Loc :: msg); flush_all ()
61 let initial_status () = {
62 ac = AO.initial_counters;
63 mc = MO.initial_counters;
64 brgc = BO.initial_counters;
65 bagc = ZO.initial_counters;
66 mst = MA.initial_status ();
67 dst = AD.initial_status ();
68 tst = TD.initial_status ();
69 ast = AA.initial_status ();
70 kst = E.initial_status ()
73 let refresh_status st = {st with
74 mst = MA.refresh_status st.mst;
75 dst = AD.refresh_status st.dst;
76 tst = TD.refresh_status st.tst;
77 kst = E.refresh_status st.kst
80 (* kernel related ***********************************************************)
82 type kernel = Brg | Bag
84 type kernel_entity = BrgEntity of Brg.entity
85 | BagEntity of Bag.entity
86 | CrgEntity of Crg.entity
87 | MetaEntity of Meta.entity
91 let print_counters st = match !kernel with
92 | Brg -> BO.print_counters C.start st.brgc
93 | Bag -> ZO.print_counters C.start st.bagc
95 let xlate_entity entity = match !kernel, entity with
97 let f e = (BrgEntity e) in E.xlate f BD.brg_of_crg e
98 | Brg, MetaEntity e ->
99 let f e = (BrgEntity e) in E.xlate f MB.brg_of_meta e
100 | Bag, MetaEntity e ->
101 let f e = (BagEntity e) in E.xlate f MZ.bag_of_meta e
102 | _, entity -> entity
106 let s = U.string_of_uri u in
107 let err () = L.warn (P.sprintf "%s" s) in
108 let f i = L.warn (P.sprintf "[%u] %s" i s) in
112 | CrgEntity e -> E.common f e
113 | BrgEntity e -> E.common f e
114 | BagEntity e -> E.common f e
115 | MetaEntity e -> E.common f e
117 let count_entity st = function
118 | MetaEntity e -> {st with mc = MO.count_entity C.start st.mc e}
119 | BrgEntity e -> {st with brgc = BO.count_entity C.start st.brgc e}
120 | BagEntity e -> {st with bagc = ZO.count_entity C.start st.bagc e}
123 let export_entity si xdir moch = function
124 | CrgEntity e -> XL.export_entity XD.export_term si xdir e
125 | BrgEntity e -> XL.export_entity BO.export_term si xdir e
127 begin match moch with
129 | Some och -> ML.write_entity C.start och e
133 let type_check st k =
134 let brg_err msg = brg_error "Type Error" msg; failwith "Interrupted" in
137 | BrgEntity entity -> BU.type_check brg_err ok st.kst entity
138 | BagEntity entity -> ZU.type_check ok st.kst entity
142 (* extended lexer ***********************************************************)
144 type 'token lexer = {
145 parse : Lexing.lexbuf -> 'token;
146 mutable tokbuf: 'token option;
150 let initial_lexer parse = {
151 parse = parse; tokbuf = None; unget = false
154 let token xl lexbuf = match xl.tokbuf with
155 | Some token when xl.unget ->
156 xl.unget <- false; token
158 let token = xl.parse lexbuf in
159 xl.tokbuf <- Some token; token
161 (* input related ************************************************************)
163 type input = Text | Automath
165 type input_entity = TxtEntity of Txt.command
166 | 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 AutParser.entry (token aut_xl) lexbuf with
191 | Some e -> aut_xl.unget <- true; AutEntity e
195 begin match TxtParser.entry (token txt_xl) lexbuf with
196 | Some e -> txt_xl.unget <- true; TxtEntity e
202 let process_input f st = function
204 let f ast e = f {st with ast = ast} (AutEntity e) in
205 AA.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 ())
224 let streaming = ref false (* parsing style (temporary) *)
226 let process_2 st entity =
227 let st = if !L.level > 2 then count_entity st entity else st in
228 if !export <> "" then export_entity !G.si !export !moch entity;
229 if !stage > 2 then type_check st entity else st
231 let process_1 st entity =
232 if !progress then pp_progress entity;
233 let st = if !L.level > 2 then count_entity st entity else st in
234 if !export <> "" && !stage = 1 then export_entity !G.si !export !moch entity;
235 if !stage > 1 then process_2 st (xlate_entity entity) else st
237 let process_0 st entity =
239 if !stage = 0 then st else
240 match entity, !old with
241 | AutEntity e, true ->
242 let frr mst = {st with mst = mst} in
243 let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
244 MA.meta_of_aut frr h st.mst e
245 | AutEntity e, false ->
246 let err dst = {st with dst = dst} in
247 let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
248 AD.crg_of_aut err g st.dst e
250 let crr tst = {st with tst = tst} in
251 let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
252 TD.crg_of_txt crr d gen_text st.tst e
253 | NoEntity, _ -> assert false
255 let st = if !L.level > 2 then count_input st entity else st in
256 if !preprocess then process_input f st entity else f st entity
258 let process_nostreaming st lexbuf input =
259 let rec aux1 book = match entity_of_input lexbuf input with
260 | NoEntity -> List.rev book
261 | e -> aux1 (e :: book)
263 let rec aux2 st = function
265 | entity :: tl -> aux2 (process_0 st entity) tl
269 let rec process_streaming st lexbuf input = match entity_of_input lexbuf input with
271 | e -> process_streaming (process_0 st e) lexbuf input
273 (****************************************************************************)
275 let process st name =
276 let process = if !streaming then process_streaming else process_nostreaming in
277 let input = type_of_input name in
278 let ich = open_in name in
279 let lexbuf = Lexing.from_channel ich in
280 let st = process st lexbuf input in
281 close_in ich; st, input
285 let version_string = "Helena 0.8.1 M - August 2010" in
286 let print_version () = L.warn (version_string ^ "\n"); exit 0 in
287 let set_hierarchy s =
288 if H.set_graph s then () else
289 L.warn (P.sprintf "Unknown type hierarchy: %s" s)
291 let set_kernel = function
292 | "brg" -> kernel := Brg
293 | "bag" -> kernel := Bag
294 | s -> L.warn (P.sprintf "Unknown kernel version: %s" s)
296 let set_summary i = L.level := i in
297 let set_stage i = stage := i in
298 let set_meta_file name =
299 let f och = moch := Some och in
302 let set_xdir s = export := s in
303 let set_root s = root := s in
306 | Some och -> ML.close_out C.start och
308 let clear_options () =
309 stage := 3; moch := None; meta := false; progress := false;
310 preprocess := false; root := ""; cc := false; export := "";
311 old := false; kernel := Brg; st := initial_status ();
312 L.clear (); G.clear (); H.clear (); O.clear_reductions ();
315 let process_file name =
316 if !L.level > 0 then Y.gmtime version_string;
318 L.warn (P.sprintf "Processing file: %s" name);
319 if !L.level > 0 then Y.utime_stamp "started";
320 let base_name = Filename.chop_extension (Filename.basename name) in
321 if !meta then set_meta_file base_name;
323 if !stage < 2 then Crg.mk_uri else
328 let cover = F.concat !root base_name in
329 G.mk_uri := mk_uri; G.cover := cover;
330 let sst, input = process (refresh_status !st) name in
332 if !L.level > 0 then Y.utime_stamp "processed";
333 if !L.level > 2 then begin
334 AO.print_counters C.start !st.ac;
335 if !preprocess then AO.print_process_counters C.start !st.ast;
336 if !stage > 0 then MO.print_counters C.start !st.mc;
337 if !stage > 1 then print_counters !st;
338 if !stage > 2 then O.print_reductions ()
343 if !L.level > 0 then Y.utime_stamp "at exit";
347 "Usage: helena [ -LPVXcgijmopqu1 | -Ss <number> | -x <dir> | -hkr <string> ]* [ <file> ]*\n\n" ^
348 "Summary levels: 0 just errors (default), 1 time stamps, 2 processed file names, \
349 3 data information, 4 typing information, 5 reduction information\n\n" ^
350 "Stages: 0 parsing, 1 to intermediate, 2 to untrusted, 3 to trusted (default)\n"
352 let help_L = " show lexer debug information" in
353 let help_P = " show parser debug information" in
354 let help_S = "<number> set summary level (see above)" in
355 let help_V = " show version information" in
356 let help_X = " clear options" in
358 let help_c = " output conversion constraints" in
359 let help_g = " always expand global definitions" in
360 let help_h = "<string> set type hierarchy (default: Z1)" in
361 let help_i = " show local references by index" in
362 let help_j = " show URI of processed kernel objects" in
363 let help_k = "<string> set kernel version (default: brg)" in
364 let help_m = " output intermediate representation (HAL)" in
365 let help_o = " use old abstract language instead of crg" in
366 let help_p = " preprocess source" in
367 let help_q = " disable quotation of identifiers" in
368 let help_r = "<string> set initial segment of URI hierarchy" in
369 let help_s = "<number> set translation stage (see above)" in
370 let help_u = " activate sort inclusion" in
371 let help_x = "<dir> export kernel entities (XML) to <dir>" in
373 let help_1 = " parse files with streaming policy" in
374 L.box 0; L.box_err ();
377 ("-L", Arg.Set G.debug_lexer, help_L);
378 ("-P", Arg.Set G.debug_parser, help_P);
379 ("-S", Arg.Int set_summary, help_S);
380 ("-V", Arg.Unit print_version, help_V);
381 ("-X", Arg.Unit clear_options, help_X);
382 ("-c", Arg.Set cc, help_c);
383 ("-g", Arg.Set G.expand, help_g);
384 ("-h", Arg.String set_hierarchy, help_h);
385 ("-i", Arg.Set G.indexes, help_i);
386 ("-j", Arg.Set progress, help_j);
387 ("-k", Arg.String set_kernel, help_k);
388 ("-m", Arg.Set meta, help_m);
389 ("-o", Arg.Set old, help_o);
390 ("-p", Arg.Set preprocess, help_p);
391 ("-q", Arg.Set G.unquote, help_q);
392 ("-r", Arg.String set_root, help_r);
393 ("-s", Arg.Int set_stage, help_s);
394 ("-u", Arg.Set G.si, help_u);
395 ("-x", Arg.String set_xdir, help_x);
396 ("-1", Arg.Set streaming, help_1);
398 with ZT.TypeError msg -> bag_error "Type Error" msg