make UTop_camlp4 public so the user can reuse it
Ignore-this: 103cabecad95046cf6686b55028de6c9 darcs-hash:20120220101444-c41ad-5e51be240cb26f1ca02fc37655d464b5acf1cbab
This commit is contained in:
parent
40c2801b99
commit
65ad8cd988
2
_oasis
2
_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
|
||||
|
|
|
@ -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 ->
|
||||
|
@ -54,9 +57,16 @@ let parse_toplevel_phrase_camlp4 str eos_is_error =
|
|||
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 ""))
|
||||
|
|
|
@ -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. *)
|
Loading…
Reference in New Issue