]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/binaries/transcript/engine.ml
Preparing for 0.5.9 release.
[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 HG = Http_getter
29 module GA = GrafiteAst
30
31 module T = Types
32 module G = Grafite
33 module O = Options
34
35 type script = {
36    name    : string;
37    is_ma   : bool;
38    contents: T.items
39 }
40
41 type status = {
42    heading_path: string;
43    heading_lines: int;
44    input_package: string;
45    output_package: string;
46    input_base_uri: string;
47    output_base_uri: string;
48    input_path: string;
49    output_path: string;
50    input_type: T.input_kind;
51    output_type: T.output_kind;
52    input_ext: string;
53    remove_lines: int;
54    excludes: string list;
55    includes: (string * string) list;
56    iparams: (string * string) list;
57    coercions: (string * string) list;
58    files: string list;
59    requires: (string * string) list;
60    scripts: script array
61 }
62
63 let default_script = { 
64    name = ""; is_ma = false; contents = []
65 }
66
67 let default_scripts = 2
68
69 let suffix = ".conf.xml"
70
71 let load_registry registry =
72    let registry = 
73       if Filename.check_suffix registry suffix then registry
74       else registry ^ suffix
75    in
76    Printf.eprintf "reading configuration %s ...\n" registry; flush stderr;
77    R.load_from registry
78
79 let set_files st =
80    let eof ich = try Some (input_line ich) with End_of_file -> None in
81    let trim l = Filename.chop_extension (Str.string_after l 2) in 
82    let cmd = Printf.sprintf "cd %s && find -name '*%s'" st.input_path st.input_ext in
83    let ich = Unix.open_process_in cmd in
84    let rec aux files = match eof ich with
85       | None   -> List.rev files
86       | Some l ->
87          let l = trim l in
88          if List.mem l st.excludes then aux files else 
89          if !O.sources = [] || List.mem l !O.sources then aux (l :: files) else
90          aux files
91    in
92    let files = aux [] in
93    let _ = Unix.close_process_in ich in
94    {st with files = files}
95
96 let set_requires st =
97    let map file = (Filename.basename file, file) in
98    let requires = List.rev_map map st.files in
99    {st with requires = requires}
100
101 let init () = 
102    let transcript_dir = Filename.dirname Sys.argv.(0) in
103    let default_registry = Filename.concat transcript_dir "transcript" in
104    let matita_registry = Filename.concat !O.cwd "matita" in
105    load_registry default_registry;
106    load_registry matita_registry;
107    HG.init ()
108
109 let make registry =
110    let id x = x in
111    let get_pairs = R.get_list (R.pair id id) in 
112    let get_input_type key1 key2 =
113       match R.get_string key1, R.get_string key2 with
114          | "gallina8", _ -> T.Gallina8, ".v", []
115          | "grafite", "" -> T.Grafite "", ".ma", []
116          | "grafite", s  -> T.Grafite s, ".ma", [s]
117          | s, _          -> failwith ("unknown input type: " ^ s)
118    in
119    let get_output_type key =
120       match R.get_string key with
121          | "procedural"  -> T.Procedural
122          | "declarative" -> T.Declarative
123          | s             -> failwith ("unknown output type: " ^ s)
124    in
125    load_registry registry;
126    let input_type, input_ext, excludes = 
127       get_input_type "package.input_type" "package.theory_file"
128    in 
129    let st = {
130       heading_path = R.get_string "transcript.heading_path";
131       heading_lines = R.get_int "transcript.heading_lines";
132       input_package = R.get_string "package.input_name";
133       output_package = R.get_string "package.output_name";
134       input_base_uri = R.get_string "package.input_base_uri";
135       output_base_uri = R.get_string "package.output_base_uri";
136       input_path = R.get_string "package.input_path";
137       output_path = R.get_string "package.output_path";
138       input_type = input_type;
139       output_type = get_output_type "package.output_type";
140       input_ext = input_ext;
141       remove_lines = R.get_int "package.heading_lines";
142       excludes = excludes;
143       includes = get_pairs "package.include";
144       iparams = get_pairs "package.inline";
145       coercions = get_pairs "package.coercion";
146       files = [];
147       requires = [];
148       scripts = Array.make default_scripts default_script
149    } in
150    let st = {st with
151       heading_path = Filename.concat !O.cwd st.heading_path;
152       input_path = Filename.concat !O.cwd st.input_path;
153       output_path = Filename.concat !O.cwd st.output_path
154    } in
155    prerr_endline "reading file names ...";
156    let st = set_files st in
157    let st = set_requires st in
158    st
159
160 let get_index st name = 
161    let rec get_index name i =
162       if i >= Array.length st.scripts then None else 
163       if st.scripts.(i).name = name then Some i else 
164       get_index name (succ i)
165    in
166    match get_index name 0, get_index "" 0 with
167       | Some i, _ | _, Some i -> i
168       | None, None            -> failwith "not enought script entries"
169
170 let is_ma st name =
171    let i = get_index st name in
172    let script = st.scripts.(i) in
173    st.scripts.(i) <- {script with is_ma = true}
174
175 let set_items st name items =
176    let i = get_index st name in
177    let script = st.scripts.(i) in
178    let contents = List.rev_append (X.list_uniq items) script.contents in
179    st.scripts.(i) <- {script with name = name; contents = contents}
180    
181 let set_heading st name = 
182    let heading = st.heading_path, st.heading_lines in
183    set_items st name [T.Heading heading] 
184    
185 let require st name moo inc =
186    set_items st name [T.Include (moo, inc)]
187
188 let get_coercion st str =
189    try List.assoc str st.coercions with Not_found -> ""
190
191 let make_path path =
192    List.fold_left Filename.concat "" (List.rev path)
193
194 let make_prefix path =
195    String.concat "__" (List.rev path) ^ "__"
196
197 let make_script_name st script name = 
198    let ext = if script.is_ma then ".ma" else ".mma" in
199    Filename.concat st.output_path (name ^ ext)
200
201 let get_iparams st name =
202    let debug debug = GA.IPDebug debug in
203    let map = function
204       | "comments"   -> GA.IPComments
205       | "nodefaults" -> GA.IPNoDefaults
206       | "coercions"  -> GA.IPCoercions
207       | "cr"         -> GA.IPCR
208       | s            -> 
209          try Scanf.sscanf s "debug-%u" debug with
210             | Scanf.Scan_failure _
211             | Failure _
212             | End_of_file ->
213                failwith ("unknown inline parameter: " ^ s)
214    in
215    List.map map (X.list_assoc_all name st.iparams) 
216
217 let commit st name =
218    let i = get_index st name in
219    let script = st.scripts.(i) in
220    let path = Filename.concat st.output_path (Filename.dirname name) in
221    let name = make_script_name st script name in
222    let cmd = Printf.sprintf "mkdir -p %s" path in
223    let _ = Sys.command cmd in
224    let och = open_out name in
225    G.commit st.output_type och script.contents;
226    close_out och;
227    st.scripts.(i) <- default_script
228
229 let produce st =
230    let init name = set_heading st name in
231    let partition = function 
232       | T.Coercion (false, _)
233       | T.Notation (false, _) -> false
234       | _                     -> true
235    in
236    let get_items = match st.input_type with
237       | T.Gallina8  -> Gallina8Parser.items Gallina8Lexer.token
238       | T.Grafite _ -> GrafiteParser.items GrafiteLexer.token
239    in
240    let produce st name =
241       let in_base_uri = Filename.concat st.input_base_uri name in
242       let out_base_uri = Filename.concat st.output_base_uri name in
243       let filter path = function
244          | T.Inline (b, k, obj, p, f, params)   -> 
245             let obj, p = 
246                if b then Filename.concat (make_path path) obj, make_prefix path
247                else obj, p
248             in
249             let ext = G.string_of_inline_kind k in
250             let s = Filename.concat in_base_uri (obj ^ ext) in
251             let params = 
252                params @
253                get_iparams st "*" @
254                get_iparams st ("*" ^ ext) @
255                get_iparams st (Filename.concat name obj)
256             in
257             path, Some (T.Inline (b, k, s, p, f, params))
258          | T.Include (moo, s)           ->
259             begin 
260                try path, Some (T.Include (moo, List.assoc s st.requires))
261                with Not_found -> path, None
262             end
263          | T.Coercion (b, obj)          ->
264             let str = get_coercion st obj in
265             if str <> "" then path, Some (T.Coercion (b, str)) else
266             let base_uri = if b then out_base_uri else in_base_uri in
267             let s = obj ^ G.string_of_inline_kind T.Con in
268             path, Some (T.Coercion (b, Filename.concat base_uri s))
269          | T.Section (b, id, _) as item ->
270             let path = if b then id :: path else List.tl path in
271             path, Some item
272          | T.Verbatim s                 ->
273             let pat, templ = st.input_base_uri, st.output_base_uri in
274             path, Some (T.Verbatim (Pcre.replace ~pat ~templ s)) 
275          | item                         -> path, Some item
276       in
277       let set_includes st name =
278          try require st name true (List.assoc name st.includes) 
279          with Not_found -> ()
280       in
281       let rec remove_lines ich n =
282          if n > 0 then let _ =  input_line ich in remove_lines ich (pred n)
283       in
284       Printf.eprintf "processing file name: %s ...\n" name; flush stderr;
285       let file = Filename.concat st.input_path name in
286       let ich = open_in (file ^ st.input_ext) in
287       begin try remove_lines ich st.remove_lines with End_of_file -> () end;
288       let lexbuf = Lexing.from_channel ich in
289       try 
290          let items = get_items lexbuf in close_in ich; 
291          let _, rev_items = X.list_rev_map_filter_fold filter [] items in
292          let items = List.rev rev_items in
293          let local_items, global_items = List.partition partition items in
294          let comment = T.Line (Printf.sprintf "From %s" name) in 
295          if global_items <> [] then 
296             set_items st st.input_package (comment :: global_items);
297          init name; 
298          begin match st.input_type with
299             | T.Grafite "" -> require st name false file
300             | _            -> require st name false st.input_package
301          end; 
302          set_includes st name; set_items st name local_items; commit st name
303       with e -> 
304          prerr_endline (Printexc.to_string e); close_in ich 
305    in
306    is_ma st st.input_package;
307    init st.input_package; require st st.input_package false "preamble"; 
308    match st.input_type with
309       | T.Grafite "" ->
310          List.iter (produce st) st.files
311       | T.Grafite s  ->
312          let theory = Filename.concat st.input_path s in
313          require st st.input_package false theory;
314          List.iter (produce st) st.files;
315          commit st st.input_package
316       | _            ->
317          List.iter (produce st) st.files;
318          commit st st.input_package