24
24
(* Note: some of the functions here should go to Ast_mapper instead,
25
25
which would encapsulate the "binary AST" protocol. *)
26
26
27
- let write_ast ( type a ) ( kind : a Ml_binary.kind ) fn ( ast : a ) =
27
+ let write_ast fn ( ast0 : Ml_binary.ast0 ) =
28
28
let oc = open_out_bin fn in
29
- output_string oc (Ml_binary. magic_of_kind kind );
29
+ output_string oc (Ml_binary. magic_of_ast0 ast0 );
30
30
output_value oc (! Location. input_name : string );
31
- output_value oc (ast : a );
31
+ (match ast0 with
32
+ | Ml_binary. Impl ast -> output_value oc (ast : Parsetree0.structure )
33
+ | Ml_binary. Intf ast -> output_value oc (ast : Parsetree0.signature ));
32
34
close_out oc
33
35
34
36
let temp_ppx_file () =
@@ -53,25 +55,29 @@ let apply_rewriter kind fn_in ppx =
53
55
fn_out
54
56
55
57
(* This is a fatal error, no need to protect it *)
56
- let read_ast (type a ) (kind : a Ml_binary.kind ) fn : a =
58
+ let read_ast (type a ) (kind : a Ml_binary.kind ) fn : Ml_binary.ast0 =
57
59
let ic = open_in_bin fn in
58
60
let magic = Ml_binary. magic_of_kind kind in
59
61
let buffer = really_input_string ic (String. length magic) in
60
62
assert (buffer = magic);
61
63
(* already checked by apply_rewriter *)
62
64
Location. set_input_name @@ (input_value ic : string );
63
- let ast = (input_value ic : a ) in
65
+ let ast0 =
66
+ match kind with
67
+ | Ml_binary. Ml -> Ml_binary. Impl (input_value ic : Parsetree0.structure )
68
+ | Ml_binary. Mli -> Ml_binary. Intf (input_value ic : Parsetree0.signature )
69
+ in
64
70
close_in ic;
65
-
66
- ast
71
+ ast0
67
72
68
73
(* * [ppxs] are a stack,
69
74
[-ppx1 -ppx2 -ppx3]
70
75
are stored as [-ppx3; -ppx2; -ppx1]
71
76
[fold_right] happens to process the first one *)
72
77
let rewrite kind ppxs ast =
73
78
let fn_in = temp_ppx_file () in
74
- write_ast kind fn_in ast;
79
+ let ast0 = Ml_binary. to_ast0 kind ast in
80
+ write_ast fn_in ast0;
75
81
let temp_files =
76
82
List. fold_right
77
83
(fun ppx fns ->
@@ -93,7 +99,7 @@ let apply_rewriters_str ?(restore = true) ~tool_name ast =
93
99
| ppxs ->
94
100
ast
95
101
|> Ast_mapper. add_ppx_context_str ~tool_name
96
- |> rewrite Ml ppxs
102
+ |> rewrite Ml ppxs |> Ml_binary. ast0_to_structure
97
103
|> Ast_mapper. drop_ppx_context_str ~restore
98
104
99
105
let apply_rewriters_sig ?(restore = true ) ~tool_name ast =
@@ -102,7 +108,7 @@ let apply_rewriters_sig ?(restore = true) ~tool_name ast =
102
108
| ppxs ->
103
109
ast
104
110
|> Ast_mapper. add_ppx_context_sig ~tool_name
105
- |> rewrite Mli ppxs
111
+ |> rewrite Mli ppxs |> Ml_binary. ast0_to_signature
106
112
|> Ast_mapper. drop_ppx_context_sig ~restore
107
113
108
114
let apply_rewriters ?restore ~tool_name (type a ) (kind : a Ml_binary.kind )
0 commit comments