Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file show.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177(*
* Generic transformers: plugins.
* Copyright (C) 2016-2022
* Dmitrii Kosarev aka Kakadu
* St.Petersburg State University, JetBrains Research
*)(** {i Show} plugin: converts value to a string.
Synthetized attributes' type (both default and for type parameters) is [string].
Inherited attributes' type (both default and for type parameters) is [unit].
For type declaration [type ('a,'b,...) typ = ...] it will create transformation
function with type
[('a -> string) -> ('b -> string) -> ... -> ('a,'b,...) typ -> string]
See also: {!Fmt} plugin.
*)openPpxlibopenPrintfopenGTCommonopenHelpersBaselettrait_name="show"moduleMake(AstHelpers:GTHELPERS_sig.S)=structlettrait_name=trait_namemoduleP=Plugin.Make(AstHelpers)openAstHelpersletapp_format_sprintf~locarg=Exp.app~loc(Exp.of_longident~loc(Ldot(Lident"Format","sprintf")))arg;;classgargstdecls=object(self)inherit[loc,Exp.t,Typ.t,type_arg,Cl.t,Ctf.t,Cf.t,Str.t,Sig.t,Pat.t]Plugin_intf.typ_ginheritP.generatorargstdeclsinheritP.no_inherit_argargstdeclsmethodtrait_name=trait_namemethodinh_of_main~loc_tdecl=Typ.ident~loc"unit"methodsyn_of_main~loc?in_class_tdecl=Typ.ident~loc"string"methodsyn_of_param~loc_=Typ.ident~loc"string"methodinh_of_param~loctdecl_name=self#inh_of_main~loctdeclmethodplugin_class_params~loctyps~typname=List.maptyps~f:Typ.from_caml@[Typ.var~loc@@Naming.make_extra_paramtypname](* Adapted to generate only single method per constructor definition *)methodon_tuple_constr~loc~is_self_rec~mutual_decls~inhetdeclconstr_infots=letconstr_name=matchconstr_infowith|Some(`Polys)->sprintf"`%s"s|Some(`Normals)->sprintf"%s"s|None->""inletnames=List.mapts~f:fstinExp.let_~loc[Pat.unit~loc,inhe]@@ifList.lengthts=0thenExp.string_const~locconstr_nameelseList.fold_leftts~f:(funacc(name,typ)->Exp.app~locacc(self#app_transformation_expr~loc(self#do_typ_gen~loc~is_self_rec~mutual_declstdecltyp)(Exp.unit~loc)(Exp.ident~locname)))~init:Exp.(app~loc(of_longident~loc(Ldot(Lident"Printf","sprintf")))@@letfmt=StringLabels.concat~sep:", "@@List.mapnames~f:(fun_->"%s")inExp.string_const~loc@@Printf.sprintf"%s%s(%s)"constr_name(ifList.is_emptyts||Option.is_noneconstr_infothen""else" ")fmt)method!on_record_constr~loc~is_self_rec~mutual_decls~inhetdeclinfobindingslabs=assert(List.lengthlabs>0);letconstr_name=matchinfowith|`Polys->sprintf"`%s"s|`Normals->sinifList.lengthbindings=0thenfailwith"Record constructors can't have empty label list"elseList.fold_leftbindings~f:(funacc(ident,labname,typ)->Exp.app~locacc@@self#app_transformation_expr~loc(self#do_typ_gen~loc~is_self_rec~mutual_declstdecltyp)(Exp.unit~loc)(Exp.ident~locident))~init:Exp.(app~loc(of_longident~loc(Ldot(Lident"Printf","sprintf")))@@letfmt=StringLabels.concat~sep:", "@@List.mapbindings~f:(fun(_,lab,_)->Printf.sprintf"%s=%%s"lab)inExp.string_const~loc@@Printf.sprintf"%s {%s}"constr_namefmt)methodon_record_declaration~loc~is_self_rec~mutual_declstdecllabs=letpat=Pat.record~loc@@List.maplabs~f:(funl->Lidentl.pld_name.txt,Pat.var~locl.pld_name.txt)inletfmt=List.fold_leftlabs~init:""~f:(funaccx->sprintf"%s %s=%%s;"accx.pld_name.txt)in[Cf.method_concrete~loc(Naming.meth_name_for_recordtdecl)@@Exp.fun_~loc(Pat.unit~loc)@@Exp.fun_~locpat@@List.fold_leftlabs~f:(funacc{pld_name;pld_type}->Exp.app~locacc(self#app_transformation_expr~loc(self#do_typ_gen~loc~is_self_rec~mutual_declstdeclpld_type)(Exp.unit~loc)(Exp.ident~locpld_name.txt)))~init:(app_format_sprintf~loc@@Exp.string_const~loc@@sprintf"{%s }"fmt)]methodtreat_type_speciallyt=Option.map~f:(fun_->letloc=loc_from_camlt.ptyp_locinExp.fun_~loc(Pat.unit~loc)@@Exp.fun_~loc(Pat.any~loc)@@Exp.string_const~loc"\"<opaque>\"")@@List.findt.ptyp_attributes~f:(fun{attr_name={txt}}->String.equaltxt"opaque")method!make_inh~loc=Pat.unit~loc,Exp.unit~locendletcreate=(newg:>P.plugin_constructor)endletregister()=Expander.register_plugintrait_name(moduleMake:Plugin_intf.MAKE)let()=register()