123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127(**************************************************************************)(* *)(* OCamlFormat *)(* *)(* Copyright (c) Facebook, Inc. and its affiliates. *)(* *)(* This source code is licensed under the MIT license found in *)(* the LICENSE file in the root directory of this source tree. *)(* *)(**************************************************************************)moduleLexing=structincludeLexingletset_positionlexbufposition=lexbuf.lex_curr_p<-{positionwithpos_fname=lexbuf.lex_curr_p.pos_fname};lexbuf.lex_abs_pos<-position.pos_cnumletset_filenamelexbuffname=lexbuf.lex_curr_p<-{lexbuf.lex_curr_pwithpos_fname=fname}endmodulePosition=structopenLexingtypet=positionletcolumn{pos_bol;pos_cnum;_}=pos_cnum-pos_bolletfmtfs{pos_lnum;pos_bol;pos_cnum;pos_fname=_}=ifpos_lnum=-1thenFormat.fprintffs"[%d]"pos_cnumelseFormat.fprintffs"[%d,%d+%d]"pos_lnum pos_bol(pos_cnum -pos_bol)letto_stringx=Format.asprintf"%a"fmtxletsexp_of_tx=Sexp.Atom(to_stringx)letcompare_colp1p2=Int.compare(columnp1)(columnp2)letcomparep1p2=ifphys_equalp1p2then0elseInt.comparep1.pos_cnump2.pos_cnuminclude(valComparator.make~compare~sexp_of_t)letdistancep1p2=p2.pos_cnum-p1.pos_cnumendmoduleLocation=structincludeLocationletfmtfs{loc_start;loc_end;loc_ghost}=Format.fprintffs"(%a..%a)%s"Position.fmtloc_startPosition.fmtloc_end(ifloc_ghostthen" ghost"else"")letto_stringx=Format.asprintf"%a"fmtxletsexp_of_tx=Sexp.Atom(to_stringx)letcompare{loc_start;loc_end;loc_ghost}b=matchPosition.compareloc_startb.loc_startwith|0->(matchPosition.compareloc_endb.loc_endwith|0->Bool.compareloc_ghostb.loc_ghost|c->c)|c->ctypelocation=tmoduleLocation_comparator=Comparator.Make(structtypet=locationletsexp_of_t=sexp_of_tletcompare=compareend)includeLocation_comparatorletcompare_startxy=Position.comparex.loc_starty.loc_startletcompare_start_colxy=Position.compare_colx.loc_starty.loc_startletcompare_endxy=Position.comparex.loc_endy.loc_endletcompare_end_colxy=Position.compare_colx.loc_endy.loc_endletline_differencefstsnd=snd.loc_start.pos_lnum-fst.loc_end.pos_lnumletcontainsl1l2=compare_startl1l2<=0&&compare_endl1l2>=0letwidthx=Position.distancex.loc_startx.loc_endletdescendingcmpab=-cmpabletcompare_width_decreasing=Comparable.lexicographic[compare_start;descendingcompare_end;compare]letis_single_linexmargin=(* The last character of a line can exceed the margin if it is not
preceded by a break. Adding 1 here is a workaround for this bug. *)widthx<=margin+1&&x.loc_start.pos_lnum=x.loc_end.pos_lnumletsmallestlocstack=letminab=ifwidtha<widthbthenaelsebinList.reduce_exn(loc::stack)~f:minletof_lexbuf(lexbuf:Lexing.lexbuf)={loc_start=lexbuf.lex_start_p;loc_end=lexbuf.lex_curr_p;loc_ghost=false}letprintppft=Caml.Format.fprintfppf"File \"%s\", line %d, characters %d-%d:"t.loc_start.pos_fnamet.loc_start.pos_lnum(t.loc_start.pos_cnum-t.loc_start.pos_bol)(t.loc_end.pos_cnum-t.loc_start.pos_bol)endmoduleLongident=structincludeLongidentletlidents=assert(not(String.containss'.'));Lidentsend