Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file Normalize_std_ast.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211(**************************************************************************)(* *)(* 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. *)(* *)(**************************************************************************)openParser_standardopenStd_astletis_doc=function|{attr_name={Location.txt="ocaml.doc"|"ocaml.text";_};_}->true|_->falseletdedup_cmtsfragmentastcomments=letof_astast=letdocs=ref(Set.empty(moduleCmt))inletattributematr=matchatrwith|{attr_payload=PStr[{pstr_desc=Pstr_eval({pexp_desc=Pexp_constant(Pconst_string(doc,_,None));pexp_loc;_},[]);_}];_}whenis_docatr->docs:=Set.add!docs(Cmt.create("*"^doc)pexp_loc);atr|_->Ast_mapper.default_mapper.attributematrinmapfragment{Ast_mapper.default_mapperwithattribute}ast|>ignore;!docsinSet.(to_list(diff(of_list(moduleCmt)comments)(of_astast)))letnormalize_codeconf(m:Ast_mapper.mapper)txt=matchParse_with_comments.parseParse.astStructureconf~source:txtwith|{ast;comments;_}->letcomments=dedup_cmtsStructureastcommentsinletprint_commentsfmt(l:Cmt.tlist)=List.sortl~compare:(fun{Cmt.loc=a;_}{Cmt.loc=b;_}->Migrate_ast.Location.compareab)|>List.iter~f:(fun{Cmt.txt;_}->Format.fprintffmt"%s,"txt)inletast=m.structuremastinFormat.asprintf"AST,%a,COMMENTS,[%a]"Printast.implementationastprint_commentscomments|exception_->txtletdocstring(c:Conf.t)=Docstring.normalize~parse_docstrings:c.fmt_opts.parse_docstringsletsort_attributes:attributes->attributes=List.sort~compare:Poly.compareletmake_mapperconf~ignore_doc_comments=letopenAst_helperin(* remove locations *)letlocation__=Location.noneinletattribute(m:Ast_mapper.mapper)(attr:attribute)=matchattr.attr_payloadwith|PStr[({pstr_desc=Pstr_eval(({pexp_desc=Pexp_constant(Pconst_string(doc,str_loc,None));_}asexp),[]);_}aspstr)]whenis_docattr->letnormalize_code=normalize_codeconfminletdoc'=docstringconf~normalize_codedocinAst_mapper.default_mapper.attributem{attrwithattr_payload=PStr[{pstrwithpstr_desc=Pstr_eval({expwithpexp_desc=Pexp_constant(Pconst_string(doc',str_loc,None));pexp_loc_stack=[]},[])}]}|_->Ast_mapper.default_mapper.attributemattrin(* sort attributes *)letattributes(m:Ast_mapper.mapper)(atrs:attributelist)=letatrs=ifignore_doc_commentsthenList.filteratrs~f:(funa->not(is_doca))elseatrsinAst_mapper.default_mapper.attributesm(sort_attributesatrs)inletexpr(m:Ast_mapper.mapper)exp=letexp={expwithpexp_loc_stack=[]}inlet{pexp_desc;pexp_loc=loc1;pexp_attributes=attrs1;_}=expinmatchpexp_descwith|Pexp_poly({pexp_desc=Pexp_constraint(e,t);_},None)->m.exprm{expwithpexp_desc=Pexp_poly(e,Somet)}|Pexp_constraint(e,{ptyp_desc=Ptyp_poly([],_t);_})->m.exprme|Pexp_sequence(exp1,{pexp_desc=Pexp_sequence(exp2,exp3);pexp_loc=loc2;pexp_attributes=attrs2;_})->m.exprm(Exp.sequence~loc:loc1~attrs:attrs1(Exp.sequence~loc:loc2~attrs:attrs2exp1exp2)exp3)|_->Ast_mapper.default_mapper.exprmexpinletpat(m:Ast_mapper.mapper)pat=letpat={patwithppat_loc_stack=[]}inlet{ppat_desc;ppat_loc=loc1;ppat_attributes=attrs1;_}=patin(* normalize nested or patterns *)matchppat_descwith|Ppat_or(pat1,{ppat_desc=Ppat_or(pat2,pat3);ppat_loc=loc2;ppat_attributes=attrs2;_})->m.patm(Pat.or_~loc:loc1~attrs:attrs1(Pat.or_~loc:loc2~attrs:attrs2pat1pat2)pat3)|_->Ast_mapper.default_mapper.patmpatinlettyp(m:Ast_mapper.mapper)typ=lettyp={typwithptyp_loc_stack=[]}inAst_mapper.default_mapper.typmtypin{Ast_mapper.default_mapperwithlocation;attribute;attributes;expr;pat;typ}letastfragment~ignore_doc_commentsc=mapfragment(make_mapperc~ignore_doc_comments)letequalfragment~ignore_doc_commentscast1ast2=letmap=astfragmentc~ignore_doc_commentsinequalfragment(mapast1)(mapast2)letast=ast~ignore_doc_comments:falseletmake_docstring_mapperdocstrings=letattribute(m:Ast_mapper.mapper)attr=match(attr.attr_name,attr.attr_payload)with|({txt="ocaml.doc"|"ocaml.text";loc},PStr[{pstr_desc=Pstr_eval({pexp_desc=Pexp_constant(Pconst_string(doc,_,None));_},[]);_}])->docstrings:=(loc,doc)::!docstrings;attr|_->Ast_mapper.default_mapper.attributemattrin(* sort attributes *)letattributes(m:Ast_mapper.mapper)atrs=letatrs=List.filteratrs~f:is_docinAst_mapper.default_mapper.attributesm(sort_attributesatrs)in{Ast_mapper.default_mapperwithattribute;attributes}letdocstrings(typea)(fragment:at)s=letdocstrings=ref[]inlet(_:a)=mapfragment(make_docstring_mapperdocstrings)sin!docstringsletdocstringconf=letmapper=make_mapperconf~ignore_doc_comments:falseinletnormalize_code=normalize_codeconfmapperindocstringconf~normalize_codeletmoved_docstringsfragmentcs1s2=letd1=docstringsfragments1inletd2=docstringsfragments2inletequal(_,x)(_,y)=String.equal(docstringcx)(docstringcy)inmatchList.zipd1d2with|Unequal_lengths->(* We only return the ones that are not in both lists. *)letl1=List.filterd1~f:(funx->not(List.mem~equald2x))inletl1=List.map~f:(fun(loc,x)->Docstring.Removed(loc,x))l1inletl2=List.filterd2~f:(funx->not(List.mem~equald1x))inletl2=List.map~f:(fun(loc,x)->Docstring.Added(loc,x))l2inList.rev_appendl1l2|Okl->letl=List.filterl~f:(fun(x,y)->not(equalxy))inList.map~f:(fun((loc,x),(_,y))->Docstring.Unstable(loc,x,y))l