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