src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Feb 20 16:00:37 2014 +0100 (2014-02-20)
changeset 55620 19dffae33cde
parent 55618 995162143ef4
child 55622 ce575c2212fc
permissions -rw-r--r--
cumulate/select wrt. precise elements guard;
tuned signature;
     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 exists(pred: String => Boolean): Boolean = rep.exists(pred)
    49 
    50     def + (name: String): Elements =
    51       if (rep(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             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 ++ (other: Markup_Tree): Markup_Tree =
   188     (this /: other.branches)({ case (tree, (range, entry)) =>
   189       ((tree ++ entry.subtree) /: entry.markup)({ case (t, elem) => t + Text.Info(range, elem) }) })
   190 
   191   def to_XML(root_range: Text.Range, text: CharSequence, filter: XML.Elem => Boolean): XML.Body =
   192   {
   193     def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
   194       if (start == stop) Nil
   195       else List(XML.Text(text.subSequence(start, stop).toString))
   196 
   197     def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
   198       (body /: rev_markups) {
   199         case (b, elem) =>
   200           if (!filter(elem)) b
   201           else if (elem.body.isEmpty) List(XML.Elem(elem.markup, b))
   202           else List(XML.Wrapped_Elem(elem.markup, elem.body, b))
   203       }
   204 
   205     def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
   206       : XML.Body =
   207     {
   208       val body = new mutable.ListBuffer[XML.Tree]
   209       var last = elem_range.start
   210       for ((range, entry) <- entries) {
   211         val subrange = range.restrict(elem_range)
   212         body ++= make_text(last, subrange.start)
   213         body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
   214         last = subrange.stop
   215       }
   216       body ++= make_text(last, elem_range.stop)
   217       make_elems(elem_markup, body.toList)
   218     }
   219    make_body(root_range, Nil, overlapping(root_range))
   220   }
   221 
   222   def to_XML(text: CharSequence): XML.Body =
   223     to_XML(Text.Range(0, text.length), text, (_: XML.Elem) => true)
   224 
   225   def cumulate[A](root_range: Text.Range, root_info: A, elements: String => Boolean,
   226     result: (A, Text.Markup) => Option[A]): List[Text.Info[A]] =
   227   {
   228     def results(x: A, entry: Entry): Option[A] =
   229     {
   230       var y = x
   231       var changed = false
   232       for {
   233         elem <- entry.rev_markup // FIXME proper cumulation order (including status markup) (!?)
   234         if elements(elem.name)
   235         y1 <- result(y, Text.Info(entry.range, elem))
   236       } { y = y1; changed = true }
   237       if (changed) Some(y) else None
   238     }
   239 
   240     def traverse(
   241       last: Text.Offset,
   242       stack: List[(Text.Info[A], List[(Text.Range, Entry)])]): List[Text.Info[A]] =
   243     {
   244       stack match {
   245         case (parent, (range, entry) :: more) :: rest =>
   246           val subrange = range.restrict(root_range)
   247           val subtree =
   248             if (entry.subtree_elements.exists(elements))
   249               entry.subtree.overlapping(subrange).toList
   250             else Nil
   251           val start = subrange.start
   252 
   253           (if (entry.elements.exists(elements)) results(parent.info, entry) else None) match {
   254             case Some(res) =>
   255               val next = Text.Info(subrange, res)
   256               val nexts = traverse(start, (next, subtree) :: (parent, more) :: rest)
   257               if (last < start) parent.restrict(Text.Range(last, start)) :: nexts
   258               else nexts
   259             case None => traverse(last, (parent, subtree ::: more) :: rest)
   260           }
   261 
   262         case (parent, Nil) :: rest =>
   263           val stop = parent.range.stop
   264           val nexts = traverse(stop, rest)
   265           if (last < stop) parent.restrict(Text.Range(last, stop)) :: nexts
   266           else nexts
   267 
   268         case Nil =>
   269           val stop = root_range.stop
   270           if (last < stop) List(Text.Info(Text.Range(last, stop), root_info))
   271           else Nil
   272       }
   273     }
   274     traverse(root_range.start,
   275       List((Text.Info(root_range, root_info), overlapping(root_range).toList)))
   276   }
   277 }
   278