src/Pure/PIDE/markup_tree.scala
author wenzelm
Sat Nov 12 12:21:42 2011 +0100 (2011-11-12)
changeset 45468 33e946d3f449
parent 45467 3f290b6288cf
child 45469 25306d92f4ad
permissions -rw-r--r--
tuned signature;
express select in terms of cumulate;
     1 /*  Title:      Pure/PIDE/markup_tree.scala
     2     Author:     Fabian Immler, TU Munich
     3     Author:     Makarius
     4 
     5 Markup trees over nested / non-overlapping text ranges.
     6 */
     7 
     8 package isabelle
     9 
    10 import java.lang.System
    11 import javax.swing.tree.DefaultMutableTreeNode
    12 
    13 import scala.collection.immutable.SortedMap
    14 
    15 
    16 object Markup_Tree
    17 {
    18   sealed case class Cumulate[A](info: A, result: PartialFunction[(A, Text.Markup), A])
    19   sealed case class Select[A](result: PartialFunction[Text.Markup, A])
    20 
    21   val empty: Markup_Tree = new Markup_Tree(Branches.empty)
    22 
    23   object Branches
    24   {
    25     type Entry = (Text.Markup, Markup_Tree)
    26     type T = SortedMap[Text.Range, Entry]
    27 
    28     val empty: T = SortedMap.empty(Text.Range.Ordering)
    29     def update(branches: T, entry: Entry): T = branches + (entry._1.range -> entry)
    30     def single(entry: Entry): T = update(empty, entry)
    31   }
    32 }
    33 
    34 
    35 class Markup_Tree private(branches: Markup_Tree.Branches.T)
    36 {
    37   import Markup_Tree._
    38 
    39   override def toString =
    40     branches.toList.map(_._2) match {
    41       case Nil => "Empty"
    42       case list => list.mkString("Tree(", ",", ")")
    43     }
    44 
    45   private def overlapping(range: Text.Range): Branches.T =  // FIXME special cases!?
    46   {
    47     val start = Text.Range(range.start)
    48     val stop = Text.Range(range.stop)
    49     val bs = branches.range(start, stop)
    50     // FIXME check after Scala 2.8.x
    51     branches.get(stop) match {
    52       case Some(end) if range overlaps end._1.range => Branches.update(bs, end)
    53       case _ => bs
    54     }
    55   }
    56 
    57   def + (new_info: Text.Markup): Markup_Tree =
    58   {
    59     val new_range = new_info.range
    60     branches.get(new_range) match {
    61       case None =>
    62         new Markup_Tree(Branches.update(branches, new_info -> empty))
    63       case Some((info, subtree)) =>
    64         val range = info.range
    65         if (range.contains(new_range))
    66           new Markup_Tree(Branches.update(branches, info -> (subtree + new_info)))
    67         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    68           new Markup_Tree(Branches.single(new_info -> this))
    69         else {
    70           val body = overlapping(new_range)
    71           if (body.forall(e => new_range.contains(e._1))) {
    72             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0
    73               if (body.size > 1)
    74                 (Branches.empty /: branches)((rest, entry) =>
    75                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    76               else branches
    77             new Markup_Tree(Branches.update(rest, new_info -> new Markup_Tree(body)))
    78           }
    79           else { // FIXME split markup!?
    80             System.err.println("Ignored overlapping markup information: " + new_info)
    81             this
    82           }
    83         }
    84     }
    85   }
    86 
    87   def cumulate[A](root_range: Text.Range)(body: Cumulate[A]): Stream[Text.Info[A]] =
    88   {
    89     val root_info = body.info
    90     val result = body.result
    91 
    92     def stream(
    93       last: Text.Offset,
    94       stack: List[(Text.Info[A], Stream[(Text.Range, Branches.Entry)])]): Stream[Text.Info[A]] =
    95     {
    96       stack match {
    97         case (parent, (range, (info, tree)) #:: more) :: rest =>
    98           val subrange = range.restrict(root_range)
    99           val subtree = tree.overlapping(subrange).toStream
   100           val start = subrange.start
   101 
   102           val arg = (parent.info, info)
   103           if (result.isDefinedAt(arg)) {
   104             val next = Text.Info(subrange, result(arg))
   105             val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   106             if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   107             else nexts
   108           }
   109           else stream(last, (parent, subtree #::: more) :: rest)
   110 
   111         case (parent, Stream.Empty) :: rest =>
   112           val stop = parent.range.stop
   113           val nexts = stream(stop, rest)
   114           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   115           else nexts
   116 
   117         case Nil =>
   118           val stop = root_range.stop
   119           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   120           else Stream.empty
   121       }
   122     }
   123     stream(root_range.start,
   124       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   125   }
   126 
   127   def swing_tree(parent: DefaultMutableTreeNode)
   128     (swing_node: Text.Markup => DefaultMutableTreeNode)
   129   {
   130     for ((_, (info, subtree)) <- branches) {
   131       val current = swing_node(info)
   132       subtree.swing_tree(current)(swing_node)
   133       parent.add(current)
   134     }
   135   }
   136 }
   137