diff --git a/_oasis b/_oasis index 204a2a0..e4bff41 100644 --- a/_oasis +++ b/_oasis @@ -47,7 +47,7 @@ Library "utop-camlp4" FindlibName: camlp4 FindlibParent: utop Path: src/camlp4 - InternalModules: UTop_camlp4 + Modules: UTop_camlp4 BuildDepends: utop, camlp4 XMETAType: syntax XMETADescription: Camlp4 integration diff --git a/src/camlp4/uTop_camlp4.ml b/src/camlp4/uTop_camlp4.ml index 9348084..7453e16 100644 --- a/src/camlp4/uTop_camlp4.ml +++ b/src/camlp4/uTop_camlp4.ml @@ -15,6 +15,9 @@ module Ast2pt = Camlp4.Struct.Camlp4Ast2OCamlAst.Make(Ast) external cast_toplevel_phrase : Camlp4_import.Parsetree.toplevel_phrase -> Parsetree.toplevel_phrase = "%identity" +let convert_camlp4_toplevel_phrase ast = + cast_toplevel_phrase (Ast2pt.phrase ast) + let print_camlp4_error pp exn = Format.fprintf pp "@[<0>%a@]" Camlp4.ErrorHandler.print exn; Format.pp_print_flush pp () @@ -36,9 +39,9 @@ let parse_toplevel_phrase_camlp4 str eos_is_error = Some x) in match Gram.parse_tokens_after_filter Syntax.top_phrase token_stream with - | Some str_item -> - let str_item = AstFilters.fold_topphrase_filters (fun t filter -> filter t) str_item in - UTop.Value (cast_toplevel_phrase (Ast2pt.phrase str_item)) + | Some ast -> + let ast = AstFilters.fold_topphrase_filters (fun t filter -> filter t) ast in + UTop.Value ast | None -> raise UTop.Need_more with exn -> @@ -48,15 +51,22 @@ let parse_toplevel_phrase_camlp4 str eos_is_error = let locs, exn = match exn with | Loc.Exc_located (loc, exn) -> - ([(Loc.start_off loc,Loc.stop_off loc)], exn) + ([(Loc.start_off loc, Loc.stop_off loc)], exn) | exn -> ([], exn) in UTop.Error (locs, UTop.get_message print_camlp4_error exn) +let parse_toplevel_phrase str eos_is_error = + match parse_toplevel_phrase_camlp4 str eos_is_error with + | UTop.Value ast -> + UTop.Value (convert_camlp4_toplevel_phrase ast) + | UTop.Error (locs, msg) -> + UTop.Error (locs, msg) + let () = UTop.set_camlp4 true; - UTop.parse_toplevel_phrase := parse_toplevel_phrase_camlp4; + UTop.parse_toplevel_phrase := parse_toplevel_phrase; (* Force camlp4 to display its welcome message. *) try ignore (!Toploop.parse_toplevel_phrase (Lexing.from_string "")) diff --git a/src/camlp4/uTop_camlp4.mli b/src/camlp4/uTop_camlp4.mli new file mode 100644 index 0000000..e9ab241 --- /dev/null +++ b/src/camlp4/uTop_camlp4.mli @@ -0,0 +1,19 @@ +(* + * uTop_camlp4.mli + * --------------- + * Copyright : (c) 2012, Jeremie Dimino + * Licence : BSD3 + * + * This file is a part of utop. + *) + +val parse_toplevel_phrase : string -> bool -> Parsetree.toplevel_phrase UTop.result + (** Toplevel phrase parser for utop using camlp4. *) + +val parse_toplevel_phrase_camlp4 : string -> bool -> Camlp4.PreCast.Ast.str_item UTop.result + (** Camlp4 toplevel phrase parser. Same as {!parse_toplevel_phrase} + but the result is not converted to an OCaml ast. *) + +val convert_camlp4_toplevel_phrase : Camlp4.PreCast.Ast.str_item -> Parsetree.toplevel_phrase + (** Converts a camlp4 toplevel phrase into a standard OCaml toplevel + phrase. *)