@@ -5,13 +5,21 @@ open Analysis
5
5
module Transform = struct
6
6
let mk_bool (b : bool ) : oak = if b then Ident " true" else Ident " false"
7
7
8
- let mk_string_option ( o : string option ) : oak =
8
+ let mk_option f o =
9
9
match o with
10
10
| None -> Ident " None"
11
- | Some s -> Application (" Some" , String s)
11
+ | Some x -> Application (" Some" , f x)
12
+
13
+ let mk_string_option (o : string option ) : oak =
14
+ mk_option (fun s -> String s) o
15
+
16
+ let mk_list f l = List (List. map f l)
12
17
13
18
let mk_string_list (items : string list ) : oak =
14
- List (items |> List. map (fun s -> String s))
19
+ mk_list (fun s -> String s) items
20
+
21
+ let mk_int_list (items : int list ) : oak =
22
+ mk_list (fun i -> Ident (string_of_int i)) items
15
23
16
24
let path_to_string path =
17
25
let buf = Buffer. create 64 in
@@ -44,20 +52,14 @@ module Transform = struct
44
52
let rec mk_type_desc (desc : Types.type_desc ) : oak =
45
53
match desc with
46
54
| Tlink {desc} -> Application (" type_desc.Tlink" , mk_type_desc desc)
47
- | Tvar var -> (
48
- match var with
49
- | None -> Application (" type_desc.Tvar" , Ident " None" )
50
- | Some s -> Application (" type_desc.Tvar" , Ident s))
55
+ | Tvar var -> Application (" type_desc.Tvar" , mk_string_option var)
51
56
| Tconstr (path , ts , _ ) ->
52
- let ts =
53
- ts |> List. map (fun (t : Types.type_expr ) -> mk_type_desc t.desc)
54
- in
55
57
Application
56
58
( " type_desc.Tconstr" ,
57
59
Tuple
58
60
[
59
61
{name = " path" ; value = Ident (path_to_string path)};
60
- {name = " ts" ; value = List ts};
62
+ {name = " ts" ; value = mk_type_expr_list ts};
61
63
] )
62
64
| Tarrow (_ , t1 , t2 , _ ) ->
63
65
Application
@@ -67,11 +69,7 @@ module Transform = struct
67
69
{name = " t1" ; value = mk_type_desc t1.desc};
68
70
{name = " t2" ; value = mk_type_desc t2.desc};
69
71
] )
70
- | Ttuple ts ->
71
- let ts =
72
- ts |> List. map (fun (t : Types.type_expr ) -> mk_type_desc t.desc)
73
- in
74
- Application (" type_desc.Ttuple" , List ts)
72
+ | Ttuple ts -> Application (" type_desc.Ttuple" , mk_type_expr_list ts)
75
73
| Tobject (t , r ) -> (
76
74
match ! r with
77
75
| None -> Application (" type_desc.Tobject" , mk_type_desc t.desc)
@@ -107,15 +105,12 @@ module Transform = struct
107
105
Application (" type_desc.Tvariant" , mk_row_desc row_descr)
108
106
| Tunivar so -> Application (" type_desc.Tunivar" , mk_string_option so)
109
107
| Tpoly (t , ts ) ->
110
- let ts =
111
- ts |> List. map (fun (t : Types.type_expr ) -> mk_type_desc t.desc)
112
- in
113
108
Application
114
109
( " type_desc.Tpoly" ,
115
110
Tuple
116
111
[
117
112
{name = " t" ; value = mk_type_desc t.desc};
118
- {name = " ts" ; value = List ts};
113
+ {name = " ts" ; value = mk_type_expr_list ts};
119
114
] )
120
115
| Tpackage (path , lids , ts ) ->
121
116
let lids =
@@ -124,16 +119,13 @@ module Transform = struct
124
119
List
125
120
(Longident. flatten lid |> List. map (fun ident -> String ident)))
126
121
in
127
- let ts =
128
- ts |> List. map (fun (t : Types.type_expr ) -> mk_type_desc t.desc)
129
- in
130
122
Application
131
123
( " type_desc.Tpackage" ,
132
124
Tuple
133
125
[
134
126
{name = " path" ; value = Ident (path_to_string path)};
135
127
{name = " lids" ; value = List lids};
136
- {name = " ts" ; value = List ts};
128
+ {name = " ts" ; value = mk_type_expr_list ts};
137
129
] )
138
130
139
131
and mk_row_desc (row_desc : Types.row_desc ) : oak =
@@ -178,6 +170,9 @@ module Transform = struct
178
170
}
179
171
:: fields)
180
172
173
+ and mk_type_expr_list ts =
174
+ List (List. map (fun (t : Types.type_expr ) -> mk_type_desc t.desc) ts)
175
+
181
176
let mk_FileSet (fileSet : SharedTypes.FileSet.t ) : oak =
182
177
List (fileSet |> SharedTypes.FileSet. to_list |> List. map (fun s -> String s))
183
178
@@ -262,12 +257,182 @@ module Transform = struct
262
257
263
258
let mk_Uri (uri : Uri.t ) : oak = String (Uri. toString uri)
264
259
260
+ let mk_rec_status = function
261
+ | Types. Trec_not -> Ident " Trec_not"
262
+ | Types. Trec_first -> Ident " Trec_first"
263
+ | Types. Trec_next -> Ident " Trec_next"
264
+
265
+ let mk_field (field : SharedTypes.field ) : oak =
266
+ Record
267
+ [
268
+ {name = " stamp" ; value = Ident (string_of_int field.stamp)};
269
+ {name = " fname" ; value = String field.fname.txt};
270
+ {name = " typ" ; value = mk_type_desc field.typ.desc};
271
+ {name = " optional" ; value = mk_bool field.optional};
272
+ {name = " docstring" ; value = mk_string_list field.docstring};
273
+ {name = " deprecated" ; value = mk_string_option field.deprecated};
274
+ ]
275
+
276
+ let mk_pos (pos : Lexing.position ) : oak =
277
+ Record
278
+ [
279
+ {name = " pos_fname" ; value = String pos.pos_fname};
280
+ {name = " pos_lnum" ; value = Ident (string_of_int pos.pos_lnum)};
281
+ {name = " pos_bol" ; value = Ident (string_of_int pos.pos_bol)};
282
+ {name = " pos_cnum" ; value = Ident (string_of_int pos.pos_cnum)};
283
+ ]
284
+
285
+ let mk_location (loc : Location.t ) =
286
+ Record
287
+ [
288
+ {name = " loc_start" ; value = mk_pos loc.loc_start};
289
+ {name = " loc_end" ; value = mk_pos loc.loc_end};
290
+ {name = " loc_ghost" ; value = mk_bool loc.loc_ghost};
291
+ ]
292
+
293
+ let mk_string_loc (loc : string Location.loc ) : oak =
294
+ Record
295
+ [
296
+ {name = " txt" ; value = String loc.txt};
297
+ {name = " loc" ; value = mk_location loc.loc};
298
+ ]
299
+
300
+ let mk_constructor_args (args : SharedTypes.constructorArgs ) : oak =
301
+ match args with
302
+ | SharedTypes. InlineRecord fields ->
303
+ Application
304
+ (" constructorArgs.InlineRecord" , List (fields |> List. map mk_field))
305
+ | SharedTypes. Args ts ->
306
+ let ts =
307
+ ts
308
+ |> List. map (fun ((t : Types.type_expr ), loc ) ->
309
+ Tuple
310
+ [
311
+ {name = " type" ; value = mk_type_desc t.desc};
312
+ {name = " loc" ; value = mk_location loc};
313
+ ])
314
+ in
315
+ Application (" constructorArgs.Tuple" , List ts)
316
+
317
+ let mk_constructor (ctor : SharedTypes.Constructor.t ) : oak =
318
+ Record
319
+ [
320
+ {name = " stamp" ; value = Ident (string_of_int ctor.stamp)};
321
+ {
322
+ name = " cname" ;
323
+ value =
324
+ Record
325
+ [
326
+ {name = " txt" ; value = String ctor.cname.txt};
327
+ {name = " loc" ; value = mk_location ctor.cname.loc};
328
+ ];
329
+ };
330
+ {name = " args" ; value = mk_constructor_args ctor.args};
331
+ {name = " docstring" ; value = mk_string_list ctor.docstring};
332
+ {name = " deprecated" ; value = mk_string_option ctor.deprecated};
333
+ ]
334
+ let mk_attribute_payload (payload : Parsetree.payload ) : oak =
335
+ match payload with
336
+ | PStr _ -> Ident " payload.PStr"
337
+ | PSig _ -> Ident " payload.PSig"
338
+ | PTyp _ -> Ident " payload.PTyp"
339
+ | PPat _ -> Ident " payload.PPat"
340
+
341
+ let mk_attribute (attribute : Parsetree.attribute ) : oak =
342
+ let loc, payload = attribute in
343
+ Tuple
344
+ [
345
+ {name = " loc" ; value = mk_string_loc loc};
346
+ {name = " payload" ; value = mk_attribute_payload payload};
347
+ ]
348
+
349
+ let mk_attribute_list (attributes : Parsetree.attributes ) =
350
+ List (attributes |> List. map mk_attribute)
351
+
352
+ let mk_type_kind (kind : SharedTypes.Type.kind ) : oak =
353
+ match kind with
354
+ | SharedTypes.Type. Abstract _ -> Ident " Type.kind.Abstract"
355
+ | SharedTypes.Type. Open -> Ident " Type.kind.Open"
356
+ | SharedTypes.Type. Tuple ts ->
357
+ Application (" Type.kind.Tuple" , mk_type_expr_list ts)
358
+ | SharedTypes.Type. Record fields ->
359
+ let fields = List. map mk_field fields in
360
+ Application (" Type.kind.Record" , List fields)
361
+ | SharedTypes.Type. Variant ctors ->
362
+ Application (" Type.kind.Variant" , List (ctors |> List. map mk_constructor))
363
+
364
+ let mk_type_declaration_type_kind (type_kind : Types.type_kind ) : oak =
365
+ match type_kind with
366
+ | Type_abstract -> Ident " type_kind.Type_abstract"
367
+ | Type_variant _ -> Ident " type_kind.Type_variant"
368
+ | Type_record _ -> Ident " type_kind.Type_record"
369
+ | Type_open -> Ident " type_kind.Type_open"
370
+
371
+ let mk_private_flag = function
372
+ | Asttypes. Private -> Ident " Private"
373
+ | Asttypes. Public -> Ident " Public"
374
+
375
+ let mk_unboxed_status (status : Types.unboxed_status ) : oak =
376
+ Record
377
+ [
378
+ {name = " unboxed" ; value = mk_bool status.unboxed};
379
+ {name = " default" ; value = mk_bool status.default};
380
+ ]
381
+
382
+ let mk_type_declaration (td : Types.type_declaration ) : oak =
383
+ Record
384
+ [
385
+ {name = " type_params" ; value = mk_type_expr_list td.type_params};
386
+ {name = " type_arity" ; value = Ident (string_of_int td.type_arity)};
387
+ {name = " type_kind" ; value = mk_type_declaration_type_kind td.type_kind};
388
+ {name = " type_private" ; value = mk_private_flag td.type_private};
389
+ {
390
+ name = " type_manifest" ;
391
+ value =
392
+ mk_option
393
+ (fun (te : Types.type_expr ) -> mk_type_desc te.desc)
394
+ td.type_manifest;
395
+ };
396
+ {
397
+ name = " type_newtype_level" ;
398
+ value =
399
+ mk_option
400
+ (fun (i1 , i2 ) ->
401
+ Tuple
402
+ [
403
+ {name = " i1" ; value = Ident (string_of_int i1)};
404
+ {name = " i2" ; value = Ident (string_of_int i2)};
405
+ ])
406
+ td.type_newtype_level;
407
+ };
408
+ {name = " type_loc" ; value = mk_location td.type_loc};
409
+ {name = " type_attributes" ; value = mk_attribute_list td.type_attributes};
410
+ {name = " type_immediate" ; value = mk_bool td.type_immediate};
411
+ {name = " type_unboxed" ; value = mk_unboxed_status td.type_unboxed};
412
+ ]
413
+
414
+ let mk_type (type_ : SharedTypes.Type.t ) : oak =
415
+ Record
416
+ [
417
+ {name = " kind" ; value = mk_type_kind type_.kind};
418
+ {name = " decl" ; value = mk_type_declaration type_.decl};
419
+ {name = " name" ; value = String type_.name};
420
+ {name = " attributes" ; value = mk_attribute_list type_.attributes};
421
+ ]
422
+
265
423
let mk_item (item : SharedTypes.Module.item ) : oak =
266
424
let kind =
267
425
match item.kind with
268
426
| SharedTypes.Module. Value v ->
269
427
Application (" SharedTypes.Module.Value" , mk_type_desc v.desc)
270
- | SharedTypes.Module. Type _ -> Ident " Type"
428
+ | SharedTypes.Module. Type (t , rec_status ) ->
429
+ Application
430
+ ( " Type" ,
431
+ Tuple
432
+ [
433
+ {name = " type" ; value = mk_type t};
434
+ {name = " rec_status" ; value = mk_rec_status rec_status};
435
+ ] )
271
436
| SharedTypes.Module. Module _ -> Ident " Module"
272
437
in
273
438
Record
0 commit comments