123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298letlookup_typetypenv=#ifOCAML_VERSION>=(4,10,0)Env.find_type_by_nametypenv|>fst#elseEnv.lookup_typetyp env#endifletlookup_valuevenv=#ifOCAML_VERSION>=(4,10,0)Env.find_value_by_namevenv#elseEnv.lookup_valuevenv#endifletfind_valueenvlocid=#ifOCAML_VERSION>=(4,10,0)Env.lookup_value~locidenv#elseTypetexp.find_valueenvlocid#endifletfind_typeenvlocid=#ifOCAML_VERSION>=(4,10,0)Env.lookup_type ~locidenv#elseTypetexp.find_typeenvlocid#endifletfind_constructor envlocid=#ifOCAML_VERSION>=(4,10,0)Env.lookup_constructor~locEnv.Positiveidenv#elseTypetexp.find_constructor envlocid#endifletfind_moduleenvlocid=#ifOCAML_VERSION>=(4,10,0)Env.lookup_module~locidenv#elseTypetexp.find_moduleenvlocid#endifletfind_modtypeenvlocid=#ifOCAML_VERSION>=(4,10,0)Env.lookup_modtype ~locidenv#elseTypetexp.find_modtypeenvlocid#endifletfind_classenvlocid=#ifOCAML_VERSION>=(4,10,0)Env.lookup_class~locidenv#elseTypetexp.find_classenvlocid#endifletfind_class_typeenvlocid=#ifOCAML_VERSION>=(4,10,0)Env.lookup_cltype~locidenv#elseTypetexp.find_class_typeenvlocid#endiflettype_structureenvstrloc=#ifOCAML_VERSION>=(4,14,0)lettstr,_,_,_,env=#elselettstr,_,_,env=#endif#ifOCAML_VERSION>=(4,12,0)let_=locinTypemod.type_structureenvstr#elseTypemod.type_structureenvstrloc#endifintstr,envletextension_constructor~ext_type_path~ext_type_params~ext_args~ext_ret_type~ext_private~ext_loc~ext_attributes=letopenTypesinletext_args=Cstr_tupleext_argsin{ext_type_path;ext_type_params;ext_args;ext_ret_type;ext_private;ext_loc;ext_attributes#ifOCAML_VERSION >=(4,11,0);ext_uid =Uid.mk~current_unit:"mdx"#endif}letmatch_env~value~empty~open_~functor_arg~constraints~copy_types~module_~persistent~type_~modtype~cltype~class_~extension~value_unbound~module_unboundenv=ignore(constraints,persistent,copy_types,value_unbound,module_unbound);matchenvwith|Env.Env_value(summary,id,_)->valuesummaryid|Env_empty->empty()|Env_open(summary,pid)->open_summarypid|Env_functor_arg(summary,id)->functor_argsummaryid|Env_module (summary,id,presence,_)->letpresent=matchpresencewith|Mp_present ->true|Mp_absent ->falseinmodule_summaryid~present|Env_type(summary,_,_)->type_ summary|Env_modtype(summary,_,_)->modtypesummary|Env_cltype(summary,_,_)->cltype summary|Env_class(summary,id,_)->class_summaryid|Env_extension(summary,id,_)->extensionsummaryid|Env_constraints(summary,_)->constraintssummary#ifOCAML_VERSION>=(4,10,0)|Env_copy_types summary ->copy_typessummary|Env_value_unbound(summary,_,_)->value_unboundsummary|Env_module_unbound(summary,_,_)->module_unboundsummary#else|Env_copy_types(summary,_)->copy_typessummary#endif|Env_persistent(summary,_)->persistentsummaryletctype_is_equal=#ifOCAML_VERSION>=(4,13,0)Ctype.is_equal#elseCtype.equal#endifletctype_expand_head_and_get_descenvty=#if OCAML_VERSION>=(4,14,0)Types.get_desc(Ctype.expand_headenvty)#else(Ctype.expand_headenvty).Types.desc#endifletctype_get_descty=#ifOCAML_VERSION>=(4,14,0)Types.get_descty#else(Ctype.reprty).Types.desc#endifexceptionExit_with_statusofintletexecute_phraseprint_outcomeppfphr=#ifOCAML_VERSION>=(4,12,0)matchToploop.execute_phraseprint_outcomeppfphrwith|v->v|exceptionCompenv.Exit_with_statusstatus->raise(Exit_with_status status)#elseToploop.execute_phraseprint_outcomeppfphr#endif#ifOCAML_VERSION<(4,14,0)letstd_err=Format.err_formatterletpatch_directivenamedirective=letpatched_name=Format.asprintf"mdx_%s"nameinletdirective_info=Toploop.{section="MDX PATCHED";doc="Patched by MDX"}inToploop.add_directivepatched_namedirectivedirective_info;patched_name(* port of Topdirs.action_on_suberror *)letaction_on_suberrorb=ifnotb&¬!Sys.interactivethenraise(Exit_with_status 125)letdir_useppfname=action_on_suberror(Toploop.use_fileppfname)letmdx_use=patch_directive"use" (Directive_string(dir_usestd_err))letmdx_install_printer=patch_directive"install_printer"(Directive_ident(Topdirs.dir_install_printerstd_err))letmdx_remove_printer=patch_directive"remove_printer"(Directive_ident(Topdirs.dir_remove_printerstd_err))#endif#ifOCAML_VERSION>=(4,11,0)&&OCAML_VERSION<(4,14,0)letdir_use_outputppfname=action_on_suberror(Toploop.use_outputppfname)letmdx_use_output=patch_directive"use_output"(Directive_string(dir_use_outputstd_err))#endif#ifOCAML_VERSION<(4,13,0)letmdx_trace=patch_directive"trace"(Directive_ident(Topdirs.dir_tracestd_err))letmdx_untrace=patch_directive"untrace"(Directive_ident(Topdirs.dir_untracestd_err))letmdx_untrace_all=patch_directive"untrace_all"(Directive_none(Topdirs.dir_untrace_allstd_err))#endif#ifOCAML_VERSION<(4,13,0)(* [load] cannot be patched to return errors because the underlying code is not exposed:
It would require [Topdirs.load_file] with the first argument to be [false] but the exposed
version hardcodes it to [true].
*)letmdx_load=patch_directive"load"(Directive_string(Topdirs.dir_loadstd_err))(* On the other hand, [load_rec] can be patched because the curried [true] is the only
difference between these directives *)letdir_load_recppfname=action_on_suberror(Topdirs.load_fileppfname)letmdx_load_rec=patch_directive"load_rec"(Directive_string(dir_load_recstd_err))#elifOCAML_VERSION>=(4,13,0)&&OCAML_VERSION<(4,14,0)(* OCaml 4.13 exposes [Topeval.load_file] which allows us to patch [#load] too *)letdir_loadppfname=action_on_suberror(Topeval.load_filefalseppfname)letmdx_load=patch_directive"load"(Directive_string(dir_loadstd_err))(* This uses [Topeval.load_file] because [Topdirs.load_file] is deprecated on 4.13 *)letdir_load_recppfname=action_on_suberror(Topeval.load_filetrueppfname)letmdx_load_rec=patch_directive"load_rec"(Directive_string(dir_load_recstd_err))#endifletredirect_directivedirective=matchdirectivewith#ifOCAML_VERSION<(4,14,0)|"load"->mdx_load|"load_rec"->mdx_load_rec|"use"->mdx_use|"install_printer"->mdx_install_printer|"remove_printer"->mdx_remove_printer#endif#ifOCAML_VERSION>=(4,11,0)&&OCAML_VERSION<(4,14,0)|"use_output"->mdx_use_output#endif#ifOCAML_VERSION<(4,13,0)|"trace"->mdx_trace|"untrace"->mdx_untrace|"untrace_all"->mdx_untrace_all#endif|v->vletrecget_id_in_path=function|Path.Pidentid->id|Path.Pdot(p,_)->get_id_in_pathp|Path.Papply(_,p)->get_id_in_pathp#ifOCAML_VERSION>=(5,1,0)|Path.Pextra_ty(p,_)->get_id_in_pathp#endifletget_id_opt=function|Path.Pidentid->Someid|Path.Pdot_->None|Path.Papply_->None#ifOCAML_VERSION>=(5,1,0)|Path.Pextra_ty_->None#endifletmk_funlocexp=letpunit=Ast_helper.Pat.construct(Location.mkloc(Longident.Lident"()")loc)Noneinletlabel=Asttypes.Nolabelinletdefault=Nonein#ifOCAML_VERSION>=(5,2,0)letparam={Parsetree.pparam_loc=loc;pparam_desc=Pparam_val(label,default,punit)}inAst_helper.Exp.function_[param]None(Pfunction_bodyexp)#elseAst_helper.Exp.fun_labeldefaultpunitexp#endif