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