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
284
285
286
287
288
289
290
291
292
293
module type S = Dolmen_intf.Location.S
type file = {
name : string;
mutable max_size : int;
mutable table : int Vec.t;
}
type t = Obj.t
type extended = {
offset : int;
length : int;
}
type full = {
file : file;
loc : t;
}
type loc = {
file : string;
start_line : int;
start_column : int;
start_line_offset : int;
stop_line : int;
stop_column : int;
stop_line_offset : int;
max_line_length : int;
}
exception Uncaught of t * exn * Printexc.raw_backtrace
exception Lexing_error of t * string
exception Syntax_error of t * [
| `Regular of Msg.t
| `Advanced of Msg.t * Msg.t * Msg.t
]
(** Exceptions that may occur during parsing *)
let compact_part_size = Sys.int_size / 2
let compact_part_mask = -1 lsr (Sys.int_size - compact_part_size)
let split_compact (c : t) =
if Obj.is_int c then begin
let i : int = Obj.magic c in
let offset = i land compact_part_mask in
let length = (i lsr compact_part_size) land compact_part_mask in
offset, length
end else begin
let e : extended = Obj.magic c in
e.offset, e.length
end
let mk_compact offset length =
if 0 <= offset && offset <= compact_part_mask &&
0 <= length && length <= compact_part_mask then begin
let i = offset + length lsl compact_part_size in
(Obj.magic i : t)
end else begin
let e = { offset; length; } in
(Obj.magic e : t)
end
let file_name { name; _ } = name
let mk_file name =
let table = Vec.create () in
let () = Vec.push table (-1) in
{ name; table; max_size = 0; }
let new_line file offset =
assert (Vec.last file.table < offset);
Vec.push file.table (offset - 1);
file.max_size <- offset
let newline file lexbuf =
Lexing.new_line lexbuf;
let offset = Lexing.lexeme_end lexbuf in
new_line file offset
let update_size file lexbuf =
let offset = Lexing.lexeme_end lexbuf in
file.max_size <- offset
let find_line file offset =
let rec aux vec offset start stop =
if start >= stop then start else begin
assert (start < stop);
let m = (start + stop) / 2 in
let o = Vec.get vec m in
if o < offset then begin
aux vec offset (m + 1) stop
end else begin
aux vec offset start m
end
end
in
let line = aux file.table offset 0 (Vec.size file.table) in
let line_offset = Vec.get file.table (line - 1) in
line_offset, line
let line_length file line =
let line_offset = Vec.get file.table (line - 1) in
let next_line_offset =
try Vec.get file.table line
with Invalid_argument _ -> file.max_size
in
next_line_offset - line_offset
let max_line_length file start_line stop_line =
let res = ref 0 in
for line = start_line to stop_line do
res := max !res (line_length file line)
done;
!res
let eq a b = a = b
let hash a = Hashtbl.hash a
let mk file
~start_line ~start_column ~start_line_offset
~stop_line ~stop_column ~stop_line_offset
~max_line_length =
{ file; max_line_length;
start_line; start_column; start_line_offset;
stop_line; stop_column; stop_line_offset; }
let no_loc : t =
mk_compact 0 0
let dummy : loc =
mk "" ~max_line_length:0
~start_line:0 ~start_column:0 ~start_line_offset:0
~stop_line:0 ~stop_column:0 ~stop_line_offset:0
let mk_pos start stop =
let open Lexing in
let start_offset = start.pos_cnum in
let stop_offset = stop.pos_cnum in
let length = stop_offset - start_offset in
mk_compact start_offset length
let is_dummy loc =
loc.start_line = loc.stop_line &&
loc.start_column = loc.stop_column
let of_lexbuf lexbuf =
let start = Lexing.lexeme_start_p lexbuf in
let stop = Lexing.lexeme_end_p lexbuf in
mk_pos start stop
let lexing_positions (loc : loc) =
let start = Lexing.{
pos_fname = loc.file;
pos_lnum = loc.start_line;
pos_bol = loc.start_line_offset;
pos_cnum = loc.start_column + loc.start_line_offset;
} in
let stop = Lexing.{
pos_fname = loc.file;
pos_lnum = loc.stop_line;
pos_bol = loc.stop_line_offset;
pos_cnum = loc.stop_column + loc.stop_line_offset;
} in
start, stop
let loc file c : loc =
let start_offset, length = split_compact c in
if length = 0 then
mk file.name ~max_line_length:0
~start_line:0 ~start_column:0 ~start_line_offset:0
~stop_line:0 ~stop_column:0 ~stop_line_offset:0
else begin
let stop_offset = start_offset + length in
let start_line_offset, start_line = find_line file start_offset in
let start_column = start_offset - start_line_offset - 1 in
let stop_line_offset, stop_line = find_line file stop_offset in
let stop_column = stop_offset - stop_line_offset - 1 in
let max_line_length = max_line_length file start_line stop_line in
mk file.name ~max_line_length
~start_line ~start_column ~start_line_offset:(start_line_offset + 1)
~stop_line ~stop_column ~stop_line_offset:(stop_line_offset + 1)
end
let full_loc { file; loc = l; } = loc file l
let compact (t : loc) =
let file = mk_file t.file in
let start_line_offset = Vec.get file.table t.start_line in
let start_offset = start_line_offset + t.start_column + 1 in
let stop_line_offset = Vec.get file.table t.stop_line in
let stop_offset = stop_line_offset + t.stop_column + 1 in
let length = stop_offset - start_offset in
file, mk_compact start_offset length
let pp buf pos =
if pos.start_line = pos.stop_line then
if pos.start_column = pos.stop_column then
if pos.file = "" then
Printf.bprintf buf "<location missing>"
else
Printf.bprintf buf "File \"%s\", <location missing>" pos.file
else
Printf.bprintf buf "File \"%s\", line %d, character %d-%d"
pos.file pos.start_line pos.start_column pos.stop_column
else
Printf.bprintf buf "File \"%s\", line %d, character %d to line %d, character %d"
pos.file
pos.start_line pos.start_column
pos.stop_line pos.stop_column
let fmt fmt pos =
if pos.start_line = pos.stop_line then
if pos.start_column = pos.stop_column then
if pos.file = "" then
Format.fprintf fmt "<location missing>"
else
Format.fprintf fmt "File \"%s\", <location missing>" pos.file
else
Format.fprintf fmt "File \"%s\", line %d, character %d-%d"
pos.file pos.start_line pos.start_column pos.stop_column
else
Format.fprintf fmt "File \"%s\", line %d, character %d to line %d, character %d"
pos.file
pos.start_line pos.start_column
pos.stop_line pos.stop_column
let fmt_hint fmt pos =
if pos.start_line = pos.stop_line then
Format.fprintf fmt "%s%s"
(String.make (pos.start_column) ' ')
(String.make (pos.stop_column - pos.start_column) '^')
let fmt_pos fmt pos =
if pos.start_line = pos.stop_line then
if pos.start_column = pos.stop_column then
Format.fprintf fmt "<location missing>"
else
Format.fprintf fmt "line %d, character %d-%d"
pos.start_line pos.start_column pos.stop_column
else
Format.fprintf fmt "line %d, character %d to line %d, character %d"
pos.start_line pos.start_column
pos.stop_line pos.stop_column