123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159moduleArray=Stdlib.ArrayLabelsmoduleList=Stdlib.ListLabelsmoduleString=Stdlib.StringLabelsmoduleBytes=Stdlib.Bytestypet=|Opaque|Unit|Intofint|Int32ofint32|Int64ofint64|Nativeintofnativeint|Boolofbool|Stringofstring|Bytesofbytes|Charofchar|Floatoffloat|Optionoftoption|Listoftlist|Arrayoftarray|Tupleoftlist|Recordof(string*t)list|Variantofstring*tlist|Mapof(t*t)list|Setoftlistletunsnocl=matchList.revlwith|last::before_last->Some(List.revbefore_last,last)|[]->None;;letstring_in_ocaml_syntaxstr=letis_space=function|' '->(* don't need to handle tabs because those are already escaped *)true|_->falseinletescape_protect_first_spaces=letfirst_char=ifString.lengths>0&&is_spaces.[0]then"\\"else" "infirst_char^String.escapedsin(* CR-someday aalekseyev: should use the method from
[Dune_lang.prepare_formatter] so that the formatter can fit multiple lines
on one line. *)matchString.split_on_char~sep:'\n'strwith|[]->assertfalse|first::rest->(matchunsnocrestwith|None->Pp.verbatim(Printf.sprintf"%S"first)|Some(middle,last)->Pp.vbox(Pp.concat~sep:Pp.cut(List.map~f:Pp.verbatim((("\""^String.escapedfirst^"\\n\\")::List.mapmiddle~f:(funs->escape_protect_first_spaces^"\\n\\"))@[escape_protect_first_spacelast^"\""]))));;letpp_sequencestartstopx~f=letopenPp.Oinmatchxwith|[]->Pp.verbatimstart++Pp.verbatimstop|_->letsep=";"^String.make(String.lengthstart)' 'inPp.hvbox(Pp.concat_mapi~sep:Pp.cutx~f:(funix->Pp.box~indent:2((ifi=0thenPp.verbatim(start^" ")elsePp.verbatimsep)++fx))++Pp.space++Pp.verbatimstop);;letrecpp?(in_arg=false)=letopenPp.Oinfunction|Opaque->Pp.verbatim"<opaque>"|Unit->Pp.verbatim"()"|Inti->Pp.verbatim(string_of_inti)|Int32i->Pp.verbatim(Int32.to_stringi)|Int64i->Pp.verbatim(Int64.to_stringi)|Nativeinti->Pp.verbatim(Nativeint.to_stringi)|Boolb->Pp.verbatim(string_of_boolb)|Strings->string_in_ocaml_syntaxs|Bytesb->string_in_ocaml_syntax(Bytes.to_stringb)|Charc->Pp.charc|Floatf->Pp.verbatim(string_of_floatf)|OptionNone->pp~in_arg(Variant("None",[]))|Option(Somex)->pp~in_arg(Variant("Some",[x]))|Listxs->pp_sequence"[""]"xs~f:pp|Arrayxs->pp_sequence"[|""|]"(Array.to_listxs)~f:pp|Setxs->Pp.box~indent:2(Pp.verbatim"set"++Pp.space++pp_sequence"{""}"xs~f:pp)|Mapxs->Pp.box~indent:2(Pp.verbatim"map"++Pp.space++pp_sequence"{""}"xs~f:(fun(k,v)->Pp.box~indent:2(ppk++Pp.space++Pp.char':'++Pp.space++ppv)))|Tuplexs->Pp.char'('++Pp.hvbox(Pp.concat_map~sep:(Pp.seq(Pp.char',')Pp.space)xs~f:pp)++Pp.char')'|Recordfields->pp_sequence"{""}"fields~f:(fun(f,v)->Pp.box~indent:2(Pp.verbatimf++Pp.space++Pp.char'='++Pp.space++ppv))|Variant(v,[])->Pp.verbatimv|Variant(v,(_::_asxs))->letarg=matchxswith|[x]->x|_->Tuplexsinletapp=Pp.hvbox~indent:2(Pp.verbatimv++Pp.space++pp~in_arg:truearg)inifin_argthenPp.char'('++app++Pp.char')'elseapp;;letppt=pptletto_stringt=Format.asprintf"%a"Pp.to_fmt(ppt)type'abuilder='a->tletunit()=Unitletcharx=Charxletstringx=Stringxletintx=Intxletint32x=Int32xletint64x=Int64xletnativeintx=Nativeintxletfloatx=Floatxletboolx=Boolxletpairfg(x,y)=Tuple[fx;gy]lettriplefgh(x,y,z)=Tuple[fx;gy;hz]letlistfl=List(List.map~fl)letarrayfa=Array(Array.map~fa)letoptionfx=Option(matchxwith|None->None|Somex->Some(fx));;letrecordr=Recordrletopaque_=Opaqueletvariantsargs=Variant(s,args)lethash=Stdlib.Hashtbl.hashletcomparexy=Ordering.of_int(comparexy)letequalxy=x=yletresultokerr=function|Oke->variant"Ok"[oke]|Errore->variant"Error"[erre];;