2002-02-08 02:14:31 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
2002-02-08 02:14:31 -08:00
|
|
|
(* *)
|
|
|
|
(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2002 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
open Format
|
|
|
|
|
2013-01-25 01:12:31 -08:00
|
|
|
type error =
|
|
|
|
| CannotRun of string
|
|
|
|
| WrongMagic of string
|
|
|
|
|
2013-09-12 06:41:01 -07:00
|
|
|
exception Error of error
|
2002-02-08 02:14:31 -08:00
|
|
|
|
|
|
|
(* Optionally preprocess a source file *)
|
|
|
|
|
|
|
|
let preprocess sourcefile =
|
|
|
|
match !Clflags.preprocessor with
|
|
|
|
None -> sourcefile
|
|
|
|
| Some pp ->
|
2012-07-26 12:21:54 -07:00
|
|
|
let tmpfile = Filename.temp_file "ocamlpp" "" in
|
2004-06-16 09:58:46 -07:00
|
|
|
let comm = Printf.sprintf "%s %s > %s"
|
|
|
|
pp (Filename.quote sourcefile) tmpfile
|
|
|
|
in
|
2002-02-08 02:14:31 -08:00
|
|
|
if Ccomp.command comm <> 0 then begin
|
|
|
|
Misc.remove_file tmpfile;
|
2013-09-12 06:41:01 -07:00
|
|
|
raise (Error (CannotRun comm));
|
2002-02-08 02:14:31 -08:00
|
|
|
end;
|
|
|
|
tmpfile
|
|
|
|
|
|
|
|
let remove_preprocessed inputfile =
|
|
|
|
match !Clflags.preprocessor with
|
|
|
|
None -> ()
|
|
|
|
| Some _ -> Misc.remove_file inputfile
|
|
|
|
|
2012-06-13 01:00:27 -07:00
|
|
|
let write_ast magic ast =
|
|
|
|
let fn = Filename.temp_file "camlppx" "" in
|
|
|
|
let oc = open_out_bin fn in
|
|
|
|
output_string oc magic;
|
|
|
|
output_value oc !Location.input_name;
|
|
|
|
output_value oc ast;
|
|
|
|
close_out oc;
|
|
|
|
fn
|
|
|
|
|
2013-01-25 01:12:31 -08:00
|
|
|
let apply_rewriter magic fn_in ppx =
|
2012-06-13 01:00:27 -07:00
|
|
|
let fn_out = Filename.temp_file "camlppx" "" in
|
2013-03-18 13:13:53 -07:00
|
|
|
let comm =
|
|
|
|
Printf.sprintf "%s %s %s" ppx (Filename.quote fn_in) (Filename.quote fn_out)
|
|
|
|
in
|
2012-06-13 01:00:27 -07:00
|
|
|
let ok = Ccomp.command comm = 0 in
|
|
|
|
Misc.remove_file fn_in;
|
|
|
|
if not ok then begin
|
|
|
|
Misc.remove_file fn_out;
|
2013-09-12 06:41:01 -07:00
|
|
|
raise (Error (CannotRun comm));
|
2013-01-25 01:12:31 -08:00
|
|
|
end;
|
2013-09-12 05:27:33 -07:00
|
|
|
if not (Sys.file_exists fn_out) then
|
2013-09-12 06:41:01 -07:00
|
|
|
raise (Error (WrongMagic comm));
|
2013-01-25 01:12:31 -08:00
|
|
|
(* check magic before passing to the next ppx *)
|
|
|
|
let ic = open_in_bin fn_out in
|
2013-03-18 13:13:53 -07:00
|
|
|
let buffer =
|
|
|
|
try Misc.input_bytes ic (String.length magic) with End_of_file -> "" in
|
2013-01-25 01:12:31 -08:00
|
|
|
close_in ic;
|
|
|
|
if buffer <> magic then begin
|
|
|
|
Misc.remove_file fn_out;
|
2013-09-12 06:41:01 -07:00
|
|
|
raise (Error (WrongMagic comm));
|
2012-06-13 01:00:27 -07:00
|
|
|
end;
|
|
|
|
fn_out
|
|
|
|
|
|
|
|
let read_ast magic fn =
|
|
|
|
let ic = open_in_bin fn in
|
|
|
|
try
|
|
|
|
let buffer = Misc.input_bytes ic (String.length magic) in
|
2013-01-25 01:12:31 -08:00
|
|
|
assert(buffer = magic); (* already checked by apply_rewriter *)
|
2012-06-13 01:00:27 -07:00
|
|
|
Location.input_name := input_value ic;
|
|
|
|
let ast = input_value ic in
|
|
|
|
close_in ic;
|
|
|
|
Misc.remove_file fn;
|
|
|
|
ast
|
|
|
|
with exn ->
|
|
|
|
close_in ic;
|
|
|
|
Misc.remove_file fn;
|
|
|
|
raise exn
|
|
|
|
|
2013-01-25 01:12:31 -08:00
|
|
|
let apply_rewriters magic ast =
|
2013-06-05 09:34:40 -07:00
|
|
|
match !Clflags.all_ppx with
|
2013-01-25 01:12:31 -08:00
|
|
|
| [] -> ast
|
|
|
|
| ppxs ->
|
2013-03-18 13:13:53 -07:00
|
|
|
let fn =
|
2013-09-10 07:15:47 -07:00
|
|
|
List.fold_left (apply_rewriter magic) (write_ast magic ast)
|
|
|
|
(List.rev ppxs)
|
|
|
|
in
|
2013-01-25 01:12:31 -08:00
|
|
|
read_ast magic fn
|
2012-06-13 01:00:27 -07:00
|
|
|
|
2013-03-18 13:13:53 -07:00
|
|
|
(* Parse a file or get a dumped syntax tree from it *)
|
|
|
|
|
|
|
|
exception Outdated_version
|
|
|
|
|
2012-01-20 06:23:34 -08:00
|
|
|
let file ppf inputfile parse_fun ast_magic =
|
2002-02-08 02:14:31 -08:00
|
|
|
let ic = open_in_bin inputfile in
|
|
|
|
let is_ast_file =
|
|
|
|
try
|
2012-02-23 11:54:44 -08:00
|
|
|
let buffer = Misc.input_bytes ic (String.length ast_magic) in
|
2002-02-08 02:14:31 -08:00
|
|
|
if buffer = ast_magic then true
|
|
|
|
else if String.sub buffer 0 9 = String.sub ast_magic 0 9 then
|
|
|
|
raise Outdated_version
|
|
|
|
else false
|
|
|
|
with
|
|
|
|
Outdated_version ->
|
2012-02-13 09:48:41 -08:00
|
|
|
Misc.fatal_error "OCaml and preprocessor have incompatible versions"
|
2002-02-08 02:14:31 -08:00
|
|
|
| _ -> false
|
|
|
|
in
|
|
|
|
let ast =
|
|
|
|
try
|
|
|
|
if is_ast_file then begin
|
|
|
|
if !Clflags.fast then
|
2013-03-18 13:13:53 -07:00
|
|
|
(* FIXME make this a proper warning *)
|
2012-01-20 06:23:34 -08:00
|
|
|
fprintf ppf "@[Warning: %s@]@."
|
2002-02-08 02:14:31 -08:00
|
|
|
"option -unsafe used with a preprocessor returning a syntax tree";
|
|
|
|
Location.input_name := input_value ic;
|
|
|
|
input_value ic
|
|
|
|
end else begin
|
|
|
|
seek_in ic 0;
|
|
|
|
Location.input_name := inputfile;
|
2002-11-01 09:06:47 -08:00
|
|
|
let lexbuf = Lexing.from_channel ic in
|
|
|
|
Location.init lexbuf inputfile;
|
|
|
|
parse_fun lexbuf
|
2002-02-08 02:14:31 -08:00
|
|
|
end
|
|
|
|
with x -> close_in ic; raise x
|
|
|
|
in
|
|
|
|
close_in ic;
|
2013-01-25 01:12:31 -08:00
|
|
|
apply_rewriters ast_magic ast
|
|
|
|
|
|
|
|
let report_error ppf = function
|
|
|
|
| CannotRun cmd ->
|
2013-03-18 13:13:53 -07:00
|
|
|
fprintf ppf "Error while running external preprocessor@.\
|
|
|
|
Command line: %s@." cmd
|
2013-01-25 01:12:31 -08:00
|
|
|
| WrongMagic cmd ->
|
2013-03-18 13:13:53 -07:00
|
|
|
fprintf ppf "External preprocessor does not produce a valid file@.\
|
|
|
|
Command line: %s@." cmd
|
2013-09-10 06:44:34 -07:00
|
|
|
|
2013-09-12 05:27:33 -07:00
|
|
|
let () =
|
|
|
|
Location.register_error_of_exn
|
|
|
|
(function
|
2013-09-12 06:47:04 -07:00
|
|
|
| Error err -> Some (Location.error_of_printer_file report_error err)
|
|
|
|
| _ -> None
|
2013-09-12 05:27:33 -07:00
|
|
|
)
|
2013-09-10 06:44:34 -07:00
|
|
|
|
2013-09-10 06:48:43 -07:00
|
|
|
let parse_all parse_fun magic ppf sourcefile =
|
2013-09-10 06:44:34 -07:00
|
|
|
Location.input_name := sourcefile;
|
|
|
|
let inputfile = preprocess sourcefile in
|
2013-09-10 06:48:43 -07:00
|
|
|
let ast =
|
|
|
|
try file ppf inputfile parse_fun magic
|
|
|
|
with exn ->
|
|
|
|
remove_preprocessed inputfile;
|
|
|
|
raise exn
|
|
|
|
in
|
|
|
|
remove_preprocessed inputfile;
|
|
|
|
ast
|
2013-09-10 06:44:34 -07:00
|
|
|
|
2013-09-10 06:48:43 -07:00
|
|
|
let parse_implementation ppf sourcefile =
|
|
|
|
parse_all Parse.implementation Config.ast_impl_magic_number ppf sourcefile
|
|
|
|
let parse_interface ppf sourcefile =
|
|
|
|
parse_all Parse.interface Config.ast_intf_magic_number ppf sourcefile
|