src/Pure/ML/ml_pretty.ML
author wenzelm
Fri Apr 01 11:45:04 2016 +0200 (2016-04-01)
changeset 62784 0371c369ab1d
parent 62711 09df6a51ad3c
child 62819 d3ff367a16a0
permissions -rw-r--r--
adapted to Poly/ML repository version 2e40cadc975a;
     1 (*  Title:      Pure/ML/ml_pretty.ML
     2     Author:     Makarius
     3 
     4 Minimal support for raw ML pretty printing, notably for toplevel pp.
     5 *)
     6 
     7 signature ML_PRETTY =
     8 sig
     9   datatype pretty =
    10     Block of (string * string) * bool * FixedInt.int * pretty list |
    11     String of string * FixedInt.int |
    12     Break of bool * FixedInt.int * FixedInt.int
    13   val block: pretty list -> pretty
    14   val str: string -> pretty
    15   val brk: FixedInt.int -> pretty
    16   val pair: ('a * FixedInt.int -> pretty) -> ('b * FixedInt.int -> pretty) ->
    17     ('a * 'b) * FixedInt.int -> pretty
    18   val enum: string -> string -> string -> ('a * FixedInt.int -> pretty) ->
    19     'a list * FixedInt.int -> pretty
    20   val to_polyml: pretty -> PolyML.pretty
    21   val from_polyml: PolyML.pretty -> pretty
    22   val get_print_depth: unit -> int
    23   val print_depth: int -> unit
    24   val format_polyml: int -> PolyML.pretty -> string
    25   val format: int -> pretty -> string
    26   val make_string_fn: string -> string
    27 end;
    28 
    29 structure ML_Pretty: ML_PRETTY =
    30 struct
    31 
    32 (* datatype pretty *)
    33 
    34 datatype pretty =
    35   Block of (string * string) * bool * FixedInt.int * pretty list |
    36   String of string * FixedInt.int |
    37   Break of bool * FixedInt.int * FixedInt.int;
    38 
    39 fun block prts = Block (("", ""), false, 2, prts);
    40 fun str s = String (s, FixedInt.fromInt (size s));
    41 fun brk width = Break (false, width, 0);
    42 
    43 fun pair pretty1 pretty2 ((x, y), depth: FixedInt.int) =
    44   block [str "(", pretty1 (x, depth), str ",", brk 1, pretty2 (y, depth - 1), str ")"];
    45 
    46 fun enum sep lpar rpar pretty (args, depth: FixedInt.int) =
    47   let
    48     fun elems _ [] = []
    49       | elems 0 _ = [str "..."]
    50       | elems d [x] = [pretty (x, d)]
    51       | elems d (x :: xs) = pretty (x, d) :: str sep :: brk 1 :: elems (d - 1) xs;
    52   in block (str lpar :: (elems (FixedInt.max (depth, 0)) args @ [str rpar])) end;
    53 
    54 
    55 (* convert *)
    56 
    57 fun to_polyml (Break (false, width, offset)) = PolyML.PrettyBreak (width, offset)
    58   | to_polyml (Break (true, _, _)) =
    59       PolyML.PrettyBlock (0, false, [PolyML.ContextProperty ("fbrk", "")],
    60         [PolyML.PrettyString " "])
    61   | to_polyml (Block ((bg, en), consistent, ind, prts)) =
    62       let val context =
    63         (if bg = "" then [] else [PolyML.ContextProperty ("begin", bg)]) @
    64         (if en = "" then [] else [PolyML.ContextProperty ("end", en)])
    65       in PolyML.PrettyBlock (ind, consistent, context, map to_polyml prts) end
    66   | to_polyml (String (s, len)) =
    67       if len = FixedInt.fromInt (size s) then PolyML.PrettyString s
    68       else
    69         PolyML.PrettyBlock
    70           (0, false,
    71             [PolyML.ContextProperty ("length", FixedInt.toString len)], [PolyML.PrettyString s]);
    72 
    73 val from_polyml =
    74   let
    75     fun convert _ (PolyML.PrettyBreak (width, offset)) = Break (false, width, offset)
    76       | convert _ (PolyML.PrettyBlock (_, _,
    77             [PolyML.ContextProperty ("fbrk", _)], [PolyML.PrettyString " "])) =
    78           Break (true, 1, 0)
    79       | convert len (PolyML.PrettyBlock (ind, consistent, context, prts)) =
    80           let
    81             fun property name default =
    82               (case List.find (fn PolyML.ContextProperty (a, _) => name = a | _ => false) context of
    83                 SOME (PolyML.ContextProperty (_, b)) => b
    84               | _ => default);
    85             val bg = property "begin" "";
    86             val en = property "end" "";
    87             val len' = property "length" len;
    88           in Block ((bg, en), consistent, ind, map (convert len') prts) end
    89       | convert len (PolyML.PrettyString s) =
    90           String (s, FixedInt.fromInt (case Int.fromString len of SOME i => i | NONE => size s))
    91   in convert "" end;
    92 
    93 
    94 (* default print depth *)
    95 
    96 local
    97   val depth = Unsynchronized.ref 0;
    98 in
    99   fun get_print_depth () = ! depth;
   100   fun print_depth n = (depth := n; PolyML.print_depth n);
   101 end;
   102 
   103 
   104 (* format *)
   105 
   106 fun format_polyml margin prt =
   107   let
   108     val result = Unsynchronized.ref [];
   109     val () = PolyML.prettyPrint (fn s => result := s :: ! result, margin) prt
   110   in String.concat (List.rev (! result)) end;
   111 
   112 fun format margin = format_polyml margin o to_polyml;
   113 
   114 
   115 (* make string *)
   116 
   117 local
   118   fun pretty_string_of s = "(fn x => Pretty.string_of (Pretty.from_polyml (" ^ s ^ ")))";
   119   fun pretty_value depth = "PolyML.prettyRepresentation (x, FixedInt.fromInt (" ^ depth ^ "))";
   120 in
   121 
   122 fun make_string_fn local_env =
   123   if local_env <> "" then
   124     pretty_string_of (pretty_value (local_env ^ ".ML_print_depth ()"))
   125   else if List.exists (fn (a, _) => a = "Pretty") (#allStruct ML_Name_Space.global ()) then
   126     pretty_string_of (pretty_value "ML_Pretty.get_print_depth ()")
   127   else "(fn x => ML_Pretty.format_polyml 78 (" ^ pretty_value "ML_Pretty.get_print_depth ()" ^ "))";
   128 
   129 end;
   130 
   131 end;