123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427(********************************************************************************)(* crs - A tool for managing inline review comments embedded in source code *)(* Copyright (C) 2024-2026 Mathieu Barbin <mathieu.barbin@gmail.com> *)(* *)(* This file is part of crs. *)(* *)(* crs is free software; you can redistribute it and/or modify it under the *)(* terms of the GNU Lesser General Public License as published by the Free *)(* Software Foundation either version 3 of the License, or any later version, *)(* with the LGPL-3.0 Linking Exception. *)(* *)(* crs is distributed in the hope that it will be useful, but WITHOUT ANY *)(* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *)(* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License and *)(* the file `NOTICE.md` at the root of this repository for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* and the LGPL-3.0 Linking Exception along with this library. If not, see *)(* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively. *)(********************************************************************************)(* Some functions are copied from [Base] version [v0.17] which is released
under MIT and may be found at [https://github.com/janestreet/base].
See Base's LICENSE below:
----------------------------------------------------------------------------
The MIT License
Copyright (c) 2016--2024 Jane Street Group,LLC <opensource-contacts@janestreet.com>
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.
----------------------------------------------------------------------------
When this is the case, we clearly indicate it next to the copied function. *)moduleCode_error=Code_errormoduleDyn=Dyn0moduleJson=JsonmoduleOrdering=Orderingletprintpp=Format.printf"%a@."Pp.to_fmtppletprint_dyndyn=print(Dyn.ppdyn)letphys_equalab=a==bmoduleChar=structincludeStdlib.Char(* ---------------------------------------------------------------------------- *)(* [is_whitespace] is copied from [Base] (MIT). See notice at the top of the
file and project global notice for licensing information. *)letis_whitespace=function|'\t'|'\n'|'\011'(* vertical tab *)|'\012'(* form feed *)|'\r'|' '->true|_->false;;(* ---------------------------------------------------------------------------- *)endmoduleInt=structincludeStdlib.Intletincr=Stdlib.incrletto_dyn=Dyn.intletto_string_humn=lets=string_of_intninletlen=String.lengthsinletis_negative=n<0inletsign_count=ifis_negativethen1else0inletabsolute_digit_count=ifis_negativethenlen-1elseleninletseparator_count=absolute_digit_count/3inletinitial_skip_count=letdigit_skip=absolute_digit_countmod3insign_count+ifdigit_skip>0thendigit_skipelse3inletbuffer=Buffer.create(len+separator_count)inletrecauxicount=ifi<lenthenifcount=0then(Buffer.add_charbuffer'_';auxi3)else(Buffer.add_charbuffers.[i];aux(i+1)(count-1))inaux0initial_skip_count;Buffer.contentsbuffer;;endmoduleList=structincludeStdlib.ListLabelsletequaleqt1t2=equal~eqt1t2letexistst~f=exists~ftletfiltert~f=filter~ftletfilter_mapt~f=filter_map~ft(* [filter_mapi] is available in stdlib but only from [5.5]. *)let[@tail_mod_cons]recfilter_mapifi=function|[]->[]|x::l->leti'=i+1in(matchfixwith|None->filter_mapifi'l|Somev->v::filter_mapifi'l);;letfilter_mapil~f=filter_mapif0lletfilter_optt=filter_mapt~f:Fun.idletfindt~f=find_opt~ftletfind_exnt~f=Stdlib.List.findftletfor_allt~f=for_all~ft(* ---------------------------------------------------------------------------- *)(* [unzip], [groupi] and [group] are copied from [Base] (MIT). See notice at
the top of the file and project global notice for licensing information. *)letunziplist=letreclooplistl1l2=matchlistwith|[]->l1,l2|(x,y)::tl->looptl(x::l1)(y::l2)inloop(revlist)[][];;letgroupil~break=(* We allocate shared position and list references so we can make the inner loop use
[[@tail_mod_cons]], and still return back information about position and where in the
list we left off. *)letpos=ref0inletl=reflin(* As a result of using local references, our inner loop does not need arguments. *)let[@tail_mod_cons]rectake_group()=match!lwith|[]->assertfalse|[_]asgroup->l:=[];group|x::(y::_astl)->pos:=!pos+1;l:=tl;ifbreak!posxythen[x]else(* Coverage is off in the second part of the expression because the
instrumentation breaks [@tail_mod_cons], triggering warning 71. *)x::(take_group()[@coverageoff])in(* Our outer loop does not need arguments, either. *)let[@tail_mod_cons]recgroups()=ifis_empty!lthen[]else(letgroup=take_group()in(* Coverage is off in the second part of the expression because the
instrumentation breaks [@tail_mod_cons], triggering warning 71. *)group::(groups()[@coverageoff]))in(groups()[@nontail]);;letgroupl~break=groupil~break:(fun_xy->breakxy)[@nontail](* ---------------------------------------------------------------------------- *)lethd=function|[]->None|hd::_->Somehd;;lethd_exn=Stdlib.List.hdletitert~f=iter~ftletmapt~f=map~ftletmemtx~equal=existst~f:(funy->equalxy)letsortt~compare=sortt~cmp:compareletsort_and_groupt~compare=sortt~compare|>group~break:(funt1t2->0<>comparet1t2);;endmoduleOption=structincludeStdlib.Optionletbindt~f=bindtfletitert~f=iterftletmapt~f=mapftendmoduleRef=structletset_temporarilyta~f=letx=!tint:=a;Fun.protect~finally:(fun()->t:=x)f;;endmoduleResult=structincludeStdlib.Resultletbindt~f=bindtfletitert~f=iterftendmoduleString=structincludeStdlib.StringLabelsletchop_prefixt~prefix=ifstarts_witht~prefixthen(letprefix_len=lengthprefixinSome(StringLabels.subt~pos:prefix_len~len:(lengtht-prefix_len)))elseNone;;letconcatt~sep=concat~septletis_emptyt=lengtht=0letlsplit2t~on=matchindex_opttonwith|None->None|Somei->Some(subt~pos:0~len:i,subt~pos:(i+1)~len:(lengtht-i-1));;letsplitt~on=split_on_char~sep:ontletstrip=trimletto_dyn=Dyn.stringletto_stringt=t(* ---------------------------------------------------------------------------- *)(* The following functions are copied from [Base] (MIT). See notice at the top
of the file and project global notice for licensing information. *)letrfindit~f=letrecloopi=ifi<0thenNoneelseiffit.[i]thenSomeielseloop(i-1)inletpos=lengtht-1in(looppos[@nontail]);;letlast_non_drop~dropt=rfindit~f:(fun_c->not(dropc))[@nontail]letrstrip?(drop=Char.is_whitespace)t=matchlast_non_dropt~dropwith|None->""|Somei->ifi=lengtht-1thentelsesubt~pos:0~len:(i+1);;letlfindi?(pos=0)t~f=letn=lengthtinletrecloopi=ifi=nthenNoneelseiffit.[i]thenSomeielseloop(i+1)in(looppos[@nontail]);;letfirst_non_drop~dropt=lfindit~f:(fun_c->not(dropc))[@nontail]letlstrip?(drop=Char.is_whitespace)t=matchfirst_non_dropt~dropwith|None->""|Some0->t|Somen->subt~pos:n~len:(lengtht-n);;(* ---------------------------------------------------------------------------- *)(* The function [split_lines] below was copied from [Base.String0.split_lines]
version [v0.17] which is released under MIT and may be found at
[https://github.com/janestreet/base].
The changes we made were minimal:
- Changed references to [Char0] to [Char].
See Base's LICENSE below:
----------------------------------------------------------------------------
The MIT License
Copyright (c) 2016--2024 Jane Street Group, LLC <opensource-contacts@janestreet.com>
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. *)letsplit_lines=letback_up_at_newline~t~pos~eol=pos:=!pos-if!pos>0&&Char.equalt.[!pos-1]'\r'then2else1;eol:=!pos+1infunt->letn=lengthtinifn=0then[]else((* Invariant: [-1 <= pos < eol]. *)letpos=ref(n-1)inleteol=refninletac=ref[]in(* We treat the end of the string specially, because if the string ends with a
newline, we don't want an extra empty string at the end of the output. *)ifChar.equalt.[!pos]'\n'thenback_up_at_newline~t~pos~eol;while!pos>=0doifnot(Char.equalt.[!pos]'\n')thendecrposelse((* Because [pos < eol], we know that [start <= eol]. *)letstart=!pos+1inac:=subt~pos:start~len:(!eol-start)::!ac;back_up_at_newline~t~pos~eol)done;subt~pos:0~len:!eol::!ac);;(* ---------------------------------------------------------------------------- *)letsubstr_replace_allt~pattern~with_=letpattern_len=lengthpatterninifpattern_len=0thentelse(lett_len=lengthtinletbuf=Buffer.create(t_len+(t_len/2))inletreclooppos=ifpos>t_len-pattern_lenthenBuffer.add_substringbuftpos(t_len-pos)else(letfound=letrecchecki=ifi=pattern_lenthentrueelseifunsafe_gett(pos+i)=unsafe_getpatternithencheck(i+1)elsefalseincheck0iniffoundthen(Buffer.add_stringbufwith_;loop(pos+pattern_len))else(Buffer.add_charbuf(unsafe_gettpos);loop(pos+1)))inloop0;Buffer.contentsbuf);;endletrequirecond=ifnotcondthenfailwith"Required condition does not hold."letrequire_does_raisef=matchf()with|_->Code_error.raise"Did not raise."[]|exceptione->print_endline(Printexc.to_stringe);;moduleWith_equal_and_dyn=structmoduletypeS=sigtypetvalequal:t->t->boolvalto_dyn:t->Dyn.tendendletrequire_equal(typea)(moduleM:With_equal_and_dyn.Swithtypet=a)(v1:a)(v2:a)=ifnot(M.equalv1v2)thenCode_error.raise"Values are not equal."["v1",v1|>M.to_dyn;"v2",v2|>M.to_dyn];;letrequire_not_equal(typea)(moduleM:With_equal_and_dyn.Swithtypet=a)(v1:a)(v2:a)=ifM.equalv1v2thenCode_error.raise"Values are equal."["v1",v1|>M.to_dyn;"v2",v2|>M.to_dyn];;letprint_endline=Stdlib.print_endline