2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
|
|
|
(* Projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2002 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. This file is distributed under the terms of *)
|
|
|
|
(* the GNU Lesser General Public License version 2.1, with the *)
|
|
|
|
(* special exception on linking described in the file LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
2002-02-13 03:09:19 -08:00
|
|
|
|
|
|
|
open Format
|
|
|
|
open Outcometree
|
|
|
|
|
|
|
|
exception Ellipsis
|
|
|
|
|
2002-02-13 04:51:21 -08:00
|
|
|
let cautious f ppf arg =
|
|
|
|
try f ppf arg with
|
|
|
|
Ellipsis -> fprintf ppf "..."
|
2002-02-13 03:09:19 -08:00
|
|
|
|
|
|
|
let rec print_ident ppf =
|
|
|
|
function
|
2012-11-07 02:50:14 -08:00
|
|
|
Oide_ident s -> pp_print_string ppf s
|
2013-09-04 08:12:37 -07:00
|
|
|
| Oide_dot (id, s) ->
|
|
|
|
print_ident ppf id; pp_print_char ppf '.'; pp_print_string ppf s
|
2002-02-13 03:09:19 -08:00
|
|
|
| Oide_apply (id1, id2) ->
|
|
|
|
fprintf ppf "%a(%a)" print_ident id1 print_ident id2
|
|
|
|
|
2010-05-31 06:18:11 -07:00
|
|
|
let parenthesized_ident name =
|
|
|
|
(List.mem name ["or"; "mod"; "land"; "lor"; "lxor"; "lsl"; "lsr"; "asr"])
|
|
|
|
||
|
|
|
|
(match name.[0] with
|
2011-12-21 00:58:56 -08:00
|
|
|
'a'..'z' | 'A'..'Z' | '\223'..'\246' | '\248'..'\255' | '_' ->
|
|
|
|
false
|
|
|
|
| _ -> true)
|
2010-05-31 06:18:11 -07:00
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
let value_ident ppf name =
|
2010-05-31 06:18:11 -07:00
|
|
|
if parenthesized_ident name then
|
2002-02-13 04:51:21 -08:00
|
|
|
fprintf ppf "( %s )" name
|
|
|
|
else
|
2012-11-07 02:50:14 -08:00
|
|
|
pp_print_string ppf name
|
2002-02-13 03:09:19 -08:00
|
|
|
|
|
|
|
(* Values *)
|
|
|
|
|
2003-07-29 02:11:13 -07:00
|
|
|
let valid_float_lexeme s =
|
|
|
|
let l = String.length s in
|
|
|
|
let rec loop i =
|
|
|
|
if i >= l then s ^ "." else
|
|
|
|
match s.[i] with
|
|
|
|
| '0' .. '9' | '-' -> loop (i+1)
|
|
|
|
| _ -> s
|
|
|
|
in loop 0
|
|
|
|
|
|
|
|
let float_repres f =
|
|
|
|
match classify_float f with
|
|
|
|
FP_nan -> "nan"
|
|
|
|
| FP_infinite ->
|
|
|
|
if f < 0.0 then "neg_infinity" else "infinity"
|
|
|
|
| _ ->
|
2008-10-08 06:09:39 -07:00
|
|
|
let float_val =
|
|
|
|
let s1 = Printf.sprintf "%.12g" f in
|
|
|
|
if f = float_of_string s1 then s1 else
|
|
|
|
let s2 = Printf.sprintf "%.15g" f in
|
|
|
|
if f = float_of_string s2 then s2 else
|
|
|
|
Printf.sprintf "%.18g" f
|
|
|
|
in valid_float_lexeme float_val
|
2003-07-29 02:11:13 -07:00
|
|
|
|
|
|
|
let parenthesize_if_neg ppf fmt v isneg =
|
|
|
|
if isneg then pp_print_char ppf '(';
|
2003-04-25 05:27:31 -07:00
|
|
|
fprintf ppf fmt v;
|
2003-07-29 02:11:13 -07:00
|
|
|
if isneg then pp_print_char ppf ')'
|
2003-04-25 05:27:31 -07:00
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
let print_out_value ppf tree =
|
2002-05-28 09:53:51 -07:00
|
|
|
let rec print_tree_1 ppf =
|
2002-02-13 03:09:19 -08:00
|
|
|
function
|
2002-09-05 03:24:41 -07:00
|
|
|
| Oval_constr (name, [param]) ->
|
|
|
|
fprintf ppf "@[<1>%a@ %a@]" print_ident name print_constr_param param
|
2002-02-13 03:09:19 -08:00
|
|
|
| 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) ->
|
2008-01-11 08:13:18 -08:00
|
|
|
fprintf ppf "@[<2>`%s@ %a@]" name print_constr_param param
|
2002-02-13 03:09:19 -08:00
|
|
|
| tree -> print_simple_tree ppf tree
|
2002-09-05 03:24:41 -07:00
|
|
|
and print_constr_param ppf = function
|
2003-07-29 02:11:13 -07:00
|
|
|
| Oval_int i -> parenthesize_if_neg ppf "%i" i (i < 0)
|
|
|
|
| Oval_int32 i -> parenthesize_if_neg ppf "%lil" i (i < 0l)
|
|
|
|
| Oval_int64 i -> parenthesize_if_neg ppf "%LiL" i (i < 0L)
|
|
|
|
| Oval_nativeint i -> parenthesize_if_neg ppf "%nin" i (i < 0n)
|
|
|
|
| Oval_float f -> parenthesize_if_neg ppf "%s" (float_repres f) (f < 0.0)
|
2002-09-05 03:24:41 -07:00
|
|
|
| tree -> print_simple_tree ppf tree
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_simple_tree ppf =
|
|
|
|
function
|
|
|
|
Oval_int i -> fprintf ppf "%i" i
|
2003-04-25 05:27:31 -07:00
|
|
|
| Oval_int32 i -> fprintf ppf "%lil" i
|
|
|
|
| Oval_int64 i -> fprintf ppf "%LiL" i
|
|
|
|
| Oval_nativeint i -> fprintf ppf "%nin" i
|
2012-11-07 02:50:14 -08:00
|
|
|
| Oval_float f -> pp_print_string ppf (float_repres f)
|
2002-06-27 02:27:14 -07:00
|
|
|
| Oval_char c -> fprintf ppf "%C" c
|
2002-02-13 03:09:19 -08:00
|
|
|
| Oval_string s ->
|
2002-06-27 02:27:14 -07:00
|
|
|
begin try fprintf ppf "%S" s with
|
2015-10-15 05:30:10 -07:00
|
|
|
Invalid_argument _ (* "String.create" *)-> fprintf ppf "<huge string>"
|
2002-02-13 03:09:19 -08:00
|
|
|
end
|
|
|
|
| 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
|
2012-11-07 02:50:14 -08:00
|
|
|
| Oval_stuff s -> pp_print_string ppf s
|
2002-02-13 03:09:19 -08:00
|
|
|
| Oval_record fel ->
|
|
|
|
fprintf ppf "@[<1>{%a}@]" (cautious (print_fields true)) fel
|
|
|
|
| Oval_ellipsis -> raise Ellipsis
|
|
|
|
| Oval_printer f -> f ppf
|
2002-05-28 09:53:51 -07:00
|
|
|
| Oval_tuple tree_list ->
|
2003-05-12 21:03:22 -07:00
|
|
|
fprintf ppf "@[<1>(%a)@]" (print_tree_list print_tree_1 ",") tree_list
|
2002-05-28 09:53:51 -07:00
|
|
|
| tree -> fprintf ppf "@[<1>(%a)@]" (cautious print_tree_1) tree
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_fields first ppf =
|
|
|
|
function
|
|
|
|
[] -> ()
|
|
|
|
| (name, tree) :: fields ->
|
|
|
|
if not first then fprintf ppf ";@ ";
|
2002-05-28 09:53:51 -07:00
|
|
|
fprintf ppf "@[<1>%a@ =@ %a@]" print_ident name (cautious print_tree_1)
|
2002-02-13 04:51:21 -08:00
|
|
|
tree;
|
2002-02-13 03:09:19 -08:00
|
|
|
print_fields false ppf fields
|
|
|
|
and print_tree_list print_item sep ppf tree_list =
|
|
|
|
let rec print_list first ppf =
|
|
|
|
function
|
|
|
|
[] -> ()
|
|
|
|
| tree :: tree_list ->
|
|
|
|
if not first then fprintf ppf "%s@ " sep;
|
|
|
|
print_item ppf tree;
|
|
|
|
print_list false ppf tree_list
|
|
|
|
in
|
|
|
|
cautious (print_list true) ppf tree_list
|
|
|
|
in
|
2002-05-28 09:53:51 -07:00
|
|
|
cautious print_tree_1 ppf tree
|
2002-02-13 03:09:19 -08:00
|
|
|
|
2002-02-13 05:08:57 -08:00
|
|
|
let out_value = ref print_out_value
|
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
(* Types *)
|
|
|
|
|
2002-02-13 04:51:21 -08:00
|
|
|
let rec print_list_init pr sep ppf =
|
|
|
|
function
|
|
|
|
[] -> ()
|
2002-02-13 04:17:56 -08:00
|
|
|
| a :: l -> sep ppf; pr ppf a; print_list_init pr sep ppf l
|
2002-02-13 03:09:19 -08:00
|
|
|
|
2002-02-13 04:51:21 -08:00
|
|
|
let rec print_list pr sep ppf =
|
|
|
|
function
|
|
|
|
[] -> ()
|
2002-02-13 03:09:19 -08:00
|
|
|
| [a] -> pr ppf a
|
2002-02-13 04:17:56 -08:00
|
|
|
| a :: l -> pr ppf a; sep ppf; print_list pr sep ppf l
|
2002-02-13 03:09:19 -08:00
|
|
|
|
|
|
|
let pr_present =
|
|
|
|
print_list (fun ppf s -> fprintf ppf "`%s" s) (fun ppf -> fprintf ppf "@ ")
|
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
let pr_vars =
|
|
|
|
print_list (fun ppf s -> fprintf ppf "'%s" s) (fun ppf -> fprintf ppf "@ ")
|
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
let rec print_out_type ppf =
|
|
|
|
function
|
2002-04-18 00:27:47 -07:00
|
|
|
| Otyp_alias (ty, s) ->
|
2003-05-12 21:03:22 -07:00
|
|
|
fprintf ppf "@[%a@ as '%s@]" print_out_type ty s
|
2002-04-18 00:27:47 -07:00
|
|
|
| Otyp_poly (sl, ty) ->
|
|
|
|
fprintf ppf "@[<hov 2>%a.@ %a@]"
|
|
|
|
pr_vars sl
|
|
|
|
print_out_type ty
|
|
|
|
| ty ->
|
|
|
|
print_out_type_1 ppf ty
|
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_out_type_1 ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
Otyp_arrow (lab, ty1, ty2) ->
|
2012-11-07 02:50:14 -08:00
|
|
|
pp_open_box ppf 0;
|
|
|
|
if lab <> "" then (pp_print_string ppf lab; pp_print_char ppf ':');
|
|
|
|
print_out_type_2 ppf ty1;
|
|
|
|
pp_print_string ppf " ->";
|
|
|
|
pp_print_space ppf ();
|
|
|
|
print_out_type_1 ppf ty2;
|
|
|
|
pp_close_box ppf ()
|
2002-02-13 04:51:21 -08:00
|
|
|
| ty -> print_out_type_2 ppf ty
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_out_type_2 ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
Otyp_tuple tyl ->
|
2002-02-13 03:09:19 -08:00
|
|
|
fprintf ppf "@[<0>%a@]" (print_typlist print_simple_out_type " *") tyl
|
2002-02-13 04:51:21 -08:00
|
|
|
| ty -> print_simple_out_type ppf ty
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_simple_out_type ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
Otyp_class (ng, id, tyl) ->
|
|
|
|
fprintf ppf "@[%a%s#%a@]" print_typargs tyl (if ng then "_" else "")
|
|
|
|
print_ident id
|
2002-02-13 03:09:19 -08:00
|
|
|
| Otyp_constr (id, tyl) ->
|
2012-11-07 02:50:14 -08:00
|
|
|
pp_open_box ppf 0;
|
|
|
|
print_typargs ppf tyl;
|
|
|
|
print_ident ppf id;
|
|
|
|
pp_close_box ppf ()
|
2002-02-13 03:09:19 -08:00
|
|
|
| Otyp_object (fields, rest) ->
|
|
|
|
fprintf ppf "@[<2>< %a >@]" (print_fields rest) fields
|
2012-11-07 02:50:14 -08:00
|
|
|
| Otyp_stuff s -> pp_print_string ppf s
|
2002-02-13 04:51:21 -08:00
|
|
|
| Otyp_var (ng, s) -> fprintf ppf "'%s%s" (if ng then "_" else "") s
|
2002-02-13 03:09:19 -08:00
|
|
|
| Otyp_variant (non_gen, row_fields, closed, tags) ->
|
|
|
|
let print_present ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
None | Some [] -> ()
|
2002-02-13 03:09:19 -08:00
|
|
|
| Some l -> fprintf ppf "@;<1 -2>> @[<hov>%a@]" pr_present l
|
|
|
|
in
|
2002-02-13 04:51:21 -08:00
|
|
|
let print_fields ppf =
|
|
|
|
function
|
2002-02-13 03:09:19 -08:00
|
|
|
Ovar_fields fields ->
|
|
|
|
print_list print_row_field (fun ppf -> fprintf ppf "@;<1 -2>| ")
|
|
|
|
ppf fields
|
|
|
|
| Ovar_name (id, tyl) ->
|
|
|
|
fprintf ppf "@[%a%a@]" print_typargs tyl print_ident id
|
|
|
|
in
|
2003-05-12 21:03:22 -07:00
|
|
|
fprintf ppf "%s[%s@[<hv>@[<hv>%a@]%a ]@]" (if non_gen then "_" else "")
|
2002-02-13 03:09:19 -08:00
|
|
|
(if closed then if tags = None then " " else "< "
|
2002-04-18 00:27:47 -07:00
|
|
|
else if tags = None then "> " else "? ")
|
|
|
|
print_fields row_fields
|
|
|
|
print_present tags
|
|
|
|
| Otyp_alias _ | Otyp_poly _ | Otyp_arrow _ | Otyp_tuple _ as ty ->
|
2012-11-07 02:50:14 -08:00
|
|
|
pp_open_box ppf 1;
|
|
|
|
pp_print_char ppf '(';
|
|
|
|
print_out_type ppf ty;
|
|
|
|
pp_print_char ppf ')';
|
|
|
|
pp_close_box ppf ()
|
2014-05-04 16:08:45 -07:00
|
|
|
| Otyp_abstract | Otyp_open
|
2014-10-14 08:51:30 -07:00
|
|
|
| Otyp_sum _ | Otyp_manifest (_, _) -> ()
|
2014-04-07 05:42:49 -07:00
|
|
|
| Otyp_record lbls -> print_record_decl ppf lbls
|
2009-10-26 03:53:16 -07:00
|
|
|
| Otyp_module (p, n, tyl) ->
|
|
|
|
fprintf ppf "@[<1>(module %s" p;
|
|
|
|
let first = ref true in
|
|
|
|
List.iter2
|
|
|
|
(fun s t ->
|
|
|
|
let sep = if !first then (first := false; "with") else "and" in
|
|
|
|
fprintf ppf " %s type %s = %a" sep s print_out_type t
|
|
|
|
)
|
|
|
|
n tyl;
|
|
|
|
fprintf ppf ")@]"
|
2015-08-25 09:18:48 -07:00
|
|
|
| Otyp_attribute (t, attr) ->
|
2015-10-06 03:58:25 -07:00
|
|
|
fprintf ppf "@[<1>(%a [@@%s])@]" print_out_type t attr.oattr_name
|
2014-04-07 05:42:49 -07:00
|
|
|
and print_record_decl ppf lbls =
|
|
|
|
fprintf ppf "{%a@;<1 -2>}"
|
|
|
|
(print_list_init print_out_label (fun ppf -> fprintf ppf "@ ")) lbls
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_fields rest ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
[] ->
|
2002-02-13 03:09:19 -08:00
|
|
|
begin match rest with
|
2002-02-13 04:51:21 -08:00
|
|
|
Some non_gen -> fprintf ppf "%s.." (if non_gen then "_" else "")
|
2002-02-13 03:09:19 -08:00
|
|
|
| None -> ()
|
|
|
|
end
|
2002-02-13 04:51:21 -08:00
|
|
|
| [s, t] ->
|
2002-02-13 03:09:19 -08:00
|
|
|
fprintf ppf "%s : %a" s print_out_type t;
|
|
|
|
begin match rest with
|
2002-02-13 04:51:21 -08:00
|
|
|
Some _ -> fprintf ppf ";@ "
|
2002-02-13 03:09:19 -08:00
|
|
|
| None -> ()
|
|
|
|
end;
|
|
|
|
print_fields rest ppf []
|
|
|
|
| (s, t) :: l ->
|
|
|
|
fprintf ppf "%s : %a;@ %a" s print_out_type t (print_fields rest) l
|
|
|
|
and print_row_field ppf (l, opt_amp, tyl) =
|
|
|
|
let pr_of ppf =
|
|
|
|
if opt_amp then fprintf ppf " of@ &@ "
|
|
|
|
else if tyl <> [] then fprintf ppf " of@ "
|
|
|
|
else fprintf ppf ""
|
|
|
|
in
|
2002-02-13 04:51:21 -08:00
|
|
|
fprintf ppf "@[<hv 2>`%s%t%a@]" l pr_of (print_typlist print_out_type " &")
|
|
|
|
tyl
|
|
|
|
and print_typlist print_elem sep ppf =
|
|
|
|
function
|
|
|
|
[] -> ()
|
2002-02-13 03:09:19 -08:00
|
|
|
| [ty] -> print_elem ppf ty
|
|
|
|
| ty :: tyl ->
|
2012-11-07 02:50:14 -08:00
|
|
|
print_elem ppf ty;
|
|
|
|
pp_print_string ppf sep;
|
|
|
|
pp_print_space ppf ();
|
|
|
|
print_typlist print_elem sep ppf tyl
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_typargs ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
[] -> ()
|
2012-11-07 02:50:14 -08:00
|
|
|
| [ty1] -> print_simple_out_type ppf ty1; pp_print_space ppf ()
|
|
|
|
| tyl ->
|
|
|
|
pp_open_box ppf 1;
|
|
|
|
pp_print_char ppf '(';
|
|
|
|
print_typlist print_out_type "," ppf tyl;
|
|
|
|
pp_print_char ppf ')';
|
|
|
|
pp_close_box ppf ();
|
|
|
|
pp_print_space ppf ()
|
2014-04-07 05:42:49 -07:00
|
|
|
and print_out_label ppf (name, mut, arg) =
|
|
|
|
fprintf ppf "@[<2>%s%s :@ %a@];" (if mut then "mutable " else "") name
|
|
|
|
print_out_type arg
|
2002-02-13 03:09:19 -08:00
|
|
|
|
2002-02-13 05:08:57 -08:00
|
|
|
let out_type = ref print_out_type
|
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
(* Class types *)
|
|
|
|
|
2004-12-09 04:40:53 -08:00
|
|
|
let type_parameter ppf (ty, (co, cn)) =
|
2011-12-27 18:22:38 -08:00
|
|
|
fprintf ppf "%s%s"
|
|
|
|
(if not cn then "+" else if not co then "-" else "")
|
|
|
|
(if ty = "_" then ty else "'"^ty)
|
2004-12-09 04:40:53 -08:00
|
|
|
|
2002-02-13 03:09:19 -08:00
|
|
|
let print_out_class_params ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
[] -> ()
|
2002-02-13 03:09:19 -08:00
|
|
|
| tyl ->
|
|
|
|
fprintf ppf "@[<1>[%a]@]@ "
|
2004-12-09 04:40:53 -08:00
|
|
|
(print_list type_parameter (fun ppf -> fprintf ppf ", "))
|
2002-02-13 03:09:19 -08:00
|
|
|
tyl
|
|
|
|
|
|
|
|
let rec print_out_class_type ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
Octy_constr (id, tyl) ->
|
|
|
|
let pr_tyl ppf =
|
|
|
|
function
|
|
|
|
[] -> ()
|
2002-02-13 03:09:19 -08:00
|
|
|
| tyl ->
|
2002-02-13 05:08:57 -08:00
|
|
|
fprintf ppf "@[<1>[%a]@]@ " (print_typlist !out_type ",") tyl
|
2002-02-13 03:09:19 -08:00
|
|
|
in
|
|
|
|
fprintf ppf "@[%a%a@]" pr_tyl tyl print_ident id
|
2013-04-16 01:59:09 -07:00
|
|
|
| Octy_arrow (lab, ty, cty) ->
|
2002-02-13 04:51:21 -08:00
|
|
|
fprintf ppf "@[%s%a ->@ %a@]" (if lab <> "" then lab ^ ":" else "")
|
2002-02-13 03:09:19 -08:00
|
|
|
print_out_type_2 ty print_out_class_type cty
|
|
|
|
| Octy_signature (self_ty, csil) ->
|
|
|
|
let pr_param ppf =
|
|
|
|
function
|
2002-02-13 05:08:57 -08:00
|
|
|
Some ty -> fprintf ppf "@ @[(%a)@]" !out_type ty
|
2002-02-13 03:09:19 -08:00
|
|
|
| None -> ()
|
|
|
|
in
|
2002-02-13 04:51:21 -08:00
|
|
|
fprintf ppf "@[<hv 2>@[<2>object%a@]@ %a@;<1 -2>end@]" pr_param self_ty
|
2002-02-13 03:09:19 -08:00
|
|
|
(print_list print_out_class_sig_item (fun ppf -> fprintf ppf "@ "))
|
|
|
|
csil
|
|
|
|
and print_out_class_sig_item ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
Ocsg_constraint (ty1, ty2) ->
|
2002-02-13 05:08:57 -08:00
|
|
|
fprintf ppf "@[<2>constraint %a =@ %a@]" !out_type ty1
|
|
|
|
!out_type ty2
|
2002-02-13 03:09:19 -08:00
|
|
|
| Ocsg_method (name, priv, virt, ty) ->
|
2002-02-13 04:51:21 -08:00
|
|
|
fprintf ppf "@[<2>method %s%s%s :@ %a@]"
|
|
|
|
(if priv then "private " else "") (if virt then "virtual " else "")
|
2002-02-13 05:08:57 -08:00
|
|
|
name !out_type ty
|
2006-04-04 19:28:13 -07:00
|
|
|
| Ocsg_value (name, mut, vr, ty) ->
|
|
|
|
fprintf ppf "@[<2>val %s%s%s :@ %a@]"
|
|
|
|
(if mut then "mutable " else "")
|
|
|
|
(if vr then "virtual " else "")
|
2002-02-13 05:08:57 -08:00
|
|
|
name !out_type ty
|
|
|
|
|
|
|
|
let out_class_type = ref print_out_class_type
|
2002-02-13 03:09:19 -08:00
|
|
|
|
|
|
|
(* Signature *)
|
|
|
|
|
2002-02-13 05:08:57 -08:00
|
|
|
let out_module_type = ref (fun _ -> failwith "Oprint.out_module_type")
|
|
|
|
let out_sig_item = ref (fun _ -> failwith "Oprint.out_sig_item")
|
|
|
|
let out_signature = ref (fun _ -> failwith "Oprint.out_signature")
|
2014-05-04 16:08:45 -07:00
|
|
|
let out_type_extension = ref (fun _ -> failwith "Oprint.out_type_extension")
|
2002-02-13 05:08:57 -08:00
|
|
|
|
2015-10-25 09:24:47 -07:00
|
|
|
let rec print_out_functor funct ppf =
|
2014-03-20 08:21:00 -07:00
|
|
|
function
|
|
|
|
Omty_functor (_, None, mty_res) ->
|
2015-10-25 09:24:47 -07:00
|
|
|
if funct then fprintf ppf "() %a" (print_out_functor true) mty_res
|
|
|
|
else fprintf ppf "functor@ () %a" (print_out_functor true) mty_res
|
|
|
|
| Omty_functor (name, Some mty_arg, mty_res) -> begin
|
|
|
|
match name, funct with
|
|
|
|
| "_", true ->
|
|
|
|
fprintf ppf "->@ %a ->@ %a"
|
|
|
|
print_out_module_type mty_arg (print_out_functor false) mty_res
|
|
|
|
| "_", false ->
|
|
|
|
fprintf ppf "%a ->@ %a"
|
|
|
|
print_out_module_type mty_arg (print_out_functor false) mty_res
|
|
|
|
| name, true ->
|
|
|
|
fprintf ppf "(%s : %a) %a" name
|
|
|
|
print_out_module_type mty_arg (print_out_functor true) mty_res
|
|
|
|
| name, false ->
|
|
|
|
fprintf ppf "functor@ (%s : %a) %a" name
|
|
|
|
print_out_module_type mty_arg (print_out_functor true) mty_res
|
|
|
|
end
|
|
|
|
| m ->
|
|
|
|
if funct then fprintf ppf "->@ %a" print_out_module_type m
|
|
|
|
else print_out_module_type ppf m
|
|
|
|
|
2014-03-20 08:21:00 -07:00
|
|
|
and print_out_module_type ppf =
|
2002-02-13 03:09:19 -08:00
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
Omty_abstract -> ()
|
2014-03-20 08:21:00 -07:00
|
|
|
| Omty_functor _ as t ->
|
2015-10-25 09:24:47 -07:00
|
|
|
fprintf ppf "@[<2>%a@]" (print_out_functor false) t
|
2002-02-13 04:51:21 -08:00
|
|
|
| Omty_ident id -> fprintf ppf "%a" print_ident id
|
2002-02-13 03:09:19 -08:00
|
|
|
| Omty_signature sg ->
|
2002-02-13 05:08:57 -08:00
|
|
|
fprintf ppf "@[<hv 2>sig@ %a@;<1 -2>end@]" !out_signature sg
|
2013-09-29 00:22:34 -07:00
|
|
|
| Omty_alias id -> fprintf ppf "(module %a)" print_ident id
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_out_signature ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
[] -> ()
|
2002-02-13 05:08:57 -08:00
|
|
|
| [item] -> !out_sig_item ppf item
|
2014-05-04 16:08:45 -07:00
|
|
|
| Osig_typext(ext, Oext_first) :: items ->
|
|
|
|
(* Gather together the extension constructors *)
|
|
|
|
let rec gather_extensions acc items =
|
|
|
|
match items with
|
|
|
|
Osig_typext(ext, Oext_next) :: items ->
|
|
|
|
gather_extensions
|
|
|
|
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
|
|
|
|
items
|
|
|
|
| _ -> (List.rev acc, items)
|
|
|
|
in
|
|
|
|
let exts, items =
|
|
|
|
gather_extensions
|
|
|
|
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
|
|
|
|
items
|
|
|
|
in
|
|
|
|
let te =
|
|
|
|
{ otyext_name = ext.oext_type_name;
|
|
|
|
otyext_params = ext.oext_type_params;
|
|
|
|
otyext_constructors = exts;
|
|
|
|
otyext_private = ext.oext_private }
|
|
|
|
in
|
|
|
|
fprintf ppf "%a@ %a" !out_type_extension te print_out_signature items
|
2002-02-13 03:09:19 -08:00
|
|
|
| item :: items ->
|
2002-02-13 05:08:57 -08:00
|
|
|
fprintf ppf "%a@ %a" !out_sig_item item print_out_signature items
|
2002-02-13 03:09:19 -08:00
|
|
|
and print_out_sig_item ppf =
|
|
|
|
function
|
2004-06-12 01:55:49 -07:00
|
|
|
Osig_class (vir_flag, name, params, clt, rs) ->
|
|
|
|
fprintf ppf "@[<2>%s%s@ %a%s@ :@ %a@]"
|
|
|
|
(if rs = Orec_next then "and" else "class")
|
2002-02-13 04:51:21 -08:00
|
|
|
(if vir_flag then " virtual" else "") print_out_class_params params
|
2002-02-13 05:08:57 -08:00
|
|
|
name !out_class_type clt
|
2004-06-12 01:55:49 -07:00
|
|
|
| Osig_class_type (vir_flag, name, params, clt, rs) ->
|
|
|
|
fprintf ppf "@[<2>%s%s@ %a%s@ =@ %a@]"
|
|
|
|
(if rs = Orec_next then "and" else "class type")
|
2002-02-13 04:51:21 -08:00
|
|
|
(if vir_flag then " virtual" else "") print_out_class_params params
|
2002-02-13 05:08:57 -08:00
|
|
|
name !out_class_type clt
|
2014-05-04 16:08:45 -07:00
|
|
|
| Osig_typext (ext, Oext_exception) ->
|
|
|
|
fprintf ppf "@[<2>exception %a@]"
|
|
|
|
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type)
|
2016-03-09 02:40:16 -08:00
|
|
|
| Osig_typext (ext, _es) ->
|
2014-05-04 16:08:45 -07:00
|
|
|
print_out_extension_constructor ppf ext
|
2002-02-13 03:09:19 -08:00
|
|
|
| Osig_modtype (name, Omty_abstract) ->
|
|
|
|
fprintf ppf "@[<2>module type %s@]" name
|
|
|
|
| Osig_modtype (name, mty) ->
|
2002-02-13 05:08:57 -08:00
|
|
|
fprintf ppf "@[<2>module type %s =@ %a@]" name !out_module_type mty
|
2013-09-29 00:22:34 -07:00
|
|
|
| Osig_module (name, Omty_alias id, _) ->
|
|
|
|
fprintf ppf "@[<2>module %s =@ %a@]" name print_ident id
|
2004-06-12 01:55:49 -07:00
|
|
|
| Osig_module (name, mty, rs) ->
|
2008-10-08 06:09:39 -07:00
|
|
|
fprintf ppf "@[<2>%s %s :@ %a@]"
|
2004-06-12 01:55:49 -07:00
|
|
|
(match rs with Orec_not -> "module"
|
|
|
|
| Orec_first -> "module rec"
|
|
|
|
| Orec_next -> "and")
|
|
|
|
name !out_module_type mty
|
|
|
|
| Osig_type(td, rs) ->
|
|
|
|
print_out_type_decl
|
2015-03-13 04:09:19 -07:00
|
|
|
(match rs with
|
|
|
|
| Orec_not -> "type nonrec"
|
|
|
|
| Orec_first -> "type"
|
|
|
|
| Orec_next -> "and")
|
2004-06-12 01:55:49 -07:00
|
|
|
ppf td
|
2015-10-06 03:58:25 -07:00
|
|
|
| Osig_value vd ->
|
|
|
|
let kwd = if vd.oval_prims = [] then "val" else "external" in
|
2002-02-13 03:09:19 -08:00
|
|
|
let pr_prims ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
[] -> ()
|
2002-02-13 03:09:19 -08:00
|
|
|
| s :: sl ->
|
|
|
|
fprintf ppf "@ = \"%s\"" s;
|
|
|
|
List.iter (fun s -> fprintf ppf "@ \"%s\"" s) sl
|
|
|
|
in
|
2015-10-06 03:58:25 -07:00
|
|
|
fprintf ppf "@[<2>%s %a :@ %a%a%a@]" kwd value_ident vd.oval_name
|
|
|
|
!out_type vd.oval_type pr_prims vd.oval_prims
|
|
|
|
(fun ppf -> List.iter (fun a -> fprintf ppf "@ [@@@@%s]" a.oattr_name))
|
|
|
|
vd.oval_attributes
|
2014-11-10 01:09:23 -08:00
|
|
|
| Osig_ellipsis ->
|
|
|
|
fprintf ppf "..."
|
2004-06-12 01:55:49 -07:00
|
|
|
|
2014-05-04 16:08:45 -07:00
|
|
|
and print_out_type_decl kwd ppf td =
|
|
|
|
let print_constraints ppf =
|
2002-02-13 03:09:19 -08:00
|
|
|
List.iter
|
|
|
|
(fun (ty1, ty2) ->
|
2002-02-13 05:08:57 -08:00
|
|
|
fprintf ppf "@ @[<2>constraint %a =@ %a@]" !out_type ty1
|
|
|
|
!out_type ty2)
|
2014-05-04 16:08:45 -07:00
|
|
|
td.otype_cstrs
|
2002-02-13 03:09:19 -08:00
|
|
|
in
|
|
|
|
let type_defined ppf =
|
2014-05-04 16:08:45 -07:00
|
|
|
match td.otype_params with
|
|
|
|
[] -> pp_print_string ppf td.otype_name
|
|
|
|
| [param] -> fprintf ppf "@[%a@ %s@]" type_parameter param td.otype_name
|
2002-02-13 03:09:19 -08:00
|
|
|
| _ ->
|
|
|
|
fprintf ppf "@[(@[%a)@]@ %s@]"
|
2014-05-04 16:08:45 -07:00
|
|
|
(print_list type_parameter (fun ppf -> fprintf ppf ",@ "))
|
|
|
|
td.otype_params
|
|
|
|
td.otype_name
|
2002-02-13 03:09:19 -08:00
|
|
|
in
|
|
|
|
let print_manifest ppf =
|
|
|
|
function
|
2002-02-13 05:08:57 -08:00
|
|
|
Otyp_manifest (ty, _) -> fprintf ppf " =@ %a" !out_type ty
|
2002-02-13 03:09:19 -08:00
|
|
|
| _ -> ()
|
|
|
|
in
|
2014-05-04 16:08:45 -07:00
|
|
|
let print_name_params ppf =
|
|
|
|
fprintf ppf "%s %t%a" kwd type_defined print_manifest td.otype_type
|
2002-02-13 03:09:19 -08:00
|
|
|
in
|
|
|
|
let ty =
|
2014-05-04 16:08:45 -07:00
|
|
|
match td.otype_type with
|
2002-02-13 04:51:21 -08:00
|
|
|
Otyp_manifest (_, ty) -> ty
|
2014-05-04 16:08:45 -07:00
|
|
|
| _ -> td.otype_type
|
2002-02-13 03:09:19 -08:00
|
|
|
in
|
2003-07-02 02:14:35 -07:00
|
|
|
let print_private ppf = function
|
2007-10-08 07:19:34 -07:00
|
|
|
Asttypes.Private -> fprintf ppf " private"
|
2014-05-04 16:08:45 -07:00
|
|
|
| Asttypes.Public -> ()
|
|
|
|
in
|
2015-05-27 07:30:33 -07:00
|
|
|
let print_immediate ppf =
|
|
|
|
if td.otype_immediate then fprintf ppf " [%@%@immediate]" else ()
|
|
|
|
in
|
2012-05-29 06:41:14 -07:00
|
|
|
let print_out_tkind ppf = function
|
2005-03-22 19:08:37 -08:00
|
|
|
| Otyp_abstract -> ()
|
|
|
|
| Otyp_record lbls ->
|
2014-04-07 05:42:49 -07:00
|
|
|
fprintf ppf " =%a %a"
|
2014-05-04 16:08:45 -07:00
|
|
|
print_private td.otype_private
|
2014-04-07 05:42:49 -07:00
|
|
|
print_record_decl lbls
|
2005-03-22 19:08:37 -08:00
|
|
|
| Otyp_sum constrs ->
|
2007-10-08 07:19:34 -07:00
|
|
|
fprintf ppf " =%a@;<1 2>%a"
|
2014-05-04 16:08:45 -07:00
|
|
|
print_private td.otype_private
|
2002-02-13 03:09:19 -08:00
|
|
|
(print_list print_out_constr (fun ppf -> fprintf ppf "@ | ")) constrs
|
2014-05-04 16:08:45 -07:00
|
|
|
| Otyp_open ->
|
|
|
|
fprintf ppf " = .."
|
2002-02-13 03:09:19 -08:00
|
|
|
| ty ->
|
2007-10-08 07:19:34 -07:00
|
|
|
fprintf ppf " =%a@;<1 2>%a"
|
2014-05-04 16:08:45 -07:00
|
|
|
print_private td.otype_private
|
2005-03-22 19:08:37 -08:00
|
|
|
!out_type ty
|
|
|
|
in
|
2015-05-27 07:30:33 -07:00
|
|
|
fprintf ppf "@[<2>@[<hv 2>%t%a@]%t%t@]"
|
2014-05-04 16:08:45 -07:00
|
|
|
print_name_params
|
2005-03-22 19:08:37 -08:00
|
|
|
print_out_tkind ty
|
2014-05-04 16:08:45 -07:00
|
|
|
print_constraints
|
2015-05-27 07:30:33 -07:00
|
|
|
print_immediate
|
2014-05-04 16:08:45 -07:00
|
|
|
|
2010-09-12 22:28:30 -07:00
|
|
|
and print_out_constr ppf (name, tyl,ret_type_opt) =
|
2016-03-30 05:58:15 -07:00
|
|
|
let name =
|
|
|
|
match name with
|
|
|
|
| "::" -> "(::)" (* #7200 *)
|
|
|
|
| s -> s
|
|
|
|
in
|
2010-09-12 22:28:30 -07:00
|
|
|
match ret_type_opt with
|
|
|
|
| None ->
|
|
|
|
begin match tyl with
|
2011-07-29 03:32:43 -07:00
|
|
|
| [] ->
|
2012-11-07 02:50:14 -08:00
|
|
|
pp_print_string ppf name
|
2010-09-12 22:28:30 -07:00
|
|
|
| _ ->
|
2011-07-29 03:32:43 -07:00
|
|
|
fprintf ppf "@[<2>%s of@ %a@]" name
|
|
|
|
(print_typlist print_simple_out_type " *") tyl
|
|
|
|
end
|
2010-09-12 22:28:30 -07:00
|
|
|
| Some ret_type ->
|
|
|
|
begin match tyl with
|
2011-07-29 03:32:43 -07:00
|
|
|
| [] ->
|
|
|
|
fprintf ppf "@[<2>%s :@ %a@]" name print_simple_out_type ret_type
|
2010-09-12 22:28:30 -07:00
|
|
|
| _ ->
|
2011-07-29 03:32:43 -07:00
|
|
|
fprintf ppf "@[<2>%s :@ %a -> %a@]" name
|
|
|
|
(print_typlist print_simple_out_type " *")
|
|
|
|
tyl print_simple_out_type ret_type
|
|
|
|
end
|
2010-09-12 22:28:30 -07:00
|
|
|
|
2014-05-04 16:08:45 -07:00
|
|
|
and print_out_extension_constructor ppf ext =
|
|
|
|
let print_extended_type ppf =
|
|
|
|
let print_type_parameter ppf ty =
|
|
|
|
fprintf ppf "%s"
|
|
|
|
(if ty = "_" then ty else "'"^ty)
|
|
|
|
in
|
|
|
|
match ext.oext_type_params with
|
|
|
|
[] -> fprintf ppf "%s" ext.oext_type_name
|
|
|
|
| [ty_param] ->
|
|
|
|
fprintf ppf "@[%a@ %s@]"
|
|
|
|
print_type_parameter
|
|
|
|
ty_param
|
|
|
|
ext.oext_type_name
|
|
|
|
| _ ->
|
|
|
|
fprintf ppf "@[(@[%a)@]@ %s@]"
|
|
|
|
(print_list print_type_parameter (fun ppf -> fprintf ppf ",@ "))
|
|
|
|
ext.oext_type_params
|
|
|
|
ext.oext_type_name
|
|
|
|
in
|
|
|
|
fprintf ppf "@[<hv 2>type %t +=%s@;<1 2>%a@]"
|
|
|
|
print_extended_type
|
|
|
|
(if ext.oext_private = Asttypes.Private then " private" else "")
|
|
|
|
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type)
|
|
|
|
|
|
|
|
and print_out_type_extension ppf te =
|
|
|
|
let print_extended_type ppf =
|
|
|
|
let print_type_parameter ppf ty =
|
|
|
|
fprintf ppf "%s"
|
|
|
|
(if ty = "_" then ty else "'"^ty)
|
|
|
|
in
|
|
|
|
match te.otyext_params with
|
|
|
|
[] -> fprintf ppf "%s" te.otyext_name
|
|
|
|
| [param] ->
|
|
|
|
fprintf ppf "@[%a@ %s@]"
|
|
|
|
print_type_parameter param
|
|
|
|
te.otyext_name
|
|
|
|
| _ ->
|
|
|
|
fprintf ppf "@[(@[%a)@]@ %s@]"
|
|
|
|
(print_list print_type_parameter (fun ppf -> fprintf ppf ",@ "))
|
|
|
|
te.otyext_params
|
|
|
|
te.otyext_name
|
|
|
|
in
|
|
|
|
fprintf ppf "@[<hv 2>type %t +=%s@;<1 2>%a@]"
|
|
|
|
print_extended_type
|
|
|
|
(if te.otyext_private = Asttypes.Private then " private" else "")
|
|
|
|
(print_list print_out_constr (fun ppf -> fprintf ppf "@ | "))
|
|
|
|
te.otyext_constructors
|
|
|
|
|
2002-02-13 05:08:57 -08:00
|
|
|
let _ = out_module_type := print_out_module_type
|
|
|
|
let _ = out_signature := print_out_signature
|
|
|
|
let _ = out_sig_item := print_out_sig_item
|
2014-05-04 16:08:45 -07:00
|
|
|
let _ = out_type_extension := print_out_type_extension
|
2002-02-13 03:09:19 -08:00
|
|
|
|
|
|
|
(* Phrases *)
|
|
|
|
|
|
|
|
let print_out_exception ppf exn outv =
|
|
|
|
match exn with
|
2002-02-13 04:51:21 -08:00
|
|
|
Sys.Break -> fprintf ppf "Interrupted.@."
|
|
|
|
| Out_of_memory -> fprintf ppf "Out of memory during evaluation.@."
|
2002-02-13 03:09:19 -08:00
|
|
|
| Stack_overflow ->
|
|
|
|
fprintf ppf "Stack overflow during evaluation (looping recursion?).@."
|
2002-02-13 05:08:57 -08:00
|
|
|
| _ -> fprintf ppf "@[Exception:@ %a.@]@." !out_value outv
|
2002-02-13 03:09:19 -08:00
|
|
|
|
|
|
|
let rec print_items ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
[] -> ()
|
2014-05-04 16:08:45 -07:00
|
|
|
| (Osig_typext(ext, Oext_first), None) :: items ->
|
|
|
|
(* Gather together extension constructors *)
|
|
|
|
let rec gather_extensions acc items =
|
|
|
|
match items with
|
|
|
|
(Osig_typext(ext, Oext_next), None) :: items ->
|
|
|
|
gather_extensions
|
|
|
|
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
|
|
|
|
items
|
|
|
|
| _ -> (List.rev acc, items)
|
|
|
|
in
|
|
|
|
let exts, items =
|
|
|
|
gather_extensions
|
|
|
|
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
|
|
|
|
items
|
|
|
|
in
|
|
|
|
let te =
|
|
|
|
{ otyext_name = ext.oext_type_name;
|
|
|
|
otyext_params = ext.oext_type_params;
|
|
|
|
otyext_constructors = exts;
|
|
|
|
otyext_private = ext.oext_private }
|
|
|
|
in
|
|
|
|
fprintf ppf "@[%a@]" !out_type_extension te;
|
|
|
|
if items <> [] then fprintf ppf "@ %a" print_items items
|
2002-02-13 03:09:19 -08:00
|
|
|
| (tree, valopt) :: items ->
|
|
|
|
begin match valopt with
|
2002-02-13 04:51:21 -08:00
|
|
|
Some v ->
|
2002-02-13 05:08:57 -08:00
|
|
|
fprintf ppf "@[<2>%a =@ %a@]" !out_sig_item tree
|
|
|
|
!out_value v
|
|
|
|
| None -> fprintf ppf "@[%a@]" !out_sig_item tree
|
2002-02-13 03:09:19 -08:00
|
|
|
end;
|
2002-02-13 04:51:21 -08:00
|
|
|
if items <> [] then fprintf ppf "@ %a" print_items items
|
2002-02-13 03:09:19 -08:00
|
|
|
|
|
|
|
let print_out_phrase ppf =
|
|
|
|
function
|
2002-02-13 04:51:21 -08:00
|
|
|
Ophr_eval (outv, ty) ->
|
2002-02-13 05:08:57 -08:00
|
|
|
fprintf ppf "@[- : %a@ =@ %a@]@." !out_type ty !out_value outv
|
2002-02-13 03:09:19 -08:00
|
|
|
| Ophr_signature [] -> ()
|
|
|
|
| Ophr_signature items -> fprintf ppf "@[<v>%a@]@." print_items items
|
|
|
|
| Ophr_exception (exn, outv) -> print_out_exception ppf exn outv
|
|
|
|
|
|
|
|
let out_phrase = ref print_out_phrase
|