123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475(************************************************************************)(* * The Coq Proof Assistant / The Coq Development Team *)(* v * INRIA, CNRS and contributors - Copyright 1999-2018 *)(* <O___,, * (see CREDITS file for the list of authors) *)(* \VV/ **************************************************************)(* // * This file is distributed under the terms of the *)(* * GNU Lesser General Public License Version 2.1 *)(* * (see LICENSE file for the text of the license) *)(************************************************************************)(************************************************************************)(* Coq Language Server Protocol *)(* Copyright 2019 MINES ParisTech -- Dual License LGPL 2.1 / GPL3+ *)(* Written by: Emilio J. Gallego Arias *)(************************************************************************)(* Status: Experimental *)(************************************************************************)moduleF=FormatmoduleJ=Yojson.Safeletmut=Mutex.create()letdebug_fmt=refF.err_formatterletlog_errorhdrmsg=Mutex.lockmut;F.fprintf!debug_fmt"[%s]: @[%s@]@\n%!"hdrmsg;Mutex.unlockmutletlog_objecthdrobj=F.fprintf!debug_fmt"[%s]: @[%a@]@\n%!"hdrJ.(pretty_print~std:false)objletflush_log()=F.pp_print_flush!debug_fmt()exceptionReadErrorofstringletread_request_rawic=letcl=input_lineicinletsin=Scanf.Scanning.from_stringclinletraw_obj=Scanf.bscanfsin"Content-Length: %d\r"(funsize->letbuf=Bytes.createsizein(* Consume the second \r\n or header *)letohdr=input_lineicin(* If the second line is a return, then no more headers *)let()=ifohdr.[0]='\r'then()else(* Fixme (or use ocaml-lsp) Skip the Content-type header *)ignore(input_lineic)inreally_inputicbuf0size;Bytes.to_stringbuf)inJ.from_stringraw_objletread_requestic=tryread_request_rawicwith(* if the end of input is encountered while some more characters are needed to
read the current conversion specification, or the lsp server closes *)|End_of_file->raise(ReadError"EOF")(* if the input does not match the format. *)|Scanf.Scan_failuremsg(* if a conversion to a number is not possible. *)|Failuremsg(* if the format string is invalid. *)|Invalid_argumentmsg->raise(ReadErrormsg)letsend_jsonfmtobj=Mutex.lockmut;ifFleche.Debug.sendthenlog_object"send"obj;letmsg=F.asprintf"%a"J.(pretty_print~std:true)objinletsize=String.lengthmsginF.fprintffmt"Content-Length: %d\r\n\r\n%s%!"sizemsg;Mutex.unlockmut