src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Sep 20 16:02:10 2012 +0200 (2012-09-20)
changeset 49469 00c301c8d569
parent 49467 25b7e843e124
child 49607 b610c0d52bfa
permissions -rw-r--r--
more direct Markup_Tree.from_XML;
wenzelm@38479
     1
/*  Title:      Pure/PIDE/markup_tree.scala
wenzelm@45673
     2
    Module:     PIDE
wenzelm@36676
     3
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     4
    Author:     Makarius
wenzelm@36676
     5
wenzelm@38479
     6
Markup trees over nested / non-overlapping text ranges.
wenzelm@36676
     7
*/
immler@34393
     8
wenzelm@34871
     9
package isabelle
immler@34393
    10
wenzelm@43520
    11
import java.lang.System
wenzelm@34701
    12
import javax.swing.tree.DefaultMutableTreeNode
wenzelm@34701
    13
wenzelm@38479
    14
import scala.collection.immutable.SortedMap
wenzelm@49467
    15
import scala.annotation.tailrec
wenzelm@38479
    16
wenzelm@38479
    17
wenzelm@38479
    18
object Markup_Tree
wenzelm@38479
    19
{
wenzelm@45456
    20
  val empty: Markup_Tree = new Markup_Tree(Branches.empty)
wenzelm@38479
    21
wenzelm@49467
    22
  def merge_disjoint(trees: List[Markup_Tree]): Markup_Tree =
wenzelm@49467
    23
    trees match {
wenzelm@49467
    24
      case Nil => empty
wenzelm@49467
    25
      case head :: tail =>
wenzelm@49467
    26
        new Markup_Tree(
wenzelm@49467
    27
          (head.branches /: tail) {
wenzelm@49467
    28
            case (branches, tree) =>
wenzelm@49467
    29
              (branches /: tree.branches) {
wenzelm@49467
    30
                case (bs, (r, entry)) =>
wenzelm@49467
    31
                  require(!bs.isDefinedAt(r))
wenzelm@49467
    32
                  bs + (r -> entry)
wenzelm@49467
    33
              }
wenzelm@49467
    34
          })
wenzelm@49467
    35
    }
wenzelm@49467
    36
wenzelm@45473
    37
  object Entry
wenzelm@45473
    38
  {
wenzelm@45473
    39
    def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
wenzelm@45474
    40
      Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree)
wenzelm@49469
    41
wenzelm@49469
    42
    def apply(range: Text.Range, rev_markups: List[XML.Elem], subtree: Markup_Tree): Entry =
wenzelm@49469
    43
      Entry(range, rev_markups, Set.empty ++ rev_markups.iterator.map(_.markup.name), subtree)
wenzelm@45473
    44
  }
wenzelm@45473
    45
wenzelm@45474
    46
  sealed case class Entry(
wenzelm@45474
    47
    range: Text.Range,
wenzelm@45474
    48
    rev_markup: List[XML.Elem],
wenzelm@45474
    49
    elements: Set[String],
wenzelm@45474
    50
    subtree: Markup_Tree)
wenzelm@45469
    51
  {
wenzelm@45474
    52
    def + (info: XML.Elem): Entry =
wenzelm@45474
    53
      if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup)
wenzelm@45474
    54
      else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name)
wenzelm@45474
    55
wenzelm@45470
    56
    def markup: List[XML.Elem] = rev_markup.reverse
wenzelm@45469
    57
  }
wenzelm@45469
    58
wenzelm@38479
    59
  object Branches
wenzelm@38479
    60
  {
wenzelm@38578
    61
    type T = SortedMap[Text.Range, Entry]
wenzelm@45456
    62
    val empty: T = SortedMap.empty(Text.Range.Ordering)
wenzelm@38479
    63
  }
wenzelm@49466
    64
wenzelm@49466
    65
wenzelm@49466
    66
  /* XML representation */
wenzelm@49466
    67
wenzelm@49467
    68
  @tailrec private def strip_elems(markups: List[Markup], body: XML.Body): (List[Markup], XML.Body) =
