@@ -67,7 +67,6 @@ type manager =
67
67
over_right_gap : limit Links .t ; (* * associates the right limit of a token to
68
68
the left limit of the next *)
69
69
cache : (srcloc * limit ) Links .t ;
70
- leftmost_in_file : (string , limit ) Hashtbl .t ;
71
70
id : string ; (* * manager identifier (for logging/debugging) *)
72
71
}
73
72
@@ -80,37 +79,25 @@ let new_manager: string -> manager =
80
79
right_of = Links. create 42 ;
81
80
over_right_gap = Links. create 42 ;
82
81
cache = Links. create 42 ;
83
- leftmost_in_file = Hashtbl. create 3 ;
84
82
id = Pretty. to_string " %s-%u" manager_name ! id;
85
83
}
86
84
87
85
(* * Returns left and right (potentially fresh) limits for the given source
88
86
location; for any given file, must be called with the leftmost location
89
87
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). *)
93
88
let limits: manager -> srcloc -> limit * limit = fun ctx loc ->
94
89
let s, e = match Cobol_common.Srcloc. as_unique_lexloc loc with
95
90
| Some lexloc -> lexloc
96
91
| _ -> Limit. make_virtual () , Limit. make_virtual ()
97
92
in
98
93
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;
101
94
s, e
102
95
103
96
(* * Links token limits *)
104
97
let link_limits ctx left right =
105
98
(* Replace to deal with overriding of limits during recovery. *)
106
99
Links. replace ctx.over_right_gap left right
107
100
108
- (* * [leftmost_limit_in ~filename ctx] finds the leftmost limit from a location
109
- in [filename] that is registered in [ctx] (internal). Use with moderation
110
- as this is quite inefficient. *)
111
- let leftmost_limit_in ~filename ctx =
112
- Hashtbl. find_opt ctx.leftmost_in_file filename
113
-
114
101
(* * Returns a source location that spans between two given limits; returns a
115
102
valid pointwise location if the two given limits are physically equal. *)
116
103
let join_limits: manager -> limit * limit -> srcloc = fun ctx (s , e ) ->
@@ -124,7 +111,7 @@ let join_limits: manager -> limit * limit -> srcloc = fun ctx (s, e) ->
124
111
in
125
112
Cobol_common.Srcloc. raw (pos, pos)
126
113
in
127
- let try_limits (s , e ) =
114
+ let try_limits (s , e : limit * limit ) =
128
115
129
116
let rec proceed_from ?loc s = (* start search from left limit [s] *)
130
117
check ?loc @@ Links. find ctx.right_of s
@@ -137,7 +124,8 @@ let join_limits: manager -> limit * limit -> srcloc = fun ctx (s, e) ->
137
124
| None -> loc'
138
125
| Some loc -> Cobol_common.Srcloc. concat loc loc'
139
126
in
140
- if e == e' (* physical comparison *)
127
+ if e.pos_cnum = e'.pos_cnum && (* compare only the fields that matter *)
128
+ e.pos_fname = e'.pos_fname
141
129
then (Links. replace ctx.cache s (loc, e); loc)
142
130
else try_cache_from ~loc @@ Links. find ctx.over_right_gap e'
143
131
@@ -163,19 +151,14 @@ let join_limits: manager -> limit * limit -> srcloc = fun ctx (s, e) ->
163
151
(* Printexc.(print_raw_backtrace Stdlib.stderr @@ get_callstack 10); *)
164
152
loc
165
153
in
166
- (* first attempt assumes proper token limits: `s` is a left and `e` is a
167
- right of tokens *)
168
- try try_limits (s, e) with Not_found ->
169
- (* otherwise try assuming `s` is an end of token *)
170
- try try_limits (Links. find ctx.over_right_gap s, e) with Not_found ->
171
- try if s.pos_cnum = 0 (* potential special case with left-position forged by
172
- the parser: retry with leftmost limit if it differs
173
- from s *)
174
- then match leftmost_limit_in ~filename: s.pos_fname ctx with
175
- | Some l when l != s -> try_limits (l, e) (* physical equality is enough *)
176
- | Some _ | None -> raise Not_found
177
- else raise Not_found
178
- with Not_found -> join_failure (s, e)
154
+ try (* first attempt assumes proper token limits: `s` is a left and `e` is a
155
+ right of tokens *)
156
+ try_limits (s, e)
157
+ with Not_found ->
158
+ try (* otherwise try assuming `s` is an end of token *)
159
+ try_limits (Links. find ctx.over_right_gap s, e)
160
+ with Not_found ->
161
+ join_failure (s, e)
179
162
180
163
module New_manager (Id : sig val name: string end ) : MANAGER = struct
181
164
let ctx = new_manager Id. name
0 commit comments