src/Pure/RAW/ml_pretty.ML
changeset 62503 19afb533028e
parent 62387 ad3eb2889f9a
equal deleted inserted replaced
62502:8857237c3a90 62503:19afb533028e
     1 (*  Title:      Pure/RAW/ml_pretty.ML
     1 (*  Title:      Pure/RAW/ml_pretty.ML
     2     Author:     Makarius
     2     Author:     Makarius
     3 
     3 
     4 Minimal support for raw ML pretty printing -- for boot-strapping only.
     4 Minimal support for raw ML pretty printing, notably for toplevel pp.
     5 *)
     5 *)
     6 
     6 
     7 structure ML_Pretty =
     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 * int -> pretty) -> ('b * int -> pretty) -> ('a * 'b) * int -> pretty
       
    17   val enum: string -> string -> string -> ('a * int -> pretty) -> 'a list * int -> pretty
       
    18   val to_polyml: pretty -> PolyML.pretty
       
    19   val from_polyml: PolyML.pretty -> pretty
       
    20 end;
       
    21 
       
    22 structure ML_Pretty: ML_PRETTY =
     8 struct
    23 struct
     9 
    24 
    10 datatype pretty =
    25 datatype pretty =
    11   Block of (string * string) * bool * FixedInt.int * pretty list |
    26   Block of (string * string) * bool * FixedInt.int * pretty list |
    12   String of string * FixedInt.int |
    27   String of string * FixedInt.int |
    25       | elems 0 _ = [str "..."]
    40       | elems 0 _ = [str "..."]
    26       | elems d [x] = [pretty (x, d)]
    41       | elems d [x] = [pretty (x, d)]
    27       | elems d (x :: xs) = pretty (x, d) :: str sep :: brk 1 :: elems (d - 1) xs;
    42       | elems d (x :: xs) = pretty (x, d) :: str sep :: brk 1 :: elems (d - 1) xs;
    28   in block (str lpar :: (elems (FixedInt.max (depth, 0)) args @ [str rpar])) end;
    43   in block (str lpar :: (elems (FixedInt.max (depth, 0)) args @ [str rpar])) end;
    29 
    44 
       
    45 
       
    46 (* convert *)
       
    47 
       
    48 fun to_polyml (Break (false, width, offset)) = PolyML.PrettyBreak (width, offset)
       
    49   | to_polyml (Break (true, _, _)) =
       
    50       PolyML.PrettyBlock (0, false, [PolyML.ContextProperty ("fbrk", "")],
       
    51         [PolyML.PrettyString " "])
       
    52   | to_polyml (Block ((bg, en), consistent, ind, prts)) =
       
    53       let val context =
       
    54         (if bg = "" then [] else [PolyML.ContextProperty ("begin", bg)]) @
       
    55         (if en = "" then [] else [PolyML.ContextProperty ("end", en)])
       
    56       in PolyML.PrettyBlock (ind, consistent, context, map to_polyml prts) end
       
    57   | to_polyml (String (s, len)) =
       
    58       if len = FixedInt.fromInt (size s) then PolyML.PrettyString s
       
    59       else
       
    60         PolyML.PrettyBlock
       
    61           (0, false,
       
    62             [PolyML.ContextProperty ("length", FixedInt.toString len)], [PolyML.PrettyString s]);
       
    63 
       
    64 val from_polyml =
       
    65   let
       
    66     fun convert _ (PolyML.PrettyBreak (width, offset)) = Break (false, width, offset)
       
    67       | convert _ (PolyML.PrettyBlock (_, _,
       
    68             [PolyML.ContextProperty ("fbrk", _)], [PolyML.PrettyString " "])) =
       
    69           Break (true, 1, 0)
       
    70       | convert len (PolyML.PrettyBlock (ind, consistent, context, prts)) =
       
    71           let
       
    72             fun property name default =
       
    73               (case List.find (fn PolyML.ContextProperty (a, _) => name = a | _ => false) context of
       
    74                 SOME (PolyML.ContextProperty (_, b)) => b
       
    75               | _ => default);
       
    76             val bg = property "begin" "";
       
    77             val en = property "end" "";
       
    78             val len' = property "length" len;
       
    79           in Block ((bg, en), consistent, ind, map (convert len') prts) end
       
    80       | convert len (PolyML.PrettyString s) =
       
    81           String (s, FixedInt.fromInt (case Int.fromString len of SOME i => i | NONE => size s))
       
    82   in convert "" end;
       
    83 
    30 end;
    84 end;