]> matita.cs.unibo.it Git - helm.git/blob - components/binaries/transcript/engine.ml
tagged 0.5.0-rc1
[helm.git] / 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
31 type script = {
32    name: string;
33    contents: T.items
34 }
35
36 type status = {
37    helm_dir: string;
38    heading_path: string;
39    heading_lines: int;
40    input_package: string;
41    output_package: string;
42    input_base_uri: string;
43    output_base_uri: string;
44    input_path: string;
45    output_path: string;
46    script_ext: string;
47    coercions: (string * string) list;
48    files: string list;
49    requires: (string * string) list;
50    scripts: script array
51 }
52
53 let default_script = { 
54    name = ""; contents = []
55 }
56
57 let default_scripts = 2
58
59 let load_registry registry =
60    let suffix = ".conf.xml" in
61    let registry = 
62       if Filename.check_suffix registry suffix then registry
63       else registry ^ suffix
64    in
65    Printf.eprintf "reading configuration %s ...\n" registry; flush stderr;
66    R.load_from registry
67
68 let set_files st =
69    let eof ich = try Some (input_line ich) with End_of_file -> None in
70    let trim l = Filename.chop_extension (Str.string_after l 2) in
71    let cmd = Printf.sprintf "cd %s && find -name *%s" st.input_path st.script_ext in
72    let ich = Unix.open_process_in cmd in
73    let rec aux files =
74       match eof ich with
75          | None   -> List.rev files
76          | Some l -> aux (trim l :: files)
77    in 
78    let files = aux [] in
79    let _ = Unix.close_process_in ich in
80    {st with files = files}
81
82 let set_requires st =
83    let map file = (Filename.basename file, file) in
84    let requires = List.rev_map map st.files in
85    {st with requires = requires}
86
87 let init () = 
88    let default_registry = "transcript" in
89    load_registry default_registry
90
91 let make registry =
92    let id x = x in
93    let get_coercions = R.get_list (R.pair id id) in 
94    load_registry registry;
95    let st = {
96       helm_dir = R.get_string "transcript.helm_dir";
97       heading_path = R.get_string "transcript.heading_path";
98       heading_lines = R.get_int "transcript.heading_lines";
99       input_package = R.get_string "package.input_name";
100       output_package = R.get_string "package.output_name";
101       input_base_uri = R.get_string "package.input_base_uri";
102       output_base_uri = R.get_string "package.output_base_uri";
103       input_path = R.get_string "package.input_path";
104       output_path = R.get_string "package.output_path";
105       script_ext = R.get_string "package.script_type";
106       coercions = get_coercions "package.coercion";
107       files = [];
108       requires = [];
109       scripts = Array.make default_scripts default_script
110    } in
111    prerr_endline "reading file names ...";
112    let st = set_files st in
113    let st = set_requires st in
114    st
115
116 let get_index st name = 
117    let rec get_index name i =
118       if i >= Array.length st.scripts then None else 
119       if st.scripts.(i).name = name then Some i else 
120       get_index name (succ i)
121    in
122    match get_index name 0, get_index "" 0 with
123       | Some i, _ | _, Some i -> i
124       | None, None            -> failwith "not enought script entries"
125
126 let set_items st name items =
127    let i = get_index st name in
128    let script = st.scripts.(i) in
129    let contents = List.rev_append items script.contents in
130    st.scripts.(i) <- {name = name; contents = contents}
131    
132 let set_heading st name =
133    let heading = Filename.concat st.helm_dir st.heading_path, st.heading_lines in
134    set_items st name [T.Heading heading] 
135    
136 let set_baseuri st name =
137    let baseuri = Filename.concat st.output_base_uri name in
138    set_items st name [T.BaseUri baseuri]
139    
140 let require st name inc =
141    set_items st name [T.Include inc]
142
143 let get_coercion st str =
144    try List.assoc str st.coercions with Not_found -> ""
145
146 let make_path path =
147    List.fold_left Filename.concat "" (List.rev path)
148
149 let make_prefix path =
150    String.concat "__" (List.rev path) ^ "__"
151
152 let commit st name =
153    let i = get_index st name in
154    let script = st.scripts.(i) in
155    let path = Filename.concat st.output_path (Filename.dirname name) in
156    let name = Filename.concat st.output_path (name ^ ".ma") in
157    let cmd = Printf.sprintf "mkdir -p %s" path in
158    let _ = Sys.command cmd in
159    let och = open_out name in
160    G.commit och script.contents;
161    close_out och;
162    st.scripts.(i) <-  default_script
163    
164 let produce st =
165    let init name = set_heading st name; set_baseuri st name in
166    let partition = function 
167       | T.Coercion (false, _)
168       | T.Notation (false, _) -> false
169       | _                     -> true
170    in
171    let produce st name =
172       let in_base_uri = Filename.concat st.input_base_uri name in
173       let out_base_uri = Filename.concat st.output_base_uri name in
174       let filter path = function
175          | T.Inline (b, k, obj, p)      -> 
176             let obj, p = 
177                if b then Filename.concat (make_path path) obj, make_prefix path
178                else obj, p
179             in 
180             let s = obj ^ G.string_of_inline_kind k in
181             path, Some (T.Inline (b, k, Filename.concat in_base_uri s, p))
182          | T.Include s                  ->
183             begin 
184                try path, Some (T.Include (List.assoc s st.requires))
185                with Not_found -> path, None
186             end
187          | T.Coercion (b, obj)          ->
188             let str = get_coercion st obj in
189             if str <> "" then path, Some (T.Coercion (b, str)) else
190             let base_uri = if b then out_base_uri else in_base_uri in
191             let s = obj ^ G.string_of_inline_kind T.Con in
192             path, Some (T.Coercion (b, Filename.concat base_uri s))
193          | T.Section (b, id, _) as item ->
194             let path = if b then id :: path else List.tl path in
195             path, Some item
196          | item                         -> path, Some item
197       in
198       Printf.eprintf "processing file name: %s ...\n" name; flush stderr; 
199       let file = Filename.concat st.input_path (name ^ st.script_ext) in
200       let ich = open_in file in
201       let lexbuf = Lexing.from_channel ich in
202       try 
203          let items = V8Parser.items V8Lexer.token lexbuf in
204          close_in ich; 
205          let _, rev_items = X.list_rev_map_filter_fold filter [] items in
206          let items = List.rev rev_items in
207          let local_items, global_items = List.partition partition items in
208          let comment = T.Line (Printf.sprintf "From %s" name) in 
209          if global_items <> [] then 
210             set_items st st.input_package (comment :: global_items);
211          init name; require st name st.input_package;
212          set_items st name local_items; commit st name
213       with e -> 
214          prerr_endline (Printexc.to_string e); close_in ich 
215    in
216    init st.input_package; require st st.input_package "preamble"; 
217    List.iter (produce st) st.files; 
218    commit st st.input_package