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