123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205(**************************************************************************)(* *)(* OCamlFormat *)(* *)(* Copyright (c) Facebook, Inc. and its affiliates. *)(* *)(* This source code is licensed under the MIT license found in *)(* the LICENSE file in the root directory of this source tree. *)(* *)(**************************************************************************)moduleAst=Ocamlformat_odoc_parser.AstmoduleOdoc_parser=Ocamlformat_odoc_parser.Odoc_parserletparse~loctext=letlocation=loc.Location.loc_startinletlocation={locationwithpos_cnum=location.pos_cnum+3(* Length of comment opening *)}inletv=Odoc_parser.parse_comment~location~textinmatchOdoc_parser.warningsvwith|[]->Ok(Odoc_parser.astv)|warnings->Errorwarningsletparse_filelocationtext=Odoc_parser.ast(Odoc_parser.parse_comment~location~text)letwarnfmtwarning=Format.fprintffmt"Warning: Invalid documentation comment:@,%s\n%!"(Odoc_parser.Warning.to_stringwarning)letis_tag_only=List.for_all~f:(function|{Odoc_parser.Loc.value=`Tag_;_}->true|_->false)typenorm_conf={normalize_code:string->string}letnormalize_texts=(* normalize consecutive whitespace chars to a single space *)String.concat~sep:" "(List.filter~f:(Fn.nonString.is_empty)(String.split_on_charss~on:['\t';'\n';'\011';'\012';'\r';' ']))letlistffmtl=letpp_sepfmt()=Format.fprintffmt""inFormat.pp_print_list~pp_sepffmtlletstrfmts=Format.fprintffmt"%s"(normalize_texts)letign_locffmtwith_loc=ffmtwith_loc.Odoc_parser.Loc.valueletfpf=Format.fprintfletodoc_reference=ign_locstrletoptionffmt=functionSomev->ffmtv|None->()letodoc_stylefmt=function|`Bold->fpffmt"Bold"|`Italic->fpffmt"Italic"|`Emphasis->fpffmt"Emphasis"|`Superscript->fpffmt"Superscript"|`Subscript->fpffmt"Subscript"letrecodoc_inline_elementfmt=function|`Space_->()|`Wordtxt->(* Ignore backspace changes *)lettxt=String.filtertxt~f:(function'\\'->false|_->true)infpffmt"Word(%a)"strtxt|`Code_spantxt->fpffmt"Code_span(%a)"strtxt|`Math_spantxt->fpffmt"Math_span(%a)"strtxt|`Raw_markup(Somelang,txt)->fpffmt"Raw_markup(%s,%a)"langstrtxt|`Raw_markup(None,txt)->fpffmt"Raw_markup(%a)"strtxt|`Styled(style,elems)->fpffmt"Styled(%a,%a)"odoc_stylestyleodoc_inline_elementselems|`Reference(_kind,ref,content)->fpffmt"Reference(%a,%a)"odoc_referencerefodoc_inline_elementscontent|`Link(txt,content)->fpffmt"Link(%a,%a)"strtxtodoc_inline_elementscontentandodoc_inline_elementsfmtelems=list(ign_locodoc_inline_element)fmtelemsletlight_heavy_to_string=function`Light->"Light"|`Heavy->"Heavy"letalignment_to_string=function|`Left->"Left"|`Right->"Right"|`Center->"Center"letheader_data_to_string=function`Header->"Header"|`Data->"Data"letmedia_to_string=function|`Audio->"Audio"|`Video->"Video"|`Image->"Image"letfmt_media_hreffmt=function|`References->fpffmt"Reference(%s)"s|`Links->fpffmt"Link(%s)"sletrecodoc_nestable_block_elementcfmt:Ast.nestable_block_element->_=function|`Paragraphelms->fpffmt"Paragraph(%a)"odoc_inline_elementselms|`Code_block(b:Ast.code_block)->letfmt_metadatafmt(m:Ast.code_block_meta)=fpffmt"(%a, %a)"(ign_locstr)m.language(option(ign_locstr))m.tagsinletfmt_content=ign_loc(funfmts->strfmt(c.normalize_codes))inletfmt_output=option(list(ign_loc(odoc_nestable_block_elementc)))infpffmt"Code_block(%a, %a, %a, %a)"(optionfmt_metadata)b.meta(optionstr)b.delimiterfmt_contentb.contentfmt_outputb.output|`Math_blocktxt->fpffmt"Math_block(%a)"strtxt|`Verbatimtxt->fpffmt"Verbatim(%a)"strtxt|`Modulesmods->fpffmt"Modules(%a)"(listodoc_reference)mods|`List(ord,_syntax,items)->letord=matchordwith`Unordered->"U"|`Ordered->"O"inletlist_itemfmtelems=fpffmt"Item(%a)"(odoc_nestable_block_elementsc)elemsinfpffmt"List(%s,%a)"ord(listlist_item)items|`Table((grid,alignment),syntax)->letpp_alignfmtaln=fpffmt"%s"(alignment_to_stringaln)inletpp_cellfmt(elems,header)=fpffmt"(%a,%s)"(odoc_nestable_block_elementsc)elems(header_data_to_stringheader)inletpp_grid=list(listpp_cell)inletpp_alignment=option(list(optionpp_align))infpffmt"Table((%a,%a),%s)"pp_gridgridpp_alignmentalignment(light_heavy_to_stringsyntax)|`Media(_kind,href,text,media)->fpffmt"Media(%a,%S,%s)"(ign_locfmt_media_href)hreftext(media_to_stringmedia)andodoc_nestable_block_elementscfmtelems=list(ign_loc(odoc_nestable_block_elementc))fmtelemsletodoc_implicitly_ended_tagcfmttagelems=fpffmt"%s(%a)"tag(odoc_nestable_block_elementsc)elemsletodoc_tagcfmt:Ast.tag->unit=function|`Authortxt->fpffmt"Author(%a)"strtxt|`Deprecatedelems->odoc_implicitly_ended_tagcfmt"Deprecated"elems|`Param(p,elems)->fpffmt"Param(%a,%a)"strp(odoc_nestable_block_elementsc)elems|`Raise(p,elems)->fpffmt"Raise(%a,%a)"strp(odoc_nestable_block_elementsc)elems|`Returnelems->odoc_implicitly_ended_tagcfmt"Return"elems|`See(kind,txt,elems)->letkind=matchkindwith`Url->"U"|`File->"F"|`Document->"D"infpffmt"See(%s,%a,%a)"kindstrtxt(odoc_nestable_block_elementsc)elems|`Sincetxt->fpffmt"Since(%a)"strtxt|`Before(p,elems)->fpffmt"Before(%a,%a)"strp(odoc_nestable_block_elementsc)elems|`Versiontxt->fpffmt"Version(%a)"strtxt|`Canonicalref->fpffmt"Canonical(%a)"odoc_referenceref|`Inline->fpffmt"Inline"|`Open->fpffmt"Open"|`Closed->fpffmt"Closed"|`Hidden->fpffmt"Hidden"|`Children_orderelems->odoc_implicitly_ended_tagcfmt"Children_order"elems|`Short_titleelems->odoc_implicitly_ended_tagcfmt"Short_title"elemsletodoc_block_elementcfmt=function|`Heading(lvl,lbl,content)->letlvl=Int.to_stringlvlinletlbl=matchlblwithSomelbl->lbl|None->""infpffmt"Heading(%s,%a,%a)"lvlstrlblodoc_inline_elementscontent|`Tagtag->fpffmt"Tag(%a)"(odoc_tagc)tag|#Odoc_parser.Ast.nestable_block_elementaselm->odoc_nestable_block_elementcfmtelmletodoc_docscfmtelems=list(ign_loc(odoc_block_elementc))fmtelemsletnormalize~parse_docstrings~normalize_codetext=ifnotparse_docstringsthennormalize_texttextelseletlocation=Lexing.dummy_posinletparsed=Odoc_parser.parse_comment~location~textinletc={normalize_code}inFormat.asprintf"Docstring(%a)%!"(odoc_docsc)(Odoc_parser.astparsed)letdumpfmtx=letc={normalize_code=Fn.id}inFormat.fprintffmt"Docstring(%a)%!"(odoc_docsc)x