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