]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/binaries/transcript/engine.ml
- transcript: bugfix
[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 HG = 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: T.input_kind;
50    output_type: T.output_kind;
51    input_ext: string;
52    remove_lines: int;
53    includes: (string * string) list;
54    coercions: (string * string) list;
55    files: string list;
56    requires: (string * string) list;
57    scripts: script array
58 }
59
60 let default_script = { 
61    name = ""; is_ma = false; contents = []
62 }
63
64 let default_scripts = 2
65
66 let load_registry registry =
67    let suffix = ".conf.xml" in
68    let registry = 
69       if Filename.check_suffix registry suffix then registry
70       else registry ^ suffix
71    in
72    Printf.eprintf "reading configuration %s ...\n" registry; flush stderr;
73    R.load_from registry
74
75 let set_files st =
76    let eof ich = try Some (input_line ich) with End_of_file -> None in
77    let trim l = Filename.chop_extension (Str.string_after l 2) in 
78    let cmd = Printf.sprintf "cd %s && find -name *%s" st.input_path st.input_ext in
79    let ich = Unix.open_process_in cmd in
80    let rec aux files =
81       match eof ich with
82          | None   -> List.rev files
83          | Some l -> aux (trim l :: files)
84    in 
85    let files = aux [] in
86    let _ = Unix.close_process_in ich in
87    {st with files = files}
88
89 let set_requires st =
90    let map file = (Filename.basename file, file) in
91    let requires = List.rev_map map st.files in
92    {st with requires = requires}
93
94 let init () = 
95    let transcript_dir = Filename.dirname Sys.argv.(0) in
96    let default_registry = Filename.concat transcript_dir "transcript" in
97    let matita_registry = Filename.concat !O.cwd "matita" in
98    load_registry default_registry;
99    load_registry matita_registry;
100    HG.init ()
101
102 let make registry =
103    let id x = x in
104    let get_pairs = R.get_list (R.pair id id) in 
105    let get_input_type key =
106       match R.get_string key with
107          | "gallina8" -> T.Gallina8, ".v"
108          | "grafite"  -> T.Grafite, ".ma"
109          | _          -> failwith "unknown input type"
110    in
111    let get_output_type key =
112       match R.get_string key with
113          | "procedural"  -> T.Procedural
114          | "declarative" -> T.Declarative
115          | _             -> failwith "unknown output type"
116    in
117    load_registry registry;
118    let input_type, input_ext = get_input_type "package.input_type" in 
119    let st = {
120       heading_path = R.get_string "transcript.heading_path";
121       heading_lines = R.get_int "transcript.heading_lines";
122       input_package = R.get_string "package.input_name";
123       output_package = R.get_string "package.output_name";
124       input_base_uri = R.get_string "package.input_base_uri";
125       output_base_uri = R.get_string "package.output_base_uri";
126       input_path = R.get_string "package.input_path";
127       output_path = R.get_string "package.output_path";
128       input_type = input_type;
129       output_type = get_output_type "package.output_type";
130       input_ext = input_ext;
131       remove_lines = R.get_int "package.heading_lines";
132       includes = get_pairs "package.include";
133       coercions = get_pairs "package.coercion";
134       files = [];
135       requires = [];
136       scripts = Array.make default_scripts default_script
137    } in
138    let st = {st with
139       heading_path = Filename.concat !O.cwd st.heading_path;
140       input_path = Filename.concat !O.cwd st.input_path;
141       output_path = Filename.concat !O.cwd st.output_path
142    } in
143    prerr_endline "reading file names ...";
144    let st = set_files st in
145    let st = set_requires st in
146    st
147
148 let get_index st name = 
149    let rec get_index name i =
150       if i >= Array.length st.scripts then None else 
151       if st.scripts.(i).name = name then Some i else 
152       get_index name (succ i)
153    in
154    match get_index name 0, get_index "" 0 with
155       | Some i, _ | _, Some i -> i
156       | None, None            -> failwith "not enought script entries"
157
158 let is_ma st name =
159    let i = get_index st name in
160    let script = st.scripts.(i) in
161    st.scripts.(i) <- {script with is_ma = true}
162
163 let set_items st name items =
164    let i = get_index st name in
165    let script = st.scripts.(i) in
166    let contents = List.rev_append items script.contents in
167    st.scripts.(i) <- {script with name = name; contents = contents}
168    
169 let set_heading st name = 
170    let heading = st.heading_path, st.heading_lines in
171    set_items st name [T.Heading heading] 
172    
173 let require st name inc =
174    set_items st name [T.Include inc]
175
176 let get_coercion st str =
177    try List.assoc str st.coercions with Not_found -> ""
178
179 let make_path path =
180    List.fold_left Filename.concat "" (List.rev path)
181
182 let make_prefix path =
183    String.concat "__" (List.rev path) ^ "__"
184
185 let make_script_name st script name = 
186    let ext = if script.is_ma then ".ma" else ".mma" in
187    Filename.concat st.output_path (name ^ ext)
188
189 let commit st name =
190    let i = get_index st name in
191    let script = st.scripts.(i) in
192    let path = Filename.concat st.output_path (Filename.dirname name) in
193    let name = make_script_name st script name in
194    let cmd = Printf.sprintf "mkdir -p %s" path in
195    let _ = Sys.command cmd in
196    let och = open_out name in
197    G.commit st.output_type och script.contents;
198    close_out och;
199    st.scripts.(i) <- default_script
200
201 let produce st =
202    let init name = set_heading st name in
203    let partition = function 
204       | T.Coercion (false, _)
205       | T.Notation (false, _) -> false
206       | _                     -> true
207    in
208    let get_items = match st.input_type with
209       | T.Gallina8 -> Gallina8Parser.items Gallina8Lexer.token
210       | T.Grafite  -> GrafiteParser.items GrafiteLexer.token
211    in
212    let produce st name =
213       let in_base_uri = Filename.concat st.input_base_uri name in
214       let out_base_uri = Filename.concat st.output_base_uri name in
215       let filter path = function
216          | T.Inline (b, k, obj, p, f)   -> 
217             let obj, p = 
218                if b then Filename.concat (make_path path) obj, make_prefix path
219                else obj, p
220             in 
221             let s = obj ^ G.string_of_inline_kind k in
222             path, Some (T.Inline (b, k, Filename.concat in_base_uri s, p, f))
223          | T.Include s                  ->
224             begin 
225                try path, Some (T.Include (List.assoc s st.requires))
226                with Not_found -> path, None
227             end
228          | T.Coercion (b, obj)          ->
229             let str = get_coercion st obj in
230             if str <> "" then path, Some (T.Coercion (b, str)) else
231             let base_uri = if b then out_base_uri else in_base_uri in
232             let s = obj ^ G.string_of_inline_kind T.Con in
233             path, Some (T.Coercion (b, Filename.concat base_uri s))
234          | T.Section (b, id, _) as item ->
235             let path = if b then id :: path else List.tl path in
236             path, Some item
237          | item                         -> path, Some item
238       in
239       let set_includes st name =
240          try require st name (List.assoc name st.includes) 
241          with Not_found -> ()
242       in
243       let rec remove_lines ich n =
244          if n > 0 then let _ =  input_line ich in remove_lines ich (pred n)
245       in
246       Printf.eprintf "processing file name: %s ...\n" name; flush stderr; 
247       let file = Filename.concat st.input_path (name ^ st.input_ext) in
248       let ich = open_in file in
249       begin try remove_lines ich st.remove_lines with End_of_file -> () end;
250       let lexbuf = Lexing.from_channel ich in
251       try 
252          let items = get_items lexbuf in close_in ich; 
253          let _, rev_items = X.list_rev_map_filter_fold filter [] items in
254          let items = List.rev rev_items in
255          let local_items, global_items = List.partition partition items in
256          let comment = T.Line (Printf.sprintf "From %s" name) in 
257          if global_items <> [] then 
258             set_items st st.input_package (comment :: global_items);
259          init name; require st name st.input_package; set_includes st name;
260          set_items st name local_items; commit st name
261       with e -> 
262          prerr_endline (Printexc.to_string e); close_in ich 
263    in
264    is_ma st st.input_package;
265    init st.input_package; require st st.input_package "preamble"; 
266    List.iter (produce st) st.files; 
267    commit st st.input_package