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