diff --git a/src/lib/uTop_main.ml b/src/lib/uTop_main.ml index cdd462c..49a3c39 100644 --- a/src/lib/uTop_main.ml +++ b/src/lib/uTop_main.ml @@ -87,10 +87,24 @@ let parse_input_multi input = let parse_and_check input eos_is_error = let buf = Buffer.create 32 in + let preprocess input = + match input with +#if ocaml_version >= (4, 02, 0) + | UTop.Value (Parsetree.Ptop_def pstr) -> + begin try + let pstr = Pparse.apply_rewriters Config.ast_impl_magic_number pstr in + UTop.Value (Parsetree.Ptop_def pstr) + with Pparse.Error error -> + Pparse.report_error Format.str_formatter error; + UTop.Error ([], Format.flush_str_formatter ()) + end +#endif + | _ -> input + in let result = UTop.collect_formatters buf [Format.err_formatter] (fun () -> - match !UTop.parse_toplevel_phrase input eos_is_error with + match preprocess (!UTop.parse_toplevel_phrase input eos_is_error) with | UTop.Error (locs, msg) -> UTop.Error (convert_locs input locs, "Error: " ^ msg ^ "\n") | UTop.Value phrase -> @@ -520,9 +534,6 @@ let rewrite_str_item pstr_item tstr_item = let rewrite phrase = match phrase with | Parsetree.Ptop_def pstr -> -#if ocaml_version >= (4, 02, 0) - let pstr = Pparse.apply_rewriters Config.ast_impl_magic_number pstr in -#endif if (UTop.get_auto_run_lwt () || UTop.get_auto_run_async ()) && List.exists is_eval pstr then let tstr, _, _ = Typemod.type_structure !Toploop.toplevel_env pstr Location.none in let tstr = str_items_of_typed_structure tstr in