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