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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Maxence Guesdon, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 2001 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(** Text generation.
This module contains the class [to_text] with methods used to transform
information about elements to a [text] structure.*)
open Odoc_info
open Exception
open Type
open Value
open Module
open Class
(** A class used to get a [text] for info structures. *)
class virtual info =
object (self)
(** The list of pairs [(tag, f)] where [f] is a function taking
the [text] associated to [tag] and returning a [text].
Add a pair here to handle a tag.*)
val mutable tag_functions = ([] : (string * (Odoc_info.text -> Odoc_info.text)) list)
(** @return [text] value for an authors list. *)
method text_of_author_list l =
match l with
[] ->
[]
| _ ->
[ Bold [Raw (Odoc_messages.authors^": ")] ;
Raw (String.concat ", " l) ;
Newline
]
(** @return [text] value for the given optional version information.*)
method text_of_version_opt v_opt =
match v_opt with
None -> []
| Some v -> [ Bold [Raw (Odoc_messages.version^": ")] ;
Raw v ;
Newline
]
(** @return [text] value for the given optional since information.*)
method text_of_since_opt s_opt =
match s_opt with
None -> []
| Some s -> [ Bold [Raw (Odoc_messages.since^": ")] ;
Raw s ;
Newline
]
(** @return [text] value to represent the list of "before" information. *)
method text_of_before = function
[] -> []
| l ->
let f (v, text) =
(Bold [Raw (Printf.sprintf "%s %s " Odoc_messages.before v) ]) ::
text @
[Newline]
in
List.flatten (List.map f l)
(** @return [text] value for the given list of raised exceptions.*)
method text_of_raised_exceptions l =
match l with
[] -> []
| (s, t) :: [] ->
[ Bold [ Raw Odoc_messages.raises ] ;
Raw " " ;
Code s ;
Raw " "
]
@ t
@ [ Newline ]
| _ ->
[ Bold [ Raw Odoc_messages.raises ] ;
Raw " " ;
List
(List.map
(fun (ex, desc) ->(Code ex) :: (Raw " ") :: desc )
l
) ;
Newline
]
(** Return [text] value for the given "see also" reference. *)
method text_of_see (see_ref, t) =
match see_ref with
Odoc_info.See_url s -> [ Odoc_info.Link (s, t) ]
| Odoc_info.See_file s -> (Odoc_info.Code s) :: (Odoc_info.Raw " ") :: t
| Odoc_info.See_doc s -> (Odoc_info.Italic [Odoc_info.Raw s]) :: (Odoc_info.Raw " ") :: t
(** Return [text] value for the given list of "see also" references.*)
method text_of_sees l =
match l with
[] -> []
| see :: [] ->
(Bold [ Raw Odoc_messages.see_also ]) ::
(Raw " ") ::
(self#text_of_see see) @ [ Newline ]
| _ ->
(Bold [ Raw Odoc_messages.see_also ]) ::
[ List
(List.map
(fun see -> self#text_of_see see)
l
);
Newline
]
(** @return [text] value for the given optional return information.*)
method text_of_return_opt return_opt =
match return_opt with
None -> []
| Some t -> (Bold [Raw (Odoc_messages.returns^" ")]) :: t @ [ Newline ]
(** Return a [text] for the given list of custom tagged texts. *)
method text_of_custom l =
List.fold_left
(fun acc -> fun (tag, text) ->
try
let f = List.assoc tag tag_functions in
match acc with
[] -> f text
| _ -> acc @ (Newline :: (f text))
with
Not_found ->
Odoc_info.warning (Odoc_messages.tag_not_handled tag) ;
acc
)
[]
l
method text_of_alerts alerts =
List.concat_map (fun al ->
let payload =
match al.alert_payload with
| Some p -> [ Raw ". "; Raw p ]
| None -> []
in
[ Bold [ Raw Odoc_messages.alert; Raw " "; Raw al.alert_name ] ]
@ payload
@ [ Newline ]
) alerts
(** @return [text] value for a description, except for the i_params field. *)
method text_of_info ?(block=true) info_opt =
match info_opt with
None ->
[]
| Some info ->
let t =
(match info.i_deprecated with
None -> []
| Some t -> ( Italic [Raw (Odoc_messages.deprecated^". ")] ) :: t
) @
(match info.i_desc with
None -> []
| Some t when t = [Odoc_info.Raw ""] -> []
| Some t -> t @ [ Newline ]
) @
(self#text_of_author_list info.i_authors) @
(self#text_of_version_opt info.i_version) @
(self#text_of_before info.i_before) @
(self#text_of_since_opt info.i_since) @
(self#text_of_raised_exceptions info.i_raised_exceptions) @
(self#text_of_return_opt info.i_return_value) @
(self#text_of_sees info.i_sees) @
(self#text_of_alerts info.i_alerts) @
(self#text_of_custom info.i_custom)
in
if block then
[Block t]
else
t
end
(** This class defines methods to generate a [text] structure from elements. *)
class virtual to_text =
object (self)
inherit info
method virtual label : ?no_: bool -> string -> string
(** Take a string and return the string where fully qualified idents
have been replaced by idents relative to the given module name.
Also remove the "hidden modules".*)
method relative_idents m_name s =
let f str_t =
let match_s = Str.matched_string str_t in
let rel = Name.get_relative m_name match_s in
Odoc_info.apply_if_equal Odoc_info.use_hidden_modules match_s rel
in
Str.global_substitute
(Str.regexp "\\([A-Z]\\([a-zA-Z_'0-9]\\)*\\.\\)+\\([a-z][a-zA-Z_'0-9]*\\)")
f
s
(** Take a string and return the string where fully qualified idents
have been replaced by idents relative to the given module name.
Also remove the "hidden modules".*)
method relative_module_idents m_name s =
let f str_t =
let match_s = Str.matched_string str_t in
let rel = Name.get_relative m_name match_s in
Odoc_info.apply_if_equal Odoc_info.use_hidden_modules match_s rel
in
Str.global_substitute
(Str.regexp "\\([A-Z]\\([a-zA-Z_'0-9]\\)*\\.\\)+\\([A-Z][a-zA-Z_'0-9]*\\)")
f
s
(** Get a string for a [Types.class_type] where all idents are relative. *)
method normal_class_type m_name t =
self#relative_idents m_name (Odoc_info.string_of_class_type t)
(** Get a string for a [Types.module_type] where all idents are relative. *)
method normal_module_type ?code m_name t =
self#relative_module_idents m_name (Odoc_info.string_of_module_type ?code t)
(** Get a string for a type where all idents are relative. *)
method normal_type m_name t =
self#relative_idents m_name (Odoc_info.string_of_type_expr t)
(** Get a string for a list of types where all idents are relative. *)
method normal_type_list ?par m_name sep t =
self#relative_idents m_name (Odoc_info.string_of_type_list ?par sep t)
method normal_cstr_args ?par m_name = function
| Cstr_tuple l -> self#normal_type_list ?par m_name " * " l
| Cstr_record r -> self#relative_idents m_name
(Odoc_str.string_of_record r)
(** Get a string for a list of class or class type type parameters
where all idents are relative. *)
method normal_class_type_param_list m_name t =
self#relative_idents m_name (Odoc_info.string_of_class_type_param_list t)
(** Get a string for the parameters of a class (with arrows) where all idents are relative. *)
method normal_class_params m_name c =
let s = Odoc_info.string_of_class_params c in
self#relative_idents m_name
(Odoc_info.remove_ending_newline s)
(** @return [text] value to represent a [Types.type_expr].*)
method text_of_type_expr module_name t =
List.flatten
(List.map
(fun s -> [Code s ; Newline ])
(Str.split (Str.regexp "\n")
(self#normal_type module_name t))
)
(** Return [text] value for a given short [Types.type_expr].*)
method text_of_short_type_expr module_name t =
[ Code (self#normal_type module_name t) ]
(** Return [text] value or the given list of [Types.type_expr], with
the given separator. *)
method text_of_type_expr_list module_name sep l =
[ Code (self#normal_type_list module_name sep l) ]
(** Return [text] value or the given list of [Types.type_expr],
as type parameters of a class of class type. *)
method text_of_class_type_param_expr_list module_name l =
[ Code (self#normal_class_type_param_list module_name l) ]
(** @return [text] value to represent parameters of a class (with arrows).*)
method text_of_class_params module_name c =
Odoc_info.text_concat
[Newline]
(List.map
(fun s -> [Code s])
(Str.split (Str.regexp "\n")
(self#normal_class_params module_name c))
)
(** @return [text] value to represent a [Types.module_type]. *)
method text_of_module_type t =
let s = String.concat "\n"
(Str.split (Str.regexp "\n") (Odoc_info.string_of_module_type t))
in
[ Code s ]
(** @return [text] value for a value. *)
method text_of_value v =
let name = v.val_name in
let s_name = Name.simple name in
let s =
Format.fprintf Format.str_formatter "@[<hov 2>val %s :@ %s"
s_name
(self#normal_type (Name.father v.val_name) v.val_type);
Format.flush_str_formatter ()
in
[ CodePre s ] @
[Latex ("\\index{"^(self#label s_name)^"@\\verb`"^(self#label ~no_:false s_name)^"`}\n")] @
(self#text_of_info v.val_info)
(** @return [text] value for a class attribute. *)
method text_of_attribute a =
let s_name = Name.simple a.att_value.val_name in
let mod_name = Name.father a.att_value.val_name in
let s =
Format.fprintf Format.str_formatter "@[<hov 2>val %s%s%s :@ %s"
(if a.att_virtual then "virtual " else "")
(if a.att_mutable then "mutable " else "")
s_name
(self#normal_type mod_name a.att_value.val_type);
Format.flush_str_formatter ()
in
(CodePre s) ::
[Latex ("\\index{"^(self#label s_name)^"@\\verb`"^(self#label ~no_:false s_name)^"`}\n")] @
(self#text_of_info a.att_value.val_info)
(** @return [text] value for a class method. *)
method text_of_method m =
let s_name = Name.simple m.met_value.val_name in
let mod_name = Name.father m.met_value.val_name in
let s =
Format.fprintf Format.str_formatter "@[<hov 2>method %s%s%s :@ %s"
(if m.met_private then "private " else "")
(if m.met_virtual then "virtual " else "")
s_name
(self#normal_type mod_name m.met_value.val_type);
Format.flush_str_formatter ()
in
(CodePre s) ::
[Latex ("\\index{"^(self#label s_name)^"@\\verb`"^(self#label ~no_:false s_name)^"`}\n")] @
(self#text_of_info m.met_value.val_info)
(** @return [text] value for an exception. *)
method text_of_exception e =
let s_name = Name.simple e.ex_name in
let father = Name.father e.ex_name in
Format.fprintf Format.str_formatter "@[<hov 2>exception %s" s_name ;
(match e.ex_args, e.ex_ret with
Cstr_tuple [], None -> ()
| Cstr_tuple [], Some r ->
Format.fprintf Format.str_formatter " %s@ %s"
":"
(self#normal_type father r)
| args, None ->
Format.fprintf Format.str_formatter " %s@ %s"
"of"
(self#normal_cstr_args ~par:false father args)
| args, Some r ->
Format.fprintf Format.str_formatter " %s@ %s@ %s@ %s"
":"
(self#normal_cstr_args ~par:false father args)
"->"
(self#normal_type father r)
);
(match e.ex_alias with
None -> ()
| Some ea ->
Format.fprintf Format.str_formatter " = %s"
(
match ea.ea_ex with
None -> ea.ea_name
| Some e -> e.ex_name
)
);
let s2 = Format.flush_str_formatter () in
[ CodePre s2 ] @
[Latex ("\\index{"^(self#label s_name)^"@\\verb`"^(self#label ~no_:false s_name)^"`}\n")] @
(self#text_of_info e.ex_info)
(** Return [text] value for the description of a function parameter. *)
method text_of_parameter_description p =
match Parameter.names p with
[] -> []
| name :: [] ->
(
(* Only one name, no need for label for the description. *)
match Parameter.desc_by_name p name with
None -> []
| Some t -> t
)
| l ->
(* A list of names, we display those with a description. *)
let l2 = List.filter (fun n -> (Parameter.desc_by_name p n) <> None) l in
match l2 with
[] -> []
| _ ->
[List
(List.map
(fun n ->
match Parameter.desc_by_name p n with
None -> [] (* should not occur *)
| Some t -> [Code (n^" ") ; Raw ": "] @ t
)
l2
)
]
(** Return [text] value for a list of parameters. *)
method text_of_parameter_list m_name l =
match l with
[] ->
[]
| _ ->
[ Bold [Raw Odoc_messages.parameters] ;
Raw ":" ;
List
(List.map
(fun p ->
(match Parameter.complete_name p with
"" -> Code "?"
| s -> Code s
) ::
[Code " : "] @
(self#text_of_short_type_expr m_name (Parameter.typ p)) @
[Newline] @
(self#text_of_parameter_description p)
)
l
)
]
(** Return [text] value for a list of module parameters. *)
method text_of_module_parameter_list l =
match l with
[] ->
[]
| _ ->
[ Newline ;
Bold [Raw Odoc_messages.parameters] ;
Raw ":" ;
List
(List.map
(fun (p, desc_opt) ->
begin match p.mp_type with None -> [Raw ""]
| Some mty ->
[Code (p.mp_name^" : ")] @
(self#text_of_module_type mty)
end @
(match desc_opt with
None -> []
| Some t -> (Raw " ") :: t)
)
l
)
]
(**/**)
(** Return [text] value for the given [class_kind].*)
method text_of_class_kind father ckind =
match ckind with
Class_structure _ ->
[Code Odoc_messages.object_end]
| Class_apply capp ->
[Code
(
(
match capp.capp_class with
None -> capp.capp_name
| Some cl -> cl.cl_name
)^
" "^
(String.concat " "
(List.map
(fun s -> "("^s^")")
capp.capp_params_code))
)
]
| Class_constr cco ->
(
match cco.cco_type_parameters with
[] -> []
| l ->
(Code "[")::
(self#text_of_type_expr_list father ", " l)@
[Code "] "]
)@
[Code (
match cco.cco_class with
None -> cco.cco_name
| Some (Cl cl) -> Name.get_relative father cl.cl_name
| Some (Cltype (clt,_)) -> Name.get_relative father clt.clt_name
)
]
| Class_constraint (ck, ctk) ->
[Code "( "] @
(self#text_of_class_kind father ck) @
[Code " : "] @
(self#text_of_class_type_kind father ctk) @
[Code " )"]
(** Return [text] value for the given [class_type_kind].*)
method text_of_class_type_kind father ctkind =
match ctkind with
Class_type cta ->
(
match cta.cta_type_parameters with
[] -> []
| l ->
(Code "[") ::
(self#text_of_class_type_param_expr_list father l) @
[Code "] "]
) @
(
match cta.cta_class with
None -> [ Code cta.cta_name ]
| Some (Cltype (clt, _)) ->
let rel = Name.get_relative father clt.clt_name in
[Code rel]
| Some (Cl cl) ->
let rel = Name.get_relative father cl.cl_name in
[Code rel]
)
| Class_signature _ ->
[Code Odoc_messages.object_end]
(** Return [text] value for a [module_kind]. *)
method text_of_module_kind ?(with_def_syntax=true) k =
match k with
Module_alias m_alias ->
(match m_alias.ma_module with
None ->
[Code ((if with_def_syntax then " = " else "")^m_alias.ma_name)]
| Some (Mod m) ->
[Code ((if with_def_syntax then " = " else "")^m.m_name)]
| Some (Modtype mt) ->
[Code ((if with_def_syntax then " = " else "")^mt.mt_name)]
)
| Module_apply (k1, k2) ->
(if with_def_syntax then [Code " = "] else []) @
(self#text_of_module_kind ~with_def_syntax: false k1) @
[Code " ( "] @
(self#text_of_module_kind ~with_def_syntax: false k2) @
[Code " ) "]
| Module_apply_unit k1 ->
(if with_def_syntax then [Code " = "] else []) @
(self#text_of_module_kind ~with_def_syntax: false k1) @
[Code "()"]
| Module_with (tk, code) ->
(if with_def_syntax then [Code " : "] else []) @
(self#text_of_module_type_kind ~with_def_syntax: false tk) @
[Code code]
| Module_constraint (k, tk) ->
(if with_def_syntax then [Code " : "] else []) @
[Code "( "] @
(self#text_of_module_kind ~with_def_syntax: false k) @
[Code " : "] @
(self#text_of_module_type_kind ~with_def_syntax: false tk) @
[Code " )"]
| Module_struct _ ->
[Code ((if with_def_syntax then " : " else "")^
Odoc_messages.struct_end^" ")]
| Module_functor (_, k) ->
(if with_def_syntax then [Code " : "] else []) @
[Code "functor ... "] @
[Code " -> "] @
(self#text_of_module_kind ~with_def_syntax: false k)
| Module_typeof s ->
let code = Printf.sprintf "%smodule type of %s"
(if with_def_syntax then " : " else "")
s
in
[Code code]
| Module_unpack (code, _) ->
let code = Printf.sprintf "%s%s"
(if with_def_syntax then " : " else "")
code
in
[Code code]
(** Return html code for a [module_type_kind].*)
method text_of_module_type_kind ?(with_def_syntax=true) tk =
match tk with
| Module_type_struct _ ->
[Code ((if with_def_syntax then " = " else "")^Odoc_messages.sig_end)]
| Module_type_functor (p, k) ->
let t1 =
[Code ("("^p.mp_name^" : ")] @
(self#text_of_module_type_kind p.mp_kind) @
[Code ") -> "]
in
let t2 = self#text_of_module_type_kind ~with_def_syntax: false k in
(if with_def_syntax then [Code " = "] else []) @ t1 @ t2
| Module_type_with (tk2, code) ->
let t = self#text_of_module_type_kind ~with_def_syntax: false tk2 in
(if with_def_syntax then [Code " = "] else []) @
t @ [Code code]
| Module_type_alias mt_alias ->
[Code ((if with_def_syntax then " = " else "")^
(match mt_alias.mta_module with
None -> mt_alias.mta_name
| Some mt -> mt.mt_name))
]
| Odoc_module.Module_type_typeof s ->
let code = Printf.sprintf "%smodule type of %s"
(if with_def_syntax then " = " else "") s
in
[ Code code ]
end
|