Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ppx_make_utils.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106moduleP=PpxlibmoduleAst_helper=Ppxlib.Ast_helper(* Core Type Utils *)letunit_core_type~loc=Ast_helper.Typ.constr~locP.{txt=P.Lident"unit";loc}[]letcore_type_of_name?(params=[])P.{txt=name;loc}=Ast_helper.Typ.constr~locP.{txt=P.Lidentname;loc}paramsletis_core_type_option(ct:P.core_type)=matchct.ptyp_descwith|Ptyp_constr({txt=Lident"option";_},_)->true|_->falseletis_core_type_list(ct:P.core_type)=matchct.ptyp_descwith|Ptyp_constr({txt=Lident"list";_},_)->true|_->falseletis_core_type_string(ct:P.core_type)=matchct.ptyp_descwith|Ptyp_constr({txt=Lident"string";_},[])->true|_->falseletis_core_type_optional(ct:P.core_type)=matchct.ptyp_descwith|Ptyp_constr({txt=Lident"option";_},_)|Ptyp_constr({txt=Lident"list";_},_)|Ptyp_constr({txt=Lident"string";_},[])->true|_->falseletstrip_option(ct:P.core_type)=matchct.ptyp_descwith|Ptyp_constr({txt=Lident"option";_},[in_ct])->in_ct|_->ctletdefault_expression_of_core_type~loc(ct:P.core_type)=letopenPinifis_core_type_listctthenSome[%expr[]]elseifis_core_type_stringctthenSome[%expr""]elseNone(* Attributes Utils *)typeattr_type=No_attr|Main|Required|DefaultofP.expressionletget_attributes(attrs:P.attributelist)=letcheck_res~locacccur=match(acc,cur)with|No_attr,_->cur|_,No_attr->acc|_,_->P.Location.raise_errorf~loc"single field cannot have more than one attributes"inList.fold_left(funacc(attr:P.attribute)->(matchattr.attr_name.txtwith|"main"->Main|"required"->Required|"default"->(matchattr.attr_payloadwith|PStr[{pstr_desc=Pstr_eval(expr,_);_}]->Defaultexpr|_->P.Location.raise_errorf~loc:attr.attr_loc"value in default attribute is not supported")(* ignore unknown attrs *)|_->No_attr)|>check_res~loc:attr.attr_locacc)No_attrattrs(* Misc. Utils *)letunsupported_errorP.{txt;loc}=P.Location.raise_errorf~loc"type %s cannot be derived"txtletmake_type_decl_generatorf=P.Deriving.Generator.V2.make_noarg(fun~ctxt(rec_flag,tds)->letloc=P.Expansion_context.Deriver.derived_item_locctxtintds|>List.map(f~locrec_flag)|>List.concat)letgen_make_nameP.{txt=name;loc}=P.{txt="make_"^name;loc}letgen_make_choice_nameP.{txt=name;_}P.{txt=choice_name;loc}=lettxt=String.lowercase_ascii("make_"^choice_name^"_of_"^name)inP.{txt;loc}letgen_tuple_label_stringindex="v"^string_of_intindexletlongident_loc_of_nameP.{txt;loc}=P.{txt=P.Lidenttxt;loc}letadd_choice_to_exprchoiceexpr=matchchoicewith|Somechoice_name->letlid=longident_loc_of_namechoice_nameinAst_helper.Exp.constructlid(Someexpr)|None->exprletparams_core_type_of_type_decl~loc:_(td:P.type_declaration)=List.map(fun(ct,_)->ct)td.ptype_params