make UTop_camlp4 public so the user can reuse it

Ignore-this: 103cabecad95046cf6686b55028de6c9

darcs-hash:20120220101444-c41ad-5e51be240cb26f1ca02fc37655d464b5acf1cbab
This commit is contained in:
Jeremie Dimino 2012-02-20 11:14:44 +01:00
parent 40c2801b99
commit 65ad8cd988
3 changed files with 35 additions and 6 deletions

2
_oasis
View File

@ -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

View File

@ -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 ""))

View File

@ -0,0 +1,19 @@
(*
* uTop_camlp4.mli
* ---------------
* Copyright : (c) 2012, Jeremie Dimino <jeremie@dimino.org>
* 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. *)