package lambdapi
All checked builds pass sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Proof assistant for the λΠ-calculus modulo rewriting
Install
dune-project
Dependency
Authors
Maintainers
Sources
lambdapi-4.0.0.tbz
sha256=0e2e73e0ea0c674f1263bf85ed3453ed3d6084d38eea29974a03ba9e382d29d7
sha512=2831db57fcd09206687a92fc9ee7d363e87bcec5167949e9c8161d41c63d5887b9f3556bfd63a7a9a0c12e5a5dc61a397baf4f496ebdb2a2fce122142c05e7a1
doc/src/lambdapi.common/pos.ml.html
Source file pos.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283(** Positions in Lambdapi files. *) open Lplib open Base let cur_dir = Unix.getcwd() (** Type of a position, corresponding to a continuous range of characters in a (utf8-encoded) source. *) type pos = { fname : string option (** 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. *) let cmp : pos cmp = fun p1 p2 -> let cmp = Int.compare in lex3 cmp cmp (lex3 (Option.cmp String.compare) cmp cmp) (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. *) let cat : pos -> pos -> pos = fun p1 p2 -> { 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. *) let file_start : string -> pos = fun fname -> { fname = Some fname ; 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}. *) let locate : ?fname:string -> Lexing.position * Lexing.position -> pos = fun ?fname (p1, p2) -> let fname = if p1.pos_fname = "" then fname else Some(p1.pos_fname) in let start_line = p1.pos_lnum in let start_col = p1.pos_cnum - p1.pos_bol in let start_offset = p1.pos_cnum in let end_line = p2.pos_lnum in let end_col = p2.pos_cnum - p2.pos_bol in let end_offset = p2.pos_cnum in {fname; start_line; start_col; start_offset; end_line; end_col; end_offset } (** [lexing p] converts a [pos] into a [Lexing.position]. *) let lexing (p:pos): Lexing.position = { pos_fname = (match p.fname with None -> "" | Some s -> 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. *) let to_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} -> let fname = if print_fname then match fname with | None -> "" | Some n -> (if print_dirname then String.remove_prefix (cur_dir^Filename.dir_sep) n else Filename.basename n) ^ ":" else "" in if start_line <> end_line then Printf.sprintf "%s%d:%d-%d:%d" fname start_line start_col end_line end_col else if start_col = end_col then Printf.sprintf "%s%d:%d" fname start_line start_col else Printf.sprintf "%s%d:%d-%d" fname start_line start_col end_col (** Type of optional positions. *) type popt = pos option (** [equal p1 p2] tells whether [p1] and [p2] denote the same position. *) let equal : popt -> popt -> bool = fun p1 p2 -> 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]. *) let pos_end : popt -> popt = fun po -> match po with | None -> None | Some p -> Some {p with start_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]. *) let cat : popt -> popt -> popt = fun p1 p2 -> match p1, p2 with | Some p1, Some p2 -> Some (cat p1 p2) | Some p, None | None, Some p -> Some p | 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). *) let prefix : int -> popt -> popt = fun n p -> match p with | None -> None | Some p -> Some { p with end_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. *) let shift : int -> popt -> popt = fun k p -> match p with | None -> assert false | Some ({start_col; _} as p) -> Some {p with start_col = start_col + k} (** [lexing_opt p] converts a [popt] into a [Lexing.position]. *) let lexing_opt (p:popt): Lexing.position = match p with | None -> {pos_fname=""; pos_lnum=1; pos_bol=0; pos_cnum=0} | Some p -> lexing p (** Type constructor extending a type (e.g. a piece of abstract syntax) with a an optional source code position. *) type 'a loc = { elt : 'a (** The element that is being localised. *) ; pos : popt (** Position of the element in the source code. *) } (** Localised string type (widely used). *) type strloc = string loc (** [make pos elt] associates the position [pos] to [elt]. *) let make : popt -> 'a -> 'a loc = fun pos elt -> { elt ; pos } (** [none elt] wraps [elt] in a ['a loc] structure without any specific source code position. *) let none : 'a -> 'a loc = fun elt -> make None elt (** [map f loc] applies function [f] on the value of [loc] and keeps the position unchanged. *) let map : ('a -> 'b) -> 'a loc -> 'b loc = fun f loc -> {loc with elt = f loc.elt} (** [in_pos pos elt] associates the position [pos] to [elt]. *) let in_pos : pos -> 'a -> 'a loc = fun p elt -> make (Some p) elt (** [make_pos lps elt] creates a located element from the lexing positions [lps] and the element [elt]. *) let make_pos : Lexing.position * Lexing.position -> 'a -> 'a loc = fun lps elt -> in_pos (locate lps) elt let popt_to_string : ?print_dirname:bool -> ?print_fname:bool -> popt -> string = fun ?(print_dirname=true) ?(print_fname=true) pop -> match pop with | None -> "Unknown location" | Some (p) -> to_string ~print_dirname ~print_fname p (** [pp ppf pos] prints the optional position [pos] on [ppf]. *) let pp : popt pp = fun ppf p -> string ppf (popt_to_string p) (** [short ppf pos] prints the optional position [pos] on [ppf]. *) let short : popt pp = fun ppf p -> string ppf (popt_to_string ~print_fname:false p) (** [pp_lexing ppf lps] prints the Lexing.position pair [lps] on [ppf]. *) let pp_lexing : (Lexing.position * Lexing.position) pp = fun ppf lps -> short ppf (Some (locate lps)) (** [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". *) let print_file_contents : parse_file:(string -> (unit -> string) * (unit -> unit)) -> escape:(string -> string) -> delimiters:(string*string) -> complain_if_location_unknown:bool -> popt pp = fun ~parse_file ~escape ~delimiters:(db,de) ~complain_if_location_unknown ppf pos -> match pos with | Some { fname=Some fname; start_line; start_col; end_line; end_col } -> (* WARNING: do not try to understand the following code! It's dangerous for your health! *) begin match parse_file fname with | exception _ -> string ppf ("Cannot open file \""^fname^"\". Maybe it was moved?") | input_line,finish -> let out = Buffer.create ((end_line - start_line) * 80 + (if end_col = -1 then 80 else end_col) + 1) in (* ignore the lines before the start_line *) for i = 0 to start_line - 2 do ignore (input_line ()) done ; (* skip the first start_col UTF8 codepoints of the start_line *) let startl = input_line () in assert (String.is_valid_utf_8 startl); let bytepos = ref 0 in for i = 0 to start_col - 1 do let uchar = String.get_utf_8_uchar startl !bytepos in assert (Uchar.utf_decode_is_valid uchar) ; bytepos := !bytepos + Uchar.utf_decode_length uchar done ; let startstr = String.sub startl !bytepos (String.length startl - !bytepos) in (* add what is left of the start_line, unless it is the end_line as well *) if end_line <> start_line then begin Buffer.add_string out (escape startstr) ; Buffer.add_string out "\n" end ; (* add the lines in between the start_line and the end_line *) for i = 0 to end_line - start_line - 2 do Buffer.add_string out (escape (input_line ())) ; Buffer.add_string out "\n" done ; (* identify what the end_line is and how many UTF8 codepoints to keep *) let endl,end_col = if start_line = end_line then if end_col = -1 then startstr, -1 else startstr, end_col - start_col else input_line (), end_col in (* keep the first end_col UTF8 codepoints of the end_line *) assert (String.is_valid_utf_8 endl); let bytepos = ref 0 in let i = ref 0 in (try while !i <= end_col -1 || end_col = -1 do let uchar = String.get_utf_8_uchar endl !bytepos in assert (Uchar.utf_decode_is_valid uchar) ; bytepos := !bytepos + Uchar.utf_decode_length uchar ; incr i done with Invalid_argument _ -> () (* End of line reached *)) ; let str = String.sub endl 0 !bytepos in Buffer.add_string out (escape str) ; finish () ; string ppf (Buffer.contents out) end | None | Some {fname=None} -> if complain_if_location_unknown then string ppf (db ^ "unknown location" ^ de) else string ppf ""
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>