11open Import
22
3- module Config = struct
4- type loc_mode = [ `Short | `Full ]
5- type t = { show_attrs : bool ; show_locs : bool ; loc_mode : loc_mode }
6-
7- module Default = struct
8- let show_attrs = false
9- let show_locs = false
10- let loc_mode = `Short
11- end
12-
13- let default =
14- let open Default in
15- { show_attrs; show_locs; loc_mode }
16-
17- let make ?(show_attrs = Default. show_attrs) ?(show_locs = Default. show_locs)
18- ?(loc_mode = Default. loc_mode) () =
19- { show_attrs; show_locs; loc_mode }
20- end
21-
22- let cnum (pos : Lexing.position ) = pos.pos_cnum - pos.pos_bol
23-
24- type simple_val =
3+ type repr =
254 | Unit
265 | Int of int
276 | String of string
287 | Bool of bool
298 | Char of char
30- | Array of simple_val list
9+ | Array of repr list
3110 | Float of float
3211 | Int32 of int32
3312 | Int64 of int64
3413 | Nativeint of nativeint
35- | Record of (string * simple_val ) list
36- | Constr of string * simple_val list
37- | Tuple of simple_val list
38- | List of simple_val list
14+ | Record of (string * repr ) list
15+ | Constr of string * repr list
16+ | Tuple of repr list
17+ | List of repr list
3918 | Special of string
4019
4120let pp_collection ~pp_elm ~open_ ~close ~sep fmt l =
@@ -46,8 +25,11 @@ let pp_collection ~pp_elm ~open_ ~close ~sep fmt l =
4625 List. iter tl ~f: (fun sv -> Format. fprintf fmt " %s %a@," sep pp_elm sv);
4726 Format. fprintf fmt " %s@]" close
4827
49- let rec pp_simple_val fmt simple_val =
50- match simple_val with
28+ type 'a pp = Format .formatter -> 'a -> unit
29+
30+ let rec pp_repr : repr pp =
31+ fun fmt repr ->
32+ match repr with
5133 | Unit -> Format. fprintf fmt " ()"
5234 | Int i -> Format. fprintf fmt " %i" i
5335 | String s -> Format. fprintf fmt " %S" s
@@ -59,27 +41,55 @@ let rec pp_simple_val fmt simple_val =
5941 | Int64 i64 -> Format. fprintf fmt " %Li" i64
6042 | Nativeint ni -> Format. fprintf fmt " %ni" ni
6143 | Array l ->
62- pp_collection ~pp_elm: pp_simple_val ~open_: " [|" ~close: " |]" ~sep: " ;" fmt l
44+ pp_collection ~pp_elm: pp_repr ~open_: " [|" ~close: " |]" ~sep: " ;" fmt l
6345 | Tuple l ->
64- pp_collection ~pp_elm: pp_simple_val ~open_: " (" ~close: " )" ~sep: " ," fmt l
65- | List l ->
66- pp_collection ~pp_elm: pp_simple_val ~open_: " [" ~close: " ]" ~sep: " ;" fmt l
46+ pp_collection ~pp_elm: pp_repr ~open_: " (" ~close: " )" ~sep: " ," fmt l
47+ | List l -> pp_collection ~pp_elm: pp_repr ~open_: " [" ~close: " ]" ~sep: " ;" fmt l
6748 | Record fields ->
6849 pp_collection ~pp_elm: pp_field ~open_: " {" ~close: " }" ~sep: " ;" fmt fields
6950 | Constr (cname , [] ) -> Format. fprintf fmt " %s" cname
7051 | Constr (cname , [ (Constr (_ , _ :: _ ) as x ) ]) ->
71- Format. fprintf fmt " @[<hv 2>%s@ (%a)@]" cname pp_simple_val x
52+ Format. fprintf fmt " @[<hv 2>%s@ (%a)@]" cname pp_repr x
7253 | Constr (cname , [ x ]) ->
73- Format. fprintf fmt " @[<hv 2>%s@ %a@]" cname pp_simple_val x
54+ Format. fprintf fmt " @[<hv 2>%s@ %a@]" cname pp_repr x
7455 | Constr (cname , l ) ->
75- Format. fprintf fmt " @[<hv 2>%s@ %a@]" cname pp_simple_val (Tuple l)
56+ Format. fprintf fmt " @[<hv 2>%s@ %a@]" cname pp_repr (Tuple l)
7657
77- and pp_field fmt (fname , simple_val ) =
78- Format. fprintf fmt " @[<hv 2>%s =@ %a@]" fname pp_simple_val simple_val
58+ and pp_field fmt (fname , repr ) =
59+ Format. fprintf fmt " @[<hv 2>%s =@ %a@]" fname pp_repr repr
7960
80- class lift_simple_val =
61+ (* TODO: split into Printer and Lifter config*)
62+ module Config = struct
63+ type loc_mode = [ `Short | `Full ]
64+
65+ type t = {
66+ show_attrs : bool ;
67+ show_locs : bool ;
68+ loc_mode : loc_mode ;
69+ printer : repr pp ;
70+ }
71+
72+ module Default = struct
73+ let show_attrs = false
74+ let show_locs = false
75+ let loc_mode = `Short
76+ let printer = pp_repr
77+ end
78+
79+ let default =
80+ let open Default in
81+ { show_attrs; show_locs; loc_mode; printer = pp_repr }
82+
83+ let make ?(show_attrs = Default. show_attrs) ?(show_locs = Default. show_locs)
84+ ?(loc_mode = Default. loc_mode) ?(printer = Default. printer) () =
85+ { show_attrs; show_locs; loc_mode; printer }
86+ end
87+
88+ let cnum (pos : Lexing.position ) = pos.pos_cnum - pos.pos_bol
89+
90+ class lift_repr =
8191 object (self )
82- inherit [simple_val ] Ast_traverse. lift as super
92+ inherit [repr ] Ast_traverse. lift as super
8393 val mutable config = Config. default
8494 method set_config new_config = config < - new_config
8595 method get_config () = config
@@ -139,12 +149,12 @@ class lift_simple_val =
139149
140150 method lift_record_with_desc :
141151 'record 'desc.
142- lift_desc:('desc -> simple_val ) ->
143- lift_record:('record -> simple_val ) ->
152+ lift_desc:('desc -> repr ) ->
153+ lift_record:('record -> repr ) ->
144154 desc:'desc ->
145155 attrs:attributes ->
146156 'record ->
147- simple_val =
157+ repr =
148158 fun ~lift_desc ~lift_record ~desc ~attrs x ->
149159 match (config.show_locs, config.show_attrs, attrs) with
150160 | false , false , _ | false , true , [] -> lift_desc desc
@@ -306,7 +316,6 @@ class lift_simple_val =
306316 | NoInjectivity -> Constr (" NoInjectivity" , [] )
307317 end
308318
309- type 'a pp = Format .formatter -> 'a -> unit
310319type 'a configurable = ?config:Config .t -> 'a pp
311320type 'a configured = 'a pp
312321
@@ -333,17 +342,17 @@ module Make (Conf : Conf) : Configured = struct
333342 type 'a printer = 'a configured
334343
335344 let lsv =
336- let lift_simple_val = new lift_simple_val in
337- lift_simple_val #set_config Conf. config;
338- lift_simple_val
339-
340- let structure fmt str = pp_simple_val fmt (lsv#structure str)
341- let structure_item fmt str = pp_simple_val fmt (lsv#structure_item str)
342- let signature fmt str = pp_simple_val fmt (lsv#signature str)
343- let signature_item fmt str = pp_simple_val fmt (lsv#signature_item str)
344- let expression fmt str = pp_simple_val fmt (lsv#expression str)
345- let pattern fmt str = pp_simple_val fmt (lsv#pattern str)
346- let core_type fmt str = pp_simple_val fmt (lsv#core_type str)
345+ let lift_repr = new lift_repr in
346+ lift_repr #set_config Conf. config;
347+ lift_repr
348+
349+ let structure fmt str = pp_repr fmt (lsv#structure str)
350+ let structure_item fmt str = pp_repr fmt (lsv#structure_item str)
351+ let signature fmt str = pp_repr fmt (lsv#signature str)
352+ let signature_item fmt str = pp_repr fmt (lsv#signature_item str)
353+ let expression fmt str = pp_repr fmt (lsv#expression str)
354+ let pattern fmt str = pp_repr fmt (lsv#pattern str)
355+ let core_type fmt str = pp_repr fmt (lsv#core_type str)
347356end
348357
349358let make config =
@@ -357,23 +366,23 @@ end)
357366
358367type 'a printer = 'a configurable
359368
360- let lift_simple_val = new lift_simple_val
369+ let lift_repr = new lift_repr
361370
362371let with_config ~config ~f =
363- let old_config = lift_simple_val #get_config () in
364- lift_simple_val #set_config config;
372+ let old_config = lift_repr #get_config () in
373+ lift_repr #set_config config;
365374 let res = f () in
366- lift_simple_val #set_config old_config;
375+ lift_repr #set_config old_config;
367376 res
368377
369- let pp_with_config (type a ) (lifter : a -> simple_val )
370- ?( config = Config. default) fmt (x : a ) =
371- with_config ~config ~f: (fun () -> pp_simple_val fmt (lifter x))
372-
373- let structure = pp_with_config lift_simple_val #structure
374- let structure_item = pp_with_config lift_simple_val #structure_item
375- let signature = pp_with_config lift_simple_val #signature
376- let signature_item = pp_with_config lift_simple_val #signature_item
377- let expression = pp_with_config lift_simple_val #expression
378- let pattern = pp_with_config lift_simple_val #pattern
379- let core_type = pp_with_config lift_simple_val #core_type
378+ let pp_with_config (type a ) (lifter : a -> repr ) ?( config = Config. default) fmt
379+ (x : a ) =
380+ with_config ~config ~f: (fun () -> config.printer fmt (lifter x))
381+
382+ let structure = pp_with_config lift_repr #structure
383+ let structure_item = pp_with_config lift_repr #structure_item
384+ let signature = pp_with_config lift_repr #signature
385+ let signature_item = pp_with_config lift_repr #signature_item
386+ let expression = pp_with_config lift_repr #expression
387+ let pattern = pp_with_config lift_repr #pattern
388+ let core_type = pp_with_config lift_repr #core_type
0 commit comments