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