src/Pure/ML/ml_pretty.ML
author wenzelm
Thu Mar 03 21:59:21 2016 +0100 (2016-03-03)
changeset 62508 d0b68218ea55
parent 62503 src/Pure/RAW/ml_pretty.ML@19afb533028e
child 62661 c23ff2f45a18
permissions -rw-r--r--
discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
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@62503
    20
end;
wenzelm@62503
    21
wenzelm@62503
    22
structure ML_Pretty: ML_PRETTY =
wenzelm@30622
    23
struct
wenzelm@30622
    24
wenzelm@30622
    25
datatype pretty =
wenzelm@62387
    26
  Block of (string * string) * bool * FixedInt.int * pretty list |
wenzelm@62387
    27
  String of string * FixedInt.int |
wenzelm@62387
    28
  Break of bool * FixedInt.int * FixedInt.int;
wenzelm@30622
    29
wenzelm@61869
    30
fun block prts = Block (("", ""), false, 2, prts);
wenzelm@62387
    31
fun str s = String (s, FixedInt.fromInt (size s));
wenzelm@61862
    32
fun brk width = Break (false, width, 0);
wenzelm@38635
    33
wenzelm@62387
    34
fun pair pretty1 pretty2 ((x, y), depth: FixedInt.int) =
wenzelm@38635
    35
  block [str "(", pretty1 (x, depth), str ",", brk 1, pretty2 (y, depth - 1), str ")"];
wenzelm@38635
    36
wenzelm@62387
    37
fun enum sep lpar rpar pretty (args, depth: FixedInt.int) =
wenzelm@38635
    38
  let
wenzelm@38635
    39
    fun elems _ [] = []
wenzelm@38635
    40
      | elems 0 _ = [str "..."]
wenzelm@38635
    41
      | elems d [x] = [pretty (x, d)]
wenzelm@38635
    42
      | elems d (x :: xs) = pretty (x, d) :: str sep :: brk 1 :: elems (d - 1) xs;
wenzelm@62387
    43
  in block (str lpar :: (elems (FixedInt.max (depth, 0)) args @ [str rpar])) end;
wenzelm@38635
    44
wenzelm@62503
    45
wenzelm@62503
    46
(* convert *)
wenzelm@62503
    47
wenzelm@62503
    48
fun to_polyml (Break (false, width, offset)) = PolyML.PrettyBreak (width, offset)
wenzelm@62503
    49
  | to_polyml (Break (true, _, _)) =
wenzelm@62503
    50
      PolyML.PrettyBlock (0, false, [PolyML.ContextProperty ("fbrk", "")],
wenzelm@62503
    51
        [PolyML.PrettyString " "])
wenzelm@62503
    52
  | to_polyml (Block ((bg, en), consistent, ind, prts)) =
wenzelm@62503
    53
      let val context =
wenzelm@62503
    54
        (if bg = "" then [] else [PolyML.ContextProperty ("begin", bg)]) @
wenzelm@62503
    55
        (if en = "" then [] else [PolyML.ContextProperty ("end", en)])
wenzelm@62503
    56
      in PolyML.PrettyBlock (ind, consistent, context, map to_polyml prts) end
wenzelm@62503
    57
  | to_polyml (String (s, len)) =
wenzelm@62503
    58
      if len = FixedInt.fromInt (size s) then PolyML.PrettyString s
wenzelm@62503
    59
      else
wenzelm@62503
    60
        PolyML.PrettyBlock
wenzelm@62503
    61
          (0, false,
wenzelm@62503
    62
            [PolyML.ContextProperty ("length", FixedInt.toString len)], [PolyML.PrettyString s]);
wenzelm@62503
    63
wenzelm@62503
    64
val from_polyml =
wenzelm@62503
    65
  let
wenzelm@62503
    66
    fun convert _ (PolyML.PrettyBreak (width, offset)) = Break (false, width, offset)
wenzelm@62503
    67
      | convert _ (PolyML.PrettyBlock (_, _,
wenzelm@62503
    68
            [PolyML.ContextProperty ("fbrk", _)], [PolyML.PrettyString " "])) =
wenzelm@62503
    69
          Break (true, 1, 0)
wenzelm@62503
    70
      | convert len (PolyML.PrettyBlock (ind, consistent, context, prts)) =
wenzelm@62503
    71
          let
wenzelm@62503
    72
            fun property name default =
wenzelm@62503
    73
              (case List.find (fn PolyML.ContextProperty (a, _) => name = a | _ => false) context of
wenzelm@62503
    74
                SOME (PolyML.ContextProperty (_, b)) => b
wenzelm@62503
    75
              | _ => default);
wenzelm@62503
    76
            val bg = property "begin" "";
wenzelm@62503
    77
            val en = property "end" "";
wenzelm@62503
    78
            val len' = property "length" len;
wenzelm@62503
    79
          in Block ((bg, en), consistent, ind, map (convert len') prts) end
wenzelm@62503
    80
      | convert len (PolyML.PrettyString s) =
wenzelm@62503
    81
          String (s, FixedInt.fromInt (case Int.fromString len of SOME i => i | NONE => size s))
wenzelm@62503
    82
  in convert "" end;
wenzelm@62503
    83
wenzelm@30622
    84
end;