src/Pure/ML/ml_pretty.ML
author wenzelm
Thu, 03 Mar 2016 21:59:21 +0100
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;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62508
d0b68218ea55 discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents: 62503
diff changeset
     1
(*  Title:      Pure/ML/ml_pretty.ML
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
     3
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
     4
Minimal support for raw ML pretty printing, notably for toplevel pp.
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
     5
*)
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
     6
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
     7
signature ML_PRETTY =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
     8
sig
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
     9
  datatype pretty =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    10
    Block of (string * string) * bool * FixedInt.int * pretty list |
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    11
    String of string * FixedInt.int |
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    12
    Break of bool * FixedInt.int * FixedInt.int
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    13
  val block: pretty list -> pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    14
  val str: string -> pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    15
  val brk: FixedInt.int -> pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    16
  val pair: ('a * int -> pretty) -> ('b * int -> pretty) -> ('a * 'b) * int -> pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    17
  val enum: string -> string -> string -> ('a * int -> pretty) -> 'a list * int -> pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    18
  val to_polyml: pretty -> PolyML.pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    19
  val from_polyml: PolyML.pretty -> pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    20
end;
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    21
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    22
structure ML_Pretty: ML_PRETTY =
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    23
struct
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    24
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    25
datatype pretty =
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    26
  Block of (string * string) * bool * FixedInt.int * pretty list |
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    27
  String of string * FixedInt.int |
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    28
  Break of bool * FixedInt.int * FixedInt.int;
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    29
61869
ba466ac335e3 clarified underlying datatypes;
wenzelm
parents: 61864
diff changeset
    30
fun block prts = Block (("", ""), false, 2, prts);
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    31
fun str s = String (s, FixedInt.fromInt (size s));
61862
e2a9e46ac0fb support pretty break indent, like underlying ML systems;
wenzelm
parents: 38635
diff changeset
    32
fun brk width = Break (false, width, 0);
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    33
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    34
fun pair pretty1 pretty2 ((x, y), depth: FixedInt.int) =
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    35
  block [str "(", pretty1 (x, depth), str ",", brk 1, pretty2 (y, depth - 1), str ")"];
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    36
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    37
fun enum sep lpar rpar pretty (args, depth: FixedInt.int) =
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    38
  let
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    39
    fun elems _ [] = []
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    40
      | elems 0 _ = [str "..."]
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    41
      | elems d [x] = [pretty (x, d)]
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    42
      | elems d (x :: xs) = pretty (x, d) :: str sep :: brk 1 :: elems (d - 1) xs;
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    43
  in block (str lpar :: (elems (FixedInt.max (depth, 0)) args @ [str rpar])) end;
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    44
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    45
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    46
(* convert *)
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    47
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    48
fun to_polyml (Break (false, width, offset)) = PolyML.PrettyBreak (width, offset)
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    49
  | to_polyml (Break (true, _, _)) =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    50
      PolyML.PrettyBlock (0, false, [PolyML.ContextProperty ("fbrk", "")],
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    51
        [PolyML.PrettyString " "])
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    52
  | to_polyml (Block ((bg, en), consistent, ind, prts)) =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    53
      let val context =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    54
        (if bg = "" then [] else [PolyML.ContextProperty ("begin", bg)]) @
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    55
        (if en = "" then [] else [PolyML.ContextProperty ("end", en)])
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    56
      in PolyML.PrettyBlock (ind, consistent, context, map to_polyml prts) end
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    57
  | to_polyml (String (s, len)) =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    58
      if len = FixedInt.fromInt (size s) then PolyML.PrettyString s
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    59
      else
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    60
        PolyML.PrettyBlock
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    61
          (0, false,
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    62
            [PolyML.ContextProperty ("length", FixedInt.toString len)], [PolyML.PrettyString s]);
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    63
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    64
val from_polyml =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    65
  let
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    66
    fun convert _ (PolyML.PrettyBreak (width, offset)) = Break (false, width, offset)
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    67
      | convert _ (PolyML.PrettyBlock (_, _,
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    68
            [PolyML.ContextProperty ("fbrk", _)], [PolyML.PrettyString " "])) =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    69
          Break (true, 1, 0)
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    70
      | convert len (PolyML.PrettyBlock (ind, consistent, context, prts)) =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    71
          let
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    72
            fun property name default =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    73
              (case List.find (fn PolyML.ContextProperty (a, _) => name = a | _ => false) context of
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    74
                SOME (PolyML.ContextProperty (_, b)) => b
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    75
              | _ => default);
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    76
            val bg = property "begin" "";
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    77
            val en = property "end" "";
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    78
            val len' = property "length" len;
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    79
          in Block ((bg, en), consistent, ind, map (convert len') prts) end
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    80
      | convert len (PolyML.PrettyString s) =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    81
          String (s, FixedInt.fromInt (case Int.fromString len of SOME i => i | NONE => size s))
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    82
  in convert "" end;
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    83
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    84
end;