123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236(* {{{ COPYING *(
This file is part of Merlin, an helper for ocaml editors
Copyright (C) 2019 Frédéric Bour <frederic.bour(_)lakaban.net>
Thomas Refis <refis.thomas(_)gmail.com>
Simon Castellan <simon.castellan(_)iuwt.fr>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The Software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall
the authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising
from, out of or in connection with the software or the use or other dealings
in the Software.
)* }}} *)openMerlin_utils.StdopenMerlin_utils.Std.ResultmoduleDirective=structtypeinclude_path=[`Bofstring|`Sofstring|`CMIofstring|`CMTofstring]typeno_processing_required=[`EXTofstringlist|`FLGofstringlist|`STDLIBofstring|`SOURCE_ROOTofstring|`UNIT_NAMEofstring|`WRAPPING_PREFIXofstring|`SUFFIXofstring|`READERofstringlist|`EXCLUDE_QUERY_DIR|`USE_PPX_CACHE|`UNKNOWN_TAGofstring]moduleProcessed=structtypeacceptable_in_input=[include_path|no_processing_required]typet=[acceptable_in_input|`ERROR_MSGofstring]endmoduleRaw=structtypet=[Processed.acceptable_in_input|`PKGofstringlist|`FINDLIBofstring|`FINDLIB_PATHofstring|`FINDLIB_TOOLCHAINofstring]endendtypedirective=Directive.Processed.tmoduleSexp=structtypet=Csexp.t=Atomofstring|Listoftlistletatoms_of_strings=List.map~f:(funs->Atoms)letstrings_of_atoms=List.filter_map~f:(function|Atoms->Somes|_->None)letrecto_string=function|Atoms->s|Listl->String.concat~sep:" "(List.concat[["("];List.map~f:to_stringl;[")"]])letto_directivesexp=matchsexpwith|List[Atomtag;Atomvalue]->beginmatchtagwith|"S"->`Svalue|"B"->`Bvalue|"CMI"->`CMIvalue|"CMT"->`CMTvalue|"STDLIB"->`STDLIBvalue|"SOURCE_ROOT"->`SOURCE_ROOTvalue|"UNIT_NAME"->`UNIT_NAMEvalue|"WRAPPING_PREFIX"->`WRAPPING_PREFIXvalue|"SUFFIX"->`SUFFIXvalue|"ERROR"->`ERROR_MSGvalue|"FLG"->(* This means merlin asked dune 2.6 for configuration.
But the protocole evolved, only dune 2.8 should be used *)`ERROR_MSG"No .merlin file found. Try building the project."|tag->`UNKNOWN_TAGtagend|List[Atomtag;Listl]->letvalue=strings_of_atomslinbeginmatchtagwith|"EXT"->`EXTvalue|"FLG"->`FLGvalue|"READER"->`READERvalue|tag->`UNKNOWN_TAGtagend|List[Atom"EXCLUDE_QUERY_DIR"]->`EXCLUDE_QUERY_DIR|List[Atom"USE_PPX_CACHE"]->`USE_PPX_CACHE|_->`ERROR_MSG"Unexpected output from external config reader"letfrom_directives(directives:Directive.Processed.tlist)=letft=lettag,body=letsingles=[Atoms]inmatchtwith|`Bs->("B",singles)|`Ss->("S",singles)|`CMIs->("CMI",singles)|`CMTs->("CMT",singles)|`INDEXs->("INDEX",singles)|`SOURCE_ROOTs->("SOURCE_ROOT",singles)|`UNIT_NAMEs->("UNIT_NAME",singles)|`WRAPPING_PREFIXs->("WRAPPING_PREFIX",singles)|`EXTss->("EXT",[List(atoms_of_stringsss)])|`FLGss->("FLG",[List(atoms_of_stringsss)])|`STDLIBs->("STDLIB",singles)|`SUFFIXs->("SUFFIX",singles)|`READERss->("READER",[List(atoms_of_stringsss)])|`EXCLUDE_QUERY_DIR->("EXCLUDE_QUERY_DIR",[])|`USE_PPX_CACHE->("USE_PPX_CACHE",[])|`UNKNOWN_TAGtag->("ERROR",single@@Printf.sprintf"Unknown tag in .merlin: %s"tag)|`ERROR_MSGs->("ERROR",singles)inList(Atomtag::body)inList(List.map~fdirectives)endtyperead_error=Unexpected_outputofstring|Csexp_parse_errorofstringtypecommand=Fileofstring|Halt|UnknownmoduletypeS=sigtype'aiotypein_chantypeout_chan(** [read] reads one csexp from the channel and returns the list of
directives it represents *)valread:in_chan->(directivelist,read_error)Merlin_utils.Std.Result.tiovalwrite:out_chan->directivelist->unitiomoduleCommands:sigvalread_input:in_chan->commandiovalsend_file:out_chan->string->unitiovalhalt:out_chan->unitioendendmoduleMake(IO:sigtype'atmoduleO:sigval(let+):'at->('a->'b)->'btendend)(Chan:sigtypein_chantypeout_chanvalread:in_chan->(Csexp.t,string)resultIO.tvalwrite:out_chan->Csexp.t->unitIO.tend)=structtype'aio='aIO.ttypein_chan=Chan.in_chantypeout_chan=Chan.out_chanmoduleCommands=structletread_inputchan=letopenSexpinletopenIO.Oinlet+input=Chan.readchaninmatchinputwith|Ok(List[Atom"File";Atompath])->Filepath|Ok(Atom"Halt")->Halt|Ok_->Unknown|Error_->Haltletsend_filechanpath=Chan.writechanSexp.(List[Atom"File";Atompath])lethaltchan=Chan.writechan(Sexp.Atom"Halt")endletreadchan=letopenIO.Oinlet+res=Chan.readchaninmatchreswith|Ok(Sexp.Listdirectives)->Ok(List.mapdirectives~f:Sexp.to_directive)|Oksexp->letmsg=Printf.sprintf"A list of directives was expected, instead got: \"%s\""(Sexp.to_stringsexp)inError(Unexpected_outputmsg)|Errormsg->Error(Csexp_parse_errormsg)letwriteout_chan(directives:directivelist)=directives|>Sexp.from_directives|>Chan.writeout_chanendmoduleBlocking=Make(structtype'at='amoduleO=structlet(let+)xf=fxendend)(structtypein_chan=in_channeltypeout_chan=out_channelletread=Csexp.inputletwrite=Csexp.to_channelend)