src/Pure/PIDE/markup_tree.scala
author wenzelm
Sat Nov 12 17:52:28 2011 +0100 (2011-11-12)
changeset 45470 81b85d4ed269
parent 45469 25306d92f4ad
child 45473 66395afbd915
permissions -rw-r--r--
more precise type;
     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   sealed case class Entry(range: Text.Range, rev_markup: List[XML.Elem], subtree: Markup_Tree)
    24   {
    25     def + (m: XML.Elem): Entry = copy(rev_markup = m :: rev_markup)
    26     def markup: List[XML.Elem] = rev_markup.reverse
    27   }
    28 
    29   object Branches
    30   {
    31     type T = SortedMap[Text.Range, Entry]
    32     val empty: T = SortedMap.empty(Text.Range.Ordering)
    33   }
    34 }
    35 
    36 
    37 class Markup_Tree private(branches: Markup_Tree.Branches.T)
    38 {
    39   private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
    40     this(branches + (entry.range -> entry))
    41 
    42 
    43   import Markup_Tree._
    44 
    45   override def toString =
    46     branches.toList.map(_._2) match {
    47       case Nil => "Empty"
    48       case list => list.mkString("Tree(", ",", ")")
    49     }
    50 
    51   private def overlapping(range: Text.Range): Branches.T =
    52   {
    53     val start = Text.Range(range.start)
    54     val stop = Text.Range(range.stop)
    55     val bs = branches.range(start, stop)
    56     // FIXME check after Scala 2.8.x
    57     branches.get(stop) match {
    58       case Some(end) if range overlaps end.range => bs + (end.range -> end)
    59       case _ => bs
    60     }
    61   }
    62 
    63   def + (new_markup: Text.Markup): Markup_Tree =
    64   {
    65     val new_range = new_markup.range
    66     val new_info = new_markup.info
    67 
    68     branches.get(new_range) match {
    69       case None =>
    70         new Markup_Tree(branches, Entry(new_range, List(new_info), empty))
    71       case Some(entry) =>
    72         if (entry.range == new_range)
    73           new Markup_Tree(branches, entry + new_info)
    74         else if (entry.range.contains(new_range))
    75           new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
    76         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    77           new Markup_Tree(Branches.empty, Entry(new_range, List(new_info), this))
    78         else {
    79           val body = overlapping(new_range)
    80           if (body.forall(e => new_range.contains(e._1))) {
    81             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
    82               if (body.size > 1)
    83                 (Branches.empty /: branches)((rest, entry) =>
    84                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    85               else branches
    86             new Markup_Tree(rest, Entry(new_range, List(new_info), new Markup_Tree(body)))
    87           }
    88           else { // FIXME split markup!?
    89             System.err.println("Ignored overlapping markup information: " + new_markup)
    90             this
    91           }
    92         }
    93     }
    94   }
    95 
    96   def cumulate[A](root_range: Text.Range)(body: Cumulate[A]): Stream[Text.Info[A]] =
    97   {
    98     val root_info = body.info
    99 
   100     def result(x: A, entry: Entry): Option[A] =
   101     {
   102       val (y, changed) =
   103         (entry.markup :\ (x, false))((info, res) =>
   104           {
   105             val (y, changed) = res
   106             val arg = (x, Text.Info(entry.range, info))
   107             if (body.result.isDefinedAt(arg)) (body.result(arg), true)
   108             else res
   109           })
   110       if (changed) Some(y) else None
   111     }
   112 
   113     def stream(
   114       last: Text.Offset,
   115       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   116     {
   117       stack match {
   118         case (parent, (range, entry) #:: more) :: rest =>
   119           val subrange = range.restrict(root_range)
   120           val subtree = entry.subtree.overlapping(subrange).toStream
   121           val start = subrange.start
   122 
   123           result(parent.info, entry) match {
   124             case Some(res) =>
   125               val next = Text.Info(subrange, res)
   126               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   127               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   128               else nexts
   129             case None => stream(last, (parent, subtree #::: more) :: rest)
   130           }
   131 
   132         case (parent, Stream.Empty) :: rest =>
   133           val stop = parent.range.stop
   134           val nexts = stream(stop, rest)
   135           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   136           else nexts
   137 
   138         case Nil =>
   139           val stop = root_range.stop
   140           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   141           else Stream.empty
   142       }
   143     }
   144     stream(root_range.start,
   145       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   146   }
   147 
   148   def swing_tree(parent: DefaultMutableTreeNode)
   149     (swing_node: Text.Markup => DefaultMutableTreeNode)
   150   {
   151     for ((_, entry) <- branches) {
   152       var current = parent
   153       for (info <- entry.markup) {
   154         val node = swing_node(Text.Info(entry.range, info))
   155         current.add(node)
   156         current = node
   157       }
   158       entry.subtree.swing_tree(current)(swing_node)
   159     }
   160   }
   161 }
   162