Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file gfmt.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155(*
* Generic Transformers: `format` plugin.
* Copyright (C) 2016-2022
* Dmitrii Kosarev a.k.a Kakadu
* St.Petersburg State University, JetBrains Research
*)(** {i Format} module: pretty-prints a value to {!Format.formatter} using {!Format} module.
For type declaration [type ('a,'b,...) typ = ...] it will create a transformation
function with type
[(Format.formatter -> 'a -> unit) -> (Format.formatter -> 'b -> unit) -> ... ->
Format.formatter -> ('a,'b,...) typ -> unit ]
Inherited attributes' type (both default and for type parameters) is [Format.formatter].
Synthesized attributes' type (both default and for type parameters) is [unit].
*)openPpxlibopenStdppxopenPrintfopenGTCommonopenHelpersBaselettrait_name="fmt"moduleMake(AstHelpers:GTHELPERS_sig.S)=structlettrait_name=trait_namemoduleP=Plugin.Make(AstHelpers)openAstHelpersletapp_format_fprintf~locefmtrefmts=Exp.app_list~locExp.(of_longident~loc(Ldot(Lident"Format","fprintf")))[efmtr;efmts];;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.with_inherited_attrargstdeclsmethodtrait_name=trait_namemethodinh_of_main~loc_tdecl=Typ.of_longident~loc(Ldot(Lident"Format","formatter"))methodsyn_of_main~loc?in_class_tdecl=Typ.ident~loc"unit"methodsyn_of_param~loc_=Typ.ident~loc"unit"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_info,tswith|Some(`Polys),[]->sprintf"`%s"s|Some(`Polys),_->sprintf"`%s "s|Some(`Normals),[]->sprintf"%s"s|Some(`Normals),_->sprintf"%s "s|None,_->""inifList.lengthts=0thenapp_format_fprintf~locinhe@@Exp.string_const~locconstr_nameelse(letfmt=List.mapts~f:(fun_->"%a")|>String.concat~sep:",@,@ "inletfmt=sprintf"%s@[(@,%s@,)@]"constr_namefmtinList.fold_leftts~f:(funacc(name,typ)->Exp.app_list~locacc[self#do_typ_gen~loc~is_self_rec~mutual_declstdecltyp;Exp.ident~locname])~init:(app_format_fprintf~locinhe@@Exp.string_const~locfmt))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)inletmethname=sprintf"do_%s"tdecl.ptype_name.txtinletfmt=List.fold_leftlabs~init:""~f:(funaccx->sprintf"%s@,@ @,@[%s@,=@,%%a;@]"accx.pld_name.txt)inletfmt_name=gen_symbol~prefix:"fmt"()in[Cf.method_concrete~locmethname@@Exp.fun_~loc(Pat.sprintf"%s"~locfmt_name)@@Exp.fun_~locpat@@List.fold_leftlabs~f:(funacc{pld_name;pld_type}->Exp.app_list~locacc[self#do_typ_gen~loc~is_self_rec~mutual_declstdeclpld_type;Exp.ident~locpld_name.txt])~init:(app_format_fprintf~loc(Exp.sprintf"%s"~locfmt_name)@@Exp.string_const~loc@@sprintf"{@[<hov>%s@]@ }@,"fmt)]method!on_record_constr~loc~is_self_rec~mutual_decls~inhetdeclinfobindingslabs=letcname=matchinfowith|`Normals->s|`Polys->sinletfmt=List.fold_leftlabs~init:""~f:(funaccl->sprintf"%s@,@ @,@[%s@,=@,%%a;@]"accl.pld_name.txt)inList.fold_leftbindings~f:(funacc(name,_,typ)->Exp.app_list~locacc[self#do_typ_gen~loc~is_self_rec~mutual_declstdecltyp;Exp.ident~locname])~init:(app_format_fprintf~locinhe@@Exp.string_const~loc@@sprintf"%s {@[<hov>%s@]@ }@,"cnamefmt)endletcreate=(newg:>P.plugin_constructor)endletregister()=Expander.register_plugintrait_name(moduleMake:Plugin_intf.MAKE)let()=register()