wenzelm@49467
    69
    body match {
wenzelm@49467
    70
      case List(XML.Elem(markup1, body1)) => strip_elems(markup1 :: markups, body1)
wenzelm@49467
    71
      case _ => (markups, body)
wenzelm@49467
    72
    }
wenzelm@49467
    73
wenzelm@49467
    74
  private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) =
wenzelm@49467
    75
    {
wenzelm@49467
    76
      val (offset, markup_trees) = acc
wenzelm@49467
    77
wenzelm@49467
    78
      strip_elems(Nil, List(tree)) match {
wenzelm@49467
    79
        case (Nil, body) =>
wenzelm@49467
    80
          (offset + XML.text_length(body), markup_trees)
wenzelm@49466
    81
wenzelm@49469
    82
        case (elems, body) =>
wenzelm@49469
    83
          val (end_offset, subtrees) = ((offset, Nil: List[Markup_Tree]) /: body)(make_trees)
wenzelm@49467
    84
          val range = Text.Range(offset, end_offset)
wenzelm@49469
    85
          val entry = Entry(range, elems.map(XML.Elem(_, Nil)), merge_disjoint(subtrees))
wenzelm@49469
    86
          (end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees)
wenzelm@49466
    87
      }
wenzelm@49467
    88
    }
wenzelm@49466
    89
wenzelm@49467
    90
  def from_XML(body: XML.Body): Markup_Tree =
wenzelm@49467
    91
    merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
wenzelm@38479
    92
}
immler@34554
    93
immler@34393
    94
wenzelm@49467
    95
final class Markup_Tree private(private val branches: Markup_Tree.Branches.T)
wenzelm@34717
    96
{
wenzelm@49417
    97
  import Markup_Tree._
wenzelm@49417
    98
wenzelm@45469
    99
  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
wenzelm@45469
   100
    this(branches + (entry.range -> entry))
wenzelm@45469
   101
wenzelm@38563
   102
  override def toString =
wenzelm@38563
   103
    branches.toList.map(_._2) match {
wenzelm@38563
   104
      case Nil => "Empty"
wenzelm@38563
   105
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
   106
    }
wenzelm@38563
   107
wenzelm@45469
   108
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@45457
   109
  {
wenzelm@45457
   110
    val start = Text.Range(range.start)
wenzelm@45457
   111
    val stop = Text.Range(range.stop)
wenzelm@45457
   112
    val bs = branches.range(start, stop)
wenzelm@45457
   113
    // FIXME check after Scala 2.8.x
wenzelm@45457
   114
    branches.get(stop) match {
wenzelm@45469
   115
      case Some(end) if range overlaps end.range => bs + (end.range -> end)
wenzelm@45457
   116
      case _ => bs
wenzelm@45457
   117
    }
wenzelm@45457
   118
  }
wenzelm@45457
   119
wenzelm@45469
   120
  def + (new_markup: Text.Markup): Markup_Tree =
wenzelm@34703
   121
  {
wenzelm@45469
   122
    val new_range = new_markup.range
wenzelm@45469
   123
wenzelm@38578
   124
    branches.get(new_range) match {
wenzelm@45473
   125
      case None => new Markup_Tree(branches, Entry(new_markup, empty))
wenzelm@45469
   126
      case Some(entry) =>
wenzelm@45469
   127
        if (entry.range == new_range)
wenzelm@45473
   128
          new Markup_Tree(branches, entry + new_markup.info)
wenzelm@45469
   129
        else if (entry.range.contains(new_range))
wenzelm@45469
   130
          new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
wenzelm@38578
   131
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@45473
   132
          new Markup_Tree(Branches.empty, Entry(new_markup, this))
wenzelm@38482
   133
        else {
wenzelm@45457
   134
          val body = overlapping(new_range)
wenzelm@38578
   135
          if (body.forall(e => new_range.contains(e._1))) {
wenzelm@45469
   136
            val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
wenzelm@38657
   137
              if (body.size > 1)
wenzelm@38657
   138
                (Branches.empty /: branches)((rest, entry) =>
wenzelm@38657
   139
                  if (body.isDefinedAt(entry._1)) rest else rest + entry)
wenzelm@38657
   140
              else branches
wenzelm@45473
   141
            new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body)))
wenzelm@38482
   142
          }
wenzelm@38482
   143
          else { // FIXME split markup!?
wenzelm@48762
   144
            System.err.println("Ignored overlapping markup information: " + new_markup +
wenzelm@48762
   145
              body.filter(e => !new_range.contains(e._1)).mkString("\n"))
wenzelm@38482
   146
            this
wenzelm@38482
   147
          }
wenzelm@38482
   148
        }
