123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339open!BaseopenPpxlibmodulePer_field=structtypet=|Getters|Setters|Names|Fieldsletall=[Getters;Setters;Names;Fields]letto_flag_name=function|Getters->"getters"|Setters->"setters"|Names->"names"|Fields->"fields";;letto_expressiont~loc=matchtwith|Getters->[%exprGetters]|Setters->[%exprSetters]|Names->[%exprNames]|Fields->[%exprFields];;endmoduleIterator=structtypet=|Create|Make_creator|Exists|Fold|Fold_right|For_all|Iter|Map|To_list|Map_polyletall=[Create;Make_creator;Exists;Fold;Fold_right;For_all;Iter;Map;To_list;Map_poly];;letto_variable_name=function|Create->"create"|Make_creator->"make_creator"|Exists->"exists"|Fold->"fold"|Fold_right->"fold_right"|For_all->"for_all"|Iter->"iter"|Map->"map"|To_list->"to_list"|Map_poly->"map_poly";;letto_expressiont~loc=matchtwith|Create->[%exprCreate]|Make_creator->[%exprMake_creator]|Exists->[%exprExists]|Fold->[%exprFold]|Fold_right->[%exprFold_right]|For_all->[%exprFor_all]|Iter->[%exprIter]|Map->[%exprMap]|To_list->[%exprTo_list]|Map_poly->[%exprMap_poly];;endmoduleDirect_iterator=structtypet=|Exists|Fold|Fold_right|For_all|Iter|Map|To_list|Set_all_mutable_fieldsletall=[Exists;Fold;Fold_right;For_all;Iter;Map;To_list;Set_all_mutable_fields];;letto_variable_name=function|Exists->"exists"|Fold->"fold"|Fold_right->"fold_right"|For_all->"for_all"|Iter->"iter"|Map->"map"|To_list->"to_list"|Set_all_mutable_fields->"set_all_mutable_fields";;letto_expressiont~loc=matchtwith|Exists->[%exprExists]|Fold->[%exprFold]|Fold_right->[%exprFold_right]|For_all->[%exprFor_all]|Iter->[%exprIter]|Map->[%exprMap]|To_list->[%exprTo_list]|Set_all_mutable_fields->[%exprSet_all_mutable_fields];;endtypet=|Per_fieldofPer_field.t|IteratorofIterator.t|Direct_iteratorofDirect_iterator.tletall=List.concat[List.mapPer_field.all~f:(funx->Per_fieldx);List.mapIterator.all~f:(funx->Iteratorx);List.mapDirect_iterator.all~f:(funx->Direct_iteratorx)];;letto_string=function|Per_fieldx->"~"^Per_field.to_flag_namex|Iteratorx->"~iterators:"^Iterator.to_variable_namex|Direct_iteratorx->"~direct_iterators:"^Direct_iterator.to_variable_namex;;letsexp_of_tt=Sexp.Atom(to_stringt)letcompare=(Poly.compare:t->t->int)include(valComparator.make~compare~sexp_of_t)letto_expressiont~loc=matchtwith|Per_fieldx->[%exprPpx_fields_conv.Selector.Per_field[%ePer_field.to_expressionx~loc]]|Iteratorx->[%exprPpx_fields_conv.Selector.Iterator[%eIterator.to_expressionx~loc]]|Direct_iteratorx->[%exprPpx_fields_conv.Selector.Direct_iterator[%eDirect_iterator.to_expressionx~loc]];;letdirect_dependencies=function|Per_field(Getters|Setters|Names)->[]|Per_fieldFields->[Per_fieldGetters;Per_fieldSetters]|Iterator_|Direct_iterator_->[Per_fieldFields];;letrecwith_dependenciesselector=selector::List.concat_map~f:with_dependencies(direct_dependenciesselector);;moduletypeS=sigtypetvalall:tlistvalto_variable_name:t->stringendletselect_id(typea)(moduleM:Swithtypet=a)~arg_name~fexpr=matchexpr.pexp_descwith|Pexp_ident{loc;txt=Lidenttxt}->(matchList.findM.all~f:(funx->String.equaltxt(M.to_variable_namex))with|Somex->Ok(fx)|None->Error(loc,Printf.sprintf"[~%s] %s"arg_name(ifString.equaltxtarg_namethenPrintf.sprintf"requires an argument"elsePrintf.sprintf"does not accept [%s] as an argument, valid arguments are: %s"(Longident.name(Lidenttxt))(String.concat~sep:", "(List.mapM.all~f:(funx->Printf.sprintf"[%s]"(M.to_variable_namex)))))))|_->Error(expr.pexp_loc,"expected a variable name");;letselect_id_tuplem~arg_name~fexpr=Result.bind(matchexpr.pexp_descwith|Pexp_tupletuple->Oktuple|Pexp_ident_->Ok[expr]|_->Error[expr.pexp_loc,"expected a variable name or a tuple of variable names"])~f:(funexprs->List.mapexprs~f:(select_idm~arg_name~f)|>Result.combine_errors);;letselect_iterators=select_id_tuple~arg_name:"iterators"~f:(funx->Iteratorx)(moduleIterator);;letselect_direct_iterators=select_id_tuple~arg_name:"direct_iterators"~f:(funx->Direct_iteratorx)(moduleDirect_iterator);;letselect_fold_rightexpr=Error[(expr.pexp_loc,"[~fold_right] is no longer supported; use [~iterators:fold_right] and/or \
[~direct_iterators:fold_right]")];;letselect_onexexpr=matchexpr.pexp_descwith|Pexp_ident{txt=Lidenttxt;_}whenString.equaltxt(Per_field.to_flag_namex)->Ok[Per_fieldx]|_->Error[(expr.pexp_loc,Printf.sprintf"expected no explicit argument to [~%s]"(Per_field.to_flag_namex))];;letselect_getters=select_oneGettersletselect_setters=select_oneSettersletselect_names=select_oneNamesletselect_fields=select_oneFieldsletdefault_selectors=List.filterall~f:(function|IteratorFold_right|Direct_iteratorFold_right->false|_->true);;letselectionlist~add_dependencies=letlist=ifadd_dependenciesthenList.concat_maplist~f:with_dependencieselselistinSet.Using_comparator.of_list~comparatorlist;;leterror_of_alists~localists=matchList.map(List.concatalists)~f:(fun(loc,message)->loc,"deriving fields: "^message)with|[(loc,message)]->Location.Error.make~locmessage~sub:[]|sub->Location.Error.make~loc"deriving fields: multiple syntax errors"~sub;;letgenerator~add_dependenciesf=Deriving.Generator.V2.make(letopenDeriving.Argsinempty+>arg"fold_right"(map1__~f:select_fold_right)+>arg"getters"(map1__~f:select_getters)+>arg"setters"(map1__~f:select_setters)+>arg"names"(map1__~f:select_names)+>arg"fields"(map1__~f:select_fields)+>arg"iterators"(map1__~f:select_iterators)+>arg"direct_iterators"(map1__~f:select_direct_iterators))(fun~ctxtastarg1arg2arg3arg4arg5arg6arg7->letloc=Expansion_context.Deriver.derived_item_locctxtinletresults=matchList.filter_opt[arg1;arg2;arg3;arg4;arg5;arg6;arg7]with|[]->[Okdefault_selectors]|_::_asnon_empty->non_emptyinletselection=Result.combine_errorsresults|>Result.map~f:List.concat|>Result.map~f:(selection~add_dependencies)|>Result.map_error~f:(error_of_alists~loc)inf~ctxtastselection);;letderiving_clause~loclist=letopenAst_builder.DefaultinifList.is_emptylistthenNoneelse(letper_field,iterators,direct_iterators=List.dedup_and_sortlist~compare|>List.partition3_map~f:(function|Per_fieldx->`Fstx|Iteratorx->`Sndx|Direct_iteratorx->`Trdx)inletper_field=List.mapper_field~f:(funx->lets=Per_field.to_flag_namexinLabelleds,evar~locs)inletiterators=ifList.is_emptyiteratorsthen[]else[(Labelled"iterators",pexp_tuple~loc(List.mapiterators~f:(funf->evar~loc(Iterator.to_variable_namef))))]inletdirect_iterators=ifList.is_emptydirect_iteratorsthen[]else[(Labelled"direct_iterators",pexp_tuple~loc(List.mapdirect_iterators~f:(funf->evar~loc(Direct_iterator.to_variable_namef))))]inSome(pexp_apply~loc[%exprfields](List.concat[per_field;iterators;direct_iterators])));;