src/Pure/General/pretty.scala
author wenzelm
Thu, 06 May 2010 23:07:21 +0200
changeset 36687 58020b59baf7
parent 36683 41a1210519fd
child 36689 379f5b1e7f91
permissions -rw-r--r--
basic formatting of pretty trees; line-up ML vs. Scala sources;
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
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    10
object Pretty
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    11
{
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    12
  /* markup trees with physical blocks and breaks */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    13
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    14
  object Block
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    15
  {
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    16
    def apply(indent: Int, body: List[XML.Tree]): XML.Tree =
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    17
      XML.Elem(Markup.BLOCK, List((Markup.INDENT, indent.toString)), body)
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    18
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    19
    def unapply(tree: XML.Tree): Option[(Int, List[XML.Tree])] =
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    20
      tree match {
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    21
        case XML.Elem(Markup.BLOCK, List((Markup.INDENT, indent)), body) =>
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    22
          Markup.parse_int(indent) match {
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    23
            case Some(i) => Some((i, body))
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    24
            case None => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    25
          }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    26
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    27
      }
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
  object Break
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    31
  {
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    32
    def apply(width: Int): XML.Tree =
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    33
      XML.Elem(Markup.BREAK, List((Markup.WIDTH, width.toString)), List(XML.Text(" " * width)))
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    34
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    35
    def unapply(tree: XML.Tree): Option[Int] =
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    36
      tree match {
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    37
        case XML.Elem(Markup.BREAK, List((Markup.WIDTH, width)), _) => Markup.parse_int(width)
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    38
        case _ => None
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    39
      }
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
  object FBreak
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    43
  {
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    44
    def apply(): XML.Tree = XML.Elem(Markup.FBREAK, Nil, List(XML.Text(" ")))
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    45
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    46
    def unapply(tree: XML.Tree): Boolean =
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    47
      tree match {
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    48
        case XML.Elem(Markup.FBREAK, Nil, _) => true
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    49
        case _ => false
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    50
      }
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    51
  }
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    52
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    53
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    54
  /* formatted output */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    55
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    56
  private case class Text(tx: List[String] = Nil, val pos: Int = 0, val nl: Int = 0)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    57
  {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    58
    def newline: Text = copy(tx = "\n" :: tx, pos = 0, nl = nl + 1)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    59
    def string(s: String): Text = copy(tx = s :: tx, pos = pos + s.length)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    60
    def blanks(wd: Int): Text = string(" " * wd)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    61
    def content: String = tx.reverse.mkString
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    62
  }
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    63
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    64
  private def breakdist(trees: List[XML.Tree], after: Int): Int =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    65
    trees match {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    66
      case Break(_) :: _ => 0
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    67
      case FBreak() :: _ => 0
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    68
      case XML.Elem(_, _, body) :: ts =>
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    69
        (0 /: body)(_ + XML.content_length(_)) + breakdist(ts, after)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    70
      case XML.Text(s) :: ts => s.length + breakdist(ts, after)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    71
      case Nil => after
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    72
    }
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    73
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    74
  private def forcenext(trees: List[XML.Tree]): List[XML.Tree] =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    75
    trees match {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    76
      case Nil => Nil
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    77
      case FBreak() :: _ => trees
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    78
      case Break(_) :: ts => FBreak() :: ts
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    79
      case t :: ts => t :: forcenext(ts)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    80
    }
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    81
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    82
  def string_of(input: List[XML.Tree], margin: Int = 76): String =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    83
  {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    84
    val breakgain = margin / 20
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    85
    val emergencypos = margin / 2
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    86
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    87
    def format(trees: List[XML.Tree], blockin: Int, after: Int, text: Text): Text =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    88
      trees match {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    89
        case Nil => text
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    90
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    91
        case Block(indent, body) :: ts =>
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    92
          val pos1 = text.pos + indent
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    93
          val pos2 = pos1 % emergencypos
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    94
          val blockin1 =
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    95
            if (pos1 < emergencypos) pos1
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    96
            else pos2
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    97
          val btext = format(body, blockin1, breakdist(ts, after), text)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    98
          val ts1 = if (text.nl < btext.nl) forcenext(ts) else ts
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    99
          format(ts1, blockin, after, btext)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   100
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   101
        case Break(wd) :: ts =>
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   102
          if (text.pos + wd <= (margin - breakdist(ts, after)).max(blockin + breakgain))
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   103
            format(ts, blockin, after, text.blanks(wd))
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   104
          else format(ts, blockin, after, text.newline.blanks(blockin))
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   105
        case FBreak() :: ts => format(ts, blockin, after, text.newline.blanks(blockin))
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   106
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   107
        case XML.Elem(_, _, body) :: ts => format(body ::: ts, blockin, after, text)
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   108
        case XML.Text(s) :: ts => format(ts, blockin, after, text.string(s))
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   109
      }
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   110
    format(input, 0, 0, Text()).content
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   111
  }
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
   112
}