Debian Codegraph @

tools/coqdoc/index.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Printf
12 open Common
13
14 type loc = int
15
16 type entry_type =
17 | Library
18 | Module
19 | Definition
20 | Inductive
21 | Constructor
22 | Lemma
23 | Record
24 | Projection
25 | Instance
26 | Class
27 | Method
28 | Variable
29 | Axiom
30 | TacticDefinition
31 | Abbreviation
32 | Notation
33 | Section
34 | Binder
35
36 type index_entry =
37 | Def of (string * entry_type) list
38 | Ref of coq_module * string * entry_type
39
40 let current_library = ref ""
41 (** refers to the file being parsed *)
42
43 (** [deftable] stores only definitions and is used to build the index *)
44 let deftable = Hashtbl.create 97
45
46 (** [byidtable] is used to interpolate idents inside comments, which are not
47 globalized otherwise. *)
48 let byidtable = Hashtbl.create 97
49
50 (** [reftable] stores references and definitions *)
51 let reftable = Hashtbl.create 97
52
53 let full_ident sp id =
54 if sp <> "<>" then
55 if id <> "<>" then
56 sp ^ "." ^ id
57 else sp
58 else if id <> "<>"
59 then id
60 else ""
61 let hashtbl_append_def t k v =
62 try
63 match Hashtbl.find t k with
64 | Def l -> Hashtbl.replace t k (Def (l @ [v]))
65 | Ref _ -> Hashtbl.add t k (Def [v])
66 with Not_found ->
67 Hashtbl.add t k (Def [v])
68
69 let add_def loc1 loc2 ty sp id =
70 let fullid = full_ident sp id in
71 let def = (fullid, ty) in
72 for loc = loc1 to loc2 do
73 hashtbl_append_def reftable (!current_library, loc) def
74 done;
75 Hashtbl.add deftable !current_library (fullid, ty);
76 Hashtbl.add byidtable id (!current_library, fullid, ty)
77
78 let add_ref m loc m' sp id ty =
79 let fullid = full_ident sp id in
80 if Hashtbl.mem reftable (m, loc) then ()
81 else Hashtbl.add reftable (m, loc) (Ref (m', fullid, ty));
82 let idx = if id = "<>" then m' else id in
83 if Hashtbl.mem byidtable idx then ()
84 else Hashtbl.add byidtable idx (m', fullid, ty)
85
86 let find m l = Hashtbl.find reftable (m, l)
87
88 let find_string s = let (m,s,t) = Hashtbl.find byidtable s in Ref (m,s,t)
89
90 (* Rocq modules *)
91 let split_sp s =
92 try
93 let i = String.rindex s '.' in
94 String.sub s 0 i, String.sub s (i + 1) (String.length s - i - 1)
95 with
96 Not_found -> "", s
97
98 let modules = Hashtbl.create 97
99 let local_modules = Hashtbl.create 97
100
101 let add_module m =
102 let _,id = split_sp m in
103 Hashtbl.add modules id m;
104 Hashtbl.add local_modules m ()
105
106 type module_kind = Local | External of string | Unknown
107
108 let external_libraries = ref []
109
110 let add_external_library logicalpath url =
111 external_libraries := (logicalpath,url) :: !external_libraries
112
113 let find_external_library logicalpath =
114 let rec aux = function
115 | [] -> raise Not_found
116 | (l,u)::rest ->
117 if String.length logicalpath > String.length l &&
118 String.sub logicalpath 0 (String.length l + 1) = l ^"."
119 then u
120 else aux rest
121 in aux !external_libraries
122
123 let init_coqlib_library () = add_external_library "Corelib" !prefs.coqlib_url
124
125 let find_module m =
126 if Hashtbl.mem local_modules m then
127 Local
128 else
129 try External (find_external_library m ^ "/" ^ m)
130 with Not_found -> Unknown
131
132
133 (* Building indexes *)
134
135 type 'a index = {
136 idx_name : string;
137 idx_entries : (char * (string * 'a) list) list;
138 idx_size : int }
139
140 let map f i =
141 { i with idx_entries =
142 List.map
143 (fun (c,l) -> (c, List.map (fun (s,x) -> (s,f s x)) l))
144 i.idx_entries }
145
146 let compare_entries (s1,_) (s2,_) = Alpha.compare_string s1 s2
147
148 let sort_entries el =
149 let t = Hashtbl.create 97 in
150 List.iter
151 (fun c -> Hashtbl.add t c [])
152 ['A'; 'B'; 'C'; 'D'; 'E'; 'F'; 'G'; 'H'; 'I'; 'J'; 'K'; 'L'; 'M'; 'N';
153 'O'; 'P'; 'Q'; 'R'; 'S'; 'T'; 'U'; 'V'; 'W'; 'X'; 'Y'; 'Z'; '_'; '*'];
154 List.iter
155 (fun ((s,_) as e) ->
156 let c = Alpha.norm_char s.[0] in
157 let c,l =
158 try c,Hashtbl.find t c with Not_found -> '*',Hashtbl.find t '*' in
159 Hashtbl.replace t c (e :: l))
160 el;
161 let res = ref [] in
162 Hashtbl.iter (fun c l -> res := (c, List.sort compare_entries l) :: !res) t;
163 List.sort (fun (c1,_) (c2,_) -> Alpha.compare_char c1 c2) !res
164
165 let display_letter c = if c = '*' then "other" else String.make 1 c
166
167 let type_name = function
168 | Library ->
169 let ln = !prefs.lib_name in
170 if ln <> "" then String.lowercase_ascii ln else "library"
171 | Module -> "module"
172 | Definition -> "definition"
173 | Inductive -> "inductive"
174 | Constructor -> "constructor"
175 | Lemma -> "lemma"
176 | Record -> "record"
177 | Projection -> "projection"
178 | Instance -> "instance"
179 | Class -> "class"
180 | Method -> "method"
181 | Variable -> "variable"
182 | Axiom -> "axiom"
183 | TacticDefinition -> "tactic"
184 | Abbreviation -> "abbreviation"
185 | Notation -> "notation"
186 | Section -> "section"
187 | Binder -> "binder"
188
189 let prepare_entry s = function
190 | Notation ->
191 (* We decode the encoding done in Dumpglob.cook_notation of coqtop *)
192 (* Encoded notations have the form section:entry:sc:x_'++'_x *)
193 (* where: *)
194 (* - the section, if any, ends with a "." *)
195 (* - the scope can be empty *)
196 (* - tokens are separated with "_" *)
197 (* - non-terminal symbols are conventionally represented by "x" *)
198 (* - terminals are enclosed within simple quotes *)
199 (* - existing simple quotes (that necessarily are parts of *)
200 (* terminals) are doubled *)
201 (* (as a consequence, when a terminal contains "_" or "x", these *)
202 (* necessarily appear enclosed within non-doubled simple quotes) *)
203 (* - non-printable characters < 32 are left encoded so that they *)
204 (* are human-readable in index files *)
205 (* Example: "x ' %x _% y %'x %'_' z" is encoded as *)
206 (* "x_''''_'%x'_'_%'_x_'%''x'_'%''_'''_x" *)
207 let err () = eprintf "Invalid notation in globalization file\n"; exit 1 in
208 let h = try String.index_from s 0 ':' with _ -> err () in
209 let i = try String.index_from s (h+1) ':' with _ -> err () in
210 let m = try String.index_from s (i+1) ':' with _ -> err () in
211 let entry = String.sub s (h+1) (i-h-1) in
212 let sc = String.sub s (i+1) (m-i-1) in
213 let ntn = Bytes.make (String.length s - m) ' ' in
214 let k = ref 0 in
215 let j = ref (m+1) in
216 let quoted = ref false in
217 let l = String.length s - 1 in
218 while !j <= l do
219 if not !quoted then begin
220 (match s.[!j] with
221 | '_' -> Bytes.set ntn !k ' '; incr k
222 | 'x' -> Bytes.set ntn !k '_'; incr k
223 | '\'' -> quoted := true
224 | _ -> assert false)
225 end
226 else
227 if s.[!j] = '\'' then
228 if (!j = l || s.[!j+1] = '_') then quoted := false
229 else (incr j; Bytes.set ntn !k s.[!j]; incr k)
230 else begin
231 Bytes.set ntn !k s.[!j];
232 incr k
233 end;
234 incr j
235 done;
236 let ntn = Bytes.sub_string ntn 0 !k in
237 let ntn = if sc = "" then ntn else ntn ^ " (" ^ sc ^ ")" in
238 if entry = "" then ntn else entry ^ ":" ^ ntn
239 | _ ->
240 s
241
242 let include_entry = function
243 | Binder -> !prefs.binder_index
244 | _ -> true
245
246 let all_entries () =
247 let gl = ref [] in
248 let add_g s m t = gl := (s,(m,t)) :: !gl in
249 let bt = Hashtbl.create 11 in
250 let add_bt t s m =
251 let l = try Hashtbl.find bt t with Not_found -> [] in
252 Hashtbl.replace bt t ((s,m) :: l)
253 in
254 let classify m (s,t) =
255 if include_entry t then begin
256 add_g s m t; add_bt t s m
257 end
258 in
259 Hashtbl.iter classify deftable;
260 Hashtbl.iter (fun id m -> add_g id m Library; add_bt Library id m) modules;
261 { idx_name = "global";
262 idx_entries = sort_entries !gl;
263 idx_size = List.length !gl },
264 Hashtbl.fold (fun t e l -> (t, { idx_name = type_name t;
265 idx_entries = sort_entries e;
266 idx_size = List.length e }) :: l) bt []