123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193(* {{{ COPYING *(
This file is part of Merlin, an helper for ocaml editors
Copyright (C) 2013 - 2015 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.
)* }}} *)openStdopenParser_rawexceptionUnknowntypet={name:string;private_def:stringlist;public_def:stringlist;packages:stringlist;keywords:(string*Parser_raw.token)list}typeset=stringlist(* Private definitions are put in a fake module named "_" with the following
* ident. Use it to test or find private definitions. *)letident=Ident.create_persistent"_"(** Definition of each extension *)letext_lwt={name="lwt";private_def=["module Lwt : sig\n\
\ val un_lwt : 'a Lwt.t -> 'a\n\
\ val in_lwt : 'a Lwt.t -> 'a Lwt.t\n\
\ val to_lwt : 'a -> 'a Lwt.t\n\
\ val finally' : 'a Lwt.t -> unit Lwt.t -> 'a Lwt.t\n\
\ val un_stream : 'a Lwt_stream.t -> 'a\n\
\ val unit_lwt : unit Lwt.t -> unit Lwt.t\n\
\ end"];public_def=["val (>>) : unit Lwt.t -> 'a Lwt.t -> 'a Lwt.t\n\
\ val raise_lwt : exn -> 'a Lwt.t\n\
\ val assert_lwt : bool -> unit Lwt.t"];keywords=[("lwt",LET_LWT);("try_lwt",TRY_LWT);("match_lwt",MATCH_LWT);("finally",FINALLY_LWT);("for_lwt",FOR_LWT);("while_lwt",WHILE_LWT)];packages=["lwt.syntax"]}letext_nonrec={name="nonrec";private_def=[];public_def=[];keywords=[("nonrec",NONREC)];packages=[]}letext_meta={name="meta";private_def=["module Meta : sig\n\
\ val code : 'a -> 'a code\n\
\ val uncode : 'a code -> 'a\n\
\ end"];public_def=[];keywords=[(">.",GREATERDOT)];packages=[]}(* Known extensions *)letregistry=[ext_lwt;ext_meta]letregistry=List.fold_leftregistry~init:String.Map.empty~f:(funmapext->String.Map.addmap~key:ext.name~data:ext)letall=String.Map.keysregistryletlookups=trySome(String.Map.findsregistry)withNot_found->Noneletempty=[](* Compute set of extensions from package names (used to enable support for
"lwt" if "lwt.syntax" is loaded by user. *)letfrom~extensions~packages=String.Map.foldregistry~init:[]~f:(fun~key:name~data:extset->ifList.memname~set:extensions||List.exists~f:(List.mem~set:ext.packages)packagesthenname::setelseset)(* Merlin expects a few extensions to be always enabled, otherwise error
recovery may fail arbitrarily *)letdefault=matchMerlin_config.ocamlversionwith|`OCaml_4_02_2|`OCaml_4_03_0->[ext_nonrec]|_->[]letdefault_kw=List.concat_map~f:(funext->ext.keywords)default(* Lexer keywords needed by extensions *)letkeywordsset=letadd_kwkwsext=matchlookupextwith|None->kws|Somedef->def.keywords@kwsinletall=List.fold_leftset~init:default_kw~f:add_kwinLexer_raw.keywordsall(* Register extensions in typing environment *)letparse_sig=letkeywords=Lexer_raw.keywords[]infunstr->letlexbuf=Lexing.from_stringstrinletstate=Lexer_raw.makekeywordsinletreclexer=function|Lexer_raw.Fail_->assertfalse|Lexer_raw.Returnx->x|Lexer_raw.Refillk->lexer(k())inletlexerlexbuf=lexer(Lexer_raw.token_without_commentsstatelexbuf)in(Parser_raw.interfacelexerlexbuf:Parsetree.signature)lettype_sigenvsg=letsg=Typemod.transl_signatureenvsginsg.Typedtree.sig_type(*
let add_hidden_signature env sign =
let add_item env comp =
match comp with
| Types.Sig_value(id, decl) -> Env.add_value (Ident.hide id) decl env
| Types.Sig_type(id, decl, _) -> Env.add_type ~check:false (Ident.hide id) decl env
| Types.Sig_typext(id, decl, _) -> Env.add_extension ~check:false (Ident.hide id) decl env
| Types.Sig_module(id, mty, _) -> Env.add_module (Ident.hide id) mty.Types.md_type env
| Types.Sig_modtype(id, decl) -> Env.add_modtype (Ident.hide id) decl env
| Types.Sig_class(id, decl, _) -> Env.add_class (Ident.hide id) decl env
| Types.Sig_class_type(id, decl, _) -> Env.add_cltype (Ident.hide id) decl env
in
List.fold_left ~f:add_item ~init:env sign
*)letregisterextsenv=(* Log errors ? *)lettry_typesg'=trytype_sigenvsg'with_exn->[]inletexts=List.filter_dupextsinletexts=List.filter_map~f:(funext->matchString.Map.findextregistrywith|ext->Someext|exceptionNot_found->None)extsinletprocess_exte=letprv=List.concat_map~f:parse_sige.private_definletpub=List.concat_map~f:parse_sige.public_defin(try_typeprv,try_typepub)inletfakes,tops=List.split(List.map~f:process_extexts)inletenv=Env.add_signature(List.concattops)envinEnv.add_merlin_extension_moduleident(Types.Mty_signature(List.concatfakes))env