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