1
1
2
2
3
- type error =
4
- | CannotRun of string
5
- | WrongMagic of string
6
-
7
- exception Error of error
8
3
9
4
(* Optionally preprocess a source file *)
10
5
@@ -15,7 +10,7 @@ let call_external_preprocessor sourcefile pp =
15
10
in
16
11
if Ccomp. command comm <> 0 then begin
17
12
Misc. remove_file tmpfile;
18
- raise ( Error ( CannotRun comm));
13
+ Cmd_ast_exception. cannot_run comm
19
14
end ;
20
15
tmpfile
21
16
@@ -32,122 +27,29 @@ let remove_preprocessed inputfile =
32
27
| Some _ -> Misc. remove_file inputfile
33
28
34
29
35
- let magic_of_kind : type a . a Ml_binary.kind -> string = function
36
- | Ml_binary. Ml -> Config. ast_impl_magic_number
37
- | Ml_binary. Mli -> Config. ast_intf_magic_number
38
30
39
- (* Note: some of the functions here should go to Ast_mapper instead,
40
- which would encapsulate the "binary AST" protocol. *)
41
31
42
- let write_ast (type a ) (kind : a Ml_binary.kind ) fn (ast : a ) =
43
- let oc = open_out_bin fn in
44
- output_string oc (magic_of_kind kind);
45
- output_value oc (! Location. input_name : string );
46
- output_value oc (ast : a );
47
- close_out oc
48
32
49
- let apply_rewriter kind fn_in ppx =
50
- let magic = magic_of_kind kind in
51
- let fn_out = Filename. temp_file " camlppx" " " in
52
- let comm =
53
- Printf. sprintf " %s %s %s" ppx (Filename. quote fn_in) (Filename. quote fn_out)
54
- in
55
- let ok = Ccomp. command comm = 0 in
56
- Misc. remove_file fn_in;
57
- if not ok then begin
58
- Misc. remove_file fn_out;
59
- raise (Error (CannotRun comm));
60
- end ;
61
- if not (Sys. file_exists fn_out) then
62
- raise (Error (WrongMagic comm));
63
- (* check magic before passing to the next ppx *)
64
- let ic = open_in_bin fn_out in
65
- let buffer =
66
- try really_input_string ic (String. length magic) with End_of_file -> " " in
67
- close_in ic;
68
- if buffer <> magic then begin
69
- Misc. remove_file fn_out;
70
- raise (Error (WrongMagic comm));
71
- end ;
72
- fn_out
73
-
74
- let read_ast (type a ) (kind : a Ml_binary.kind ) fn : a =
75
- let ic = open_in_bin fn in
76
- try
77
- let magic = magic_of_kind kind in
78
- let buffer = really_input_string ic (String. length magic) in
79
- assert (buffer = magic); (* already checked by apply_rewriter *)
80
- Location. set_input_name @@ (input_value ic : string );
81
- let ast = (input_value ic : a ) in
82
- close_in ic;
83
- Misc. remove_file fn;
84
- ast
85
- with exn ->
86
- close_in ic;
87
- Misc. remove_file fn;
88
- raise exn
89
-
90
- let rewrite kind ppxs ast =
91
- let fn = Filename. temp_file " camlppx" " " in
92
- write_ast kind fn ast;
93
- let fn = List. fold_left (apply_rewriter kind) fn (List. rev ppxs) in
94
- read_ast kind fn
95
-
96
- let apply_rewriters_str ?(restore = true ) ~tool_name ast =
97
- match ! Clflags. all_ppx with
98
- | [] -> ast
99
- | ppxs ->
100
- ast
101
- |> Ast_mapper. add_ppx_context_str ~tool_name
102
- |> rewrite Ml ppxs
103
- |> Ast_mapper. drop_ppx_context_str ~restore
104
-
105
- let apply_rewriters_sig ?(restore = true ) ~tool_name ast =
106
- match ! Clflags. all_ppx with
107
- | [] -> ast
108
- | ppxs ->
109
- ast
110
- |> Ast_mapper. add_ppx_context_sig ~tool_name
111
- |> rewrite Mli ppxs
112
- |> Ast_mapper. drop_ppx_context_sig ~restore
113
-
114
- let apply_rewriters ?restore ~tool_name
115
- (type a ) (kind : a Ml_binary.kind ) (ast : a ) : a =
116
- match kind with
117
- | Ml_binary. Ml ->
118
- apply_rewriters_str ?restore ~tool_name ast
119
- | Ml_binary. Mli ->
120
- apply_rewriters_sig ?restore ~tool_name ast
121
33
122
34
(* Parse a file or get a dumped syntax tree from it *)
123
35
124
- exception Outdated_version
125
-
126
- let open_and_check_magic inputfile ast_magic =
127
- let ic = open_in_bin inputfile in
128
- let is_ast_file =
129
- try
130
- let buffer = really_input_string ic (String. length ast_magic) in
131
- if buffer = ast_magic then true
132
- else if String. sub buffer 0 9 = String. sub ast_magic 0 9 then
133
- raise Outdated_version
134
- else false
135
- with
136
- Outdated_version ->
137
- Misc. fatal_error " OCaml and preprocessor have incompatible versions"
138
- | _ -> false
139
- in
140
- (ic, is_ast_file)
141
-
142
36
let parse (type a ) (kind : a Ml_binary.kind ) lexbuf : a =
143
37
match kind with
144
38
| Ml_binary. Ml -> Parse. implementation lexbuf
145
39
| Ml_binary. Mli -> Parse. interface lexbuf
146
40
147
41
let file_aux ppf inputfile (type a ) (parse_fun : _ -> a )
148
42
(kind : a Ml_binary.kind ) : a =
149
- let ast_magic = magic_of_kind kind in
150
- let (ic, is_ast_file) = open_and_check_magic inputfile ast_magic in
43
+ let ast_magic = Ml_binary. magic_of_kind kind in
44
+ let ic = open_in_bin inputfile in
45
+ let is_ast_file =
46
+ match really_input_string ic (String. length ast_magic) with
47
+ | exception _ -> false
48
+ | buffer ->
49
+ if buffer = ast_magic then true
50
+ else if Ext_string. starts_with buffer " Caml1999" then
51
+ Cmd_ast_exception. wrong_magic buffer
52
+ else false in
151
53
let ast =
152
54
try
153
55
if is_ast_file then begin
@@ -169,20 +71,7 @@ let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
169
71
170
72
171
73
172
- let report_error ppf = function
173
- | CannotRun cmd ->
174
- Format. fprintf ppf " Error while running external preprocessor@.\
175
- Command line: %s@." cmd
176
- | WrongMagic cmd ->
177
- Format. fprintf ppf " External preprocessor does not produce a valid file@.\
178
- Command line: %s@." cmd
179
-
180
- let () =
181
- Location. register_error_of_exn
182
- (function
183
- | Error err -> Some (Location. error_of_printer_file report_error err)
184
- | _ -> None
185
- )
74
+
186
75
187
76
let parse_file kind ppf sourcefile =
188
77
Location. set_input_name sourcefile;
@@ -200,8 +89,8 @@ let parse_file kind ppf sourcefile =
200
89
201
90
202
91
let parse_implementation ppf ~tool_name sourcefile =
203
- apply_rewriters ~restore: false ~tool_name Ml (parse_file
92
+ Cmd_ppx_apply. apply_rewriters ~restore: false ~tool_name Ml (parse_file
204
93
Ml ppf sourcefile)
205
94
let parse_interface ppf ~tool_name sourcefile =
206
- apply_rewriters ~restore: false ~tool_name Mli (parse_file
95
+ Cmd_ppx_apply. apply_rewriters ~restore: false ~tool_name Mli (parse_file
207
96
Mli ppf sourcefile)
0 commit comments