]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/binaries/transcript/engine.ml
120d31fdd3ffa2da105e152a96a7cd81c62c608d
[helm.git] / helm / software / components / binaries / transcript / engine.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 module R  = Helm_registry
27 module X  = HExtlib
28 module T  = Types
29 module G  = Grafite
30 module HP = Http_getter
31
32 module O = Options
33
34 type script = {
35    name    : string;
36    is_ma   : bool;
37    contents: T.items
38 }
39
40 type status = {
41    heading_path: string;
42    heading_lines: int;
43    input_package: string;
44    output_package: string;
45    input_base_uri: string;
46    output_base_uri: string;
47    input_path: string;
48    output_path: string;
49    input_type: string;
50    output_type: T.output_kind;
51    coercions: (string * string) list;
52    files: string list;
53    requires: (string * string) list;
54    scripts: script array
55 }
56
57 let default_script = { 
58    name = ""; is_ma = false; contents = []
59 }
60
61 let default_scripts = 2
62
63 let load_registry registry =
64    let suffix = ".conf.xml" in
65    let registry = 
66       if Filename.check_suffix registry suffix then registry
67       else registry ^ suffix
68    in
69    Printf.eprintf "reading configuration %s ...\n" registry; flush stderr;
70    R.load_from registry
71
72 let set_files st =
73    let eof ich = try Some (input_line ich) with End_of_file -> None in
74    let trim l = Filename.chop_extension (Str.string_after l 2) in
75    let cmd = Printf.sprintf "cd %s && find -name *%s" st.input_path st.input_type in
76    let ich = Unix.open_process_in cmd in
77    let rec aux files =
78       match eof ich with
79          | None   -> List.rev files
80          | Some l -> aux (trim l :: files)
81    in 
82    let files = aux [] in
83    let _ = Unix.close_process_in ich in
84    {st with files = files}
85
86 let set_requires st =
87    let map file = (Filename.basename file, file) in
88    let requires = List.rev_map map st.files in
89    {st with requires = requires}
90
91 let init () = 
92    let transcript_dir = Filename.dirname Sys.argv.(0) in
93    let default_registry = Filename.concat transcript_dir "transcript" in
94    let matita_registry = Filename.concat !O.cwd "matita" in
95    load_registry default_registry;
96    load_registry matita_registry;
97    HP.init ()
98
99 let make registry =
100    let id x = x in
101    let get_coercions = R.get_list (R.pair id id) in 
102    let get_output_type key =
103       match R.get_string key with
104          | "procedural"  -> T.Procedural
105          | "declarative" -> T.Declarative
106          | _             -> failwith "unknown output type"
107    in
108    load_registry registry;
109    let st = {
110       heading_path = R.get_string "transcript.heading_path";
111       heading_lines = R.get_int "transcript.heading_lines";
112       input_package = R.get_string "package.input_name";
113       output_package = R.get_string "package.output_name";
114       input_base_uri = R.get_string "package.input_base_uri";
115       output_base_uri = R.get_string "package.output_base_uri";
116       input_path = R.get_string "package.input_path";
117       output_path = R.get_string "package.output_path";
118       input_type = R.get_string "package.input_type";
119       output_type = get_output_type "package.output_type";
120       coercions = get_coercions "package.coercion";
121       files = [];
122       requires = [];
123       scripts = Array.make default_scripts default_script
124    } in
125    let st = {st with
126       heading_path = Filename.concat !O.cwd st.heading_path;
127       output_path = Filename.concat !O.cwd st.output_path;
128    } in
129    prerr_endline "reading file names ...";
130    let st = set_files st in
131    let st = set_requires st in
132    st
133
134 let get_index st name = 
135    let rec get_index name i =
136       if i >= Array.length st.scripts then None else 
137       if st.scripts.(i).name = name then Some i else 
138       get_index name (succ i)
139    in
140    match get_index name 0, get_index "" 0 with
141       | Some i, _ | _, Some i -> i
142       | None, None            -> failwith "not enought script entries"
143
144 let is_ma st name =
145    let i = get_index st name in
146    let script = st.scripts.(i) in
147    st.scripts.(i) <- {script with is_ma = true}
148
149 let set_items st name items =
150    let i = get_index st name in
151    let script = st.scripts.(i) in
152    let contents = List.rev_append items script.contents in
153    st.scripts.(i) <- {script with name = name; contents = contents}
154    
155 let set_heading st name =
156    let heading = st.heading_path, st.heading_lines in
157    set_items st name [T.Heading heading] 
158    
159 let require st name inc =
160    set_items st name [T.Include inc]
161
162 let get_coercion st str =
163    try List.assoc str st.coercions with Not_found -> ""
164
165 let make_path path =
166    List.fold_left Filename.concat "" (List.rev path)
167
168 let make_prefix path =
169    String.concat "__" (List.rev path) ^ "__"
170
171 let make_script_name st script name = 
172    let ext = if script.is_ma then ".ma" else ".mma" in
173    Filename.concat st.output_path (name ^ ext)
174
175 let commit st name =
176    let i = get_index st name in
177    let script = st.scripts.(i) in
178    let path = Filename.concat st.output_path (Filename.dirname name) in
179    let name = make_script_name st script name in
180    let cmd = Printf.sprintf "mkdir -p %s" path in
181    let _ = Sys.command cmd in
182    let och = open_out name in
183    G.commit st.output_type och script.contents;
184    close_out och;
185    st.scripts.(i) <- default_script
186
187 let produce st =
188    let init name = set_heading st name in
189    let partition = function 
190       | T.Coercion (false, _)
191       | T.Notation (false, _) -> false
192       | _                     -> true
193    in
194    let produce st name =
195       let in_base_uri = Filename.concat st.input_base_uri name in
196       let out_base_uri = Filename.concat st.output_base_uri name in
197       let filter path = function
198          | T.Inline (b, k, obj, p, f)   -> 
199             let obj, p = 
200                if b then Filename.concat (make_path path) obj, make_prefix path
201                else obj, p
202             in 
203             let s = obj ^ G.string_of_inline_kind k in
204             path, Some (T.Inline (b, k, Filename.concat in_base_uri s, p, f))
205          | T.Include s                  ->
206             begin 
207                try path, Some (T.Include (List.assoc s st.requires))
208                with Not_found -> path, None
209             end
210          | T.Coercion (b, obj)          ->
211             let str = get_coercion st obj in
212             if str <> "" then path, Some (T.Coercion (b, str)) else
213             let base_uri = if b then out_base_uri else in_base_uri in
214             let s = obj ^ G.string_of_inline_kind T.Con in
215             path, Some (T.Coercion (b, Filename.concat base_uri s))
216          | T.Section (b, id, _) as item ->
217             let path = if b then id :: path else List.tl path in
218             path, Some item
219          | item                         -> path, Some item
220       in
221       Printf.eprintf "processing file name: %s ...\n" name; flush stderr; 
222       let file = Filename.concat st.input_path (name ^ st.input_type) in
223       let ich = open_in file in
224       let lexbuf = Lexing.from_channel ich in
225       try 
226          let items = V8Parser.items V8Lexer.token lexbuf in
227          close_in ich; 
228          let _, rev_items = X.list_rev_map_filter_fold filter [] items in
229          let items = List.rev rev_items in
230          let local_items, global_items = List.partition partition items in
231          let comment = T.Line (Printf.sprintf "From %s" name) in 
232          if global_items <> [] then 
233             set_items st st.input_package (comment :: global_items);
234          init name; require st name st.input_package;
235          set_items st name local_items; commit st name
236       with e -> 
237          prerr_endline (Printexc.to_string e); close_in ich 
238    in
239    is_ma st st.input_package;
240    init st.input_package; require st st.input_package "preamble"; 
241    List.iter (produce st) st.files; 
242    commit st st.input_package