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
@@ -306,44 +316,43 @@ class lift_simple_val =
306
316
| NoInjectivity -> Constr (" NoInjectivity" , [] )
307
317
end
308
318
309
- type 'a pp = Format .formatter -> 'a -> unit
310
319
type 'a configurable = ?config:Config .t -> 'a pp
311
320
type 'a configured = 'a pp
312
321
313
322
module type S = sig
314
- type 'a printer
315
-
316
- val structure : structure printer
317
- val structure_item : structure_item printer
318
- val signature : signature printer
319
- val signature_item : signature_item printer
320
- val expression : expression printer
321
- val pattern : pattern printer
322
- val core_type : core_type printer
323
+ type 'a ast_printer
324
+
325
+ val structure : structure ast_printer
326
+ val structure_item : structure_item ast_printer
327
+ val signature : signature ast_printer
328
+ val signature_item : signature_item ast_printer
329
+ val expression : expression ast_printer
330
+ val pattern : pattern ast_printer
331
+ val core_type : core_type ast_printer
323
332
end
324
333
325
334
module type Conf = sig
326
335
val config : Config .t
327
336
end
328
337
329
- module type Configured = S with type 'a printer = 'a configured
330
- module type Configurable = S with type 'a printer = 'a configurable
338
+ module type Configured = S with type 'a ast_printer = 'a configured
339
+ module type Configurable = S with type 'a ast_printer = 'a configurable
331
340
332
341
module Make (Conf : Conf ) : Configured = struct
333
- type 'a printer = 'a configured
342
+ type 'a ast_printer = 'a configured
334
343
335
344
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)
347
356
end
348
357
349
358
let make config =
@@ -355,25 +364,25 @@ module Default = Make (struct
355
364
let config = Config. default
356
365
end )
357
366
358
- type 'a printer = 'a configurable
367
+ type 'a ast_printer = 'a configurable
359
368
360
- let lift_simple_val = new lift_simple_val
369
+ let lift_repr = new lift_repr
361
370
362
371
let 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;
365
374
let res = f () in
366
- lift_simple_val #set_config old_config;
375
+ lift_repr #set_config old_config;
367
376
res
368
377
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