]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitadep.ml
matitadep now parses notation.
[helm.git] / helm / matita / matitadep.ml
1 (* Copyright (C) 2005, 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://helm.cs.unibo.it/
24  *)
25
26 let paths_to_search_in = ref [];;
27
28 let add_l l = fun s -> l := s :: !l ;;
29
30 let arg_spec = [
31   "-I", Arg.String (add_l paths_to_search_in), 
32     "<path> Adds path to the list of searched paths for the include command";
33 ]
34 let usage =
35   Printf.sprintf "MatitaDep v%s\nUsage: matitadep [options] file...\nOptions:"
36     BuildTimeConf.version
37
38 module GA = GrafiteAst 
39 module U = UriManager
40
41 let deps = Hashtbl.create (Array.length Sys.argv)
42 let baseuris = ref []
43 let aliases = Hashtbl.create (Array.length Sys.argv)
44
45 (*
46 let uri_of_alias = function 
47   | Ident_alias (_, uri)
48   | Symbol_alias (_, _, uri) 
49   | Number_alias (_, uri) -> uri 
50 *)
51
52 let buri alias = 
53   U.buri_of_uri (U.uri_of_string alias)
54
55 let resolve alias =
56  let buri = buri alias in
57   if List.exists (fun u -> u = buri) !baseuris then Some buri else None
58  
59   (*** TODO MANCANO LE URI VERBATIM DENTRO GLI AST DEI TERMINI ****)
60
61 let find path = 
62   let rec aux = function
63   | [] -> close_in (open_in path); path
64   | p :: tl ->
65       try
66         close_in (open_in (p ^ "/" ^ path)); p ^ "/" ^ path
67       with Sys_error _ -> aux tl
68   in
69   let paths = !paths_to_search_in in
70   try
71     aux paths
72   with Sys_error _ as exc ->
73     MatitaLog.error ("Unable to read " ^ path);
74     MatitaLog.error ("opts.include_paths was " ^ String.concat ":" paths);
75     raise exc
76 ;;
77
78 (* note, this function works cause moo files are transitively closed *)
79 let process_notation_of_file file =
80     let ic = open_in file in
81     let istream = Stream.of_channel ic in
82     let notation_ids = ref [] in
83     try
84       while true do
85         match GrafiteParser.parse_statement istream with
86         | GA.Executable (_, GA.Command (_, notation)) ->
87           let stm, ids = CicNotation.process_notation notation in
88           notation_ids := ids @ !notation_ids
89         | _ -> ()
90       done; []
91     with
92     | End_of_file -> close_in ic; !notation_ids
93     | CicNotationParser.Parse_error _ as exn ->
94         prerr_endline ("Unable to parse: " ^ file);
95         prerr_endline (MatitaExcPp.to_string exn);
96         raise exn
97
98 let main () =
99   Helm_registry.load_from BuildTimeConf.matita_conf;
100   CicNotation.load_notation BuildTimeConf.core_notation_script;
101   let files = ref [] in
102   Arg.parse arg_spec (add_l files) usage;
103   List.iter
104    (fun file -> 
105     let ic = open_in file in
106     let istream = Stream.of_channel ic in
107     let notation_ids = ref [] in
108     (try
109       while true do
110         try 
111           let stm = GrafiteParser.parse_statement istream in
112           (match stm with 
113           | GA.Executable (_, GA.Command (_, notation)) ->
114             let stm, ids = CicNotation.process_notation notation in
115             notation_ids := ids @ !notation_ids
116           | _ -> ());
117           match stm with
118           | GA.Executable (_, GA.Command (_, GA.Set (_, "baseuri", uri))) ->
119               let uri = MatitaMisc.strip_trailing_slash uri in
120               baseuris := uri :: !baseuris
121           | GA.Executable (_, GA.Command 
122               (_, GA.Alias (_, GA.Ident_alias(_, uri)))) -> 
123                 Hashtbl.add aliases file uri
124           | GA.Executable (_, GA.Command (_, GA.Include (_, path))) ->
125               (* maybe to process coq.ma is useles since it contains only
126                * interpretations... no idea if notations will be added later *)
127               let moo_file = 
128                 MatitaMisc.obj_file_of_script (
129                   if path <> "coq.ma" then find path else path)
130               in
131               let ids = process_notation_of_file moo_file in
132               notation_ids := ids @ !notation_ids;
133               (try
134                 Hashtbl.add deps file moo_file
135                with
136                 Sys_error _ ->
137                  MatitaLog.error
138                   ("In file " ^ file ^ " unable to include " ^ path)
139               )
140           | _ -> ()
141         with 
142           CicNotationParser.Parse_error _ as exn ->
143             prerr_endline ("Unable to parse: " ^ file);
144             prerr_endline (MatitaExcPp.to_string exn);
145             ()
146       done
147     with
148     | End_of_file -> close_in ic);
149     Hashtbl.iter 
150       (fun file alias -> 
151         let dep = resolve alias in
152         match dep with 
153         | None -> ()
154         | Some u -> Hashtbl.add deps file (MatitaMisc.obj_file_of_baseuri u))
155       aliases;
156     (* remove notation ids *)
157     List.iter CicNotation.remove_notation !notation_ids;
158    ) !files;
159   List.iter
160    (fun file -> 
161     let deps = Hashtbl.find_all deps file in
162     let deps = List.fast_sort Pervasives.compare deps in
163     let deps = MatitaMisc.list_uniq deps in
164     let deps = file :: deps in
165     let moo = MatitaMisc.obj_file_of_script file in
166      Printf.printf "%s: %s\n" moo (String.concat " " deps);
167      Printf.printf "%s: %s\n" (Pcre.replace ~pat:"ma$" ~templ:"mo" file) moo
168    ) !files
169 ;;
170   
171 let _ =
172   main ()