1997-03-22 12:16:52 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy and Jerome Vouillon, projet Cristal, INRIA Rocquencourt*)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1997-03-22 12:16:52 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* To print values *)
|
|
|
|
|
|
|
|
open Misc
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1997-03-22 12:16:52 -08:00
|
|
|
open Longident
|
|
|
|
open Path
|
|
|
|
open Types
|
2001-08-02 01:51:55 -07:00
|
|
|
open Outcometree
|
1997-03-22 12:16:52 -08:00
|
|
|
|
1998-10-01 05:34:32 -07:00
|
|
|
module type OBJ =
|
1997-03-22 12:16:52 -08:00
|
|
|
sig
|
|
|
|
type t
|
|
|
|
val obj : t -> 'a
|
|
|
|
val is_block : t -> bool
|
|
|
|
val tag : t -> int
|
|
|
|
val size : t -> int
|
|
|
|
val field : t -> int -> t
|
|
|
|
end
|
|
|
|
|
2000-03-25 10:55:45 -08:00
|
|
|
module type EVALPATH =
|
|
|
|
sig
|
|
|
|
type value
|
|
|
|
val eval_path: Path.t -> value
|
|
|
|
exception Error
|
2000-03-26 04:11:10 -08:00
|
|
|
val same_value: value -> value -> bool
|
2000-03-25 10:55:45 -08:00
|
|
|
end
|
|
|
|
|
1997-03-22 12:16:52 -08:00
|
|
|
module type S =
|
|
|
|
sig
|
|
|
|
type t
|
2001-07-03 04:04:10 -07:00
|
|
|
val install_printer :
|
|
|
|
Path.t -> Types.type_expr -> (formatter -> t -> unit) -> unit
|
1997-03-22 12:16:52 -08:00
|
|
|
val remove_printer : Path.t -> unit
|
2001-08-02 01:51:55 -07:00
|
|
|
val outval_of_untyped_exception : t -> Outcometree.out_value
|
|
|
|
val outval_of_value :
|
|
|
|
int -> int ->
|
|
|
|
(int -> t -> Types.type_expr -> Outcometree.out_value option) ->
|
|
|
|
Env.t -> t -> type_expr -> Outcometree.out_value
|
|
|
|
val print_outval : formatter -> Outcometree.out_value -> unit
|
1997-03-22 12:16:52 -08:00
|
|
|
end
|
|
|
|
|
2000-03-25 10:55:45 -08:00
|
|
|
module Make(O : OBJ)(EVP : EVALPATH with type value = O.t) = struct
|
1997-03-22 12:16:52 -08:00
|
|
|
|
1998-10-01 05:34:32 -07:00
|
|
|
type t = O.t
|
1997-03-22 12:16:52 -08:00
|
|
|
|
|
|
|
(* Given an exception value, we cannot recover its type,
|
|
|
|
hence we cannot print its arguments in general.
|
|
|
|
Here, we do a feeble attempt to print
|
|
|
|
integer, string and float arguments... *)
|
2001-08-02 01:51:55 -07:00
|
|
|
let outval_of_untyped_exception_args obj start_offset =
|
1998-10-01 05:34:32 -07:00
|
|
|
if O.size obj > start_offset then begin
|
2001-08-02 01:51:55 -07:00
|
|
|
let list = ref [] in
|
1998-10-01 05:34:32 -07:00
|
|
|
for i = start_offset to O.size obj - 1 do
|
|
|
|
let arg = O.field obj i in
|
|
|
|
if not (O.is_block arg) then
|
2001-08-02 01:51:55 -07:00
|
|
|
list := Oval_int (O.obj arg : int) :: !list
|
2000-03-25 10:55:45 -08:00
|
|
|
(* Note: this could be a char or a constant constructor... *)
|
2000-03-06 14:12:09 -08:00
|
|
|
else if O.tag arg = Obj.string_tag then
|
2001-08-02 01:51:55 -07:00
|
|
|
list :=
|
|
|
|
Oval_string (String.escaped (O.obj arg : string)) :: !list
|
2000-03-06 14:12:09 -08:00
|
|
|
else if O.tag arg = Obj.double_tag then
|
2001-08-02 01:51:55 -07:00
|
|
|
list := Oval_float (O.obj arg : float) :: !list
|
1997-03-22 12:16:52 -08:00
|
|
|
else
|
2001-08-02 01:51:55 -07:00
|
|
|
list := Oval_constr (Oide_ident "_", []) :: !list
|
1997-03-22 12:16:52 -08:00
|
|
|
done;
|
2001-08-02 01:51:55 -07:00
|
|
|
List.rev !list
|
1997-03-22 12:16:52 -08:00
|
|
|
end
|
2001-08-02 01:51:55 -07:00
|
|
|
else []
|
1997-03-22 12:16:52 -08:00
|
|
|
|
2001-08-02 01:51:55 -07:00
|
|
|
let outval_of_untyped_exception bucket =
|
1998-10-01 05:34:32 -07:00
|
|
|
let name = (O.obj(O.field(O.field bucket 0) 0) : string) in
|
2001-08-02 01:51:55 -07:00
|
|
|
let args =
|
|
|
|
if (name = "Match_failure" || name = "Assert_failure")
|
|
|
|
&& O.size bucket = 2
|
|
|
|
&& O.tag(O.field bucket 1) = 0
|
|
|
|
then outval_of_untyped_exception_args (O.field bucket 1) 0
|
|
|
|
else outval_of_untyped_exception_args bucket 1 in
|
|
|
|
Oval_constr (Oide_ident name, args)
|
1997-12-09 01:12:30 -08:00
|
|
|
|
1997-03-22 12:16:52 -08:00
|
|
|
(* The user-defined printers. Also used for some builtin types. *)
|
|
|
|
|
|
|
|
let printers = ref ([
|
|
|
|
Pident(Ident.create "print_int"), Predef.type_int,
|
2001-08-02 01:51:55 -07:00
|
|
|
(fun x -> Oval_int (O.obj x : int));
|
1997-03-22 12:16:52 -08:00
|
|
|
Pident(Ident.create "print_float"), Predef.type_float,
|
2001-08-02 01:51:55 -07:00
|
|
|
(fun x -> Oval_float (O.obj x : float));
|
1997-03-22 12:16:52 -08:00
|
|
|
Pident(Ident.create "print_char"), Predef.type_char,
|
2001-08-02 01:51:55 -07:00
|
|
|
(fun x -> Oval_char (O.obj x : char));
|
1997-03-22 12:16:52 -08:00
|
|
|
Pident(Ident.create "print_string"), Predef.type_string,
|
2001-08-02 01:51:55 -07:00
|
|
|
(fun x -> Oval_string (O.obj x : string));
|
2000-03-05 11:18:50 -08:00
|
|
|
Pident(Ident.create "print_int32"), Predef.type_int32,
|
2001-08-02 01:51:55 -07:00
|
|
|
(fun x -> Oval_string (Int32.to_string (O.obj x : int32)));
|
2000-03-05 11:18:50 -08:00
|
|
|
Pident(Ident.create "print_nativeint"), Predef.type_nativeint,
|
2001-08-02 01:51:55 -07:00
|
|
|
(fun x -> Oval_string (Nativeint.to_string (O.obj x : nativeint)));
|
2000-03-05 11:18:50 -08:00
|
|
|
Pident(Ident.create "print_int64"), Predef.type_int64,
|
2001-08-02 01:51:55 -07:00
|
|
|
(fun x -> Oval_string (Int64.to_string (O.obj x : int64)))
|
|
|
|
] : (Path.t * type_expr * (O.t -> Outcometree.out_value)) list)
|
1997-03-22 12:16:52 -08:00
|
|
|
|
|
|
|
let install_printer path ty fn =
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_val ppf obj =
|
2001-07-03 04:04:10 -07:00
|
|
|
try fn ppf obj with
|
2000-03-06 14:12:09 -08:00
|
|
|
| exn ->
|
|
|
|
fprintf ppf "<printer %a raised an exception>" Printtyp.path path in
|
2001-08-02 01:51:55 -07:00
|
|
|
let printer obj = Oval_printer (fun ppf -> print_val ppf obj) in
|
|
|
|
printers := (path, ty, printer) :: !printers
|
1997-03-22 12:16:52 -08:00
|
|
|
|
|
|
|
let remove_printer path =
|
|
|
|
let rec remove = function
|
2000-03-06 14:12:09 -08:00
|
|
|
| [] -> raise Not_found
|
1997-03-22 12:16:52 -08:00
|
|
|
| (p, ty, fn as printer) :: rem ->
|
|
|
|
if Path.same p path then rem else printer :: remove rem in
|
|
|
|
printers := remove !printers
|
|
|
|
|
|
|
|
let find_printer env ty =
|
|
|
|
let rec find = function
|
2000-03-06 14:12:09 -08:00
|
|
|
| [] -> raise Not_found
|
1997-03-22 12:16:52 -08:00
|
|
|
| (name, sch, printer) :: remainder ->
|
1997-04-01 12:52:36 -08:00
|
|
|
if Ctype.moregeneral env false sch ty
|
1997-03-22 12:16:52 -08:00
|
|
|
then printer
|
|
|
|
else find remainder
|
|
|
|
in find !printers
|
|
|
|
|
|
|
|
(* Print a constructor or label, giving it the same prefix as the type
|
|
|
|
it comes from. Attempt to omit the prefix if the type comes from
|
|
|
|
a module that has been opened. *)
|
|
|
|
|
2001-08-02 01:51:55 -07:00
|
|
|
let tree_of_qualified lookup_fun env ty_path name =
|
1997-03-22 12:16:52 -08:00
|
|
|
match ty_path with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Pident id ->
|
2001-08-02 01:51:55 -07:00
|
|
|
Oide_ident name
|
1997-03-22 12:16:52 -08:00
|
|
|
| Pdot(p, s, pos) ->
|
|
|
|
if try
|
|
|
|
match (lookup_fun (Lident name) env).desc with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tconstr(ty_path', _, _) -> Path.same ty_path ty_path'
|
1997-03-22 12:16:52 -08:00
|
|
|
| _ -> false
|
|
|
|
with Not_found -> false
|
2001-08-02 01:51:55 -07:00
|
|
|
then Oide_ident name
|
2001-08-04 03:08:19 -07:00
|
|
|
else Oide_dot (Printtyp.tree_of_path p, name)
|
1997-03-22 12:16:52 -08:00
|
|
|
| Papply(p1, p2) ->
|
2001-08-04 03:08:19 -07:00
|
|
|
Printtyp.tree_of_path ty_path
|
1997-03-22 12:16:52 -08:00
|
|
|
|
2001-08-02 01:51:55 -07:00
|
|
|
let tree_of_constr =
|
|
|
|
tree_of_qualified
|
1997-03-22 12:16:52 -08:00
|
|
|
(fun lid env -> (Env.lookup_constructor lid env).cstr_res)
|
|
|
|
|
2001-08-02 01:51:55 -07:00
|
|
|
and tree_of_label =
|
|
|
|
tree_of_qualified (fun lid env -> (Env.lookup_label lid env).lbl_res)
|
1997-03-22 12:16:52 -08:00
|
|
|
|
|
|
|
(* An abstract type *)
|
|
|
|
|
|
|
|
let abstract_type =
|
|
|
|
Ctype.newty (Tconstr (Pident (Ident.create "abstract"), [], ref Mnil))
|
|
|
|
|
|
|
|
(* The main printing function *)
|
|
|
|
|
2001-08-02 01:51:55 -07:00
|
|
|
let outval_of_value max_steps max_depth check_depth env obj ty =
|
1997-03-22 12:16:52 -08:00
|
|
|
|
|
|
|
let printer_steps = ref max_steps in
|
|
|
|
|
2001-08-02 01:51:55 -07:00
|
|
|
let rec tree_of_val depth obj ty =
|
1997-03-22 12:16:52 -08:00
|
|
|
decr printer_steps;
|
2001-08-02 01:51:55 -07:00
|
|
|
if !printer_steps < 0 || depth < 0 then Oval_ellipsis
|
|
|
|
else begin
|
1997-03-22 12:16:52 -08:00
|
|
|
try
|
2001-08-02 01:51:55 -07:00
|
|
|
find_printer env ty obj
|
1997-03-22 12:16:52 -08:00
|
|
|
with Not_found ->
|
|
|
|
match (Ctype.repr ty).desc with
|
2000-03-06 14:12:09 -08:00
|
|
|
| Tvar ->
|
2001-08-02 01:51:55 -07:00
|
|
|
Oval_stuff "<poly>"
|
2001-04-19 01:34:21 -07:00
|
|
|
| Tarrow(_, ty1, ty2, _) ->
|
2001-08-02 01:51:55 -07:00
|
|
|
Oval_stuff "<fun>"
|
1997-03-22 12:16:52 -08:00
|
|
|
| Ttuple(ty_list) ->
|
2001-08-02 01:51:55 -07:00
|
|
|
Oval_tuple (tree_of_val_list 0 depth obj ty_list)
|
1997-03-22 12:16:52 -08:00
|
|
|
| Tconstr(path, [], _) when Path.same path Predef.path_exn ->
|
2001-08-02 01:51:55 -07:00
|
|
|
tree_of_exception depth obj
|
|
|
|
| Tconstr(path, [ty_arg], _)
|
|
|
|
when Path.same path Predef.path_list ->
|
|
|
|
if O.is_block obj then
|
|
|
|
match check_depth depth obj ty with
|
|
|
|
Some x -> x
|
|
|
|
| None ->
|
|
|
|
let rec tree_of_conses tree_list obj =
|
|
|
|
if !printer_steps < 0 || depth < 0 then
|
|
|
|
Oval_ellipsis :: tree_list
|
|
|
|
else if O.is_block obj then
|
|
|
|
let tree =
|
|
|
|
tree_of_val (depth - 1) (O.field obj 0) ty_arg in
|
|
|
|
let next_obj = O.field obj 1 in
|
|
|
|
tree_of_conses (tree :: tree_list) next_obj
|
|
|
|
else tree_list
|
|
|
|
in
|
|
|
|
Oval_list (List.rev (tree_of_conses [] obj))
|
|
|
|
else
|
|
|
|
Oval_list []
|
|
|
|
| Tconstr(path, [ty_arg], _)
|
|
|
|
when Path.same path Predef.path_array ->
|
1998-10-01 05:34:32 -07:00
|
|
|
let length = O.size obj in
|
2001-08-02 01:51:55 -07:00
|
|
|
if length > 0 then
|
|
|
|
match check_depth depth obj ty with
|
|
|
|
Some x -> x
|
|
|
|
| None ->
|
|
|
|
let rec tree_of_items tree_list i =
|
|
|
|
if !printer_steps < 0 || depth < 0 then
|
|
|
|
Oval_ellipsis :: tree_list
|
|
|
|
else if i < length then
|
|
|
|
let tree =
|
|
|
|
tree_of_val (depth - 1) (O.field obj i) ty_arg in
|
|
|
|
tree_of_items (tree :: tree_list) (i + 1)
|
|
|
|
else tree_list
|
|
|
|
in
|
|
|
|
Oval_array (List.rev (tree_of_items [] 0))
|
|
|
|
else
|
|
|
|
Oval_array []
|
1997-03-22 12:16:52 -08:00
|
|
|
| Tconstr(path, ty_list, _) ->
|
|
|
|
begin try
|
|
|
|
let decl = Env.find_type path env in
|
|
|
|
match decl with
|
2000-03-06 14:12:09 -08:00
|
|
|
| {type_kind = Type_abstract; type_manifest = None} ->
|
2001-08-02 01:51:55 -07:00
|
|
|
Oval_stuff "<abstr>"
|
1997-03-22 12:16:52 -08:00
|
|
|
| {type_kind = Type_abstract; type_manifest = Some body} ->
|
2001-08-02 01:51:55 -07:00
|
|
|
tree_of_val depth obj
|
1997-03-22 12:16:52 -08:00
|
|
|
(try Ctype.apply env decl.type_params body ty_list with
|
|
|
|
Ctype.Cannot_apply -> abstract_type)
|
|
|
|
| {type_kind = Type_variant constr_list} ->
|
|
|
|
let tag =
|
1998-10-01 05:34:32 -07:00
|
|
|
if O.is_block obj
|
|
|
|
then Cstr_block(O.tag obj)
|
|
|
|
else Cstr_constant(O.obj obj) in
|
1997-03-22 12:16:52 -08:00
|
|
|
let (constr_name, constr_args) =
|
1999-10-29 07:42:37 -07:00
|
|
|
Datarepr.find_constr_by_tag tag constr_list in
|
1997-03-22 12:16:52 -08:00
|
|
|
let ty_args =
|
|
|
|
List.map
|
|
|
|
(function ty ->
|
|
|
|
try Ctype.apply env decl.type_params ty ty_list with
|
|
|
|
Ctype.Cannot_apply -> abstract_type)
|
|
|
|
constr_args in
|
2001-08-02 01:51:55 -07:00
|
|
|
tree_of_constr_with_args (tree_of_constr env path)
|
|
|
|
constr_name 0 depth obj ty_args
|
2000-03-21 06:43:25 -08:00
|
|
|
| {type_kind = Type_record(lbl_list, rep)} ->
|
2001-08-02 01:51:55 -07:00
|
|
|
match check_depth depth obj ty with
|
|
|
|
Some x -> x
|
|
|
|
| None ->
|
|
|
|
let rec tree_of_fields pos = function
|
|
|
|
| [] -> []
|
|
|
|
| (lbl_name, _, lbl_arg) :: remainder ->
|
|
|
|
let ty_arg =
|
|
|
|
try
|
|
|
|
Ctype.apply env decl.type_params lbl_arg
|
|
|
|
ty_list
|
|
|
|
with
|
|
|
|
Ctype.Cannot_apply -> abstract_type in
|
|
|
|
let lid = tree_of_label env path lbl_name in
|
|
|
|
let v =
|
|
|
|
tree_of_val (depth - 1) (O.field obj pos)
|
|
|
|
ty_arg
|
|
|
|
in
|
|
|
|
(lid, v) :: tree_of_fields (pos + 1) remainder
|
|
|
|
in
|
|
|
|
Oval_record (tree_of_fields 0 lbl_list)
|
1997-03-22 12:16:52 -08:00
|
|
|
with
|
|
|
|
Not_found -> (* raised by Env.find_type *)
|
2001-08-02 01:51:55 -07:00
|
|
|
Oval_stuff "<abstr>"
|
1999-10-29 07:42:37 -07:00
|
|
|
| Datarepr.Constr_not_found -> (* raised by find_constr_by_tag *)
|
2001-08-02 01:51:55 -07:00
|
|
|
Oval_stuff "<unknown constructor>"
|
1997-03-22 12:16:52 -08:00
|
|
|
end
|
2000-01-07 08:47:25 -08:00
|
|
|
| Tvariant row ->
|
|
|
|
let row = Btype.row_repr row in
|
2001-08-02 01:51:55 -07:00
|
|
|
if O.is_block obj then
|
2000-01-07 08:47:25 -08:00
|
|
|
let tag : int = O.obj (O.field obj 0) in
|
2001-08-02 01:51:55 -07:00
|
|
|
let rec find = function
|
|
|
|
| (l, f) :: fields ->
|
|
|
|
if Btype.hash_variant l = tag then
|
|
|
|
match Btype.row_field_repr f with
|
|
|
|
| Rpresent(Some ty) ->
|
|
|
|
let args =
|
|
|
|
tree_of_val (depth - 1) (O.field obj 1) ty in
|
|
|
|
Oval_variant (l, Some args)
|
|
|
|
| _ -> find fields
|
|
|
|
else find fields
|
|
|
|
| [] -> Oval_stuff "<variant>" in
|
|
|
|
find row.row_fields
|
|
|
|
else
|
2000-01-07 08:47:25 -08:00
|
|
|
let tag : int = O.obj obj in
|
2001-08-02 01:51:55 -07:00
|
|
|
let rec find = function
|
|
|
|
| (l, _) :: fields ->
|
|
|
|
if Btype.hash_variant l = tag then
|
|
|
|
Oval_variant (l, None)
|
|
|
|
else find fields
|
|
|
|
| [] -> Oval_stuff "<variant>" in
|
|
|
|
find row.row_fields
|
1997-03-22 12:16:52 -08:00
|
|
|
| Tobject (_, _) ->
|
2001-08-02 01:51:55 -07:00
|
|
|
Oval_stuff "<obj>"
|
2000-01-07 08:47:25 -08:00
|
|
|
| Tsubst ty ->
|
2001-08-02 01:51:55 -07:00
|
|
|
tree_of_val (depth - 1) obj ty
|
1997-05-11 14:48:21 -07:00
|
|
|
| Tfield(_, _, _, _) | Tnil | Tlink _ ->
|
2001-08-02 01:51:55 -07:00
|
|
|
fatal_error "Printval.outval_of_value"
|
|
|
|
end
|
1997-03-22 12:16:52 -08:00
|
|
|
|
2001-08-02 01:51:55 -07:00
|
|
|
and tree_of_val_list start depth obj ty_list =
|
|
|
|
let rec tree_list i = function
|
|
|
|
| [] -> []
|
2000-03-06 14:12:09 -08:00
|
|
|
| ty :: ty_list ->
|
2001-08-02 01:51:55 -07:00
|
|
|
let tree = tree_of_val (depth - 1) (O.field obj i) ty in
|
|
|
|
tree :: tree_list (i + 1) ty_list in
|
|
|
|
tree_list start ty_list
|
|
|
|
|
|
|
|
and tree_of_constr_with_args
|
|
|
|
tree_of_cstr cstr_name start depth obj ty_args =
|
|
|
|
let lid = tree_of_cstr cstr_name in
|
|
|
|
let args = tree_of_val_list start depth obj ty_args in
|
|
|
|
Oval_constr (lid, args)
|
|
|
|
|
|
|
|
and tree_of_exception depth bucket =
|
2000-03-25 10:55:45 -08:00
|
|
|
let name = (O.obj(O.field(O.field bucket 0) 0) : string) in
|
|
|
|
let lid = Longident.parse name in
|
|
|
|
try
|
|
|
|
(* Attempt to recover the constructor description for the exn
|
|
|
|
from its name *)
|
|
|
|
let cstr = Env.lookup_constructor lid env in
|
|
|
|
let path =
|
|
|
|
match cstr.cstr_tag with
|
|
|
|
Cstr_exception p -> p | _ -> raise Not_found in
|
|
|
|
(* Make sure this is the right exception and not an homonym,
|
2001-08-02 01:51:55 -07:00
|
|
|
by evaluating the exception found and comparing with the
|
|
|
|
identifier contained in the exception bucket *)
|
2000-03-26 04:11:10 -08:00
|
|
|
if not (EVP.same_value (O.field bucket 0) (EVP.eval_path path))
|
|
|
|
then raise Not_found;
|
2001-08-02 01:51:55 -07:00
|
|
|
tree_of_constr_with_args
|
|
|
|
(fun x -> Oide_ident x) name 1 depth bucket cstr.cstr_args
|
2000-03-25 10:55:45 -08:00
|
|
|
with Not_found | EVP.Error ->
|
2001-08-02 01:51:55 -07:00
|
|
|
match check_depth depth obj ty with
|
|
|
|
Some x -> x
|
|
|
|
| None -> outval_of_untyped_exception obj
|
|
|
|
|
|
|
|
in tree_of_val max_depth obj ty
|
|
|
|
|
|
|
|
exception Ellipsis
|
|
|
|
|
2001-08-07 13:04:59 -07:00
|
|
|
let cautious f ppf arg = try f ppf arg with Ellipsis -> fprintf ppf "..."
|
1997-03-22 12:16:52 -08:00
|
|
|
|
2001-08-02 01:51:55 -07:00
|
|
|
let print_outval ppf tree =
|
|
|
|
let rec print_ident ppf =
|
|
|
|
function
|
|
|
|
Oide_ident s -> fprintf ppf "%s" s
|
|
|
|
| Oide_dot (id, s) -> fprintf ppf "%a.%s" print_ident id s
|
|
|
|
| Oide_apply (id1, id2) ->
|
|
|
|
fprintf ppf "%a(%a)" print_ident id1 print_ident id2
|
|
|
|
in
|
|
|
|
let rec print_tree ppf =
|
|
|
|
function
|
|
|
|
Oval_tuple tree_list ->
|
|
|
|
fprintf ppf "@[%a@]" (print_tree_list print_tree_1 ",") tree_list
|
|
|
|
| tree -> print_tree_1 ppf tree
|
|
|
|
and print_tree_1 ppf =
|
|
|
|
function
|
|
|
|
Oval_constr (name, [param]) ->
|
|
|
|
fprintf ppf "@[<1>%a@ %a@]" print_ident name print_simple_tree
|
|
|
|
param
|
|
|
|
| Oval_constr (name, (_ :: _ as params)) ->
|
|
|
|
fprintf ppf "@[<1>%a@ (%a)@]" print_ident name
|
|
|
|
(print_tree_list print_tree_1 ",") params
|
|
|
|
| Oval_variant (name, Some param) ->
|
|
|
|
fprintf ppf "@[<2>`%s@ %a@]" name print_simple_tree param
|
|
|
|
| tree -> print_simple_tree ppf tree
|
|
|
|
and print_simple_tree ppf =
|
|
|
|
function
|
|
|
|
Oval_int i -> fprintf ppf "%i" i
|
|
|
|
| Oval_float f -> fprintf ppf "%.12g" f
|
|
|
|
| Oval_char c -> fprintf ppf "'%s'" (Char.escaped c)
|
2001-08-08 07:25:39 -07:00
|
|
|
| Oval_string s ->
|
|
|
|
(* String.escaped may raise [Invalid_argument "String.create"]
|
|
|
|
if the escaped string is longer than [Sys.max_string_length] *)
|
|
|
|
begin try
|
|
|
|
fprintf ppf "\"%s\"" (String.escaped s)
|
|
|
|
with Invalid_argument "String.create" ->
|
|
|
|
fprintf ppf "<huge string>"
|
|
|
|
end
|
2001-08-02 01:51:55 -07:00
|
|
|
| Oval_list tl ->
|
|
|
|
fprintf ppf "@[<1>[%a]@]" (print_tree_list print_tree_1 ";") tl
|
|
|
|
| Oval_array tl ->
|
|
|
|
fprintf ppf "@[<2>[|%a|]@]" (print_tree_list print_tree_1 ";") tl
|
|
|
|
| Oval_constr (name, []) -> print_ident ppf name
|
|
|
|
| Oval_variant (name, None) -> fprintf ppf "`%s" name
|
|
|
|
| Oval_stuff s -> fprintf ppf "%s" s
|
|
|
|
| Oval_record fel ->
|
2001-08-07 13:04:59 -07:00
|
|
|
fprintf ppf "@[<1>{%a}@]" (cautious (print_fields true)) fel
|
2001-08-02 01:51:55 -07:00
|
|
|
| Oval_ellipsis -> raise Ellipsis
|
|
|
|
| Oval_printer f -> f ppf
|
2001-08-07 13:04:59 -07:00
|
|
|
| tree -> fprintf ppf "@[<1>(%a)@]" (cautious print_tree) tree
|
2001-08-02 01:51:55 -07:00
|
|
|
and print_fields first ppf =
|
|
|
|
function
|
|
|
|
[] -> ()
|
|
|
|
| (name, tree) :: fields ->
|
|
|
|
if not first then fprintf ppf ";@ ";
|
2001-09-15 09:34:59 -07:00
|
|
|
fprintf ppf "@[<1>%a@ =@ %a@]" print_ident name
|
2001-08-07 13:04:59 -07:00
|
|
|
(cautious print_tree) tree;
|
2001-08-02 01:51:55 -07:00
|
|
|
print_fields false ppf fields
|
|
|
|
and print_tree_list print_item sep ppf tree_list =
|
2001-08-07 13:04:59 -07:00
|
|
|
let rec print_list first ppf =
|
2001-08-02 01:51:55 -07:00
|
|
|
function
|
|
|
|
[] -> ()
|
|
|
|
| tree :: tree_list ->
|
|
|
|
if not first then fprintf ppf "%s@ " sep;
|
|
|
|
print_item ppf tree;
|
2001-08-07 13:04:59 -07:00
|
|
|
print_list false ppf tree_list
|
2001-08-02 01:51:55 -07:00
|
|
|
in
|
|
|
|
cautious (print_list true) ppf tree_list
|
|
|
|
in
|
2001-08-07 13:04:59 -07:00
|
|
|
cautious print_tree ppf tree
|
1997-03-22 12:16:52 -08:00
|
|
|
|
|
|
|
end
|