Skip to content

Commit 773c8a1

Browse files
committed
Avoid use of deprecated fold function on weak hash tables
1 parent b4b345e commit 773c8a1

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/lsp/cobol_preproc/src_overlay.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type manager =
6767
over_right_gap: limit Links.t; (** associates the right limit of a token to
6868
the left limit of the next *)
6969
cache: (srcloc * limit) Links.t;
70+
leftmost_in_file: (string, limit) Hashtbl.t;
7071
id: string; (** manager identifier (for logging/debugging) *)
7172
}
7273

@@ -79,18 +80,24 @@ let new_manager: string -> manager =
7980
right_of = Links.create 42;
8081
over_right_gap = Links.create 42;
8182
cache = Links.create 42;
83+
leftmost_in_file = Hashtbl.create 3;
8284
id = Pretty.to_string "%s-%u" manager_name !id;
8385
}
8486

8587
(** Returns left and right (potentially fresh) limits for the given source
8688
location; for any given file, must be called with the leftmost location
8789
first. *)
90+
(* TODO: try to see whether registering the leftmost location in each file could
91+
be done more efficiently wihtout a membership test on each new location (but
92+
the pre-processor does not provide change-of-file info to the parser). *)
8893
let limits: manager -> srcloc -> limit * limit = fun ctx loc ->
8994
let s, e = match Cobol_common.Srcloc.as_unique_lexloc loc with
9095
| Some lexloc -> lexloc
9196
| _ -> Limit.make_virtual (), Limit.make_virtual ()
9297
in
9398
Links.replace ctx.right_of s (loc, e); (* replace to deal with duplicates *)
99+
if not (Hashtbl.mem ctx.leftmost_in_file s.pos_fname)
100+
then Hashtbl.add ctx.leftmost_in_file s.pos_fname s;
94101
s, e
95102

96103
(** Links token limits *)
@@ -102,12 +109,7 @@ let link_limits ctx left right =
102109
in [filename] that is registered in [ctx] (internal). Use with moderation
103110
as this is quite inefficient. *)
104111
let leftmost_limit_in ~filename ctx =
105-
Links.fold begin fun l _ -> function
106-
| None when l.Lexing.pos_fname = filename -> Some l
107-
| Some l' when l.Lexing.pos_cnum < l'.pos_cnum &&
108-
l.Lexing.pos_fname = filename -> Some l
109-
| res -> res
110-
end ctx.right_of None
112+
Hashtbl.find_opt ctx.leftmost_in_file filename
111113

112114
(** Returns a source location that spans between two given limits; returns a
113115
valid pointwise location if the two given limits are physically equal. *)

0 commit comments

Comments
 (0)