src/Pure/PIDE/markup_tree.scala
author wenzelm
Sat Dec 15 18:30:09 2012 +0100 (2012-12-15)
changeset 50552 2b7fd8c9c4ac
parent 50551 67d934cdc9b9
child 50642 aca12f646772
permissions -rw-r--r--
maintain subtree_elements for improved performance of cumulate operator;
     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 import scala.collection.mutable
    16 import scala.annotation.tailrec
    17 
    18 
    19 object Markup_Tree
    20 {
    21   /* construct trees */
    22 
    23   val empty: Markup_Tree = new Markup_Tree(Branches.empty)
    24 
    25   def merge_disjoint(trees: List[Markup_Tree]): Markup_Tree =
    26     trees match {
    27       case Nil => empty
    28       case head :: tail =>
    29         new Markup_Tree(
    30           (head.branches /: tail) {
    31             case (branches, tree) =>
    32               (branches /: tree.branches) {
    33                 case (bs, (r, entry)) =>
    34                   require(!bs.isDefinedAt(r))
    35                   bs + (r -> entry)
    36               }
    37           })
    38     }
    39 
    40 
    41   /* tree building blocks */
    42 
    43   object Elements
    44   {
    45     val empty = new Elements(Set.empty)
    46   }
    47 
    48   final class Elements private(private val rep: Set[String])
    49   {
    50     def contains(name: String): Boolean = rep.contains(name)
    51 
    52     def + (name: String): Elements =
    53       if (contains(name)) this
    54       else new Elements(rep + name)
    55 
    56     def + (elem: XML.Elem): Elements = this + elem.markup.name
    57     def ++ (elems: Iterable[XML.Elem]): Elements = (this /: elems.iterator)(_ + _)
    58 
    59     def ++ (other: Elements): Elements =
    60       if (this eq other) this
    61       else if (rep.isEmpty) other
    62       else (this /: other.rep)(_ + _)
    63   }
    64 
    65   object Entry
    66   {
    67     def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
    68       Entry(markup.range, List(markup.info), Elements.empty + markup.info,
    69         subtree, subtree.make_elements)
    70 
    71     def apply(range: Text.Range, rev_markups: List[XML.Elem], subtree: Markup_Tree): Entry =
    72       Entry(range, rev_markups, Elements.empty ++ rev_markups,
    73         subtree, subtree.make_elements)
    74   }
    75 
    76   sealed case class Entry(
    77     range: Text.Range,
    78     rev_markup: List[XML.Elem],
    79     elements: Elements,
    80     subtree: Markup_Tree,
    81     subtree_elements: Elements)
    82   {
    83     def markup: List[XML.Elem] = rev_markup.reverse
    84 
    85     def + (markup: Text.Markup): Entry =
    86       copy(rev_markup = markup.info :: rev_markup, elements = elements + markup.info)
    87 
    88     def \ (markup: Text.Markup): Entry =
    89       copy(subtree = subtree + markup, subtree_elements = subtree_elements + markup.info)
    90   }
    91 
    92   object Branches
    93   {
    94     type T = SortedMap[Text.Range, Entry]
    95     val empty: T = SortedMap.empty(Text.Range.Ordering)
    96   }
    97 
    98 
    99   /* XML representation */
   100 
   101   @tailrec private def strip_elems(
   102       elems: List[XML.Elem], body: XML.Body): (List[XML.Elem], XML.Body) =
   103     body match {
   104       case List(XML.Wrapped_Elem(markup1, body1, body2)) =>
   105         strip_elems(XML.Elem(markup1, body1) :: elems, body2)
   106       case List(XML.Elem(markup1, body1)) =>
   107         strip_elems(XML.Elem(markup1, Nil) :: elems, body1)
   108       case _ => (elems, body)
   109     }
   110 
   111   private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) =
   112     {
   113       val (offset, markup_trees) = acc
   114 
   115       strip_elems(Nil, List(tree)) match {
   116         case (Nil, body) =>
   117           (offset + XML.text_length(body), markup_trees)
   118 
   119         case (elems, body) =>
   120           val (end_offset, subtrees) = ((offset, Nil: List[Markup_Tree]) /: body)(make_trees)
   121           val range = Text.Range(offset, end_offset)
   122           val entry = Entry(range, elems, merge_disjoint(subtrees))
   123           (end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees)
   124       }
   125     }
   126 
   127   def from_XML(body: XML.Body): Markup_Tree =
   128     merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
   129 }
   130 
   131 
   132 final class Markup_Tree private(private val branches: Markup_Tree.Branches.T)
   133 {
   134   import Markup_Tree._
   135 
   136   private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
   137     this(branches + (entry.range -> entry))
   138 
   139   override def toString =
   140     branches.toList.map(_._2) match {
   141       case Nil => "Empty"
   142       case list => list.mkString("Tree(", ",", ")")
   143     }
   144 
   145   private def overlapping(range: Text.Range): Branches.T =
   146   {
   147     val start = Text.Range(range.start)
   148     val stop = Text.Range(range.stop)
   149     val bs = branches.range(start, stop)
   150     branches.get(stop) match {
   151       case Some(end) if range overlaps end.range => bs + (end.range -> end)
   152       case _ => bs
   153     }
   154   }
   155 
   156   def make_elements: Elements =
   157     (Elements.empty /: branches)(
   158       { case (elements, (_, entry)) => elements ++ entry.subtree_elements ++ entry.elements })
   159 
   160   def + (new_markup: Text.Markup): Markup_Tree =
   161   {
   162     val new_range = new_markup.range
   163 
   164     branches.get(new_range) match {
   165       case None => new Markup_Tree(branches, Entry(new_markup, empty))
   166       case Some(entry) =>
   167         if (entry.range == new_range)
   168           new Markup_Tree(branches, entry + new_markup)
   169         else if (entry.range.contains(new_range))
   170           new Markup_Tree(branches, entry \ new_markup)
   171         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
   172           new Markup_Tree(Branches.empty, Entry(new_markup, this))
   173         else {
   174           val body = overlapping(new_range)
   175           if (body.forall(e => new_range.contains(e._1)))
   176             new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
   177           else {
   178             java.lang.System.err.println("Ignored overlapping markup information: " + new_markup +
   179               body.filter(e => !new_range.contains(e._1)).mkString("\n"))
   180             this
   181           }
   182         }
   183     }
   184   }
   185 
   186   def to_XML(root_range: Text.Range, text: CharSequence, filter: XML.Elem => Boolean): XML.Body =
   187   {
   188     def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
   189       if (start == stop) Nil
   190       else List(XML.Text(text.subSequence(start, stop).toString))
   191 
   192     def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
   193       (body /: rev_markups) {
   194         case (b, elem) =>
   195           if (!filter(elem)) b
   196           else if (elem.body.isEmpty) List(XML.Elem(elem.markup, b))
   197           else List(XML.Wrapped_Elem(elem.markup, elem.body, b))
   198       }
   199 
   200     def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
   201       : XML.Body =
   202     {
   203       val body = new mutable.ListBuffer[XML.Tree]
   204       var last = elem_range.start
   205       for ((range, entry) <- entries) {
   206         val subrange = range.restrict(elem_range)
   207         body ++= make_text(last, subrange.start)
   208         body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
   209         last = subrange.stop
   210       }
   211       body ++= make_text(last, elem_range.stop)
   212       make_elems(elem_markup, body.toList)
   213     }
   214    make_body(root_range, Nil, overlapping(root_range))
   215   }
   216 
   217   def to_XML(text: CharSequence): XML.Body =
   218     to_XML(Text.Range(0, text.length), text, (_: XML.Elem) => true)
   219 
   220   def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
   221     result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
   222   {
   223     val notable: Elements => Boolean =
   224       result_elements match {
   225         case Some(res) => (elements: Elements) => res.exists(elements.contains)
   226         case None => (elements: Elements) => true
   227       }
   228 
   229     def results(x: A, entry: Entry): Option[A] =
   230     {
   231       val (y, changed) =
   232         // FIXME proper cumulation order (including status markup) (!?)
   233         ((x, false) /: entry.rev_markup)((res, info) =>
   234           {
   235             val (y, changed) = res
   236             val arg = (y, Text.Info(entry.range, info))
   237             if (result.isDefinedAt(arg)) (result(arg), true)
   238             else res
   239           })
   240       if (changed) Some(y) else None
   241     }
   242 
   243     def stream(
   244       last: Text.Offset,
   245       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   246     {
   247       stack match {
   248         case (parent, (range, entry) #:: more) :: rest =>
   249           val subrange = range.restrict(root_range)
   250           val subtree =
   251             if (notable(entry.subtree_elements))
   252               entry.subtree.overlapping(subrange).toStream
   253             else Stream.empty
   254           val start = subrange.start
   255 
   256           (if (notable(entry.elements)) results(parent.info, entry) else None) match {
   257             case Some(res) =>
   258               val next = Text.Info(subrange, res)
   259               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   260               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   261               else nexts
   262             case None => stream(last, (parent, subtree #::: more) :: rest)
   263           }
   264 
   265         case (parent, Stream.Empty) :: rest =>
   266           val stop = parent.range.stop
   267           val nexts = stream(stop, rest)
   268           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   269           else nexts
   270 
   271         case Nil =>
   272           val stop = root_range.stop
   273           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   274           else Stream.empty
   275       }
   276     }
   277     stream(root_range.start,
   278       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   279   }
   280 
   281   def swing_tree(parent: DefaultMutableTreeNode,
   282     swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
   283   {
   284     for ((_, entry) <- branches) {
   285       val node = swing_node(Text.Info(entry.range, entry.markup))
   286       entry.subtree.swing_tree(node, swing_node)
   287       parent.add(node)
   288     }
   289   }
   290 }
   291