1
1
open Import
2
2
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 =
25
4
| Unit
26
5
| Int of int
27
6
| String of string
28
7
| Bool of bool
29
8
| Char of char
30
- | Array of simple_val list
9
+ | Array of repr list
31
10
| Float of float
32
11
| Int32 of int32
33
12
| Int64 of int64
34
13
| 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
39
18
| Special of string
40
19
41
20
let pp_collection ~pp_elm ~open_ ~close ~sep fmt l =
@@ -46,8 +25,11 @@ let pp_collection ~pp_elm ~open_ ~close ~sep fmt l =
46
25
List. iter tl ~f: (fun sv -> Format. fprintf fmt " %s %a@," sep pp_elm sv);
47
26
Format. fprintf fmt " %s@]" close
48
27
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
51
33
| Unit -> Format. fprintf fmt " ()"
52
34
| Int i -> Format. fprintf fmt " %i" i
53
35
| String s -> Format. fprintf fmt " %S" s
@@ -59,27 +41,55 @@ let rec pp_simple_val fmt simple_val =
59
41
| Int64 i64 -> Format. fprintf fmt " %Li" i64
60
42
| Nativeint ni -> Format. fprintf fmt " %ni" ni
61
43
| 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
63
45
| 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
67
48
| Record fields ->
68
49
pp_collection ~pp_elm: pp_field ~open_: " {" ~close: " }" ~sep: " ;" fmt fields
69
50
| Constr (cname , [] ) -> Format. fprintf fmt " %s" cname
70
51
| 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
72
53
| 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
74
55
| 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)
76
57
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
79
60
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 =
81
91
object (self )
82
- inherit [simple_val ] Ast_traverse. lift as super
92
+ inherit [repr ] Ast_traverse. lift as super
83
93
val mutable config = Config. default
84
94
method set_config new_config = config < - new_config
85
95
method get_config () = config
@@ -139,12 +149,12 @@ class lift_simple_val =
139
149
140
150
method lift_record_with_desc :
141
151
'record 'desc.
142
- lift_desc:('desc -> simple_val ) ->
143
- lift_record:('record -> simple_val ) ->
152
+ lift_desc:('desc -> repr ) ->
153
+ lift_record:('record -> repr ) ->
144
154
desc:'desc ->
145
155
attrs:attributes ->
146
156
'record ->
147
- simple_val =
157
+ repr =
148
158
fun ~lift_desc ~lift_record ~desc ~attrs x ->
149
159
match (config.show_locs, config.show_attrs, attrs) with
150
160
| false , false , _ | false , true , [] -> lift_desc desc
@@ -302,44 +312,43 @@ class lift_simple_val =
302
312
| NoInjectivity -> Constr (" NoInjectivity" , [] )
303
313
end
304
314
305
- type 'a pp = Format .formatter -> 'a -> unit
306
315
type 'a configurable = ?config:Config .t -> 'a pp
307
316
type 'a configured = 'a pp
308
317
309
318
module type S = sig
310
- type 'a printer
311
-
312
- val structure : structure printer
313
- val structure_item : structure_item printer
314
- val signature : signature printer
315
- val signature_item : signature_item printer
316
- val expression : expression printer
317
- val pattern : pattern printer
318
- val core_type : core_type printer
319
+ type 'a ast_printer
320
+
321
+ val structure : structure ast_printer
322
+ val structure_item : structure_item ast_printer
323
+ val signature : signature ast_printer
324
+ val signature_item : signature_item ast_printer
325
+ val expression : expression ast_printer
326
+ val pattern : pattern ast_printer
327
+ val core_type : core_type ast_printer
319
328
end
320
329
321
330
module type Conf = sig
322
331
val config : Config .t
323
332
end
324
333
325
- module type Configured = S with type 'a printer = 'a configured
326
- module type Configurable = S with type 'a printer = 'a configurable
334
+ module type Configured = S with type 'a ast_printer = 'a configured
335
+ module type Configurable = S with type 'a ast_printer = 'a configurable
327
336
328
337
module Make (Conf : Conf ) : Configured = struct
329
- type 'a printer = 'a configured
338
+ type 'a ast_printer = 'a configured
330
339
331
340
let lsv =
332
- let lift_simple_val = new lift_simple_val in
333
- lift_simple_val #set_config Conf. config;
334
- lift_simple_val
335
-
336
- let structure fmt str = pp_simple_val fmt (lsv#structure str)
337
- let structure_item fmt str = pp_simple_val fmt (lsv#structure_item str)
338
- let signature fmt str = pp_simple_val fmt (lsv#signature str)
339
- let signature_item fmt str = pp_simple_val fmt (lsv#signature_item str)
340
- let expression fmt str = pp_simple_val fmt (lsv#expression str)
341
- let pattern fmt str = pp_simple_val fmt (lsv#pattern str)
342
- let core_type fmt str = pp_simple_val fmt (lsv#core_type str)
341
+ let lift_repr = new lift_repr in
342
+ lift_repr #set_config Conf. config;
343
+ lift_repr
344
+
345
+ let structure fmt str = pp_repr fmt (lsv#structure str)
346
+ let structure_item fmt str = pp_repr fmt (lsv#structure_item str)
347
+ let signature fmt str = pp_repr fmt (lsv#signature str)
348
+ let signature_item fmt str = pp_repr fmt (lsv#signature_item str)
349
+ let expression fmt str = pp_repr fmt (lsv#expression str)
350
+ let pattern fmt str = pp_repr fmt (lsv#pattern str)
351
+ let core_type fmt str = pp_repr fmt (lsv#core_type str)
343
352
end
344
353
345
354
let make config =
@@ -351,25 +360,25 @@ module Default = Make (struct
351
360
let config = Config. default
352
361
end )
353
362
354
- type 'a printer = 'a configurable
363
+ type 'a ast_printer = 'a configurable
355
364
356
- let lift_simple_val = new lift_simple_val
365
+ let lift_repr = new lift_repr
357
366
358
367
let with_config ~config ~f =
359
- let old_config = lift_simple_val #get_config () in
360
- lift_simple_val #set_config config;
368
+ let old_config = lift_repr #get_config () in
369
+ lift_repr #set_config config;
361
370
let res = f () in
362
- lift_simple_val #set_config old_config;
371
+ lift_repr #set_config old_config;
363
372
res
364
373
365
- let pp_with_config (type a ) (lifter : a -> simple_val )
366
- ?( config = Config. default) fmt (x : a ) =
367
- with_config ~config ~f: (fun () -> pp_simple_val fmt (lifter x))
368
-
369
- let structure = pp_with_config lift_simple_val #structure
370
- let structure_item = pp_with_config lift_simple_val #structure_item
371
- let signature = pp_with_config lift_simple_val #signature
372
- let signature_item = pp_with_config lift_simple_val #signature_item
373
- let expression = pp_with_config lift_simple_val #expression
374
- let pattern = pp_with_config lift_simple_val #pattern
375
- let core_type = pp_with_config lift_simple_val #core_type
374
+ let pp_with_config (type a ) (lifter : a -> repr ) ?( config = Config. default) fmt
375
+ (x : a ) =
376
+ with_config ~config ~f: (fun () -> config.printer fmt (lifter x))
377
+
378
+ let structure = pp_with_config lift_repr #structure
379
+ let structure_item = pp_with_config lift_repr #structure_item
380
+ let signature = pp_with_config lift_repr #signature
381
+ let signature_item = pp_with_config lift_repr #signature_item
382
+ let expression = pp_with_config lift_repr #expression
383
+ let pattern = pp_with_config lift_repr #pattern
384
+ let core_type = pp_with_config lift_repr #core_type
0 commit comments