1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
(*
* ocamlweb - A WEB-like tool for ocaml
* Copyright (C) 1999-2001 Jean-Christophe FILLITRE and Claude MARCH
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2, as published by the Free Software Foundation.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU Library General Public License version 2 for more details
* (enclosed in the file LGPL).
*)
(*i $Id$ i*)
(*i*)
open Filename
open Cross
open Output
open Pretty
(*i*)
type sub_paragraph =
| CamlCode of string
| LexCode of string
| YaccCode of string
type paragraph =
| Documentation of bool * int * string
| RawLaTeX of string
| Code of int * string
| LexYaccCode of int * (sub_paragraph list)
type raw_section = {
sec_contents : paragraph list;
sec_beg : int }
type content = {
content_file : string;
content_name : string;
content_contents : raw_section list }
type file =
| Implem of content
| Interf of content
| Lex of content
| Yacc of content
| Other of string
(*i To print a "Web.file" (for testing) *)
let print_string s = Format.printf "\"%s\"" s
(* To print c "(" arg with the function pr ")" *)
let print c pr arg =
Format.printf "@[<hv 0>%s(@," c;
pr arg;
Format.printf ")@]"
(* to print a list between "[" "]" *)
let rec print_end_list f = function
| [] ->
Format.printf "]@]"
| x :: l ->
Format.printf ";@ ";
f x;
print_end_list f l
let print_list f = function
| [] ->
Format.printf "[]"
| x :: l ->
Format.printf "@[<hv 2>[ ";
f x;
print_end_list f l
(* To print a subparagraph *)
let print_sub_paragraph = function
| CamlCode s -> print "CamlCode" print_string s;
| LexCode s -> print "LexCode" print_string s;
| YaccCode s -> print "YaccCode" print_string s
(* To print a paragraph *)
let print_paragraph = function
| Documentation (_,_,s) ->
print "Documentation" print_string s
| RawLaTeX (s) ->
print "RawLaTeX" print_string s
| Code (i,s) ->
Format.printf "Code(%d,@ %s)" i s
| LexYaccCode (i,spl) ->
Format.printf "@[<hv 5>LexYaccCode(%d,@ " i;
print_list print_sub_paragraph spl;
Format.printf ")@]"
(* To print a section *)
let print_raw_section { sec_contents = sc; sec_beg = sb } =
Format.printf "@[<hv 2>{ sec_beg = %d ;@ sec_contents =@ " sb ;
print_list print_paragraph sc;
Format.printf ";@ }@]"
(* To print a [Web.content] *)
let print_content { content_file = c;
content_name = cn;
content_contents = rl } =
Format.printf "@[<hv 2>{ content_file = \"%s\" ;@ content_name = \"%s\" ;@ contents_contents =@ " c cn ;
print_list print_raw_section rl ;
Format.printf ";@ }@]"
(* To print a [Web.file] *)
let print_file f =
begin
match f with
| Implem c -> print "Implem" print_content c
| Interf c -> print "Interf" print_content c
| Lex c -> print "Lex" print_content c
| Yacc c -> print "Yacc" print_content c
| Other s -> print "Other" print_string s
end;
Format.printf "@."
(*i*)
(*s Options of the engine. *)
let index = ref true
let web = ref true
let extern_defs = ref false
let latex_options = ref ""
let add_latex_option s =
if !latex_options = "" then
latex_options := s
else
latex_options := !latex_options ^ "," ^ s
(*s Construction of the global index. *)
let index_file = function
| Implem i -> cross_implem i.content_file i.content_name
| Interf i -> cross_interf i.content_file i.content_name
| Yacc i -> cross_yacc i.content_file i.content_name
| Lex i -> cross_lex i.content_file i.content_name
| Other _ -> ()
let build_index l = List.iter index_file l
(*s The locations tables. \label{counters} *)
module Smap = Map.Make(struct type t = string let compare = compare end)
let sec_locations = ref Smap.empty
let code_locations = ref Smap.empty
let add_loc table file ((_,s) as loc) =
let l = try Smap.find file !table with Not_found -> [(0,s)] in
table := Smap.add file (loc :: l) !table
let add_par_loc =
let par_counter = ref 0 in
fun f p -> match p with
| Code (l,_) ->
incr par_counter;
add_loc code_locations f (l,!par_counter)
| LexYaccCode (l,_) ->
incr par_counter;
add_loc code_locations f (l,!par_counter)
| Documentation _ -> ()
| RawLaTeX _ -> ()
let add_sec_loc =
let sec_counter = ref 0 in
fun f s ->
incr sec_counter;
add_loc sec_locations f (s.sec_beg,!sec_counter);
(*i
Printf.eprintf "section %d starts at character %d of file %s\n"
!sec_counter s.sec_beg f;
i*)
List.iter (add_par_loc f) s.sec_contents
let add_file_loc it =
List.iter (add_sec_loc it.content_file) it.content_contents
let locations_for_a_file = function
| Implem i -> add_file_loc i
| Interf i -> add_file_loc i
| Lex i -> add_file_loc i
| Yacc i -> add_file_loc i
| Other _ -> ()
let find_where w =
let rec lookup = function
| [] -> raise Not_found
| (l,n) :: r -> if w.w_loc >= l then ((w.w_filename,l),n) else lookup r
in
let table = if !web then !sec_locations else !code_locations in
lookup (Smap.find w.w_filename table)
(*s Printing of the index. *)
(*s Alphabetic order for index entries.
To sort index entries, we define the following order relation
[alpha_string]. It puts symbols first (identifiers that do not begin
with a letter), and symbols are compared using Caml's generic order
relation. For real identifiers, we first normalize them by translating
lowercase characters to uppercase ones and by removing all the accents,
and then we use Caml's comparison.
*)
let norm_char c = match Char.uppercase c with
| '\192'..'\198' -> 'A'
| '\199' -> 'C'
| '\200'..'\203' -> 'E'
| '\204'..'\207' -> 'I'
| '\209' -> 'N'
| '\210'..'\214' | '\216' -> 'O'
| '\217'..'\220' -> 'U'
| '\221' -> 'Y'
| c -> c
let norm_string s =
let u = String.copy s in
for i = 0 to String.length s - 1 do
u.[i] <- norm_char s.[i]
done;
u
let alpha_string s1 s2 =
match what_is_first_char s1, what_is_first_char s2 with
| Symbol, Symbol -> s1 < s2
| Symbol, _ -> true
| _, Symbol -> false
| _,_ -> norm_string s1 < norm_string s2
let order_entry e1 e2 =
(alpha_string e1.e_name e2.e_name) ||
(e1.e_name = e2.e_name && e1.e_type < e2.e_type)
(*s The following function collects all the index entries and sort them
using [alpha_string], returning a list. *)
module Idset = Set.Make(struct type t = index_entry let compare = compare end)
let all_entries () =
let s = Idmap.fold (fun x _ s -> Idset.add x s) !used Idset.empty in
let s = Idmap.fold (fun x _ s -> Idset.add x s) !defined s in
Sort.list order_entry (Idset.elements s)
(*s When we are in \LaTeX\ style, an index entry only consists in two lists
of labels, which are treated by the \LaTeX\ macro \verb!\ocwrefindexentry!.
When we are in WEB style, we can do a bit better, replacing a list
like 1,2,3,4,7,8,10 by 1--4,7,8,10, as in usual \LaTeX\ indexes.
The following function [intervals] is used to group together the lists
of at least three consecutive integers.
*)
let intervals l =
let rec group = function
| (acc, []) -> List.rev acc
| (Interval (s1,(_,n2)) :: acc, (f,n) :: rem) when n = succ n2 ->
group (Interval (s1,(f,n)) :: acc, rem)
| ((Single _)::(Single (f1,n1))::acc, (f,n)::rem) when n = n1 + 2 ->
group (Interval ((f1,n1),(f,n)) :: acc, rem)
| (acc, (f,n) :: rem) ->
group ((Single (f,n)) :: acc, rem)
in
group ([],l)
let make_label_name (f,n) = f ^ ":" ^ (string_of_int n)
let label_list l =
List.map (fun x -> make_label_name (fst x)) l
let elem_map f = function
| Single x -> Single (f x)
| Interval (x,y) -> Interval (f x, f y)
let web_list l =
let l = intervals l in
List.map (elem_map (fun x -> make_label_name (fst x))) l
(*s Printing one index entry.
The function call [(list_in_table id t)] collects all the sections for
the identifier [id] in the table [t], using the function [find_where],
and sort the result thanks to the counter which was associated to each
new location (see section~\ref{counters}). It also removes the duplicates
labels.
*)
let rec uniquize = function
| [] | [_] as l -> l
| x :: (y :: r as l) -> if x = y then uniquize l else x :: (uniquize l)
let map_succeed_nf f l =
let rec map = function
| [] -> []
| x :: l -> try (f x) :: (map l) with Not_found -> map l
in
map l
let list_in_table id t =
try
let l = Whereset.elements (Idmap.find id t) in
let l = map_succeed_nf find_where l in
let l = Sort.list (fun x x' -> snd x < snd x') l in
uniquize l
with Not_found ->
[]
let entry_type_name = function
| Value | Constructor -> ""
| Field -> "(field)"
| Label -> "(label)"
| Type -> "(type)"
| Exception -> "(exn)"
| Module -> "(module)"
| ModuleType -> "(sig)"
| Class -> "(class)"
| Method -> "(method)"
| LexParseRule -> "(camllex parsing rule)"
| RegExpr -> "(camllex regexpr)"
| YaccNonTerminal -> "(camlyacc non-terminal)"
| YaccTerminal -> "(camlyacc token)"
let print_one_entry id =
let def = list_in_table id !defined in
if !extern_defs || def <> [] then begin
let use = list_in_table id !used in
let s = id.e_name in
let t = entry_type_name id.e_type in
if !web then
output_index_entry s t (web_list def) (web_list use)
else
output_raw_index_entry s t (label_list def) (label_list use)
end
(*s Then printing the index is just iterating [print_one_entry] on all the
index entries, given by [(all_entries())]. *)
let print_index () =
begin_index ();
List.iter print_one_entry (all_entries());
end_index ()
(*s Pretty-printing of the document. *)
let rec pretty_print_sub_paragraph = function
| CamlCode(s) ->
pretty_print_caml_subpar s
| YaccCode(s) ->
pretty_print_yacc_subpar s
| LexCode(s) ->
pretty_print_lex_subpar s
let pretty_print_paragraph is_first_paragraph is_last_paragraph f = function
| Documentation (b,n,s) ->
end_code ();
pretty_print_doc is_first_paragraph (b,n,s);
end_line() (*i ajout Dorland-Muller i*)
| RawLaTeX s ->
end_code ();
output_string s;
end_line()
| Code (l,s) ->
if l > 0 then output_label (make_label_name (f,l));
begin_code_paragraph ();
begin_code ();
pretty_print_code is_last_paragraph s
| LexYaccCode (l,s) ->
if l > 0 then output_label (make_label_name (f,l));
begin_code_paragraph ();
begin_code ();
List.iter pretty_print_sub_paragraph s;
end_code_paragraph is_last_paragraph
let pretty_print_section first f s =
if !web then begin_section ();
if first && s.sec_beg > 0 then output_label (make_label_name (f,0));
output_label (make_label_name (f,s.sec_beg));
let rec loop is_first_paragraph = function
| [] ->
()
| [ p ] ->
pretty_print_paragraph is_first_paragraph true f p
| p :: rest ->
pretty_print_paragraph is_first_paragraph false f p;
loop false rest
in
loop true s.sec_contents;
end_code ()
let pretty_print_sections f = function
| [] -> ()
| s :: r ->
pretty_print_section true f s;
List.iter (pretty_print_section false f) r
let pretty_print_content output_header content =
reset_pretty();
output_header content.content_name;
pretty_print_sections content.content_file content.content_contents
let pretty_print_file = function
| Implem i -> pretty_print_content output_module i
| Interf i -> pretty_print_content output_interface i
| Lex i -> pretty_print_content output_lexmodule i
| Yacc i -> pretty_print_content output_yaccmodule i
| Other f -> output_file f
(*s Production of the document. We proceed in three steps:
\begin{enumerate}
\item Build the index;
\item Pretty-print of files;
\item Printing of the index.
\end{enumerate}
*)
let produce_document l =
(*i
List.iter print_file l;
i*)
List.iter locations_for_a_file l;
build_index l;
latex_header !latex_options;
List.iter pretty_print_file l;
if !index then print_index ();
latex_trailer ();
close_output ()
|