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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
open! Stdlib
module Source_content = struct
type t = Sc_as_Stringlit of string
let create s = Sc_as_Stringlit (Yojson.Safe.to_string (`String s))
let of_stringlit (`Stringlit s) = Sc_as_Stringlit s
let to_json (Sc_as_Stringlit s) = `Stringlit s
end
type map =
| Gen of
{ gen_line : int
; gen_col : int
}
| Gen_Ori of
{ gen_line : int
; gen_col : int
; ori_source : int
; ori_line : int
; ori_col : int
}
| Gen_Ori_Name of
{ gen_line : int
; gen_col : int
; ori_source : int
; ori_line : int
; ori_col : int
; ori_name : int
}
let gen_line = function
| Gen { gen_line; _ } | Gen_Ori { gen_line; _ } | Gen_Ori_Name { gen_line; _ } ->
gen_line
let gen_col = function
| Gen { gen_col; _ } | Gen_Ori { gen_col; _ } | Gen_Ori_Name { gen_col; _ } -> gen_col
module Offset = struct
type t =
{ gen_line : int
; gen_column : int
}
end
module Mappings = struct
type decoded = map list
type t = Uninterpreted of string [@@unboxed]
let empty = Uninterpreted ""
let is_empty (Uninterpreted s) = String.equal s ""
let of_string_unsafe : string -> t = fun s -> Uninterpreted s
let to_string : t -> string = fun (Uninterpreted s) -> s
let number_of_lines (Uninterpreted s) =
match s with
| "" -> 0
| _ ->
let c = ref 1 in
String.iter s ~f:(function
| ';' -> incr c
| _ -> ());
!c
let first_line (Uninterpreted s) =
let len = String.length s in
let rec loop i =
if i >= len
then i
else
match String.get s i with
| ';' -> loop (i + 1)
| _ -> i
in
loop 0
let encode' ~offset mapping =
match mapping with
| [] -> 0, empty
| _ ->
let a = Array.of_list mapping in
let len = Array.length a in
Array.stable_sort
~cmp:(fun t1 t2 ->
match compare (gen_line t1) (gen_line t2) with
| 0 -> compare (gen_col t1) (gen_col t2)
| n -> n)
a;
let buf = Buffer.create 1024 in
let gen_line_r = ref 1 in
let gen_col_r = ref 0 in
let ori_source_r = ref 0 in
let ori_line_r = ref 1 in
let ori_col_r = ref 0 in
let ori_name_r = ref 0 in
let rec loop prev i =
if i < len
then
let c = a.(i) in
if
i + 1 < len
&& gen_line c = gen_line a.(i + 1)
&& gen_col c = gen_col a.(i + 1)
then
loop prev (i + 1)
else (
if !gen_line_r <> gen_line c
then (
assert (!gen_line_r < gen_line c);
for _i = !gen_line_r to gen_line c - 1 do
Buffer.add_char buf ';'
done;
gen_col_r := 0;
gen_line_r := gen_line c)
else if i > 0
then Buffer.add_char buf ',';
let l =
match c with
| Gen { gen_line = _; gen_col } ->
let res = [ gen_col - !gen_col_r ] in
gen_col_r := gen_col;
res
| Gen_Ori { gen_line = _; gen_col; ori_source; ori_line; ori_col } ->
let res =
[ gen_col - !gen_col_r
; ori_source - !ori_source_r
; ori_line - !ori_line_r
; ori_col - !ori_col_r
]
in
gen_col_r := gen_col;
ori_col_r := ori_col;
ori_line_r := ori_line;
ori_source_r := ori_source;
res
| Gen_Ori_Name
{ gen_line = _; gen_col; ori_source; ori_line; ori_col; ori_name } ->
let res =
[ gen_col - !gen_col_r
; ori_source - !ori_source_r
; ori_line - !ori_line_r
; ori_col - !ori_col_r
; ori_name - !ori_name_r
]
in
gen_col_r := gen_col;
ori_col_r := ori_col;
ori_line_r := ori_line;
ori_source_r := ori_source;
ori_name_r := ori_name;
res
in
Vlq64.encode_l buf l;
loop i (i + 1))
in
let offset =
let first_line = gen_line a.(0) in
assert (first_line > 0);
if offset
then (
gen_line_r := first_line;
first_line - 1)
else 0
in
loop (-1) 0;
offset, Uninterpreted (Buffer.contents buf)
let encode mapping =
let gen_line, res = encode' ~offset:false mapping in
assert (gen_line = 0);
res
let encode_with_offset mapping =
let gen_line, res = encode' ~offset:true mapping in
{ Offset.gen_line; gen_column = 0 }, res
let decode_exn (Uninterpreted str) =
let total_len = String.length str in
let gen_col = ref 0 in
let ori_source = ref 0 in
let ori_line = ref 1 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 acc
else
let last = try String.index_from str pos ';' with Not_found -> total_len in
gen_col := 0;
let pos, acc =
if pos = last then pos + 1, acc else 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 { gen_line = line; gen_col = !gen_col }
| [ 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_Ori
{ gen_line = line
; gen_col = !gen_col
; ori_source = !ori_source
; ori_line = !ori_line
; ori_col = !ori_col
}
| [ 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_Ori_Name
{ gen_line = line
; gen_col = !gen_col
; ori_source = !ori_source
; ori_line = !ori_line
; ori_col = !ori_col
; ori_name = !ori_name
}
| _ -> invalid_arg "Source_map.Mappings.decode_exn"
in
let acc = v :: acc in
if last = stop then last + 1, acc else read_tokens line (last + 1) stop acc
in
readline 1 0 []
let invariant ~names:_ ~sources:_ (Uninterpreted str) =
if
not
(String.for_all str ~f:(function
| ';' | ',' -> true
| x -> Vlq64.in_alphabet x))
then invalid_arg "Mappings.invariant"
end
let version_is_valid = function
| 3 -> true
| _ -> false
let rewrite_path path =
if Filename.is_relative path
then path
else
match Build_path_prefix_map.get_build_path_prefix_map () with
| Some map -> Build_path_prefix_map.rewrite map path
| None -> path
let invalid () = invalid_arg "Source_map.of_json"
let string_of_stringlit ?tmp_buf (`Stringlit s) =
match Yojson.Safe.from_string ?buf:tmp_buf s with
| `String s -> s
| _ -> invalid ()
let int_of_intlit (`Intlit s) =
match Yojson.Safe.from_string s with
| `Int s -> s
| _ -> invalid ()
let stringlit name rest : [ `Stringlit of string ] option =
try
match List.assoc name rest with
| `Stringlit _ as s -> Some s
| `Null -> None
| _ -> invalid ()
with Not_found -> None
let list_stringlit name rest =
try
match List.assoc name rest with
| `List l ->
Some
(List.map l ~f:(function
| `Stringlit _ as s -> s
| _ -> invalid ()))
| _ -> invalid ()
with Not_found -> None
let list_stringlit_opt name rest =
try
match List.assoc name rest with
| `List l ->
Some
(List.map l ~f:(function
| `Stringlit _ as s -> Some s
| `Null -> None
| _ -> invalid ()))
| _ -> invalid ()
with Not_found -> None
let list_intlit name rest =
try
match List.assoc name rest with
| `List l ->
Some
(List.map l ~f:(function
| `Intlit _ as s -> s
| _ -> invalid ()))
| _ -> invalid ()
with Not_found -> None
module Standard = struct
type t =
{ version : int
; file : string option
; sourceroot : string option
; sources : string list
; sources_content : Source_content.t option list option
; names : string list
; mappings : Mappings.t
; ignore_list : string list
}
let empty ~inline_source_content =
{ version = 3
; file = None
; sourceroot = None
; sources = []
; sources_content = (if inline_source_content then Some [] else None)
; names = []
; mappings = Mappings.empty
; ignore_list = []
}
let maps ~sources_offset ~names_offset x =
match x with
| Gen _ -> x
| Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col } ->
let ori_source = ori_source + sources_offset in
Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col }
| Gen_Ori_Name { gen_line; gen_col; ori_source; ori_line; ori_col; ori_name } ->
let ori_source = ori_source + sources_offset in
let ori_name = ori_name + names_offset in
Gen_Ori_Name { gen_line; gen_col; ori_source; ori_line; ori_col; ori_name }
let filter_map sm ~f =
let a = Array.of_list (Mappings.decode_exn sm.mappings) in
Array.stable_sort
~cmp:(fun t1 t2 ->
match compare (gen_line t1) (gen_line t2) with
| 0 -> compare (gen_col t1) (gen_col t2)
| n -> n)
a;
let l = Array.to_list a |> List.group ~f:(fun a b -> gen_line a = gen_line b) in
let rec loop acc mapping =
match mapping with
| [] -> List.rev acc
| x :: xs ->
let gen_line = gen_line (List.hd x) in
let acc =
match f gen_line with
| None -> acc
| Some gen_line ->
List.rev_append_map
x
~f:(function
| Gen { gen_line = _; gen_col } -> Gen { gen_line; gen_col }
| Gen_Ori { gen_line = _; gen_col; ori_source; ori_line; ori_col } ->
Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col }
| Gen_Ori_Name
{ gen_line = _; gen_col; ori_source; ori_line; ori_col; ori_name }
->
Gen_Ori_Name
{ gen_line; gen_col; ori_source; ori_line; ori_col; ori_name })
acc
in
loop acc xs
in
let mappings = loop [] l in
{ sm with mappings = Mappings.encode mappings }
let merge = function
| [] -> None
| _ :: _ as l ->
let rec loop acc_rev mappings_rev ~sources_offset ~names_offset l =
match l with
| [] -> acc_rev, mappings_rev
| sm :: rest ->
let acc_rev, mappings_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 acc_rev.sources_content with
| Some acc_rev ->
let contents =
match sm.sources_content with
| Some x ->
assert (List.length x = List.length sm.sources);
x
| None -> List.map sm.sources ~f:(fun _ -> None)
in
Some (List.rev_append contents acc_rev)
| None -> None)
; mappings = Mappings.empty
}
, List.rev_append_map
~f:(maps ~sources_offset ~names_offset)
(Mappings.decode_exn sm.mappings)
mappings_rev )
in
loop
acc_rev
mappings_rev
~sources_offset:(sources_offset + List.length sm.sources)
~names_offset:(names_offset + List.length sm.names)
rest
in
let acc_rev, mappings_rev =
loop (empty ~inline_source_content:true) [] ~sources_offset:0 ~names_offset:0 l
in
Some
{ acc_rev with
mappings = Mappings.encode (List.rev mappings_rev)
; sources = List.rev acc_rev.sources
; names = List.rev acc_rev.names
; sources_content = Option.map ~f:List.rev acc_rev.sources_content
}
let json t =
let stringlit s = `Stringlit (Yojson.Safe.to_string (`String s)) in
`Assoc
(List.filter_map
~f:(fun (name, v) ->
match v with
| None -> None
| Some v -> Some (name, v))
[ "version", Some (`Intlit (string_of_int t.version))
; ( "file"
, match t.file with
| None -> None
| Some file -> Some (stringlit (rewrite_path file)) )
; ( "sourceRoot"
, match t.sourceroot with
| None -> None
| Some s -> Some (stringlit (rewrite_path s)) )
; "names", Some (`List (List.map t.names ~f:(fun s -> stringlit s)))
; ( "sources"
, Some (`List (List.map t.sources ~f:(fun s -> stringlit (rewrite_path s)))) )
; "mappings", Some (stringlit (Mappings.to_string t.mappings))
; ( "sourcesContent"
, match t.sources_content with
| None -> None
| Some l ->
Some
(`List
(List.map l ~f:(function
| None -> `Null
| Some x -> Source_content.to_json x))) )
; ( "ignoreList"
, match t.ignore_list with
| [] -> None
| _ ->
Some
(`List
(let s = StringSet.of_list t.ignore_list in
List.filter_map
~f:(fun x -> x)
(List.mapi
~f:(fun i nm ->
if StringSet.mem nm s
then Some (`Intlit (string_of_int i))
else None)
t.sources))) )
])
let of_json ?tmp_buf (json : Yojson.Raw.t) =
match json with
| `Assoc (("version", `Intlit version) :: rest)
when version_is_valid (int_of_string version) ->
let string name json =
Option.map ~f:(fun s -> string_of_stringlit ?tmp_buf s) (stringlit name json)
in
let file = string "file" rest in
let sourceroot = string "sourceRoot" rest in
let names =
match list_stringlit "names" rest with
| None -> []
| Some l -> List.map ~f:string_of_stringlit l
in
let sources =
match list_stringlit "sources" rest with
| None -> []
| Some l -> List.map ~f:string_of_stringlit l
in
let sources_content =
match list_stringlit_opt "sourcesContent" rest with
| None -> None
| Some l ->
Some
(List.map l ~f:(function
| None -> None
| Some s -> Some (Source_content.of_stringlit s)))
in
let mappings =
match string "mappings" rest with
| None -> Mappings.empty
| Some s -> Mappings.of_string_unsafe s
in
let ignore_list =
let s =
IntSet.of_list
(List.map
~f:int_of_intlit
(Option.value ~default:[] (list_intlit "ignoreList" rest)))
in
List.filter_map
~f:(fun x -> x)
(List.mapi ~f:(fun i nm -> if IntSet.mem i s then Some nm else None) sources)
in
{ version = int_of_string version
; file
; sourceroot
; names
; sources_content
; sources
; mappings
; ignore_list
}
| _ -> invalid ()
let of_string ?tmp_buf s = of_json ?tmp_buf (Yojson.Raw.from_string ?buf:tmp_buf s)
let of_file ?tmp_buf f = of_json ?tmp_buf (Yojson.Raw.from_file ?buf:tmp_buf f)
let to_string m = Yojson.Raw.to_string (json m)
let to_file m file = Yojson.Raw.to_file file (json m)
let invariant
{ version
; file = _
; sourceroot = _
; names
; sources_content
; sources
; mappings
; ignore_list
} =
if not (version_is_valid version)
then invalid_arg "Source_map.Standard.invariant: invalid version";
(if not (List.is_empty ignore_list)
then
let s = StringSet.of_list sources in
if List.exists ~f:(fun nm -> not (StringSet.mem nm s)) ignore_list
then
invalid_arg
"Source_map.Standard.invariant: ignore list should be a subset of sources");
match sources_content with
| None -> ()
| Some x ->
if not (List.length sources = List.length x)
then
invalid_arg
"Source_map.Standard.invariant: sources and sourcesContent must have the \
same size";
Mappings.invariant ~names ~sources mappings
end
module Index = struct
type section =
{ offset : Offset.t
; map : Standard.t
}
type t =
{ version : int
; file : string option
; sections : section list
}
let json t =
let stringlit s = `Stringlit (Yojson.Safe.to_string (`String s)) in
`Assoc
(List.filter_map
~f:(fun (name, v) ->
match v with
| None -> None
| Some v -> Some (name, v))
[ "version", Some (`Intlit (string_of_int t.version))
; ( "file"
, match t.file with
| None -> None
| Some file -> Some (stringlit (rewrite_path file)) )
; ( "sections"
, Some
(`List
(List.map
~f:(fun { offset = { gen_line; gen_column }; map } ->
`Assoc
[ ( "offset"
, `Assoc
[ "line", `Intlit (string_of_int gen_line)
; "column", `Intlit (string_of_int gen_column)
] )
; "map", Standard.json map
])
t.sections)) )
])
let intlit ~errmsg name json =
match List.assoc name json with
| `Intlit i -> int_of_string i
| _ -> invalid_arg errmsg
| exception Not_found -> invalid_arg errmsg
let section_of_json ?tmp_buf : Yojson.Raw.t -> section = function
| `Assoc json ->
let offset =
match List.assoc "offset" json with
| `Assoc fields ->
let gen_line =
intlit
"line"
fields
~errmsg:
"Source_map.Index.of_json: field 'line' absent or invalid from \
section"
in
let gen_column =
intlit
"column"
fields
~errmsg:
"Source_map.Index.of_json: field 'column' absent or invalid from \
section"
in
{ Offset.gen_line; gen_column }
| _ -> invalid_arg "Source_map.Index.of_json: 'offset' field of unexpected type"
in
(match List.assoc "url" json with
| _ ->
invalid_arg
"Source_map.Index.of_json: URLs in index maps are not currently supported"
| exception Not_found -> ());
let map =
try Standard.of_json ?tmp_buf (List.assoc "map" json) with
| Not_found -> invalid_arg "Source_map.Index.of_json: field 'map' absent"
| Invalid_argument _ ->
invalid_arg "Source_map.Index.of_json: invalid sub-map object"
in
{ offset; map }
| _ -> invalid_arg "Source_map.Index.of_json: section of unexpected type"
let of_json ?tmp_buf = function
| `Assoc (("version", `Intlit version) :: fields)
when version_is_valid (int_of_string version) -> (
let string name json = Option.map ~f:string_of_stringlit (stringlit name json) in
let file = string "file" fields in
match List.assoc "sections" fields with
| `List sections ->
let sections = List.map ~f:(section_of_json ?tmp_buf) sections in
{ version = int_of_string version; file; sections }
| _ -> invalid_arg "Source_map.Index.of_json: `sections` is not an array"
| exception Not_found ->
invalid_arg "Source_map.Index.of_json: no `sections` field")
| _ -> invalid_arg "Source_map.Index.of_json"
let to_string m = Yojson.Raw.to_string (json m)
let to_file m file = Yojson.Raw.to_file file (json m)
let invariant { version; file = _; sections } =
if not (version_is_valid version)
then invalid_arg "Source_map.Index.invariant: invalid version";
let _ : int =
List.fold_left
sections
~init:(-1)
~f:(fun acc { offset = { gen_line; gen_column }; map } ->
if gen_line < 0 || gen_column < 0
then invalid_arg "Source_map.Index.invariant: invalid offset";
if acc >= gen_line
then
invalid_arg
"Source_map.Index.invariant: overlapping or unordered map in sections";
Standard.invariant map;
gen_line + Mappings.number_of_lines map.mappings)
in
()
end
type t =
| Standard of Standard.t
| Index of Index.t
let of_json ?tmp_buf = function
| `Assoc fields as json -> (
match List.assoc "sections" fields with
| _ -> Index (Index.of_json ?tmp_buf json)
| exception Not_found -> Standard (Standard.of_json ?tmp_buf json))
| _ -> invalid_arg "Source_map.of_json: map is not an object"
let of_string ?tmp_buf s = of_json ?tmp_buf (Yojson.Raw.from_string ?buf:tmp_buf s)
let of_file ?tmp_buf f = of_json ?tmp_buf (Yojson.Raw.from_file ?buf:tmp_buf f)
let to_string = function
| Standard m -> Standard.to_string m
| Index i -> Index.to_string i
let to_file x f =
match x with
| Standard m -> Standard.to_file m f
| Index i -> Index.to_file i f
let invariant = function
| Standard m -> Standard.invariant m
| Index i -> Index.invariant i
type info =
{ mappings : Mappings.decoded
; sources : string list
; names : string list
}