@@ -365064,6 +365064,8 @@ val read_ast : 'a kind -> in_channel -> 'a
365064365064
365065365065val write_ast :
365066365066 'a kind -> string -> 'a -> out_channel -> unit
365067+
365068+ val magic_of_kind : 'a kind -> string
365067365069end = struct
365068365070#1 "ml_binary.ml"
365069365071(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -365117,6 +365119,12 @@ let write_ast (type t) (kind : t kind)
365117365119 output_string oc magic ;
365118365120 output_value oc fname;
365119365121 output_value oc pt
365122+
365123+ let magic_of_kind : type a . a kind -> string = function
365124+ | Ml -> Config.ast_impl_magic_number
365125+ | Mli -> Config.ast_intf_magic_number
365126+
365127+
365120365128end
365121365129module Ast_extract : sig
365122365130#1 "ast_extract.mli"
@@ -403474,6 +403482,125 @@ let lambda_as_module
403474403482 However, use filename instead of {!Env.current_unit} is more honest, since node-js module system is coupled with the file name
403475403483*)
403476403484
403485+ end
403486+ module Cmd_ast_exception
403487+ = struct
403488+ #1 "cmd_ast_exception.ml"
403489+ type error =
403490+ | CannotRun of string
403491+ | WrongMagic of string
403492+
403493+ exception Error of error
403494+
403495+
403496+ let report_error ppf = function
403497+ | CannotRun cmd ->
403498+ Format.fprintf ppf "Error while running external preprocessor@.\
403499+ Command line: %s@." cmd
403500+ | WrongMagic cmd ->
403501+ Format.fprintf ppf "External preprocessor does not produce a valid file@.\
403502+ Command line: %s@." cmd
403503+
403504+ let () =
403505+ Location.register_error_of_exn
403506+ (function
403507+ | Error err -> Some (Location.error_of_printer_file report_error err)
403508+ | _ -> None
403509+ )
403510+
403511+ let cannot_run comm =
403512+ raise (Error (CannotRun comm))
403513+
403514+ let wrong_magic magic =
403515+ raise (Error (WrongMagic magic))
403516+ end
403517+ module Cmd_ppx_apply
403518+ = struct
403519+ #1 "cmd_ppx_apply.ml"
403520+
403521+
403522+ (* Note: some of the functions here should go to Ast_mapper instead,
403523+ which would encapsulate the "binary AST" protocol. *)
403524+
403525+ let write_ast (type a) (kind : a Ml_binary.kind) fn (ast : a) =
403526+ let oc = open_out_bin fn in
403527+ output_string oc (Ml_binary.magic_of_kind kind);
403528+ output_value oc (!Location.input_name : string);
403529+ output_value oc (ast : a);
403530+ close_out oc
403531+
403532+ let apply_rewriter kind fn_in ppx =
403533+ let magic = Ml_binary.magic_of_kind kind in
403534+ let fn_out = Filename.temp_file "camlppx" "" in
403535+ let comm =
403536+ Printf.sprintf "%s %s %s" ppx (Filename.quote fn_in) (Filename.quote fn_out)
403537+ in
403538+ let ok = Ccomp.command comm = 0 in
403539+ Misc.remove_file fn_in;
403540+ if not ok then begin
403541+ Misc.remove_file fn_out;
403542+ Cmd_ast_exception.cannot_run comm
403543+ end;
403544+ if not (Sys.file_exists fn_out) then
403545+ Cmd_ast_exception.cannot_run comm;
403546+ (* check magic before passing to the next ppx *)
403547+ let ic = open_in_bin fn_out in
403548+ let buffer =
403549+ try really_input_string ic (String.length magic) with End_of_file -> "" in
403550+ close_in ic;
403551+ if buffer <> magic then begin
403552+ Misc.remove_file fn_out;
403553+ Cmd_ast_exception.wrong_magic buffer;
403554+ end;
403555+ fn_out
403556+
403557+ let read_ast (type a) (kind : a Ml_binary.kind) fn : a =
403558+ let ic = open_in_bin fn in
403559+ try
403560+ let magic = Ml_binary.magic_of_kind kind in
403561+ let buffer = really_input_string ic (String.length magic) in
403562+ assert(buffer = magic); (* already checked by apply_rewriter *)
403563+ Location.set_input_name @@ (input_value ic : string);
403564+ let ast = (input_value ic : a) in
403565+ close_in ic;
403566+ Misc.remove_file fn;
403567+ ast
403568+ with exn ->
403569+ close_in ic;
403570+ Misc.remove_file fn;
403571+ raise exn
403572+ let rewrite kind ppxs ast =
403573+ let fn = Filename.temp_file "camlppx" "" in
403574+ write_ast kind fn ast;
403575+ let fn = List.fold_left (apply_rewriter kind) fn (List.rev ppxs) in
403576+ read_ast kind fn
403577+
403578+ let apply_rewriters_str ?(restore = true) ~tool_name ast =
403579+ match !Clflags.all_ppx with
403580+ | [] -> ast
403581+ | ppxs ->
403582+ ast
403583+ |> Ast_mapper.add_ppx_context_str ~tool_name
403584+ |> rewrite Ml ppxs
403585+ |> Ast_mapper.drop_ppx_context_str ~restore
403586+
403587+ let apply_rewriters_sig ?(restore = true) ~tool_name ast =
403588+ match !Clflags.all_ppx with
403589+ | [] -> ast
403590+ | ppxs ->
403591+ ast
403592+ |> Ast_mapper.add_ppx_context_sig ~tool_name
403593+ |> rewrite Mli ppxs
403594+ |> Ast_mapper.drop_ppx_context_sig ~restore
403595+
403596+ let apply_rewriters ?restore ~tool_name
403597+ (type a) (kind : a Ml_binary.kind) (ast : a) : a =
403598+ match kind with
403599+ | Ml_binary.Ml ->
403600+ apply_rewriters_str ?restore ~tool_name ast
403601+ | Ml_binary.Mli ->
403602+ apply_rewriters_sig ?restore ~tool_name ast
403603+
403477403604end
403478403605module Parse : sig
403479403606#1 "parse.mli"
@@ -403592,11 +403719,6 @@ end = struct
403592403719#1 "pparse_driver.ml"
403593403720
403594403721
403595- type error =
403596- | CannotRun of string
403597- | WrongMagic of string
403598-
403599- exception Error of error
403600403722
403601403723(* Optionally preprocess a source file *)
403602403724
@@ -403607,7 +403729,7 @@ let call_external_preprocessor sourcefile pp =
403607403729 in
403608403730 if Ccomp.command comm <> 0 then begin
403609403731 Misc.remove_file tmpfile;
403610- raise (Error (CannotRun comm));
403732+ Cmd_ast_exception.cannot_run comm
403611403733 end;
403612403734 tmpfile
403613403735
@@ -403623,126 +403745,30 @@ let remove_preprocessed inputfile =
403623403745 None -> ()
403624403746 | Some _ -> Misc.remove_file inputfile
403625403747
403626- type 'a ast_kind =
403627- | Structure : Parsetree.structure ast_kind
403628- | Signature : Parsetree.signature ast_kind
403629-
403630- let magic_of_kind : type a . a ast_kind -> string = function
403631- | Structure -> Config.ast_impl_magic_number
403632- | Signature -> Config.ast_intf_magic_number
403633-
403634- (* Note: some of the functions here should go to Ast_mapper instead,
403635- which would encapsulate the "binary AST" protocol. *)
403636-
403637- let write_ast (type a) (kind : a ast_kind) fn (ast : a) =
403638- let oc = open_out_bin fn in
403639- output_string oc (magic_of_kind kind);
403640- output_value oc (!Location.input_name : string);
403641- output_value oc (ast : a);
403642- close_out oc
403643-
403644- let apply_rewriter kind fn_in ppx =
403645- let magic = magic_of_kind kind in
403646- let fn_out = Filename.temp_file "camlppx" "" in
403647- let comm =
403648- Printf.sprintf "%s %s %s" ppx (Filename.quote fn_in) (Filename.quote fn_out)
403649- in
403650- let ok = Ccomp.command comm = 0 in
403651- Misc.remove_file fn_in;
403652- if not ok then begin
403653- Misc.remove_file fn_out;
403654- raise (Error (CannotRun comm));
403655- end;
403656- if not (Sys.file_exists fn_out) then
403657- raise (Error (WrongMagic comm));
403658- (* check magic before passing to the next ppx *)
403659- let ic = open_in_bin fn_out in
403660- let buffer =
403661- try really_input_string ic (String.length magic) with End_of_file -> "" in
403662- close_in ic;
403663- if buffer <> magic then begin
403664- Misc.remove_file fn_out;
403665- raise (Error (WrongMagic comm));
403666- end;
403667- fn_out
403668-
403669- let read_ast (type a) (kind : a ast_kind) fn : a =
403670- let ic = open_in_bin fn in
403671- try
403672- let magic = magic_of_kind kind in
403673- let buffer = really_input_string ic (String.length magic) in
403674- assert(buffer = magic); (* already checked by apply_rewriter *)
403675- Location.set_input_name @@ (input_value ic : string);
403676- let ast = (input_value ic : a) in
403677- close_in ic;
403678- Misc.remove_file fn;
403679- ast
403680- with exn ->
403681- close_in ic;
403682- Misc.remove_file fn;
403683- raise exn
403684403748
403685- let rewrite kind ppxs ast =
403686- let fn = Filename.temp_file "camlppx" "" in
403687- write_ast kind fn ast;
403688- let fn = List.fold_left (apply_rewriter kind) fn (List.rev ppxs) in
403689- read_ast kind fn
403690403749
403691- let apply_rewriters_str ?(restore = true) ~tool_name ast =
403692- match !Clflags.all_ppx with
403693- | [] -> ast
403694- | ppxs ->
403695- ast
403696- |> Ast_mapper.add_ppx_context_str ~tool_name
403697- |> rewrite Structure ppxs
403698- |> Ast_mapper.drop_ppx_context_str ~restore
403699403750
403700- let apply_rewriters_sig ?(restore = true) ~tool_name ast =
403701- match !Clflags.all_ppx with
403702- | [] -> ast
403703- | ppxs ->
403704- ast
403705- |> Ast_mapper.add_ppx_context_sig ~tool_name
403706- |> rewrite Signature ppxs
403707- |> Ast_mapper.drop_ppx_context_sig ~restore
403708403751
403709- let apply_rewriters ?restore ~tool_name
403710- (type a) (kind : a ast_kind) (ast : a) : a =
403711- match kind with
403712- | Structure ->
403713- apply_rewriters_str ?restore ~tool_name ast
403714- | Signature ->
403715- apply_rewriters_sig ?restore ~tool_name ast
403716403752
403717403753(* Parse a file or get a dumped syntax tree from it *)
403718403754
403719- exception Outdated_version
403755+ let parse (type a) (kind : a Ml_binary.kind) lexbuf : a =
403756+ match kind with
403757+ | Ml_binary.Ml -> Parse.implementation lexbuf
403758+ | Ml_binary.Mli -> Parse.interface lexbuf
403720403759
403721- let open_and_check_magic inputfile ast_magic =
403760+ let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
403761+ (kind : a Ml_binary.kind) : a =
403762+ let ast_magic = Ml_binary.magic_of_kind kind in
403722403763 let ic = open_in_bin inputfile in
403723403764 let is_ast_file =
403724- try
403725- let buffer = really_input_string ic (String.length ast_magic) in
403765+ match really_input_string ic (String.length ast_magic) with
403766+ | exception _ -> false
403767+ | buffer ->
403726403768 if buffer = ast_magic then true
403727- else if String.sub buffer 0 9 = String.sub ast_magic 0 9 then
403728- raise Outdated_version
403729- else false
403730- with
403731- Outdated_version ->
403732- Misc.fatal_error "OCaml and preprocessor have incompatible versions"
403733- | _ -> false
403734- in
403735- (ic, is_ast_file)
403736-
403737- let parse (type a) (kind : a ast_kind) lexbuf : a =
403738- match kind with
403739- | Structure -> Parse.implementation lexbuf
403740- | Signature -> Parse.interface lexbuf
403741-
403742- let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
403743- (kind : a ast_kind) : a =
403744- let ast_magic = magic_of_kind kind in
403745- let (ic, is_ast_file) = open_and_check_magic inputfile ast_magic in
403769+ else if Ext_string.starts_with buffer "Caml1999" then
403770+ Cmd_ast_exception.wrong_magic buffer
403771+ else false in
403746403772 let ast =
403747403773 try
403748403774 if is_ast_file then begin
@@ -403764,20 +403790,7 @@ let file_aux ppf inputfile (type a) (parse_fun : _ -> a)
403764403790
403765403791
403766403792
403767- let report_error ppf = function
403768- | CannotRun cmd ->
403769- Format.fprintf ppf "Error while running external preprocessor@.\
403770- Command line: %s@." cmd
403771- | WrongMagic cmd ->
403772- Format.fprintf ppf "External preprocessor does not produce a valid file@.\
403773- Command line: %s@." cmd
403774403793
403775- let () =
403776- Location.register_error_of_exn
403777- (function
403778- | Error err -> Some (Location.error_of_printer_file report_error err)
403779- | _ -> None
403780- )
403781403794
403782403795let parse_file kind ppf sourcefile =
403783403796 Location.set_input_name sourcefile;
@@ -403795,11 +403808,11 @@ let parse_file kind ppf sourcefile =
403795403808
403796403809
403797403810let parse_implementation ppf ~tool_name sourcefile =
403798- apply_rewriters ~restore:false ~tool_name Structure (parse_file
403799- Structure ppf sourcefile)
403811+ Cmd_ppx_apply. apply_rewriters ~restore:false ~tool_name Ml (parse_file
403812+ Ml ppf sourcefile)
403800403813let parse_interface ppf ~tool_name sourcefile =
403801- apply_rewriters ~restore:false ~tool_name Signature (parse_file
403802- Signature ppf sourcefile)
403814+ Cmd_ppx_apply. apply_rewriters ~restore:false ~tool_name Mli (parse_file
403815+ Mli ppf sourcefile)
403803403816
403804403817end
403805403818module Pprintast : sig
0 commit comments