123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331# 1 "ppx_deriving_show.cppo.ml"openPpxlibopenAsttypesopenParsetreeopenAst_helperopenPpx_deriving.Ast_convenienceletderiver="show"letraise_errorf=Ppx_deriving.raise_errorftypeoptions={with_path:bool}(* The option [with_path] controls whether a full path should be displayed
as part of data constructor names and record field names. (In the case
of record fields, it is displayed only as part of the name of the first
field.) By default, this option is [true], which means that full paths
are shown. *)letexpand_pathshow_opts~pathname=letpath=ifshow_opts.with_paththenpathelse[]inPpx_deriving.expand_path~pathnameletparse_optionsoptions=letwith_path=reftrueinoptions|>List.iter(fun(name,expr)->matchnamewith|"with_path"->with_path:=Ppx_deriving.Arg.(get_expr~deriverbool)expr|_->raise_errorf~loc:expr.pexp_loc"%s does not support option %s"derivername);{with_path=!with_path}letattr_nobuiltinattrs=Ppx_deriving.(attrs|>attr~deriver"nobuiltin"|>Arg.get_flag~deriver)letattr_printerattrs=Ppx_deriving.(attrs|>attr~deriver"printer"|>Arg.(get_attr~deriverexpr))letattr_polyprinterattrs=Ppx_deriving.(attrs|>attr~deriver"polyprinter"|>Arg.(get_attr~deriverexpr))letattr_opaqueattrs=Ppx_deriving.(attrs|>attr~deriver"opaque"|>Arg.get_flag~deriver)letargn=Printf.sprintf"a%d"letargl=Printf.sprintf"a%s"letpattntyps=List.mapi(funi_->pvar(argni))typsletpattllabels=List.map(fun{pld_name={txt=n}}->n,pvar(argln))labelsletpconstrrecnamefields=pconstrname[precord~closed:Closedfields]letwrap_printerquoterprinter=letloc=!Ast_helper.default_locinPpx_deriving.quote~quoter[%expr(letfprintf=Ppx_deriving_runtime.Format.fprintfin[%eprinter])[@ocaml.warning"-26"]]letpp_type_of_decl~options~pathtype_decl=letloc=type_decl.ptype_locinlet_=parse_optionsoptionsinlettyp=Ppx_deriving.core_type_of_type_decltype_declinPpx_deriving.poly_arrow_of_type_decl(funvar->[%type:Ppx_deriving_runtime.Format.formatter->[%tvar]->Ppx_deriving_runtime.unit])type_decl[%type:Ppx_deriving_runtime.Format.formatter->[%ttyp]->Ppx_deriving_runtime.unit]letshow_type_of_decl~options~pathtype_decl=letloc=type_decl.ptype_locinlet_=parse_optionsoptionsinlettyp=Ppx_deriving.core_type_of_type_decltype_declinPpx_deriving.poly_arrow_of_type_decl(funvar->[%type:Ppx_deriving_runtime.Format.formatter->[%tvar]->Ppx_deriving_runtime.unit])type_decl[%type:[%ttyp]->Ppx_deriving_runtime.string]letsig_of_type~options~pathtype_decl=let_=parse_optionsoptionsin[Sig.value(Val.mk(mknoloc(Ppx_deriving.mangle_type_decl(`Prefix"pp")type_decl))(pp_type_of_decl~options~pathtype_decl));Sig.value(Val.mk(mknoloc(Ppx_deriving.mangle_type_decl(`Prefix"show")type_decl))(show_type_of_decl~options~pathtype_decl))]letrecexpr_of_typquotertyp=letloc=typ.ptyp_locinletexpr_of_typ=expr_of_typquoterinmatchattr_printertyp.ptyp_attributeswith|Someprinter->[%expr[%ewrap_printerquoterprinter]fmt]|None->ifattr_opaquetyp.ptyp_attributesthen[%exprfun_->Ppx_deriving_runtime.Format.pp_print_stringfmt"<opaque>"]elseletformatx=[%exprPpx_deriving_runtime.Format.fprintffmt[%estrx]]inletseqstartfinishfoldtyp=[%exprfunx->Ppx_deriving_runtime.Format.fprintffmt[%estrstart];ignore([%efold](funsepx->ifsepthenPpx_deriving_runtime.Format.fprintffmt";@ ";[%eexpr_of_typtyp]x;true)falsex);Ppx_deriving_runtime.Format.fprintffmt[%estrfinish];]inlettyp=Ppx_deriving.remove_pervasives~derivertypinmatchtypwith|[%type:_]->[%exprfun_->Ppx_deriving_runtime.Format.pp_print_stringfmt"_"]|{ptyp_desc=Ptyp_arrow_}->[%exprfun_->Ppx_deriving_runtime.Format.pp_print_stringfmt"<fun>"]|{ptyp_desc=Ptyp_constr_}->letbuiltin=not(attr_nobuiltintyp.ptyp_attributes)inbeginmatchbuiltin,typwith|true,[%type:unit]->[%exprfun()->Ppx_deriving_runtime.Format.pp_print_stringfmt"()"]|true,[%type:int]->format"%d"|true,[%type:int32]|true,[%type:Int32.t]->format"%ldl"|true,[%type:int64]|true,[%type:Int64.t]->format"%LdL"|true,[%type:nativeint]|true,[%type:Nativeint.t]->format"%ndn"|true,[%type:float]->format"%F"|true,[%type:bool]->format"%B"|true,[%type:char]->format"%C"|true,[%type:string]|true,[%type:String.t]->format"%S"|true,[%type:bytes]|true,[%type:Bytes.t]->[%exprfunx->Ppx_deriving_runtime.Format.fprintffmt"%S"(Bytes.to_stringx)]|true,[%type:[%t?typ]ref]->[%exprfunx->Ppx_deriving_runtime.Format.pp_print_stringfmt"ref (";[%eexpr_of_typtyp]!x;Ppx_deriving_runtime.Format.pp_print_stringfmt")"]|true,[%type:[%t?typ]list]->seq"@[<2>[""@,]@]"[%exprList.fold_left]typ|true,[%type:[%t?typ]array]->seq"@[<2>[|""@,|]@]"[%exprArray.fold_left]typ|true,[%type:[%t?typ]option]->[%exprfunction|None->Ppx_deriving_runtime.Format.pp_print_stringfmt"None"|Somex->Ppx_deriving_runtime.Format.pp_print_stringfmt"(Some ";[%eexpr_of_typtyp]x;Ppx_deriving_runtime.Format.pp_print_stringfmt")"]|true,([%type:([%t?ok_t],[%t?err_t])result]|[%type:([%t?ok_t],[%t?err_t])Result.result])->[%exprfunction|Result.Okok->Ppx_deriving_runtime.Format.pp_print_stringfmt"(Ok ";[%eexpr_of_typok_t]ok;Ppx_deriving_runtime.Format.pp_print_stringfmt")"|Result.Errore->Ppx_deriving_runtime.Format.pp_print_stringfmt"(Error ";[%eexpr_of_typerr_t]e;Ppx_deriving_runtime.Format.pp_print_stringfmt")"]|true,([%type:[%t?typ]lazy_t]|[%type:[%t?typ]Lazy.t])->[%exprfunx->ifLazy.is_valxthen[%eexpr_of_typtyp](Lazy.forcex)elsePpx_deriving_runtime.Format.pp_print_stringfmt"<not evaluated>"]|_,{ptyp_desc=Ptyp_constr({txt=lid},args)}->letargs_pp=List.map(funtyp->[%exprfunfmt->[%eexpr_of_typtyp]])argsinletprinter=matchattr_polyprintertyp.ptyp_attributeswith|Someprinter->wrap_printerquoterprinter|None->letprinter=Exp.ident(mknoloc(Ppx_deriving.mangle_lid(`Prefix"pp")lid))inPpx_deriving.quote~quoterprinterinappprinter(args_pp@[[%exprfmt]])|_->assertfalseend|{ptyp_desc=Ptyp_tupletyps}->letargs=List.mapi(funityp->app(expr_of_typtyp)[evar(argni)])typsin[%exprfun[%pptuple(List.mapi(funi_->pvar(argni))typs)]->Ppx_deriving_runtime.Format.fprintffmt"(@[";[%eargs|>Ppx_deriving.(fold_exprs(seq_reduce~sep:[%exprPpx_deriving_runtime.Format.fprintffmt",@ "]))];Ppx_deriving_runtime.Format.fprintffmt"@])"]|{ptyp_desc=Ptyp_variant(fields,_,_);ptyp_loc}->letcases=fields|>List.map(funfield->matchfield.prf_descwith|Rtag(label,true(*empty*),[])->letlabel=label.txtinExp.case(Pat.variantlabelNone)[%exprPpx_deriving_runtime.Format.pp_print_stringfmt[%estr("`"^label)]]|Rtag(label,false,[typ])->letlabel=label.txtinExp.case(Pat.variantlabel(Some[%pat?x]))[%exprPpx_deriving_runtime.Format.fprintffmt[%estr("`"^label^" (@[<hov>")];[%eexpr_of_typtyp]x;Ppx_deriving_runtime.Format.fprintffmt"@])"]|Rinherit({ptyp_desc=Ptyp_constr(tname,_)}astyp)->Exp.case[%pat?[%pPat.type_tname]asx][%expr[%eexpr_of_typtyp]x]|_->raise_errorf~loc:ptyp_loc"%s cannot be derived for %s"deriver(Ppx_deriving.string_of_core_typetyp))inExp.function_cases|{ptyp_desc=Ptyp_varname}->[%expr[%eevar("poly_"^name)]fmt]|{ptyp_desc=Ptyp_alias(typ,_)}->expr_of_typtyp|{ptyp_loc}->raise_errorf~loc:ptyp_loc"%s cannot be derived for %s"deriver(Ppx_deriving.string_of_core_typetyp)andexpr_of_label_declquoter{pld_type;pld_attributes}=letattrs=pld_type.ptyp_attributes@pld_attributesinexpr_of_typquoter{pld_typewithptyp_attributes=attrs}letstr_of_type~options~path({ptype_loc=loc}astype_decl)=letshow_opts=parse_optionsoptionsinletquoter=Ppx_deriving.create_quoter()inletpath=Ppx_deriving.path_of_type_decl~pathtype_declinletprettyprinter=matchtype_decl.ptype_kind,type_decl.ptype_manifestwith|Ptype_abstract,Somemanifest->[%exprfunfmt->[%eexpr_of_typquotermanifest]]|Ptype_variantconstrs,_->letcases=constrs|>List.map(fun{pcd_name={txt=name'};pcd_args;pcd_attributes}->letconstr_name=expand_pathshow_opts~pathname'inmatchattr_printerpcd_attributes,pcd_argswith|Someprinter,Pcstr_tuple(args)->letrecrangefrom_idxto_idx=iffrom_idx=to_idxthen[]elsefrom_idx::(range(from_idx+1)to_idx)inletindices=range0(List.lengthargs)inletpattern_vars=List.map(funi->pvar("a"^string_of_inti))indicesinletexpr_vars=List.map(funi->evar("a"^string_of_inti))indicesinExp.case(pconstrname'pattern_vars)[%expr[%ewrap_printerquoterprinter]fmt[%etupleexpr_vars]]|Someprinter,Pcstr_record(labels)->letargs=labels|>List.map(fun{pld_name={txt=n}}->evar(argln))inExp.case(pconstrrecname'(pattllabels))(app(wrap_printerquoterprinter)([%exprfmt]::args))|None,Pcstr_tuple(typs)->letargs=List.mapi(funityp->app(expr_of_typquotertyp)[evar(argni)])typsinletprinter=matchargswith|[]->[%exprPpx_deriving_runtime.Format.pp_print_stringfmt[%estrconstr_name]]|[arg]->[%exprPpx_deriving_runtime.Format.fprintffmt[%estr("(@[<2>"^constr_name^"@ ")];[%earg];Ppx_deriving_runtime.Format.fprintffmt"@])"]|args->[%exprPpx_deriving_runtime.Format.fprintffmt[%estr("(@[<2>"^constr_name^" (@,")];[%eargs|>Ppx_deriving.(fold_exprs(seq_reduce~sep:[%exprPpx_deriving_runtime.Format.fprintffmt",@ "]))];Ppx_deriving_runtime.Format.fprintffmt"@,))@]"]inExp.case(pconstrname'(pattntyps))printer|None,Pcstr_record(labels)->letargs=labels|>List.map(fun({pld_name={txt=n};_}aspld)->[%exprPpx_deriving_runtime.Format.fprintffmt"@[%s =@ "[%estrn];[%eexpr_of_label_declquoterpld][%eevar(argln)];Ppx_deriving_runtime.Format.fprintffmt"@]"])inletprinter=[%exprPpx_deriving_runtime.Format.fprintffmt[%estr("@[<2>"^constr_name^" {@,")];[%eargs|>Ppx_deriving.(fold_exprs(seq_reduce~sep:[%exprPpx_deriving_runtime.Format.fprintffmt";@ "]))];Ppx_deriving_runtime.Format.fprintffmt"@]}"]inExp.case(pconstrrecname'(pattllabels))printer)in[%exprfunfmt->[%eExp.function_cases]]|Ptype_recordlabels,_->letfields=labels|>List.mapi(funi({pld_name={txt=name};_}aspld)->letfield_name=ifi=0thenexpand_pathshow_opts~pathnameelsenamein[%exprPpx_deriving_runtime.Format.fprintffmt"@[%s =@ "[%estrfield_name];[%eexpr_of_label_declquoterpld][%eExp.field(evar"x")(mknoloc(Lidentname))];Ppx_deriving_runtime.Format.fprintffmt"@]"])in[%exprfunfmtx->Ppx_deriving_runtime.Format.fprintffmt"@[<2>{ ";[%efields|>Ppx_deriving.(fold_exprs(seq_reduce~sep:[%exprPpx_deriving_runtime.Format.fprintffmt";@ "]))];Ppx_deriving_runtime.Format.fprintffmt"@ }@]"]|Ptype_abstract,None->raise_errorf~loc"%s cannot be derived for fully abstract types"deriver|Ptype_open,_->raise_errorf~loc"%s cannot be derived for open types"deriverinletpp_poly_apply=Ppx_deriving.poly_apply_of_type_decltype_decl(evar(Ppx_deriving.mangle_type_decl(`Prefix"pp")type_decl))inletstringprinter=[%exprfunx->Ppx_deriving_runtime.Format.asprintf"%a"[%epp_poly_apply]x]inletpolymorphize=Ppx_deriving.poly_fun_of_type_decltype_declinletpp_type=Ppx_deriving.strong_type_of_type@@pp_type_of_decl~options~pathtype_declinletshow_type=Ppx_deriving.strong_type_of_type@@show_type_of_decl~options~pathtype_declinletpp_var=pvar(Ppx_deriving.mangle_type_decl(`Prefix"pp")type_decl)inletshow_var=pvar(Ppx_deriving.mangle_type_decl(`Prefix"show")type_decl)inletno_warn_32=Ppx_deriving.attr_warning[%expr"-32"]in[Vb.mk(Pat.constraint_pp_varpp_type)(Ppx_deriving.sanitize~quoter(polymorphizeprettyprinter));Vb.mk~attrs:[no_warn_32](Pat.constraint_show_varshow_type)(polymorphizestringprinter);]let()=letloc=!Ast_helper.default_locinPpx_deriving.(register(createderiver~core_type:(Ppx_deriving.with_quoter(funquotertyp->[%exprfunx->Ppx_deriving_runtime.Format.asprintf"%a"(funfmt->[%eexpr_of_typquotertyp])x]))~type_decl_str:(fun~options~pathtype_decls->[Str.valueRecursive(List.concat(List.map(str_of_type~options~path)type_decls))])~type_decl_sig:(fun~options~pathtype_decls->List.concat(List.map(sig_of_type~options~path)type_decls))()))