package odoc
OCaml Documentation Generator
Install
Dune Dependency
Authors
Maintainers
Sources
odoc-3.0.0.tbz
sha256=ce84fa7e0cc5f3e8a54e6adeb10826152798b602057b9e46c5ae7e5d5206812b
sha512=9febd413450ca2e3824c9ef7e1c9ae8d8094aa72ed71327a69d8d6b42f6f197b3f3f40d674de0d11fa1242ee0df95c693b5d74467d530704e1339f3a523452f6
doc/src/odoc.model/semantics.ml.html
Source file semantics.ml
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 627 628 629 630 631 632 633 634 635 636 637 638 639
open Odoc_utils module Location = Location_ module Ast = Odoc_parser.Ast (** {!Ast.block_element} without internal tags. *) let describe_internal_tag = function | `Canonical _ -> "@canonical" | `Inline -> "@inline" | `Open -> "@open" | `Closed -> "@closed" | `Hidden -> "@hidden" | `Children_order _ -> "@children_order" | `Toc_status _ -> "@toc_status" | `Short_title _ -> "@short_title" | `Order_category _ -> "@order_category" let warn_unexpected_tag { Location.value; location } = Error.raise_warning @@ Error.make "Unexpected tag '%s' at this location." (describe_internal_tag value) location let warn_root_canonical location = Error.raise_warning @@ Error.make "Canonical paths must contain a dot, eg. X.Y." location let rec find_tag ~filter = function | [] -> None | hd :: tl -> ( match filter hd.Location.value with | Some x -> Some (x, hd.location) | None -> warn_unexpected_tag hd; find_tag ~filter tl) let rec acc ~filter = function | [] -> List.rev acc | hd :: tl -> ( match filter hd.Location.value with | Some x -> find_tags ((x, hd.location) :: acc) ~filter tl | None -> warn_unexpected_tag hd; find_tags acc ~filter tl) (* Errors *) let invalid_raw_markup_target : string -> Location.span -> Error.t = Error.make ~suggestion:"try '{%html:...%}'." "'{%%%s:': bad raw markup target." let default_raw_markup_target_not_supported : Location.span -> Error.t = Error.make ~suggestion:"try '{%html:...%}'." "'{%%...%%}' (raw markup) needs a target language." let bad_heading_level : int -> Location.span -> Error.t = Error.make "'%d': bad heading level (0-5 allowed)." let heading_level_should_be_lower_than_top_level : int -> int -> Location.span -> Error.t = fun this_heading_level top_heading_level -> Error.make "%s: heading level should be lower than top heading level '%d'." (Printf.sprintf "'{%i'" this_heading_level) top_heading_level let page_heading_required : string -> Error.t = Error.filename_only "Pages (.mld files) should start with a heading." let : Location.span -> Error.t = Error.make "Tags are not allowed in pages." let not_allowed : ?suggestion:string -> what:string -> in_what:string -> Location.span -> Error.t = fun ?suggestion ~what ~in_what -> Error.make ?suggestion "%s is not allowed in %s." (Astring.String.Ascii.capitalize what) in_what let describe_element = function | `Reference (`Simple, _, _) -> "'{!...}' (cross-reference)" | `Reference (`With_text, _, _) -> "'{{!...} ...}' (cross-reference)" | `Link (_, _) -> "'{{:...} ...}' (external link)" | `Heading (level, _, _) -> Printf.sprintf "'{%i ...}' (section heading)" level | `Specific s -> s (* End of errors *) type 'a with_location = 'a Location.with_location type ast_leaf_inline_element = [ `Space of string | `Word of string | `Code_span of string | `Math_span of string | `Raw_markup of string option * string ] type sections_allowed = [ `All | `No_titles | `None ] type alerts = [ `Tag of [ `Alert of string * string option ] ] Location_.with_location list type status = { tags_allowed : bool; parent_of_sections : Paths.Identifier.LabelParent.t; } let leaf_inline_element : ast_leaf_inline_element with_location -> Comment.leaf_inline_element with_location = fun element -> match element with | { value = `Word _ | `Code_span _ | `Math_span _; _ } as element -> element | { value = `Space _; _ } -> Location.same element `Space | { value = `Raw_markup (target, s); location } -> ( match target with | Some invalid_target when String.trim invalid_target = "" || String.exists (function '%' | '}' -> true | _ -> false) invalid_target -> Error.raise_warning (invalid_raw_markup_target invalid_target location); Location.same element (`Code_span s) | None -> Error.raise_warning (default_raw_markup_target_not_supported location); Location.same element (`Code_span s) | Some target -> Location.same element (`Raw_markup (target, s))) type surrounding = [ `Link of string * Odoc_parser.Ast.inline_element Location_.with_location list | `Reference of [ `Simple | `With_text ] * string Location_.with_location * Odoc_parser.Ast.inline_element Location_.with_location list | `Specific of string ] let rec non_link_inline_element : surrounding:surrounding -> Odoc_parser.Ast.inline_element with_location -> Comment.non_link_inline_element with_location = fun ~surrounding element -> match element with | { value = #ast_leaf_inline_element; _ } as element -> (leaf_inline_element element :> Comment.non_link_inline_element with_location) | { value = `Styled (style, content); _ } -> `Styled (style, non_link_inline_elements ~surrounding content) |> Location.same element | ( { value = `Reference (_, _, content); _ } | { value = `Link (_, content); _ } ) as element -> not_allowed ~what:(describe_element element.value) ~in_what:(describe_element surrounding) element.location |> Error.raise_warning; `Styled (`Emphasis, non_link_inline_elements ~surrounding content) |> Location.same element and non_link_inline_elements ~surrounding elements = List.map (non_link_inline_element ~surrounding) elements let rec inline_element : Odoc_parser.Ast.inline_element with_location -> Comment.inline_element with_location = fun element -> match element with | { value = #ast_leaf_inline_element; _ } as element -> (leaf_inline_element element :> Comment.inline_element with_location) | { value = `Styled (style, content); location } -> `Styled (style, inline_elements content) |> Location.at location | { value = `Reference (kind, target, content) as value; location } -> ( let { Location.value = target; location = target_location } = target in match Error.raise_warnings (Reference.parse target_location target) with | Result.Ok target -> let content = non_link_inline_elements ~surrounding:value content in Location.at location (`Reference (target, content)) | Result.Error error -> Error.raise_warning error; let placeholder = match kind with | `Simple -> `Code_span target | `With_text -> `Styled (`Emphasis, content) in inline_element (Location.at location placeholder)) | { value = `Link (target, content) as value; location } -> `Link (target, non_link_inline_elements ~surrounding:value content) |> Location.at location and inline_elements elements = List.map inline_element elements let rec nestable_block_element : Odoc_parser.Ast.nestable_block_element with_location -> Comment.nestable_block_element with_location = fun element -> match element with | { value = `Paragraph content; location } -> Location.at location (`Paragraph (inline_elements content)) | { value = `Code_block { meta; delimiter = _; content; output }; location } -> let lang_tag = match meta with | Some { language = { Location.value; _ }; _ } -> Some value | None -> None in let outputs = match output with | None -> None | Some l -> Some (List.map nestable_block_element l) in let trimmed_content, warnings = Odoc_parser.codeblock_content location content.value in let warnings = List.map Error.t_of_parser_t warnings in List.iter (Error.raise_warning ~non_fatal:true) warnings; let content = Location.at content.location trimmed_content in Location.at location (`Code_block (lang_tag, content, outputs)) | { value = `Math_block s; location } -> Location.at location (`Math_block s) | { value = `Verbatim v; location } -> let v, warnings = Odoc_parser.codeblock_content location v in let warnings = List.map Error.t_of_parser_t warnings in List.iter (Error.raise_warning ~non_fatal:true) warnings; Location.at location (`Verbatim v) | { value = `Modules modules; location } -> let modules = List.fold_left (fun acc { Location.value; location } -> match Error.raise_warnings (Reference.read_mod_longident location value) with | Result.Ok r -> { Comment.module_reference = r; module_synopsis = None } :: acc | Result.Error error -> Error.raise_warning error; acc) [] modules |> List.rev in Location.at location (`Modules modules) | { value = `List (kind, _syntax, items); location } -> `List (kind, List.map nestable_block_elements items) |> Location.at location | { value = `Table ((grid, align), (`Heavy | `Light)); location } -> let data = List.map (List.map (fun (cell, cell_type) -> (nestable_block_elements cell, cell_type))) grid in `Table { Comment.data; align } |> Location.at location | { value = `Media (_, { value = `Link href; _ }, content, m); location } -> `Media (`Link href, m, content) |> Location.at location | { value = `Media (kind, { value = `Reference href; location = href_location }, content, m); location; } -> ( let fallback error = Error.raise_warning error; let placeholder = match kind with | `Simple -> `Code_span href | `With_text -> `Styled (`Emphasis, [ `Word content |> Location.at location ]) in `Paragraph (inline_elements [ placeholder |> Location.at location ]) |> Location.at location in match Error.raise_warnings (Reference.parse_asset href_location href) with | Result.Ok target -> `Media (`Reference target, m, content) |> Location.at location | Result.Error error -> fallback error) and nestable_block_elements elements = List.map nestable_block_element elements let tag : location:Location.span -> status -> Ast.ocamldoc_tag -> ( Comment.block_element with_location, internal_tags_removed with_location ) Result.result = fun ~location status tag -> if not status.tags_allowed then (* Trigger a warning but do not remove the tag. Avoid turning tags into text that would render the same. *) Error.raise_warning (tags_not_allowed location); let ok t = Result.Ok (Location.at location (`Tag t)) in match tag with | (`Author _ | `Since _ | `Version _) as tag -> ok tag | `Deprecated content -> ok (`Deprecated (nestable_block_elements content)) | `Param (name, content) -> ok (`Param (name, nestable_block_elements content)) | `Raise (name, content) -> ( match Error.raise_warnings (Reference.parse location name) with (* TODO: location for just name *) | Result.Ok target -> ok (`Raise (`Reference (target, []), nestable_block_elements content)) | Result.Error error -> Error.raise_warning error; let placeholder = `Code_span name in ok (`Raise (placeholder, nestable_block_elements content))) | `Return content -> ok (`Return (nestable_block_elements content)) | `See (kind, target, content) -> ok (`See (kind, target, nestable_block_elements content)) | `Before (version, content) -> ok (`Before (version, nestable_block_elements content)) (* When the user does not give a section heading a label (anchor), we generate one from the text in the heading. This is the common case. This involves simply scanning the AST for words, lowercasing them, and joining them with hyphens. This must be done in the parser (i.e. early, not at HTML/other output generation time), so that the cross-referencer can see these anchors. *) let generate_heading_label : Comment.inline_element with_location list -> string = fun content -> (* Code spans can contain spaces, so we need to replace them with hyphens. We also lowercase all the letters, for consistency with the rest of this procedure. *) let replace_spaces_with_hyphens_and_lowercase s = let result = Bytes.create (String.length s) in s |> String.iteri (fun index c -> let c = match c with | ' ' | '\t' | '\r' | '\n' -> '-' | _ -> Astring.Char.Ascii.lowercase c in Bytes.set result index c); Bytes.unsafe_to_string result in let strip_locs li = List.map (fun ele -> ele.Location.value) li in (* Perhaps this should be done using a [Buffer.t]; we can switch to that as needed. *) let rec scan_inline_elements anchor = function | [] -> anchor | element :: more -> let anchor = match (element : Comment.inline_element) with | `Space -> anchor ^ "-" | `Word w -> anchor ^ Astring.String.Ascii.lowercase w | `Code_span c | `Math_span c -> anchor ^ replace_spaces_with_hyphens_and_lowercase c | `Raw_markup _ -> (* TODO Perhaps having raw markup in a section heading should be an error? *) anchor | `Styled (_, content) -> content |> strip_locs |> scan_inline_elements anchor | `Reference (_, content) | `Link (_, content) -> content |> strip_locs |> List.map (fun (ele : Comment.non_link_inline_element) -> (ele :> Comment.inline_element)) |> scan_inline_elements anchor in scan_inline_elements anchor more in content |> List.map (fun ele -> ele.Location.value) |> scan_inline_elements "" let section_heading : status -> top_heading_level:int option -> Location.span -> [ `Heading of _ ] -> int option * Comment.block_element with_location = fun status ~top_heading_level location heading -> let (`Heading (level, label, content)) = heading in let text = inline_elements content in let heading_label_explicit, label = match label with | Some label -> (true, label) | None -> (false, generate_heading_label text) in let label = Paths.Identifier.Mk.label (status.parent_of_sections, Names.LabelName.make_std label) in let mk_heading heading_level = let attrs = { Comment.heading_level; heading_label_explicit } in let element = Location.at location (`Heading (attrs, label, text)) in let top_heading_level = match top_heading_level with None -> Some level | some -> some in (top_heading_level, element) in let level' = match level with | 0 -> `Title | 1 -> `Section | 2 -> `Subsection | 3 -> `Subsubsection | 4 -> `Paragraph | 5 -> `Subparagraph | _ -> Error.raise_warning (bad_heading_level level location); (* Implicitly promote to level-5. *) `Subparagraph in let () = match top_heading_level with | Some top_level when level <= top_level && level <= 5 -> Error.raise_warning (heading_level_should_be_lower_than_top_level level top_level location) | _ -> () in mk_heading level' let validate_first_page_heading status ast_element = match status.parent_of_sections.iv with | `Page (_, name) | `LeafPage (_, name) -> ( match ast_element with | { Location.value = `Heading (_, _, _); _ } -> () | _invalid_ast_element -> let filename = Names.PageName.to_string name ^ ".mld" in Error.raise_warning (page_heading_required filename)) | _not_a_page -> () let top_level_block_elements status ast_elements = let rec traverse : top_heading_level:int option -> Comment.block_element with_location list -> internal_tags_removed with_location list -> Comment.block_element with_location list = fun ~top_heading_level comment_elements_acc ast_elements -> match ast_elements with | [] -> List.rev comment_elements_acc | ast_element :: ast_elements -> ( (* The first [ast_element] in pages must be a title or section heading. *) if top_heading_level = None then validate_first_page_heading status ast_element; match ast_element with | { value = #Odoc_parser.Ast.nestable_block_element; _ } as element -> let element = nestable_block_element element in let element = (element :> Comment.block_element with_location) in traverse ~top_heading_level (element :: comment_elements_acc) ast_elements | { value = `Tag the_tag; location } -> ( match tag ~location status the_tag with | Result.Ok element -> traverse ~top_heading_level (element :: comment_elements_acc) ast_elements | Result.Error placeholder -> traverse ~top_heading_level comment_elements_acc (placeholder :: ast_elements)) | { value = `Heading _ as heading; _ } -> let top_heading_level, element = section_heading status ~top_heading_level ast_element.Location.location heading in traverse ~top_heading_level (element :: comment_elements_acc) ast_elements) in let top_heading_level = (* Non-page documents have a generated title. *) match status.parent_of_sections.iv with | `Page _ | `LeafPage _ -> None | _parent_with_generated_title -> Some 0 in traverse ~top_heading_level [] ast_elements let ast : internal_tags_removed with_location list * _ = let rec loop ~start ast' = function | ({ Location.value = `Tag (#Ast.internal_tag as tag); _ } as wloc) :: tl -> ( let next tag = loop ~start ({ wloc with value = tag } :: tags) ast' tl in match tag with | (`Inline | `Open | `Closed | `Hidden) as tag -> next tag | ( `Children_order _ | `Short_title _ | `Toc_status _ | `Order_category _ ) as tag -> let tag_name = describe_internal_tag tag in if not start then Error.raise_warning (Error.make "%s tag has to be before any content" tag_name wloc.location); next tag | `Canonical { Location.value = s; location = r_location } -> ( match Error.raise_warnings (Reference.read_path_longident r_location s) with | Result.Ok path -> next (`Canonical path) | Result.Error e -> Error.raise_warning e; loop ~start tags ast' tl)) | ({ value = ( `Tag #Ast.ocamldoc_tag | `Heading _ | `Media _ | #Ast.nestable_block_element ); _; } as hd) :: tl -> loop ~start:false tags (hd :: ast') tl | [] -> (List.rev ast', List.rev tags) in loop ~start:true [] [] ast (** Append alerts at the end of the comment. Tags are favoured in case of alerts of the same name. *) let append_alerts_to_comment alerts (comment : Comment.block_element with_location list) = let alerts = List.filter (fun alert -> let (`Tag alert) = alert.Location_.value in List.for_all (fun elem -> match (elem.Location_.value, alert) with | `Tag (`Deprecated _), `Alert ("deprecated", _) -> false | _ -> true) comment) alerts in comment @ (alerts :> Comment.elements) let (type a) : a handle_internal_tags -> a = function | Expect_status -> ( match find_tag ~filter:(function | (`Inline | `Open | `Closed) as t -> Some t | _ -> None) tags with | Some (status, _) -> status | None -> `Default) | Expect_canonical -> ( match find_tag ~filter:(function `Canonical p -> Some p | _ -> None) tags with | Some (`Root _, location) -> warn_root_canonical location; None | Some ((`Dot _ as p), _) -> Some p | None -> None) | Expect_page_tags -> let unparsed_lines = find_tags [] ~filter:(function | ( `Children_order _ | `Toc_status _ | `Short_title _ | `Order_category _ ) as p -> Some p | _ -> None) tags in let lines = let do_ parse loc els = let els = nestable_block_elements els in match parse loc els with | Ok res -> Some res | Error e -> Error.raise_warning e; None in List.filter_map (function | `Children_order co, loc -> do_ Frontmatter.parse_children_order loc co | `Toc_status co, loc -> do_ Frontmatter.parse_toc_status loc co | `Short_title t, loc -> do_ Frontmatter.parse_short_title loc t | `Order_category t, loc -> do_ Frontmatter.parse_order_category loc t) unparsed_lines in Frontmatter.of_lines lines |> Error.raise_warnings | Expect_none -> (* Will raise warnings. *) ignore (find_tag ~filter:(fun _ -> None) tags); () let ast_to_comment ~ ~ ~parent_of_sections (ast : Ast.t) alerts = Error.catch_warnings (fun () -> let status = { tags_allowed; parent_of_sections } in let ast, = strip_internal_tags ast in let elts = top_level_block_elements status ast |> append_alerts_to_comment alerts in (elts, handle_internal_tags tags internal_tags)) let parse_comment ~ ~ ~containing_definition ~location ~text = Error.catch_warnings (fun () -> let ast = Odoc_parser.parse_comment ~location ~text |> Error.raise_parser_warnings in ast_to_comment ~internal_tags ~tags_allowed ~parent_of_sections:containing_definition ast [] |> Error.raise_warnings) let parse_reference text = let location = Location_. { file = ""; start = { line = 0; column = 0 }; end_ = { line = 0; column = String.length text }; } in Reference.parse location text let non_link_inline_element : context:string -> Odoc_parser.Ast.inline_element with_location list -> Comment.non_link_inline_element with_location list = fun ~context elements -> let surrounding = `Specific context in non_link_inline_elements ~surrounding elements
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>