You can search for identifiers within the package.
in-package search v0.2.0
Source file pos.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283(** Positions in Lambdapi files. *)openLplibopenBaseletcur_dir=Unix.getcwd()(** Type of a position, corresponding to a continuous range of characters in a
(utf8-encoded) source. *)typepos={fname:stringoption(** File name for the position. *);start_line:int(** Line number of the starting point. *);start_col:int(** Column number (utf8) of the starting point. *);start_offset:int(** Number of chars since the beginning of the document
for the starting point *);end_line:int(** Line number of the ending point. *);end_col:int(** Column number (utf8) of the ending point. *);end_offset:int(** Number of chars since the beginning of the document
for the ending point *)}(** Total comparison function on pos. *)letcmp:poscmp=funp1p2->letcmp=Int.compareinlex3cmpcmp(lex3(Option.cmpString.compare)cmpcmp)(p1.start_line,p1.start_col,(p1.fname,p1.end_line,p1.end_col))(p2.start_line,p2.start_col,(p2.fname,p2.end_line,p2.end_col))(** [cat p1 p2] returns a position starting from [p1] start and ending with
[p2] end. The result position uses [p2] fname. *)letcat:pos->pos->pos=funp1p2->{fname=p2.fname;start_line=p1.start_line;start_col=p1.start_col;start_offset=p1.start_offset;end_line=p2.end_line;end_col=p2.end_col;end_offset=p2.end_offset}(** [file_start fname] is a zero-length position at the start of file [fname],
used as a fallback for error reporting when no finer position is known. *)letfile_start:string->pos=funfname->{fname=Somefname;start_line=1;start_col=0;start_offset=0;end_line=1;end_col=0;end_offset=0}(** [locate ?fname (p1,p2)] converts the pair of Lexing positions [p1,p2] and
filename [fname] into a {!type:pos}. *)letlocate:?fname:string->Lexing.position*Lexing.position->pos=fun?fname(p1,p2)->letfname=ifp1.pos_fname=""thenfnameelseSome(p1.pos_fname)inletstart_line=p1.pos_lnuminletstart_col=p1.pos_cnum-p1.pos_bolinletstart_offset=p1.pos_cnuminletend_line=p2.pos_lnuminletend_col=p2.pos_cnum-p2.pos_bolinletend_offset=p2.pos_cnumin{fname;start_line;start_col;start_offset;end_line;end_col;end_offset}(** [lexing p] converts a [pos] into a [Lexing.position]. *)letlexing(p:pos):Lexing.position={pos_fname=(matchp.fnamewithNone->""|Somes->s);pos_lnum=p.start_line;pos_bol=p.start_offset-p.start_col;pos_cnum=p.start_offset}(** [to_string ?print_fname pos] transforms [pos] into a readable string. If
[print_fname] is [true] (the default), the filename contained in [pos] is
printed. *)letto_string:?print_dirname:bool->?print_fname:bool->pos->string=fun?(print_dirname=true)?(print_fname=true){fname;start_line;start_col;end_line;end_col}->letfname=ifprint_fnamethenmatchfnamewith|None->""|Somen->(ifprint_dirnamethenString.remove_prefix(cur_dir^Filename.dir_sep)nelseFilename.basenamen)^":"else""inifstart_line<>end_linethenPrintf.sprintf"%s%d:%d-%d:%d"fnamestart_linestart_colend_lineend_colelseifstart_col=end_colthenPrintf.sprintf"%s%d:%d"fnamestart_linestart_colelsePrintf.sprintf"%s%d:%d-%d"fnamestart_linestart_colend_col(** Type of optional positions. *)typepopt=posoption(** [equal p1 p2] tells whether [p1] and [p2] denote the same position. *)letequal:popt->popt->bool=funp1p2->match(p1,p2)with|(Some(p1),Some(p2))->p1=p2|(None,None)->true|(_,_)->false(** [pos_end po] creates a position from the end of position [po]. *)letpos_end:popt->popt=funpo->matchpowith|None->None|Somep->Some{pwithstart_offset=p.end_offset;start_line=p.end_line;start_col=p.end_col}(** [cat] extends and hide the above [cat] function from [pos] to [popt]. *)letcat:popt->popt->popt=funp1p2->matchp1,p2with|Somep1,Somep2->Some(catp1p2)|Somep,None|None,Somep->Somep|None,None->None(** [prefix n p] is the sub-position of [p] covering only its first [n]
characters, which are assumed to lie on the first line of [p] and to be
ASCII (so that [n] is both a column count and a character count). *)letprefix:int->popt->popt=funnp->matchpwith|None->None|Somep->Some{pwithend_line=p.start_line;end_col=p.start_col+n;end_offset=p.start_offset+n}(** [shift k p] returns a position that is [k] characters after [p].
/!\ The generated position may not actually exist in the user input.
The fields start_offset, end_offset and end_col are inconsistent. *)letshift:int->popt->popt=funkp->matchpwith|None->assertfalse|Some({start_col;_}asp)->Some{pwithstart_col=start_col+k}(** [lexing_opt p] converts a [popt] into a [Lexing.position]. *)letlexing_opt(p:popt):Lexing.position=matchpwith|None->{pos_fname="";pos_lnum=1;pos_bol=0;pos_cnum=0}|Somep->lexingp(** Type constructor extending a type (e.g. a piece of abstract syntax) with a
an optional source code position. *)type'aloc={elt:'a(** The element that is being localised. *);pos:popt(** Position of the element in the source code. *)}(** Localised string type (widely used). *)typestrloc=stringloc(** [make pos elt] associates the position [pos] to [elt]. *)letmake:popt->'a->'aloc=funposelt->{elt;pos}(** [none elt] wraps [elt] in a ['a loc] structure without any specific source
code position. *)letnone:'a->'aloc=funelt->makeNoneelt(** [map f loc] applies function [f] on the value of [loc] and keeps the
position unchanged. *)letmap:('a->'b)->'aloc->'bloc=funfloc->{locwithelt=floc.elt}(** [in_pos pos elt] associates the position [pos] to [elt]. *)letin_pos:pos->'a->'aloc=funpelt->make(Somep)elt(** [make_pos lps elt] creates a located element from the lexing positions
[lps] and the element [elt]. *)letmake_pos:Lexing.position*Lexing.position->'a->'aloc=funlpselt->in_pos(locatelps)eltletpopt_to_string:?print_dirname:bool->?print_fname:bool->popt->string=fun?(print_dirname=true)?(print_fname=true)pop->matchpopwith|None->"Unknown location"|Some(p)->to_string~print_dirname~print_fnamep(** [pp ppf pos] prints the optional position [pos] on [ppf]. *)letpp:poptpp=funppfp->stringppf(popt_to_stringp)(** [short ppf pos] prints the optional position [pos] on [ppf]. *)letshort:poptpp=funppfp->stringppf(popt_to_string~print_fname:falsep)(** [pp_lexing ppf lps] prints the Lexing.position pair [lps] on [ppf]. *)letpp_lexing:(Lexing.position*Lexing.position)pp=funppflps->shortppf(Some(locatelps))(** [print_file_contents parse_file escape sep delimiters pos] prints the
contents of the file at position [pos]. The [parse_file] function
takes in input [pos.fname] (that in reality may be a filename or
a URI, e.g. when the text comes from LSP) and it returns both a stream
of lines provided by a function that raises End_of_file if the file
content is terminated, and a function to close the resources when
we are done with the stream.
[sep] is the separator replacing each newline
(e.g. "<br>\n"). [delimiters] is a pair of delimiters used to wrap the
"unknown location" message returned when the position does not refer to a
file. [escape] is used to escape the file contents.
The value -1 for end_col is to be interpreted as "at the end of line". *)letprint_file_contents:parse_file:(string->(unit->string)*(unit->unit))->escape:(string->string)->delimiters:(string*string)->complain_if_location_unknown:bool->poptpp=fun~parse_file~escape~delimiters:(db,de)~complain_if_location_unknownppfpos->matchposwith|Some{fname=Somefname;start_line;start_col;end_line;end_col}->(* WARNING: do not try to understand the following code!
It's dangerous for your health! *)beginmatchparse_filefnamewith|exception_->stringppf("Cannot open file \""^fname^"\". Maybe it was moved?")|input_line,finish->letout=Buffer.create((end_line-start_line)*80+(ifend_col=-1then80elseend_col)+1)in(* ignore the lines before the start_line *)fori=0tostart_line-2doignore(input_line())done;(* skip the first start_col UTF8 codepoints of the start_line *)letstartl=input_line()inassert(String.is_valid_utf_8startl);letbytepos=ref0infori=0tostart_col-1doletuchar=String.get_utf_8_ucharstartl!byteposinassert(Uchar.utf_decode_is_validuchar);bytepos:=!bytepos+Uchar.utf_decode_lengthuchardone;letstartstr=String.substartl!bytepos(String.lengthstartl-!bytepos)in(* add what is left of the start_line, unless it is the end_line
as well *)ifend_line<>start_linethenbeginBuffer.add_stringout(escapestartstr);Buffer.add_stringout"\n"end;(* add the lines in between the start_line and the end_line *)fori=0toend_line-start_line-2doBuffer.add_stringout(escape(input_line()));Buffer.add_stringout"\n"done;(* identify what the end_line is and how many UTF8 codepoints to keep *)letendl,end_col=ifstart_line=end_linethenifend_col=-1thenstartstr,-1elsestartstr,end_col-start_colelseinput_line(),end_colin(* keep the first end_col UTF8 codepoints of the end_line *)assert(String.is_valid_utf_8endl);letbytepos=ref0inleti=ref0in(trywhile!i<=end_col-1||end_col=-1doletuchar=String.get_utf_8_ucharendl!byteposinassert(Uchar.utf_decode_is_validuchar);bytepos:=!bytepos+Uchar.utf_decode_lengthuchar;incridonewithInvalid_argument_->()(* End of line reached *));letstr=String.subendl0!byteposinBuffer.add_stringout(escapestr);finish();stringppf(Buffer.contentsout)end|None|Some{fname=None}->ifcomplain_if_location_unknownthenstringppf(db^"unknown location"^de)elsestringppf""