Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file utils.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198openPpxlibopenAst_builder.Defaultletverbose=matchSys.getenv_opt"PPX_ENCODING_DEBUG"with|None|Some"0"|Some"false"|Some"no"->0|Somes->matchswith|"true"->1|s->matchint_of_string_optswith|Somei->i|None->0letdebug?(v=1)?(force=false)fmt=ifforce||verbose>=vthenFormat.ksprintf(funs->Format.eprintf"%s@."s)fmtelsePrintf.ifprintf()fmtletraise_error~locs=Location.raise_errorf~locsletenc_namename=ifname="t"then"enc"elsename^"_enc"letenc_mods="Json_encoding."^sletenc_var~locs=evar~loc(enc_mods)letenc_apply~locsl=eapply~loc(enc_var~locs)lletacc_mapfl=letencs,accs=List.split@@List.mapflinencs,List.flattenaccsletpexp_fun~locpe=pexp_fun~locNolabelNonepeletllid~locs={txt=Longident.parses;loc}letesome~loce=pexp_construct~loc(llid~loc"Some")(Some(e~loc))letpsome~loce=ppat_construct~loc(llid~loc"Some")(Some(e~loc))letenone~loc=pexp_construct~loc(llid~loc"None")Noneletconv1~locconstructdestructenc=enc_apply~loc"conv"[pexp_fun~loc(pvar~loc"x")(construct(evar~loc"x"));pexp_fun~loc(pvar~loc"x")(destruct(evar~loc"x"));enc]letrecadd_params_fun~locexpr=function|[]->expr|{ptyp_desc=Ptyp_varx;_}::t->pexp_fun~loc(ppat_constraint~loc(pvar~loc("_"^enc_namex))(ptyp_constr~loc(llid~loc(enc_mod"encoding"))[ptyp_var~locx]))(add_params_funexpr~loct)|_::t->add_params_fun~locexprtletrecadd_params_fun_sig~loctyp=function|[]->typ|{ptyp_desc=Ptyp_varx;_}::t->ptyp_arrow~locNolabel(ptyp_constr~loc(llid~loc(enc_mod"encoding"))[ptyp_var~locx])(add_params_fun_sigtyp~loct)|_::t->add_params_fun_sig~loctyptletparam_namesparams=List.rev@@List.fold_left(funacc(p,_)->matchp.ptyp_descwith|Ptyp_varx->x::acc|_->acc)[]paramsletget_expr_attr=function|PStr[{pstr_desc=Pstr_eval(e,_);_}]->Somee|_->Noneletget_string_attr=function|PStr[{pstr_desc=Pstr_eval({pexp_desc=Pexp_constant(Pconst_string(s,_,_));_},_);_}]->Somes|_->Nonetypefield_attributes={fa_field:string*bool*expressionoption;fa_key:string;fa_title:expressionoption;fa_description:expressionoption;fa_assoc:booloption;fa_enum:booloption;fa_exclude:expressionoption;fa_obj:booloption;fa_enc:expressionoption;fa_obj1:stringoption;fa_merge:bool;}letfield_attrs~key?(opt=false)?(option="dft")l=letfa_field=matchopt,optionwith|false,_->("req",false,None)|_,"opt"->("opt",true,None)|_,"req"->("req",false,None)|_->("dft",false,Some(enone~loc:!Ast_helper.default_loc))inList.fold_left(funfaa->matcha.attr_name.txtwith|"req"->{fawithfa_field=("req",false,None)}|"opt"->{fawithfa_field=("opt",true,None)}|"dft"->{fawithfa_field=("dft",false,get_expr_attra.attr_payload)}|"key"->beginmatchget_string_attra.attr_payloadwith|None->failwith"key expression must be a string constant"|Somefa_key->{fawithfa_key}end|"title"->{fawithfa_title=get_expr_attra.attr_payload}|"description"->{fawithfa_description=get_expr_attra.attr_payload}|"assoc"->{fawithfa_assoc=Sometrue}|"enum"->{fawithfa_enum=Sometrue}|"exclude"->{fawithfa_exclude=get_expr_attra.attr_payload}|"object"->{fawithfa_obj=Sometrue}|"encoding"->{fawithfa_enc=get_expr_attra.attr_payload}|"obj1"->{fawithfa_obj1=get_string_attra.attr_payload}|"merge"->{fawithfa_merge=true}|_->fa){fa_field;fa_key=key;fa_title=None;fa_description=None;fa_assoc=None;fa_enum=None;fa_exclude=None;fa_obj=None;fa_enc=None;fa_obj1=None;fa_merge=false}ltypecs_attributes={cs_kind:stringoption;cs_assoc:booloption;cs_enum:booloption;cs_key:stringoption;cs_obj:booloption;cs_enc:expressionoption;cs_title:expressionoption;cs_description:expressionoption;cs_ignore:bool;cs_rm_prefix:bool;cs_obj1:stringoption;}letconstructor_attrsl=List.fold_left(funcsa->matcha.attr_name.txtwith|"kind"->{cswithcs_kind=get_string_attra.attr_payload}|"assoc"->{cswithcs_assoc=Sometrue}|"enum"->{cswithcs_enum=Sometrue}|"key"->{cswithcs_key=get_string_attra.attr_payload}|"object"->{cswithcs_obj=Sometrue}|"encoding"->{cswithcs_enc=get_expr_attra.attr_payload}|"title"->{cswithcs_title=get_expr_attra.attr_payload}|"description"->{cswithcs_description=get_expr_attra.attr_payload}|"ignore"->{cswithcs_ignore=true}|"remove_prefix"->{cswithcs_rm_prefix=true}|"obj1"->{cswithcs_obj1=get_string_attra.attr_payload}|_->cs){cs_kind=None;cs_assoc=None;cs_enum=None;cs_key=None;cs_obj=None;cs_enc=None;cs_title=None;cs_description=None;cs_ignore=false;cs_rm_prefix=false;cs_obj1=None}ltypecore_attributes={co_assoc:booloption;co_enum:booloption;co_exclude:expressionoption;co_obj:booloption;co_enc:expressionoption;co_obj1:stringoption;co_merge:bool;}letcore_attrs?assoc?enum?obj?enc?obj1l=List.fold_left(funcoa->matcha.attr_name.txtwith|"assoc"->{cowithco_assoc=Sometrue}|"enum"->{cowithco_enum=Sometrue}|"exclude"->{cowithco_exclude=get_expr_attra.attr_payload}|"object"->{cowithco_obj=Sometrue}|"encoding"->{cowithco_enc=get_expr_attra.attr_payload}|"obj1"->{cowithco_obj1=get_string_attra.attr_payload}|"merge"->{cowithco_merge=true}|_->co){co_assoc=assoc;co_enum=enum;co_exclude=None;co_obj=obj;co_enc=enc;co_obj1=obj1;co_merge=false}lletnew_var=leti=ref(-1)infun()->incri;"v"^string_of_int!iletstr_of_structuree=Pprintast.string_of_structureeletstr_of_signaturee=Pprintast.signatureFormat.str_formattere;Format.flush_str_formatter()letrecencaps_tuple~locvartuple=function|[]->assertfalse|[h]->var~loch|h::t->tuple~loc[var~loch;encaps_tuple~locvartuplet]letrecencaps_merge~loc?(f="merge_objs")=function|[]->assertfalse|[h,merge]->ifmergethenhelseenc_apply~loc"obj1"[h]|[f1,m1;f2,m2]whennotm1&¬m2->enc_apply~loc"obj2"[f1;f2]|(h,merge)::t->enc_apply~locf[ifmergethenhelseenc_apply~loc"obj1"[h];encaps_merge~loc~ft]letobj_expr~loc?(kind="obj")l=letv=List.mapi(funi_->"x"^string_of_inti)linletno_merge=List.for_all(fun(_,b)->notb)linletn=List.lengthlinifn<11&&no_mergetheneapply~loc(evar~loc(enc_mod(kind^string_of_intn)))(List.mapfstl)elseletf="merge_"^kind^"s"inenc_apply~loc"conv"[pexp_fun~loc(ppat_tuple~loc(List.map(pvar~loc)v))(encaps_tuple~locevarpexp_tuplev);pexp_fun~loc(encaps_tuple~locpvarppat_tuplev)(pexp_tuple~loc(List.map(evar~loc)v));encaps_merge~loc~fl]