123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139(*
* Generic Transformers PPX syntax extension.
* Copyright (C) 2016-2021
* Dmitrii Kosarev aka Kakadu
* St.Petersburg State University, JetBrains Research
*
*)openPpxlibopenGTCommonmoduleE=Expander.Make(PpxHelpers)letgt_paramname=letopenDeriving.Argsinargname__;;letr=letopenDeriving.Argsinmap1~f:(List.map(fun({txt},e)->txt,e))@@pexp_record__none;;moduleH=Expander.Make(PpxHelpers)letstr_type_decl:(_,_)Deriving.Generator.t=Deriving.Generator.makeDeriving.Args.(empty+>arg"options"r+>arg"plugins"r)(fun~loc~pathinfooptionsplugins->(* Expander.notify "with annotations %s" (String.concat "," info); *)letcfg=matchoptions,pluginswith|None,None->None|Somep,None|None,Somep->Somep|Some_,Some_->Location.raise_errorf~loc"You can't specify both options and plugins. Use only one of them"inH.str_type_decl_many_plugins~loc[](matchcfgwith|None->[]|Somexs->List.map(function|Lidentname,e->letextra=matche.pexp_descwith|Pexp_record(xs,_)->List.map(fun({txt},b)->txt,b)xs|Pexp_ident{txt=Lidents}whens=name->[]|_->Location.raise_errorf~loc"bad argument of a plugin"inname,Expander.Useextra|_->Location.raise_errorf~loc"only lowercase identifiers are allowed")xs)info);;letsig_type_decl:(_,_)Deriving.Generator.t=Deriving.Generator.makeDeriving.Args.(empty+>arg"options"r+>arg"plugins"r)(fun~loc~pathinfooptionsplugins->letoptions=matchoptions,pluginswith|None,None->None|Somep,None|None,Somep->Somep|Some_,Some_->Location.raise_errorf~loc"You can't specify both options and plugins. Use only one of them"inletgenerator_fsi=H.sig_type_decl_many_plugins~locsi(matchoptionswith|None->[]|Somexs->List.map(function|Lidentname,e->name,Expander.Use[]|_->Location.raise_errorf~loc"only lowercase identifiers are allowed")xs)ingenerator_f[]info);;let()=Expander.set_inline_registration(funname(moduleM:Plugin_intf.MAKE)->letmoduleP=M(PpxHelpers)inletp=letloc=Location.noneinletdummy_decl=match[%stritypenonrect=int]with|{pstr_desc=Pstr_type(_,x)}->x|_->Location.raise_errorf~loc"Should not happen %s %d"__FILE____LINE__inP.create[](false,dummy_decl)inletextension~loc~path:_typ=letnames=HelpersBase.vars_from_core_typetyp|>HelpersBase.SS.elementsinlettdecl=letopenPpxlib.Ast_builder.Defaultintype_declaration~loc~name:(Located.mk~loc"dummy")~params:(List.map(funs->ptyp_var~locs,(Asttypes.NoVariance,Asttypes.NoInjectivity))names)~cstrs:[]~private_:Public~manifest:(Sometyp)~kind:Ptype_abstractinletrhs=p#do_typ_gen~loc:(PpxHelpers.loc_from_camlloc)~mutual_decls:[]~is_self_rec:(fun_->`Nonrecursive)tdecltypinletopenPpxlib.Ast_builder.Defaultinletpats,silence_warns=p#prepare_fa_args~loc(fun~loc~flg~pat~expr->pexp_let~locflg[value_binding~loc~pat~expr])tdeclinList.fold_right(pexp_fun~locNolabelNone)pats(silence_warnsrhs)inDeriving.add~extensionname|>Deriving.ignore);;let()=Deriving.add~str_type_decl~sig_type_decl"gt"|>Deriving.ignore