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