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