src/Pure/General/pretty.scala
author wenzelm
Fri, 17 Dec 2010 20:21:35 +0100
changeset 41253 42f24340ae53
parent 38724 d1feec02cf02
child 43714 3749d1e6dde9
permissions -rw-r--r--
more explicit references to structure Raw_Simplifier;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/pretty.scala
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     3
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
     4
Generic pretty printing module.
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     5
*/
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     6
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     7
package isabelle
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     8
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     9
36820
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    10
import java.awt.FontMetrics
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    11
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    12
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    13
object Pretty
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    14
{
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    15
  /* markup trees with physical blocks and breaks */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    16
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    17
  def block(body: XML.Body): XML.Tree = Block(2, body)
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    18
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    19
  object Block
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    20
  {
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    21
    def apply(i: Int, body: XML.Body): XML.Tree =
38724
d1feec02cf02 Pretty: tuned markup objects;
wenzelm
parents: 38573
diff changeset
    22
      XML.Elem(Markup(Markup.BLOCK, Markup.Indent(i)), body)
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    23
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    24
    def unapply(tree: XML.Tree): Option[(Int, XML.Body)] =
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    25
      tree match {
38724
d1feec02cf02 Pretty: tuned markup objects;
wenzelm
parents: 38573
diff changeset
    26
        case XML.Elem(Markup(Markup.BLOCK, Markup.Indent(i)), body) => Some((i, body))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    27
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    28
      }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    29
  }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    30
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    31
  object Break
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    32
  {
38414
49f1f657adc2 more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents: 38230
diff changeset
    33
    def apply(w: Int): XML.Tree =
38724
d1feec02cf02 Pretty: tuned markup objects;
wenzelm
parents: 38573
diff changeset
    34
      XML.Elem(Markup(Markup.BREAK, Markup.Width(w)), List(XML.Text(Symbol.spaces(w))))
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    35
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    36
    def unapply(tree: XML.Tree): Option[Int] =
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    37
      tree match {
38724
d1feec02cf02 Pretty: tuned markup objects;
wenzelm
parents: 38573
diff changeset
    38
        case XML.Elem(Markup(Markup.BREAK, Markup.Width(w)), _) => Some(w)
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    39
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    40
      }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    41
  }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    42
36689
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
    43
  val FBreak = XML.Text("\n")
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    44
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    45
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    46
  /* formatted output */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    47
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    48
  private def standard_format(tree: XML.Tree): XML.Body =
36689
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
    49
    tree match {
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
    50
      case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(standard_format)))
36689
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
    51
      case XML.Text(text) =>
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
    52
        Library.separate(FBreak,
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
    53
          Library.chunks(text).toList.map((s: CharSequence) => XML.Text(s.toString)))
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
    54
    }
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
    55
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    56
  case class Text(tx: XML.Body = Nil, val pos: Double = 0.0, val nl: Int = 0)
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    57
  {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    58
    def newline: Text = copy(tx = FBreak :: tx, pos = 0.0, nl = nl + 1)
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    59
    def string(s: String, len: Double): Text = copy(tx = XML.Text(s) :: tx, pos = pos + len)
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    60
    def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble)
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    61
    def content: XML.Body = tx.reverse
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    62
  }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    63
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36736
diff changeset
    64
  private val margin_default = 76
36818
wenzelm
parents: 36817
diff changeset
    65
  private def metric_default(s: String) = s.length.toDouble
36734
d9b10c173330 Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents: 36689
diff changeset
    66
