123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openMichelinetypelocation={comment:stringoption}typenode=(location,string)Micheline.nodeletprintable?(comment=fun_->None)map_primexpr=letmap_locloc={comment=commentloc}inmap_nodemap_locmap_prim(rootexpr)letprint_commentppftext=Format.fprintfppf"/* @[<h>%a@] */"Format.pp_print_texttextletprint_stringppftext=Format.fprintfppf"\"";String.iter(function|'"'->Format.fprintfppf"\\\""|'\n'->Format.fprintfppf"\\n"|'\r'->Format.fprintfppf"\\r"|'\b'->Format.fprintfppf"\\b"|'\t'->Format.fprintfppf"\\t"|'\\'->Format.fprintfppf"\\\\"|c->Format.fprintfppf"%c"c)text;Format.fprintfppf"\""letprint_annotations=Format.pp_print_list~pp_sep:Format.pp_print_spaceFormat.pp_print_stringletpreformatroot=letpreformat_loc=function|{comment=None}->(false,0)|{comment=Sometext}->(String.containstext'\n',String.lengthtext+1)inletpreformat_annots=function|[]->0|annots->String.length(String.concat" "annots)+2inletrecpreformat_expr=function|Int(loc,value)->let(cml,csz)=preformat_loclocinInt((cml,String.length(Z.to_stringvalue)+csz,loc),value)|String(loc,value)->let(cml,csz)=preformat_loclocinString((cml,String.lengthvalue+csz,loc),value)|Bytes(loc,value)->let(cml,csz)=preformat_loclocinBytes((cml,(Bytes.lengthvalue*2)+2+csz,loc),value)|Prim(loc,name,items,annots)->let(cml,csz)=preformat_loclocinletasz=preformat_annotsannotsinletitems=List.mappreformat_expritemsinlet(ml,sz)=List.fold_left(fun(tml,tsz)e->let(ml,sz,_)=locationein(tml||ml,tsz+1+sz))(cml,String.lengthname+csz+asz)itemsinPrim((ml,sz,loc),name,items,annots)|Seq(loc,items)->let(cml,csz)=preformat_loclocinletitems=List.mappreformat_expritemsinlet(ml,sz)=List.fold_left(fun(tml,tsz)e->let(ml,sz,_)=locationein(tml||ml,tsz+3+sz))(cml,4+csz)itemsinSeq((ml,sz,loc),items)inpreformat_exprrootletrecprint_expr_unwrappedppf=function|Prim((ml,s,{comment}),name,args,annot)->letname=matchannotwith|[]->name|annots->Format.asprintf"%s @[<h>%a@]"nameprint_annotationsannotsinif(notml)&&s<80then(ifargs=[]thenFormat.fprintfppf"%s"nameelseFormat.fprintfppf"@[<h>%s %a@]"name(Format.pp_print_list~pp_sep:Format.pp_print_spaceprint_expr)args;matchcommentwith|None->()|Sometext->Format.fprintfppf"@ /* %s */"text)else(ifargs=[]thenFormat.fprintfppf"%s"nameelseifString.lengthname<=4thenFormat.fprintfppf"%s @[<v 0>%a@]"name(Format.pp_print_listprint_expr)argselseFormat.fprintfppf"@[<v 2>%s@,%a@]"name(Format.pp_print_listprint_expr)args;matchcommentwith|None->()|Somecomment->Format.fprintfppf"@ %a"print_commentcomment)|Int((_,_,{comment}),value)->(matchcommentwith|None->Format.fprintfppf"%s"(Z.to_stringvalue)|Somecomment->Format.fprintfppf"%s@ %a"(Z.to_stringvalue)print_commentcomment)|String((_,_,{comment}),value)->(matchcommentwith|None->print_stringppfvalue|Somecomment->Format.fprintfppf"%a@ %a"print_stringvalueprint_commentcomment)|Bytes((_,_,{comment}),value)->(matchcommentwith|None->Format.fprintfppf"0x%a"Hex.pp(Hex.of_bytesvalue)|Somecomment->Format.fprintfppf"0x%a@ %a"Hex.pp(Hex.of_bytesvalue)print_commentcomment)|Seq((_,_,{comment=None}),[])->Format.fprintfppf"{}"|Seq((ml,s,{comment}),items)->if(notml)&&s<80thenFormat.fprintfppf"{ @[<h 0>"elseFormat.fprintfppf"{ @[<v 0>";(match(comment,items)with|(None,_)->()|(Somecomment,[])->Format.fprintfppf"%a"print_commentcomment|(Somecomment,_)->Format.fprintfppf"%a@ "print_commentcomment);Format.pp_print_list~pp_sep:(funppf()->Format.fprintfppf" ;@ ")print_expr_unwrappedppfitems;Format.fprintfppf"@] }"andprint_exprppf=function|(Prim(_,_,_::_,_)|Prim(_,_,[],_::_))asexpr->Format.fprintfppf"(%a)"print_expr_unwrappedexpr|expr->print_expr_unwrappedppfexprletwith_unbounded_formatterppffx=letbuf=Buffer.create10000inletsppf=Format.formatter_of_bufferbufinFormat.pp_set_marginsppf199999;Format.pp_set_max_indentsppf99999;Format.pp_set_max_boxessppf99999;fsppfx;Format.fprintfsppf"%!";letlines=String.split_on_char'\n'(Buffer.contentsbuf)inFormat.pp_print_list~pp_sep:Format.pp_force_newlineFormat.pp_print_stringppflinesletprint_expr_unwrappedppfexpr=with_unbounded_formatterppfprint_expr_unwrapped(preformatexpr)letprint_exprppfexpr=with_unbounded_formatterppfprint_expr(preformatexpr)