Source file source_map.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
open! Stdlib
type map =
{ gen_line : int
; gen_col : int
; ori_source : int
; ori_line : int
; ori_col : int
; ori_name : int option
}
type mapping = map list
type t =
{ version : int
; file : string
; sourceroot : string option
; sources : string list
; sources_content : string option list option
; names : string list
; mappings : mapping
}
let empty ~filename =
{ version = 3
; file = filename
; sourceroot = None
; sources = []
; sources_content = None
; names = []
; mappings = []
}
let map_line_number ~f =
let f i = if i < 0 then i else f i in
fun m -> { m with ori_line = f m.ori_line; gen_line = f m.gen_line }
let string_of_mapping mapping =
let mapping =
List.map mapping ~f:(map_line_number ~f:pred)
in
let a = Array.of_list mapping in
let len = Array.length a in
Array.stable_sort
~cmp:(fun t1 t2 ->
match compare t1.gen_line t2.gen_line with
| 0 -> compare t1.gen_col t2.gen_col
| n -> n)
a;
let buf = Buffer.create 1024 in
let gen_line = ref 0 in
let gen_col = ref 0 in
let ori_source = ref 0 in
let ori_line = ref 0 in
let ori_col = ref 0 in
let ori_name = ref 0 in
let rec loop prev i =
if i < len
then
let c = a.(i) in
if prev >= 0
&& c.ori_source = a.(prev).ori_source
&& c.ori_line = a.(prev).ori_line
&& c.ori_col = a.(prev).ori_col
then
loop prev (i + 1)
else if i + 1 < len
&& c.gen_line = a.(i + 1).gen_line
&& c.gen_col = a.(i + 1).gen_col
then
loop prev (i + 1)
else (
if !gen_line <> c.gen_line
then (
assert (!gen_line < c.gen_line);
for _i = !gen_line to c.gen_line - 1 do
Buffer.add_char buf ';'
done;
gen_col := 0;
gen_line := c.gen_line)
else if i > 0
then Buffer.add_char buf ',';
let l =
(c.gen_col - !gen_col)
::
(if c.ori_source = -1
then []
else
(c.ori_source - !ori_source)
:: (c.ori_line - !ori_line)
:: (c.ori_col - !ori_col)
::
(match c.ori_name with
| None -> []
| Some n ->
let n' = !ori_name in
ori_name := n;
[ n - n' ]))
in
gen_col := c.gen_col;
if c.ori_source <> -1
then (
ori_source := c.ori_source;
ori_line := c.ori_line;
ori_col := c.ori_col);
Vlq64.encode_l buf l;
loop i (i + 1))
in
loop (-1) 0;
Buffer.contents buf
let mapping_of_string str =
let total_len = String.length str in
let gen_col = ref 0 in
let ori_source = ref 0 in
let ori_line = ref 0 in
let ori_col = ref 0 in
let ori_name = ref 0 in
let rec readline line pos acc =
if pos >= total_len
then
List.rev_map acc ~f:(map_line_number ~f:succ)
else
let last = try String.index_from str pos ';' with Not_found -> total_len in
gen_col := 0;
let pos, acc = read_tokens line pos last acc in
readline (succ line) pos acc
and read_tokens line start stop acc =
let last = try min (String.index_from str start ',') stop with Not_found -> stop in
let v = Vlq64.decode_l str ~pos:start ~len:(last - start) in
match v with
| [] -> last + 1, acc
| v ->
let v =
match v with
| [ g ] ->
gen_col := !gen_col + g;
{ gen_line = line
; gen_col = !gen_col
; ori_source = -1
; ori_line = -1
; ori_col = -1
; ori_name = None
}
| [ g; os; ol; oc ] ->
gen_col := !gen_col + g;
ori_source := !ori_source + os;
ori_line := !ori_line + ol;
ori_col := !ori_col + oc;
{ gen_line = line
; gen_col = !gen_col
; ori_source = !ori_source
; ori_line = !ori_line
; ori_col = !ori_col
; ori_name = None
}
| [ g; os; ol; oc; on ] ->
gen_col := !gen_col + g;
ori_source := !ori_source + os;
ori_line := !ori_line + ol;
ori_col := !ori_col + oc;
ori_name := !ori_name + on;
{ gen_line = line
; gen_col = !gen_col
; ori_source = !ori_source
; ori_line = !ori_line
; ori_col = !ori_col
; ori_name = Some !ori_name
}
| _ -> invalid_arg "Source_map.mapping_of_string"
in
let acc = v :: acc in
if last = stop then last + 1, acc else read_tokens line (last + 1) stop acc
in
readline 0 0 []
let maps ~sources_offset ~names_offset x =
let gen_line = x.gen_line in
let ori_source = x.ori_source + sources_offset in
let ori_name =
match x.ori_name with
| None -> None
| Some ori_name -> Some (ori_name + names_offset)
in
{ x with gen_line; ori_source; ori_name }
let filter_map sm ~f =
let a = Array.of_list sm.mappings in
Array.stable_sort
~cmp:(fun t1 t2 ->
match compare t1.gen_line t2.gen_line with
| 0 -> compare t1.gen_col t2.gen_col
| n -> n)
a;
let l = Array.to_list a |> List.group ~f:(fun a b -> a.gen_line = b.gen_line) in
let rec loop acc mapping =
match mapping with
| [] -> List.rev acc
| x :: xs ->
let gen_line = (List.hd x).gen_line in
let acc =
match f gen_line with
| None -> acc
| Some gen_line -> List.rev_append_map x ~f:(fun x -> { x with gen_line }) acc
in
loop acc xs
in
let mappings = loop [] l in
{ sm with mappings }
let merge = function
| [] -> None
| _ :: _ as l ->
let rec loop acc_rev ~sources_offset ~names_offset l =
match l with
| [] -> acc_rev
| sm :: rest ->
let acc_rev =
{ acc_rev with
sources = List.rev_append sm.sources acc_rev.sources
; names = List.rev_append sm.names acc_rev.names
; sources_content =
(match sm.sources_content, acc_rev.sources_content with
| Some x, Some acc_rev -> Some (List.rev_append x acc_rev)
| None, _ | _, None -> None)
; mappings =
List.rev_append_map
~f:(maps ~sources_offset ~names_offset)
sm.mappings
acc_rev.mappings
}
in
loop
acc_rev
~sources_offset:(sources_offset + List.length sm.sources)
~names_offset:(names_offset + List.length sm.names)
rest
in
let acc_rev =
loop
{ (empty ~filename:"") with sources_content = Some [] }
~sources_offset:0
~names_offset:0
l
in
Some
{ acc_rev with
mappings = List.rev acc_rev.mappings
; sources = List.rev acc_rev.sources
; names = List.rev acc_rev.names
; sources_content = Option.map ~f:List.rev acc_rev.sources_content
}