@@ -57,7 +57,6 @@ let gen_line = function
5757let gen_col = function
5858 | Gen { gen_col; _ } | Gen_Ori { gen_col; _ } | Gen_Ori_Name { gen_col; _ } -> gen_col
5959
60-
6160module Line_edits = struct
6261 type action =
6362 | Keep
@@ -76,7 +75,6 @@ module Line_edits = struct
7675 let pp fmt = Format. (pp_print_list pp_action fmt)
7776end
7877
79-
8078module Mappings = struct
8179 type t = Uninterpreted of string [@@ unboxed]
8280
@@ -579,7 +577,6 @@ let empty ~filename =
579577 ; mappings = Mappings. empty
580578 }
581579
582-
583580let concat ~file ~sourceroot s1 s2 =
584581 if not (Int. equal s1.version s2.version)
585582 then invalid_arg " Source_map.concat: different versions" ;
@@ -695,15 +692,15 @@ let merge = function
695692
696693(* IO *)
697694
695+ let rewrite_path path =
696+ if Filename. is_relative path
697+ then path
698+ else
699+ match Build_path_prefix_map. get_build_path_prefix_map () with
700+ | Some map -> Build_path_prefix_map. rewrite map path
701+ | None -> path
702+
698703let json t =
699- let rewrite_path path =
700- if Filename. is_relative path
701- then path
702- else
703- match Build_path_prefix_map. get_build_path_prefix_map () with
704- | Some map -> Build_path_prefix_map. rewrite map path
705- | None -> path
706- in
707704 let stringlit s = `Stringlit (Yojson.Safe. to_string (`String s)) in
708705 `Assoc
709706 [ " version" , `Intlit (string_of_int t.version)
@@ -764,7 +761,7 @@ let list_stringlit_opt name rest =
764761 | _ -> invalid ()
765762 with Not_found -> None
766763
767- let of_json (json : Yojson.Raw.t ) =
764+ let standard_map_of_json (json : Yojson.Raw.t ) =
768765 match json with
769766 | `Assoc (("version" , `Intlit version ) :: rest ) when int_of_string version = 3 ->
770767 let string name json = Option. map ~f: string_of_stringlit (stringlit name json) in
@@ -808,8 +805,116 @@ let of_json (json : Yojson.Raw.t) =
808805 }
809806 | _ -> invalid ()
810807
811- let of_string s = of_json (Yojson.Raw. from_string s)
812-
813808let to_string m = Yojson.Raw. to_string (json m)
814809
815810let to_file m file = Yojson.Raw. to_file file (json m)
811+
812+ module Index = struct
813+ type offset =
814+ { gen_line : int
815+ ; gen_column : int
816+ }
817+
818+ (* Type synonym to avoid confusion between toplevel [t] and this submodule's [t]. *)
819+ type map = t
820+
821+ type t =
822+ { version : int
823+ ; file : string
824+ ; sections : (offset * [ `Map of map ]) list
825+ }
826+
827+ let json t =
828+ let stringlit s = `Stringlit (Yojson.Safe. to_string (`String s)) in
829+ `Assoc
830+ [ " version" , `Intlit (string_of_int t.version)
831+ ; " file" , stringlit (rewrite_path t.file)
832+ ; ( " sections"
833+ , `List
834+ (List. map
835+ ~f: (fun ({ gen_line; gen_column } , `Map sm ) ->
836+ `Assoc
837+ [ ( " offset"
838+ , `Assoc
839+ [ " line" , `Intlit (string_of_int gen_line)
840+ ; " column" , `Intlit (string_of_int gen_column)
841+ ] )
842+ ; " map" , json sm
843+ ])
844+ t.sections) )
845+ ]
846+
847+ let intlit ~errmsg name json =
848+ match List. assoc name json with
849+ | `Intlit i -> int_of_string i
850+ | _ -> invalid_arg errmsg
851+ | exception Not_found -> invalid_arg errmsg
852+
853+ let section_of_json : Yojson.Raw.t -> offset * [ `Map of map ] = function
854+ | `Assoc json ->
855+ let offset =
856+ match List. assoc " offset" json with
857+ | `Assoc fields ->
858+ let gen_line =
859+ intlit
860+ " line"
861+ fields
862+ ~errmsg:
863+ " Source_map_io.Index.of_json: field 'line' absent or invalid from \
864+ section"
865+ in
866+ let gen_column =
867+ intlit
868+ " column"
869+ fields
870+ ~errmsg:
871+ " Source_map_io.Index.of_json: field 'column' absent or invalid from \
872+ section"
873+ in
874+ { gen_line; gen_column }
875+ | _ ->
876+ invalid_arg " Source_map_io.Index.of_json: 'offset' field of unexpected type"
877+ in
878+ (match List. assoc " url" json with
879+ | _ ->
880+ invalid_arg
881+ " Source_map_io.Index.of_json: URLs in index maps are not currently \
882+ supported"
883+ | exception Not_found -> () );
884+ let map =
885+ try standard_map_of_json (List. assoc " map" json) with
886+ | Not_found -> invalid_arg " Source_map_io.Index.of_json: field 'map' absent"
887+ | Invalid_argument _ ->
888+ invalid_arg " Source_map_io.Index.of_json: invalid sub-map object"
889+ in
890+ offset, `Map map
891+ | _ -> invalid_arg " Source_map_io.Index.of_json: section of unexpected type"
892+
893+ let of_json = function
894+ | `Assoc fields -> (
895+ let string name json = Option. map ~f: string_of_stringlit (stringlit name json) in
896+ let file = string " file" fields in
897+ match List. assoc " sections" fields with
898+ | `List sections ->
899+ let sections = List. map ~f: section_of_json sections in
900+ { version = 3 ; file = Option. value file ~default: " " ; sections }
901+ | _ -> invalid_arg " Source_map_io.Index.of_json: `sections` is not an array"
902+ | exception Not_found ->
903+ invalid_arg " Source_map_io.Index.of_json: no `sections` field" )
904+ | _ -> invalid_arg " Source_map_io.of_json: map is not an object"
905+
906+ let to_string m = Yojson.Raw. to_string (json m)
907+
908+ let to_file m file = Yojson.Raw. to_file file (json m)
909+ end
910+
911+ let of_json = function
912+ | `Assoc fields as json -> (
913+ match List. assoc " sections" fields with
914+ | _ -> `Index (Index. of_json json)
915+ | exception Not_found -> `Standard (standard_map_of_json json))
916+ | _ -> invalid_arg " Source_map_io.of_json: map is not an object"
917+
918+ let of_string s = of_json (Yojson.Raw. from_string s)
919+
920+ let of_file f = of_json (Yojson.Raw. from_file f)
0 commit comments