36820
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    67
  def font_metric(metrics: FontMetrics): String => Double =
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    68
    if (metrics == null) ((s: String) => s.length.toDouble)
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    69
    else {
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    70
      val unit = metrics.charWidth(Symbol.spc).toDouble
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    71
      ((s: String) => if (s == "\n") 1.0 else metrics.stringWidth(s) / unit)
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    72
    }
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
    73
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    74
  def formatted(input: XML.Body, margin: Int = margin_default,
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    75
    metric: String => Double = metric_default): XML.Body =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    76
  {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    77
    val breakgain = margin / 20
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    78
    val emergencypos = margin / 2
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    79
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    80
    def content_length(tree: XML.Tree): Double =
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    81
      tree match {
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
    82
        case XML.Elem(_, body) => (0.0 /: body)(_ + content_length(_))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    83
        case XML.Text(s) => metric(s)
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    84
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    85
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    86
    def breakdist(trees: XML.Body, after: Double): Double =
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    87
      trees match {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    88
        case Break(_) :: _ => 0.0
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    89
        case FBreak :: _ => 0.0
36818
wenzelm
parents: 36817
diff changeset
    90
        case t :: ts => content_length(t) + breakdist(ts, after)
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    91
        case Nil => after
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    92
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    93
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
    94
    def forcenext(trees: XML.Body): XML.Body =
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    95
      trees match {
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    96
        case Nil => Nil
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    97
        case FBreak :: _ => trees
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    98
        case Break(_) :: ts => FBreak :: ts
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
    99
        case t :: ts => t :: forcenext(ts)
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   100
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   101
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   102
    def format(trees: XML.Body, blockin: Double, after: Double, text: Text): Text =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   103
      trees match {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   104
        case Nil => text
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   105
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   106
        case Block(indent, body) :: ts =>
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   107
          val pos1 = text.pos + indent
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   108
          val pos2 = pos1 % emergencypos
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   109
          val blockin1 =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   110
            if (pos1 < emergencypos) pos1
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   111
            else pos2
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   112
          val btext = format(body, blockin1, breakdist(ts, after), text)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   113
          val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   114
          format(ts1, blockin, after, btext)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   115
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   116
        case Break(wd) :: ts =>
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   117
          if (text.pos + wd <= (margin - breakdist(ts, after)).max(blockin + breakgain))
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   118
            format(ts, blockin, after, text.blanks(wd))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   119
          else format(ts, blockin, after, text.newline.blanks(blockin.toInt))
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   120
        case FBreak :: ts => format(ts, blockin, after, text.newline.blanks(blockin.toInt))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   121
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   122
        case XML.Elem(markup, body) :: ts =>
36734
d9b10c173330 Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents: 36689
diff changeset
   123
          val btext = format(body, blockin, breakdist(ts, after), text.copy(tx = Nil))
d9b10c173330 Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents: 36689
diff changeset
   124
          val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   125
          val btext1 = btext.copy(tx = XML.Elem(markup, btext.content) :: text.tx)
36734
d9b10c173330 Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents: 36689
diff changeset
   126
          format(ts1, blockin, after, btext1)
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   127
        case XML.Text(s) :: ts => format(ts, blockin, after, text.string(s, metric(s)))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   128
      }
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   129
    format(input.flatMap(standard_format), 0.0, 0.0, Text()).content
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   130
  }
36734
d9b10c173330 Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents: 36689
diff changeset
   131
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   132
  def string_of(input: XML.Body, margin: Int = margin_default,
36818
wenzelm
parents: 36817
diff changeset
   133
      metric: String => Double = metric_default): String =
37374
d66e6cc47fab Pretty.string_of (in Scala): actually observe margin/metric;
wenzelm
parents: 36820
diff changeset
   134
    formatted(input, margin, metric).iterator.flatMap(XML.content).mkString
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   135
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   136
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   137
  /* unformatted output */
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   138
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   139
  def unformatted(input: XML.Body): XML.Body =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   140
  {
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   141
    def fmt(tree: XML.Tree): XML.Body =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   142
      tree match {
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   143
        case Block(_, body) => body.flatMap(fmt)
36763
096ebe74aeaf static Symbol.spaces;
wenzelm
parents: 36745
diff changeset
   144
        case Break(wd) => List(XML.Text(Symbol.spaces(wd)))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   145
        case FBreak => List(XML.Text(Symbol.space))
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 37374
diff changeset
   146
        case XML.Elem(markup, body) => List(XML.Elem(markup, body.flatMap(fmt)))
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   147
        case XML.Text(_) => List(tree)
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   148
      }
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   149
    input.flatMap(standard_format).flatMap(fmt)
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   150
  }
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   151
38573
d163f0f28e8c tuned signatures;
wenzelm
parents: 38414
diff changeset
   152
  def str_of(input: XML.Body): String =
36736
93753a8c9550 unformatted output;
wenzelm
parents: 36734
diff changeset
   153
    unformatted(input).iterator.flatMap(XML.content).mkString
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
   154
}