Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file docstrings.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353(**************************************************************************)(* *)(* OCaml *)(* *)(* Leo White *)(* *)(* Copyright 1996 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. *)(* *)(**************************************************************************)openImportopenLocation(* Docstrings *)(* A docstring is "attached" if it has been inserted in the AST. This
is used for generating unexpected docstring warnings. *)typeds_attached=|Unattached(* Not yet attached anything.*)|Info(* Attached to a field or constructor. *)|Docs(* Attached to an item or as floating text. *)(* A docstring is "associated" with an item if there are no blank lines between
them. This is used for generating docstring ambiguity warnings. *)typeds_associated=|Zero(* Not associated with an item *)|One(* Associated with one item *)|Many(* Associated with multiple items (ambiguity) *)typedocstring={ds_body:string;ds_loc:Location.t;mutableds_attached:ds_attached;mutableds_associated:ds_associated;}(* List of docstrings *)letdocstrings:docstringlistref=ref[](* Warn for unused and ambiguous docstrings *)letwarn_bad_docstrings()=ifWarnings.is_active(Warnings.Bad_docstringtrue)thenbeginList.iter(funds->matchds.ds_attachedwith|Info->()|Unattached->prerr_warningds.ds_loc(Warnings.Bad_docstringtrue)|Docs->matchds.ds_associatedwith|Zero|One->()|Many->prerr_warningds.ds_loc(Warnings.Bad_docstringfalse))(List.rev!docstrings)end(* Docstring constructors and destructors *)letdocstringbodyloc=letds={ds_body=body;ds_loc=loc;ds_attached=Unattached;ds_associated=Zero;}indsletregisterds=docstrings:=ds::!docstringsletdocstring_bodyds=ds.ds_bodyletdocstring_locds=ds.ds_loc(* Docstrings attached to items *)typedocs={docs_pre:docstringoption;docs_post:docstringoption;}letempty_docs={docs_pre=None;docs_post=None}letdoc_loc={txt="ocaml.doc";loc=Location.none}letdocs_attrds=letopenParsetreeinletexp={pexp_desc=Pexp_constant(Pconst_string(ds.ds_body,None));pexp_loc=ds.ds_loc;pexp_attributes=[];}inletitem={pstr_desc=Pstr_eval(exp,[]);pstr_loc=exp.pexp_loc}in(doc_loc,PStr[item])letadd_docs_attrsdocsattrs=letattrs=matchdocs.docs_prewith|None|Some{ds_body="";_}->attrs|Someds->docs_attrds::attrsinletattrs=matchdocs.docs_postwith|None|Some{ds_body="";_}->attrs|Someds->attrs@[docs_attrds]inattrs(* Docstrings attached to constructors or fields *)typeinfo=docstringoptionletempty_info=Noneletinfo_attr=docs_attrletadd_info_attrsinfoattrs=matchinfowith|None|Some{ds_body="";_}->attrs|Someds->attrs@[info_attrds](* Docstrings not attached to a specific item *)typetext=docstringlistletempty_text=[]letempty_text_lazy=lazy[]lettext_loc={txt="ocaml.text";loc=Location.none}lettext_attrds=letopenParsetreeinletexp={pexp_desc=Pexp_constant(Pconst_string(ds.ds_body,None));pexp_loc=ds.ds_loc;pexp_attributes=[];}inletitem={pstr_desc=Pstr_eval(exp,[]);pstr_loc=exp.pexp_loc}in(text_loc,PStr[item])letadd_text_attrsdslattrs=letfdsl=List.filter(function{ds_body=""}->false|_->true)dslin(List.maptext_attrfdsl)@attrs(* Find the first non-info docstring in a list, attach it and return it *)letget_docstring~infodsl=letrecloop=function|[]->None|{ds_attached=Info;_}::rest->looprest|ds::_->ds.ds_attached<-ifinfothenInfoelseDocs;Somedsinloopdsl(* Find all the non-info docstrings in a list, attach them and return them *)letget_docstringsdsl=letrecloopacc=function|[]->List.revacc|{ds_attached=Info;_}::rest->loopaccrest|ds::rest->ds.ds_attached<-Docs;loop(ds::acc)restinloop[]dsl(* "Associate" all the docstrings in a list *)letassociate_docstringsdsl=List.iter(funds->matchds.ds_associatedwith|Zero->ds.ds_associated<-One|(One|Many)->ds.ds_associated<-Many)dsl(* Map from positions to pre docstrings *)letpre_table:(Lexing.position,docstringlist)Hashtbl.t=Hashtbl.create50letset_pre_docstringsposdsl=ifdsl<>[]thenHashtbl.addpre_tableposdslletget_pre_docspos=tryletdsl=Hashtbl.findpre_tableposinassociate_docstringsdsl;get_docstring~info:falsedslwithNot_found->Noneletmark_pre_docspos=tryletdsl=Hashtbl.findpre_tableposinassociate_docstringsdslwithNot_found->()(* Map from positions to post docstrings *)letpost_table:(Lexing.position,docstringlist)Hashtbl.t=Hashtbl.create50letset_post_docstringsposdsl=ifdsl<>[]thenHashtbl.addpost_tableposdslletget_post_docspos=tryletdsl=Hashtbl.findpost_tableposinassociate_docstringsdsl;get_docstring~info:falsedslwithNot_found->Noneletmark_post_docspos=tryletdsl=Hashtbl.findpost_tableposinassociate_docstringsdslwithNot_found->()letget_infopos=tryletdsl=Hashtbl.findpost_tableposinget_docstring~info:truedslwithNot_found->None(* Map from positions to floating docstrings *)letfloating_table:(Lexing.position,docstringlist)Hashtbl.t=Hashtbl.create50letset_floating_docstringsposdsl=ifdsl<>[]thenHashtbl.addfloating_tableposdslletget_textpos=tryletdsl=Hashtbl.findfloating_tableposinget_docstringsdslwithNot_found->[]letget_post_textpos=tryletdsl=Hashtbl.findpost_tableposinget_docstringsdslwithNot_found->[](* Maps from positions to extra docstrings *)letpre_extra_table:(Lexing.position,docstringlist)Hashtbl.t=Hashtbl.create50letset_pre_extra_docstringsposdsl=ifdsl<>[]thenHashtbl.addpre_extra_tableposdslletget_pre_extra_textpos=tryletdsl=Hashtbl.findpre_extra_tableposinget_docstringsdslwithNot_found->[]letpost_extra_table:(Lexing.position,docstringlist)Hashtbl.t=Hashtbl.create50letset_post_extra_docstringsposdsl=ifdsl<>[]thenHashtbl.addpost_extra_tableposdslletget_post_extra_textpos=tryletdsl=Hashtbl.findpost_extra_tableposinget_docstringsdslwithNot_found->[](* Docstrings from parser actions *)letsymbol_docs()={docs_pre=get_pre_docs(Parsing.symbol_start_pos());docs_post=get_post_docs(Parsing.symbol_end_pos());}letsymbol_docs_lazy()=letp1=Parsing.symbol_start_pos()inletp2=Parsing.symbol_end_pos()inlazy{docs_pre=get_pre_docsp1;docs_post=get_post_docsp2;}letrhs_docspos1pos2={docs_pre=get_pre_docs(Parsing.rhs_start_pospos1);docs_post=get_post_docs(Parsing.rhs_end_pospos2);}letrhs_docs_lazypos1pos2=letp1=Parsing.rhs_start_pospos1inletp2=Parsing.rhs_end_pospos2inlazy{docs_pre=get_pre_docsp1;docs_post=get_post_docsp2;}letmark_symbol_docs()=mark_pre_docs(Parsing.symbol_start_pos());mark_post_docs(Parsing.symbol_end_pos())letmark_rhs_docspos1pos2=mark_pre_docs(Parsing.rhs_start_pospos1);mark_post_docs(Parsing.rhs_end_pospos2)letsymbol_info()=get_info(Parsing.symbol_end_pos())letrhs_infopos=get_info(Parsing.rhs_end_pospos)letsymbol_text()=get_text(Parsing.symbol_start_pos())letsymbol_text_lazy()=letpos=Parsing.symbol_start_pos()inlazy(get_textpos)letrhs_textpos=get_text(Parsing.rhs_start_pospos)letrhs_post_textpos=get_post_text(Parsing.rhs_end_pospos)letrhs_text_lazypos=letpos=Parsing.rhs_start_posposinlazy(get_textpos)letsymbol_pre_extra_text()=get_pre_extra_text(Parsing.symbol_start_pos())letsymbol_post_extra_text()=get_post_extra_text(Parsing.symbol_end_pos())letrhs_pre_extra_textpos=get_pre_extra_text(Parsing.rhs_start_pospos)letrhs_post_extra_textpos=get_post_extra_text(Parsing.rhs_end_pospos)(* (Re)Initialise all comment state *)letinit()=docstrings:=[];Hashtbl.resetpre_table;Hashtbl.resetpost_table;Hashtbl.resetfloating_table;Hashtbl.resetpre_extra_table;Hashtbl.resetpost_extra_table