|
| 1 | + |
| 2 | + |
| 3 | +(* Note: some of the functions here should go to Ast_mapper instead, |
| 4 | + which would encapsulate the "binary AST" protocol. *) |
| 5 | + |
| 6 | +let write_ast (type a) (kind : a Ml_binary.kind) fn (ast : a) = |
| 7 | + let oc = open_out_bin fn in |
| 8 | + output_string oc (Ml_binary.magic_of_kind kind); |
| 9 | + output_value oc (!Location.input_name : string); |
| 10 | + output_value oc (ast : a); |
| 11 | + close_out oc |
| 12 | + |
| 13 | +let apply_rewriter kind fn_in ppx = |
| 14 | + let magic = Ml_binary.magic_of_kind kind in |
| 15 | + let fn_out = Filename.temp_file "camlppx" "" in |
| 16 | + let comm = |
| 17 | + Printf.sprintf "%s %s %s" ppx (Filename.quote fn_in) (Filename.quote fn_out) |
| 18 | + in |
| 19 | + let ok = Ccomp.command comm = 0 in |
| 20 | + Misc.remove_file fn_in; |
| 21 | + if not ok then begin |
| 22 | + Misc.remove_file fn_out; |
| 23 | + Cmd_ast_exception.cannot_run comm |
| 24 | + end; |
| 25 | + if not (Sys.file_exists fn_out) then |
| 26 | + Cmd_ast_exception.cannot_run comm; |
| 27 | + (* check magic before passing to the next ppx *) |
| 28 | + let ic = open_in_bin fn_out in |
| 29 | + let buffer = |
| 30 | + try really_input_string ic (String.length magic) with End_of_file -> "" in |
| 31 | + close_in ic; |
| 32 | + if buffer <> magic then begin |
| 33 | + Misc.remove_file fn_out; |
| 34 | + Cmd_ast_exception.wrong_magic buffer; |
| 35 | + end; |
| 36 | + fn_out |
| 37 | + |
| 38 | +let read_ast (type a) (kind : a Ml_binary.kind) fn : a = |
| 39 | + let ic = open_in_bin fn in |
| 40 | + try |
| 41 | + let magic = Ml_binary.magic_of_kind kind in |
| 42 | + let buffer = really_input_string ic (String.length magic) in |
| 43 | + assert(buffer = magic); (* already checked by apply_rewriter *) |
| 44 | + Location.set_input_name @@ (input_value ic : string); |
| 45 | + let ast = (input_value ic : a) in |
| 46 | + close_in ic; |
| 47 | + Misc.remove_file fn; |
| 48 | + ast |
| 49 | + with exn -> |
| 50 | + close_in ic; |
| 51 | + Misc.remove_file fn; |
| 52 | + raise exn |
| 53 | +let rewrite kind ppxs ast = |
| 54 | + let fn = Filename.temp_file "camlppx" "" in |
| 55 | + write_ast kind fn ast; |
| 56 | + let fn = List.fold_left (apply_rewriter kind) fn (List.rev ppxs) in |
| 57 | + read_ast kind fn |
| 58 | + |
| 59 | +let apply_rewriters_str ?(restore = true) ~tool_name ast = |
| 60 | + match !Clflags.all_ppx with |
| 61 | + | [] -> ast |
| 62 | + | ppxs -> |
| 63 | + ast |
| 64 | + |> Ast_mapper.add_ppx_context_str ~tool_name |
| 65 | + |> rewrite Ml ppxs |
| 66 | + |> Ast_mapper.drop_ppx_context_str ~restore |
| 67 | + |
| 68 | +let apply_rewriters_sig ?(restore = true) ~tool_name ast = |
| 69 | + match !Clflags.all_ppx with |
| 70 | + | [] -> ast |
| 71 | + | ppxs -> |
| 72 | + ast |
| 73 | + |> Ast_mapper.add_ppx_context_sig ~tool_name |
| 74 | + |> rewrite Mli ppxs |
| 75 | + |> Ast_mapper.drop_ppx_context_sig ~restore |
| 76 | + |
| 77 | +let apply_rewriters ?restore ~tool_name |
| 78 | + (type a) (kind : a Ml_binary.kind) (ast : a) : a = |
| 79 | + match kind with |
| 80 | + | Ml_binary.Ml -> |
| 81 | + apply_rewriters_str ?restore ~tool_name ast |
| 82 | + | Ml_binary.Mli -> |
| 83 | + apply_rewriters_sig ?restore ~tool_name ast |
0 commit comments