wenzelm@34703
   149
    }
wenzelm@34703
   150
  }
wenzelm@34703
   151
wenzelm@46178
   152
  def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
wenzelm@46178
   153
    result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   154
  {
wenzelm@46178
   155
    def results(x: A, entry: Entry): Option[A] =
wenzelm@46178
   156
      if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
wenzelm@45474
   157
        val (y, changed) =
wenzelm@45474
   158
          (entry.markup :\ (x, false))((info, res) =>
wenzelm@45474
   159
            {
wenzelm@45474
   160
              val (y, changed) = res
wenzelm@46165
   161
              val arg = (y, Text.Info(entry.range, info))
wenzelm@46178
   162
              if (result.isDefinedAt(arg)) (result(arg), true)
wenzelm@45474
   163
              else res
wenzelm@45474
   164
            })
wenzelm@45474
   165
        if (changed) Some(y) else None
wenzelm@45474
   166
      }
wenzelm@45474
   167
      else None
wenzelm@45467
   168
wenzelm@45459
   169
    def stream(
wenzelm@45459
   170
      last: Text.Offset,
wenzelm@45469
   171
      stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
wenzelm@45459
   172
    {
wenzelm@45459
   173
      stack match {
wenzelm@45469
   174
        case (parent, (range, entry) #:: more) :: rest =>
wenzelm@45467
   175
          val subrange = range.restrict(root_range)
wenzelm@45469
   176
          val subtree = entry.subtree.overlapping(subrange).toStream
wenzelm@45459
   177
          val start = subrange.start
wenzelm@45459
   178
wenzelm@46178
   179
          results(parent.info, entry) match {
wenzelm@45469
   180
            case Some(res) =>
wenzelm@45469
   181
              val next = Text.Info(subrange, res)
wenzelm@45469
   182
              val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@45469
   183
              if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
wenzelm@45469
   184
              else nexts
wenzelm@45469
   185
            case None => stream(last, (parent, subtree #::: more) :: rest)
wenzelm@45459
   186
          }
wenzelm@45459
   187
wenzelm@45459
   188
        case (parent, Stream.Empty) :: rest =>
wenzelm@45459
   189
          val stop = parent.range.stop
wenzelm@45459
   190
          val nexts = stream(stop, rest)
wenzelm@45459
   191
          if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
wenzelm@45459
   192
          else nexts
wenzelm@45459
   193
wenzelm@45459
   194
        case Nil =>
wenzelm@45467
   195
          val stop = root_range.stop
wenzelm@45467
   196
          if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
wenzelm@45459
   197
          else Stream.empty
wenzelm@45459
   198
      }
wenzelm@45459
   199
    }
wenzelm@45467
   200
    stream(root_range.start,
wenzelm@45467
   201
      List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
wenzelm@45459
   202
  }
wenzelm@45459
   203
wenzelm@49465
   204
  def swing_tree(parent: DefaultMutableTreeNode,
wenzelm@49465
   205
    swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
wenzelm@38479
   206
  {
wenzelm@45469
   207
    for ((_, entry) <- branches) {
wenzelm@47539
   208
      val node = swing_node(Text.Info(entry.range, entry.markup))
wenzelm@49465
   209
      entry.subtree.swing_tree(node, swing_node)
wenzelm@49465
   210
      parent.add(node)
immler@34514
   211
    }
immler@34514
   212
  }
wenzelm@34717
   213
}
immler@34514
   214