1
+ open Analysis
2
+
1
3
(* * Transform the AST types to the more generic Oak format *)
2
4
module Oak = struct
3
5
type application = {name : string ; argument : oak }
@@ -10,12 +12,39 @@ module Oak = struct
10
12
| Ident of string
11
13
| Tuple of namedField list
12
14
| List of oak list
15
+ | String of string
16
+ let mk_bool (b : bool ) : oak = if b then Ident " true" else Ident " false"
17
+
18
+ let mk_string_option (o : string option ) : oak =
19
+ match o with
20
+ | None -> Ident " None"
21
+ | Some s -> Application {name = " Some" ; argument = String s}
22
+
23
+ let mk_string_list (items : string list ) : oak =
24
+ List (items |> List. map (fun s -> String s))
25
+
26
+ let path_to_string path =
27
+ let buf = Buffer. create 64 in
28
+ let rec aux = function
29
+ | Path. Pident id -> Buffer. add_string buf (Ident. name id)
30
+ | Path. Pdot (p , s , _ ) ->
31
+ aux p;
32
+ Buffer. add_char buf '.' ;
33
+ Buffer. add_string buf s
34
+ | Path. Papply (p1 , p2 ) ->
35
+ aux p1;
36
+ Buffer. add_char buf '(' ;
37
+ aux p2;
38
+ Buffer. add_char buf ')'
39
+ in
40
+ aux path;
41
+ Buffer. contents buf
13
42
14
- let rec path_to_string = function
15
- | Path. Pident id -> Ident. name id
16
- | Path. Pdot ( p , s , _ ) -> path_to_string p ^ " . " ^ s
17
- | Path. Papply ( p1 , p2 ) -> path_to_string p1 ^ " ( " ^ path_to_string p2 ^ " ) "
18
-
43
+ let mk_row_field ( row_field : Types.row_field ) : oak =
44
+ match row_field with
45
+ | Rpresent _ -> Ident " row_field.Rpresent "
46
+ | Reither _ -> Ident " row_field.Reither "
47
+ | Rabsent -> Ident " row_field.Rabsent "
19
48
let rec mk_type_desc (desc : Types.type_desc ) : oak =
20
49
match desc with
21
50
| Tvar var -> (
@@ -103,13 +132,57 @@ module Oak = struct
103
132
}
104
133
:: fields)
105
134
106
- and mk_row_field (row_field : Types.row_field ) : oak =
107
- match row_field with
108
- | Rpresent _ -> Ident " row_field.Rpresent"
109
- | Reither _ -> Ident " row_field.Reither"
110
- | Rabsent -> Ident " row_field.Rabsent"
135
+ let mk_package (package : SharedTypes.package ) : oak =
136
+ Record
137
+ [
138
+ {
139
+ name = " genericJsxModule" ;
140
+ value = mk_string_option package.genericJsxModule;
141
+ };
142
+ ]
111
143
112
- and mk_bool (b : bool ) : oak = if b then Ident " true" else Ident " false"
144
+ let mk_Uri (uri : Uri.t ) : oak = String (Uri. toString uri)
145
+
146
+ let mk_item (item : SharedTypes.Module.item ) : oak =
147
+ let kind =
148
+ match item.kind with
149
+ | SharedTypes.Module. Value v ->
150
+ Application
151
+ {name = " SharedTypes.Module.Value" ; argument = mk_type_desc v.desc}
152
+ | SharedTypes.Module. Type _ -> Ident " Type"
153
+ | SharedTypes.Module. Module _ -> Ident " Module"
154
+ in
155
+ Record
156
+ [
157
+ {name = " kind" ; value = kind};
158
+ {name = " name" ; value = String item.name};
159
+ {name = " docstring" ; value = mk_string_list item.docstring};
160
+ {name = " deprecated" ; value = mk_string_option item.deprecated};
161
+ ]
162
+
163
+ let mk_structure (structure : SharedTypes.Module.structure ) : oak =
164
+ Record
165
+ [
166
+ {name = " name" ; value = String structure.name};
167
+ {name = " docstring" ; value = mk_string_list structure.docstring};
168
+ {name = " items" ; value = List (List. map mk_item structure.items)};
169
+ {name = " deprecated" ; value = mk_string_option structure.deprecated};
170
+ ]
171
+
172
+ let mk_file (file : SharedTypes.File.t ) : oak =
173
+ Record
174
+ [
175
+ {name = " uri" ; value = mk_Uri file.uri};
176
+ {name = " moduleName" ; value = String file.moduleName};
177
+ {name = " structure" ; value = mk_structure file.structure};
178
+ ]
179
+
180
+ let mk_full (full : SharedTypes.full ) : oak =
181
+ Record
182
+ [
183
+ {name = " package" ; value = mk_package full.package};
184
+ {name = " file" ; value = mk_file full.file};
185
+ ]
113
186
end
114
187
115
188
(* * Transform the Oak types to string *)
@@ -231,23 +304,31 @@ module CodePrinter = struct
231
304
232
305
(* * Fold all the events in context into text *)
233
306
let dump (ctx : context ) =
234
- let addSpaces n = String. make n ' ' in
307
+ let buf = Buffer. create 1024 in
308
+ let addSpaces n = Buffer. add_string buf (String. make n ' ' ) in
235
309
236
310
List. fold_right
237
- (fun event ( acc , current_indent ) ->
311
+ (fun event current_indent ->
238
312
match event with
239
- | Write str -> (acc ^ str, current_indent)
240
- | WriteLine -> (acc ^ " \n " ^ addSpaces current_indent, current_indent)
241
- | IndentBy n -> (acc, current_indent + n)
242
- | UnindentBy n -> (acc, current_indent - n))
243
- ctx.events (" " , 0 )
244
- |> fst
313
+ | Write str ->
314
+ Buffer. add_string buf str;
315
+ current_indent
316
+ | WriteLine ->
317
+ Buffer. add_char buf '\n' ;
318
+ addSpaces current_indent;
319
+ current_indent
320
+ | IndentBy n -> current_indent + n
321
+ | UnindentBy n -> current_indent - n)
322
+ ctx.events ctx.current_indent
323
+ |> ignore;
324
+ Buffer. contents buf
245
325
246
326
let rec genOak (oak : Oak.oak ) : appendEvents =
247
327
match oak with
248
328
| Oak. Application application -> genApplication application
249
329
| Oak. Record record -> genRecord record
250
330
| Oak. Ident ident -> genIdent ident
331
+ | Oak. String str -> ! - (Format. sprintf " \" %s\" " str)
251
332
| Oak. Tuple ts -> genTuple ts
252
333
| Oak. List xs -> genList xs
253
334
@@ -268,9 +349,12 @@ module CodePrinter = struct
268
349
269
350
and genRecord (recordFields : Oak.namedField list ) : appendEvents =
270
351
let short =
271
- sepOpenR +> sepSpace
272
- +> col genNamedField sepSemi recordFields
273
- +> sepSpace +> sepCloseR
352
+ match recordFields with
353
+ | [] -> sepOpenR +> sepCloseR
354
+ | fields ->
355
+ sepOpenR +> sepSpace
356
+ +> col genNamedField sepSemi fields
357
+ +> sepSpace +> sepCloseR
274
358
in
275
359
let long =
276
360
sepOpenR
@@ -292,7 +376,7 @@ module CodePrinter = struct
292
376
! - (field.name) +> sepEq
293
377
+>
294
378
match field.value with
295
- | Oak. List _ -> genOak field.value
379
+ | Oak. List _ | Oak. Record _ -> genOak field.value
296
380
| _ -> indentAndNln (genOak field.value)
297
381
in
298
382
expressionFitsOnRestOfLine short long
318
402
let print_type_expr (typ : Types.type_expr ) : string =
319
403
CodePrinter. genOak (Oak. mk_type_desc typ.desc) CodePrinter. emptyContext
320
404
|> CodePrinter. dump
405
+
406
+ let print_full (full : SharedTypes.full ) : string =
407
+ CodePrinter. genOak (Oak. mk_full full) CodePrinter. emptyContext
408
+ |> CodePrinter. dump
0 commit comments