Debian Codegraph @

lib/loc.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 type source =
12 (* OCaml won't allow using DirPath.t in InFile *)
13 | InFile of { dirpath : string option; file : string }
14 | ToplevelInput
15
16 type t = {
17 fname : source;
18 line_nb : int;
19 bol_pos : int;
20 line_nb_last : int;
21 bol_pos_last : int;
22 bp : int;
23 ep : int;
24 }
25
26 let create fname line_nb bol_pos bp ep = {
27 fname = fname; line_nb = line_nb; bol_pos = bol_pos;
28 line_nb_last = line_nb; bol_pos_last = bol_pos; bp = bp; ep = ep;
29 }
30
31 let initial source = create source 1 0 0 0
32
33 let make_loc (bp, ep) = {
34 fname = ToplevelInput; line_nb = -1; bol_pos = 0; line_nb_last = -1; bol_pos_last = 0;
35 bp = bp; ep = ep;
36 }
37
38 let mergeable loc1 loc2 =
39 loc1.fname = loc2.fname
40
41 let merge loc1 loc2 =
42 if not (mergeable loc1 loc2) then
43 failwith "Trying to merge unmergeable locations.";
44 if loc1.bp < loc2.bp then
45 if loc1.ep < loc2.ep then {
46 fname = loc1.fname;
47 line_nb = loc1.line_nb;
48 bol_pos = loc1.bol_pos;
49 line_nb_last = loc2.line_nb_last;
50 bol_pos_last = loc2.bol_pos_last;
51 bp = loc1.bp; ep = loc2.ep;
52 }
53 else loc1
54 else if loc2.ep < loc1.ep then {
55 fname = loc2.fname;
56 line_nb = loc2.line_nb;
57 bol_pos = loc2.bol_pos;
58 line_nb_last = loc1.line_nb_last;
59 bol_pos_last = loc1.bol_pos_last;
60 bp = loc2.bp; ep = loc1.ep;
61 }
62 else loc2
63
64 let merge_opt l1 l2 = match l1, l2 with
65 | None, None -> None
66 | Some l , None -> Some l
67 | None, Some l -> Some l
68 | Some l1, Some l2 -> Some (merge l1 l2)
69
70 let sub loc sh len = {loc with bp = loc.bp + sh; ep = loc.bp + sh + len}
71
72 let after loc sh len = {loc with bp = loc.ep + sh; ep = loc.ep + sh + len}
73
74 let finer l1 l2 = match l1, l2 with
75 | None, _ -> false
76 | Some l , None -> true
77 | Some l1, Some l2 -> l1.fname = l2.fname && merge l1 l2 = l2
78
79 let unloc loc = (loc.bp, loc.ep)
80
81 let shift_loc kb kp loc = { loc with bp = loc.bp + kb ; ep = loc.ep + kp }
82
83 (** Located type *)
84
85 type 'a located = t option * 'a
86
87 let tag ?loc x = loc, x
88 let map f (l,x) = (l, f x)
89
90 (** Exceptions *)
91
92 let location : t Exninfo.t = Exninfo.make "location"
93
94 let add_loc e loc = Exninfo.add e location loc
95 let get_loc e = Exninfo.get e location
96
97 let raise ?loc e =
98 match loc with
99 | None -> raise e
100 | Some loc ->
101 let info = Exninfo.add Exninfo.null location loc in
102 Exninfo.iraise (e, info)
103
104 let pr loc =
105 let open Pp in
106 let fname = loc.fname in
107 match fname with
108 | ToplevelInput ->
109 (str"Toplevel input, characters " ++ int loc.bp ++
110 str"-" ++ int loc.ep)
111 | InFile { file } ->
112 (str"File " ++ str "\"" ++ str file ++ str "\"" ++
113 str", line " ++ int loc.line_nb ++ str", characters " ++
114 int (loc.bp-loc.bol_pos) ++ str"-" ++ int (loc.ep-loc.bol_pos))
115
116 let current_command_loc = ref None
117
118 let get_current_command_loc () = !current_command_loc
119
120 let set_current_command_loc v = current_command_loc := v