]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/lambda-delta/toplevel/top.ml
- we added a parser for lambda-delta textual syntax (file extension .hln)
[helm.git] / helm / software / lambda-delta / toplevel / top.ml
index 515e6590ee03c4113efb02d24a72bb5b4e43c894..e9ae20eeb8c00067fc7e1aee83ceaf75be4b6d17 100644 (file)
@@ -9,6 +9,7 @@
      \ /   This software is distributed as is, NO WARRANTY.              
       V_______________________________________________________________ *)
 
+module F    = Filename
 module P    = Printf
 module U    = NUri
 module C    = Cps
@@ -21,6 +22,7 @@ module X    = Library
 module AL   = AutLexer
 module AP   = AutProcess
 module AO   = AutOutput
+module DT   = CrgTxt
 module DA   = CrgAut
 module MA   = MetaAut
 module MO   = MetaOutput
@@ -40,6 +42,7 @@ type status = {
    ast : AP.status;
    dst : DA.status;
    mst : MA.status;
+   tst : DT.status;
    ac  : AO.counters;
    mc  : MO.counters;
    brgc: BrgO.counters;
@@ -47,6 +50,14 @@ type status = {
    kst : Y.status
 }
 
+let flush_all () = L.flush 0; L.flush_err ()
+
+let bag_error s msg =
+   L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all () 
+
+let brg_error s msg =
+   L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
+
 let initial_status mk_uri cover g expand si = {
    ac   = AO.initial_counters;
    mc   = MO.initial_counters;
@@ -54,22 +65,11 @@ let initial_status mk_uri cover g expand si = {
    bagc = BagO.initial_counters;
    mst  = MA.initial_status ~cover ();
    dst  = DA.initial_status (mk_uri si cover);
+   tst  = DT.initial_status (mk_uri si cover);
    ast  = AP.initial_status;
    kst  = Y.initial_status g expand si
 }
 
-let flush_all () = L.flush 0; L.flush_err ()
-
-let bag_error s msg =
-   L.error BagO.specs (L.Warn s :: L.Loc :: msg); flush_all () 
-
-let brg_error s msg =
-   L.error BrgR.specs (L.Warn s :: L.Loc :: msg); flush_all () 
-
-let process_entity f st entity =
-   let f ast = f {st with ast = ast} in
-   AP.process_entity f st.ast entity
-
 (* kernel related ***********************************************************)
 
 type kernel = Brg | Bag
@@ -132,6 +132,65 @@ let type_check st k =
       | CrgEntity _
       | MetaEntity _     -> st
 
+(* extended lexer ***********************************************************)
+
+type 'token lexer = {
+   parse : Lexing.lexbuf -> 'token;
+   mutable tokbuf: 'token option;
+   mutable unget : bool
+}
+
+let initial_lexer parse = {
+   parse = parse; tokbuf = None; unget = false
+}
+
+let token xl lexbuf = match xl.tokbuf with
+   | Some token when xl.unget ->   
+      xl.unget <- false; token
+   | _                        ->
+      let token = xl.parse lexbuf in
+      xl.tokbuf <- Some token; token
+
+(* input related ************************************************************)
+
+type input = Text | Automath
+
+type input_entity = TxtEntity of Txt.entity
+                  | AutEntity of Aut.entity
+
+let type_of_input name =
+   if F.check_suffix name ".hln" then Text 
+   else if F.check_suffix name ".aut" then Automath
+   else begin
+      L.warn (P.sprintf "Unknown file type: %s" name); exit 2
+   end
+
+let txt_xl = initial_lexer TxtLexer.token 
+
+let aut_xl = initial_lexer AutLexer.token 
+
+let entity_of_input lexbuf = function
+   | Text     -> 
+      begin match TxtParser.entry (token txt_xl) lexbuf with
+         | Some e, unget -> txt_xl.unget <- unget; Some (TxtEntity e)
+         | None, _       -> None
+      end
+   | Automath -> 
+      begin match AutParser.entry (token aut_xl) lexbuf with
+         | Some e, unget -> aut_xl.unget <- unget; Some (AutEntity e)
+         | None, _       -> None
+      end
+
+let process_input f st = function 
+   | AutEntity e     ->
+      let f ast e = f {st with ast = ast} (AutEntity e) in
+      AP.process_entity f st.ast e
+   | xe              -> f st xe
+
+let count_input st = function
+   | AutEntity e -> {st with ac = AO.count_entity C.start st.ac e}
+   | xe          -> st
+
 (****************************************************************************)
 
 let stage = ref 3
@@ -165,24 +224,32 @@ let process_0 st entity =
       let h mst e = process_1 {st with mst = mst} (MetaEntity e) in
       let err dst = {st with dst = dst} in
       let g dst e = process_1 {st with dst = dst} (CrgEntity e) in
-      if !old then MA.meta_of_aut frr h st.mst entity else 
-      DA.crg_of_aut err g st.dst entity
+      let crr tst = {st with tst = tst} in
+      let d tst e = process_1 {st with tst = tst} (CrgEntity e) in
+      match entity, !old with
+         | AutEntity e, true  -> MA.meta_of_aut frr h st.mst e 
+         | AutEntity e, false -> DA.crg_of_aut err g st.dst e
+        | TxtEntity e, false -> DT.crg_of_txt crr d st.tst e
+        | _                  -> assert false
    in
-   let st = 
-      if !L.level > 2 then {st with ac = AO.count_entity C.start st.ac entity}
-      else st
-   in 
-   if !preprocess then process_entity f st entity else f st entity
+   let st = if !L.level > 2 then count_input st entity else st in 
+   if !preprocess then process_input f st entity else f st entity
 
-let rec process st = function
-   | []           -> st
-   | entity :: tl -> process (process_0 st entity) tl
+let process st name =
+   let input = type_of_input name in
+   let ich = open_in name in
+   let lexbuf = Lexing.from_channel ich in 
+   let rec aux st = match entity_of_input lexbuf input with
+      | None   -> close_in ich; st, input
+      | Some e -> aux (process_0 st e)
+   in
+   aux st
 
 (****************************************************************************)
 
 let main =
 try 
-   let version_string = "Helena 0.8.1 M - January 2010" in
+   let version_string = "Helena 0.8.1 M - February 2010" in
    let set_hierarchy s = 
       let err () = L.warn (P.sprintf "Unknown type hierarchy: %s" s) in
       let f g = graph := g in
@@ -207,18 +274,13 @@ try
       | None     -> ()
       | Some och -> ML.close_out C.start och
    in
-   let read_file name =
+   let process_file name =
       if !L.level > 0 then T.gmtime version_string;      
       if !L.level > 1 then
          L.warn (P.sprintf "Processing file: %s" name);
       if !L.level > 0 then T.utime_stamp "started";
       let base_name = Filename.chop_extension (Filename.basename name) in
       if !meta then set_meta_file base_name;
-      let ich = open_in name in
-      let lexbuf = Lexing.from_channel ich in
-      let book = AutParser.book AutLexer.token lexbuf in
-      close_in ich;
-      if !L.level > 0 then T.utime_stamp "parsed";
       O.clear_reductions ();
       let mk_uri =
          if !stage < 2 then Crg.mk_uri else
@@ -228,7 +290,7 @@ try
       in
       let cover = if !use_cover then base_name else "" in
       let st = initial_status mk_uri cover !graph !expand !si in
-      let st = process st book in
+      let st, input = process st name in
       if !L.level > 0 then T.utime_stamp "processed";
       if !L.level > 2 then begin
          AO.print_counters C.start st.ac;
@@ -286,5 +348,5 @@ try
       ("-s", Arg.Int set_stage, help_s);
       ("-u", Arg.Set si, help_u);
       ("-x", Arg.Set export, help_x)
-   ] read_file help;
+   ] process_file help;
 with BagT.TypeError msg -> bag_error "Type Error" msg