| Attached Files | patch_ast_rewriter.diff [^] (6,092 bytes) 2012-06-05 10:05 [Show Content] [Hide Content]Index: driver/pparse.ml
===================================================================
--- driver/pparse.ml (revision 12564)
+++ driver/pparse.ml (working copy)
@@ -47,6 +47,48 @@
exception Outdated_version
+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
+
+let apply_rewriter fn_in ppx =
+ let fn_out = Filename.temp_file "camlppx" "" in
+ let comm = Printf.sprintf "%s %s %s" ppx (Filename.quote fn_in) (Filename.quote fn_out) in
+ let ok = Ccomp.command comm = 0 in
+ Misc.remove_file fn_in;
+ if not ok then begin
+ Misc.remove_file fn_out;
+ raise Error;
+ end;
+ if not (Sys.file_exists fn_out) then raise Error;
+ 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
+ if buffer <> magic then
+ Misc.fatal_error "OCaml and preprocessor have incompatible versions";
+ 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
+
+let apply_rewriters magic ast ppxs =
+ if ppxs = [] then ast
+ else let fn = List.fold_left apply_rewriter (write_ast magic ast) ppxs in
+ read_ast magic fn
+
let file ppf inputfile parse_fun ast_magic =
let ic = open_in_bin inputfile in
let is_ast_file =
@@ -79,4 +121,4 @@
with x -> close_in ic; raise x
in
close_in ic;
- ast
+ apply_rewriters ast_magic ast !Clflags.ppx
Index: driver/main_args.mli
===================================================================
--- driver/main_args.mli (revision 12564)
+++ driver/main_args.mli (working copy)
@@ -44,6 +44,7 @@
val _output_obj : unit -> unit
val _pack : unit -> unit
val _pp : string -> unit
+ val _ppx : string -> unit
val _principal : unit -> unit
val _rectypes : unit -> unit
val _runtime_variant : string -> unit
@@ -134,6 +135,7 @@
val _p : unit -> unit
val _pack : unit -> unit
val _pp : string -> unit
+ val _ppx : string -> unit
val _principal : unit -> unit
val _rectypes : unit -> unit
val _runtime_variant : string -> unit
Index: driver/main.ml
===================================================================
--- driver/main.ml (revision 12564)
+++ driver/main.ml (working copy)
@@ -121,6 +121,7 @@
let _output_obj () = output_c_object := true; custom_runtime := true
let _pack = set make_package
let _pp s = preprocessor := Some s
+ let _ppx s = ppx := s :: !ppx
let _principal = set principal
let _rectypes = set recursive_types
let _runtime_variant s = runtime_variant := s
Index: driver/main_args.ml
===================================================================
--- driver/main_args.ml (revision 12564)
+++ driver/main_args.ml (working copy)
@@ -209,6 +209,10 @@
"-pp", Arg.String f, "<command> Pipe sources through preprocessor <command>"
;;
+let mk_ppx f =
+ "-ppx", Arg.String f, "<command> Pipe abstract syntax trees through preprocessor <command>"
+;;
+
let mk_principal f =
"-principal", Arg.Unit f, " Check principality of type inference"
;;
@@ -428,6 +432,7 @@
val _output_obj : unit -> unit
val _pack : unit -> unit
val _pp : string -> unit
+ val _ppx : string -> unit
val _principal : unit -> unit
val _rectypes : unit -> unit
val _runtime_variant : string -> unit
@@ -517,6 +522,7 @@
val _p : unit -> unit
val _pack : unit -> unit
val _pp : string -> unit
+ val _ppx : string -> unit
val _principal : unit -> unit
val _rectypes : unit -> unit
val _runtime_variant : string -> unit
@@ -644,6 +650,7 @@
mk_output_obj F._output_obj;
mk_pack_byt F._pack;
mk_pp F._pp;
+ mk_ppx F._ppx;
mk_principal F._principal;
mk_rectypes F._rectypes;
mk_runtime_variant F._runtime_variant;
@@ -741,6 +748,7 @@
mk_p F._p;
mk_pack_opt F._pack;
mk_pp F._pp;
+ mk_ppx F._ppx;
mk_principal F._principal;
mk_rectypes F._rectypes;
mk_runtime_variant F._runtime_variant;
Index: driver/optmain.ml
===================================================================
--- driver/optmain.ml (revision 12564)
+++ driver/optmain.ml (working copy)
@@ -132,6 +132,7 @@
let _p = set gprofile
let _pack = set make_package
let _pp s = preprocessor := Some s
+ let _ppx s = ppx := s :: !ppx
let _principal = set principal
let _rectypes = set recursive_types
let _runtime_variant s = runtime_variant := s
Index: tools/ocamlcp.ml
===================================================================
--- tools/ocamlcp.ml (revision 12564)
+++ tools/ocamlcp.ml (working copy)
@@ -73,6 +73,7 @@
let _output_obj = option "-output-obj"
let _pack = option "-pack"
let _pp s = incompatible "-pp"
+ let _ppx s = incompatible "-ppx"
let _principal = option "-principal"
let _rectypes = option "-rectypes"
let _runtime_variant s = option_with_arg "-runtime-variant" s
Index: utils/clflags.ml
===================================================================
--- utils/clflags.ml (revision 12564)
+++ utils/clflags.ml (working copy)
@@ -33,6 +33,7 @@
and classic = ref false (* -nolabels *)
and nopervasives = ref false (* -nopervasives *)
and preprocessor = ref(None : string option) (* -pp *)
+and ppx = ref ([] : string list) (* -ppx *)
let annotations = ref false (* -annot *)
let binary_annotations = ref false (* -annot *)
and use_threads = ref false (* -thread *)
Index: utils/clflags.mli
===================================================================
--- utils/clflags.mli (revision 12564)
+++ utils/clflags.mli (working copy)
@@ -30,6 +30,7 @@
val classic : bool ref
val nopervasives : bool ref
val preprocessor : string option ref
+val ppx : string list ref
val annotations : bool ref
val binary_annotations : bool ref
val use_threads : bool ref
|