]> matita.cs.unibo.it Git - helm.git/blobdiff - components/binaries/transcript/engine.ml
- components: composed coercions mus be generated with current base uri
[helm.git] / components / binaries / transcript / engine.ml
index 8908897dd3017684192f90736d83280981cee3ea..61787318b430edc46b92c71df2aa9547cf93ddea 100644 (file)
@@ -37,12 +37,14 @@ type status = {
    helm_dir: string;
    heading_path: string;
    heading_lines: int;
-   package: string;
+   input_package: string;
+   output_package: string;
    input_base_uri: string;
    output_base_uri: string;
    input_path: string;
    output_path: string;
    script_ext: string;
+   coercions: (string * string) list;
    files: string list;
    requires: (string * string) list;
    scripts: script array
@@ -87,17 +89,21 @@ let init () =
    load_registry default_registry
 
 let make registry =
+   let id x = x in
+   let get_coercions = R.get_list (R.pair id id) in 
    load_registry registry;
    let st = {
       helm_dir = R.get_string "transcript.helm_dir";
       heading_path = R.get_string "transcript.heading_path";
       heading_lines = R.get_int "transcript.heading_lines";
-      package = R.get_string "package.name";
+      input_package = R.get_string "package.input_name";
+      output_package = R.get_string "package.output_name";
       input_base_uri = R.get_string "package.input_base_uri";
       output_base_uri = R.get_string "package.output_base_uri";
       input_path = R.get_string "package.input_path";
       output_path = R.get_string "package.output_path";
       script_ext = R.get_string "package.script_type";
+      coercions = get_coercions "package.coercion";
       files = [];
       requires = [];
       scripts = Array.make default_scripts default_script
@@ -131,9 +137,18 @@ let set_baseuri st name =
    let baseuri = Filename.concat st.output_base_uri name in
    set_items st name [T.BaseUri baseuri]
    
-let require st name =
-   set_items st name [T.Include st.package]
-   
+let require st name inc =
+   set_items st name [T.Include inc]
+
+let get_coercion st str =
+   try List.assoc str st.coercions with Not_found -> ""
+
+let make_path path =
+   List.fold_left Filename.concat "" (List.rev path)
+
+let make_prefix path =
+   String.concat "__" (List.rev path) ^ "__"
+
 let commit st name =
    let i = get_index st name in
    let script = st.scripts.(i) in
@@ -149,25 +164,36 @@ let commit st name =
 let produce st =
    let init name = set_heading st name; set_baseuri st name in
    let partition = function 
-      | T.Notation _ -> false
-      | _            -> true
+      | T.Coercion (false, _)
+      | T.Notation (false, _) -> false
+      | _                     -> true
    in
    let produce st name =
       let in_base_uri = Filename.concat st.input_base_uri name in
       let out_base_uri = Filename.concat st.output_base_uri name in
-      let filter = function
-         | T.Inline (k, obj) -> 
+      let filter path = function
+         | T.Inline (b, k, obj, p)      -> 
+           let obj, p = 
+              if b then Filename.concat (make_path path) obj, make_prefix path
+              else obj, p
+           in 
            let s = obj ^ G.string_of_inline_kind k in
-           Some (T.Inline (k, Filename.concat in_base_uri s))
-        | T.Include s       ->
+           path, Some (T.Inline (b, k, Filename.concat in_base_uri s, p))
+        | T.Include s                  ->
            begin 
-              try Some (T.Include (List.assoc s st.requires))
-              with Not_found -> None
+              try path, Some (T.Include (List.assoc s st.requires))
+              with Not_found -> path, None
            end
-        | T.Coercion obj    ->
+        | T.Coercion (b, obj)          ->
+           let str = get_coercion st obj in
+           if str <> "" then path, Some (T.Coercion (b, str)) else
+           let base_uri = if b then out_base_uri else in_base_uri in
            let s = obj ^ G.string_of_inline_kind T.Con in
-           Some (T.Coercion (Filename.concat out_base_uri s))
-        | item              -> Some item
+           path, Some (T.Coercion (b, Filename.concat base_uri s))
+        | T.Section (b, id, _) as item ->
+           let path = if b then id :: path else List.tl path in
+           path, Some item
+        | item                         -> path, Some item
       in
       Printf.eprintf "processing file name: %s ...\n" name; flush stderr; 
       let file = Filename.concat st.input_path (name ^ st.script_ext) in
@@ -175,15 +201,18 @@ let produce st =
       let lexbuf = Lexing.from_channel ich in
       try 
          let items = V8Parser.items V8Lexer.token lexbuf in
-         close_in ich;
-         let items = List.rev (X.list_rev_map_filter filter items) in
+         close_in ich; 
+        let _, rev_items = X.list_rev_map_filter_fold filter [] items in
+        let items = List.rev rev_items in
         let local_items, global_items = List.partition partition items in
         let comment = T.Line (Printf.sprintf "From %s" name) in 
         if global_items <> [] then 
-           set_items st st.package (comment :: global_items);
-        init name; require st name;
+           set_items st st.input_package (comment :: global_items);
+        init name; require st name st.input_package;
         set_items st name local_items; commit st name
       with e -> 
          prerr_endline (Printexc.to_string e); close_in ich 
    in
-   init st.package; List.iter (produce st) st.files; commit st st.package
+   init st.input_package; 
+   List.iter (produce st) st.files; 
+   commit st st.input_package