src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Mar 27 11:19:31 2014 +0100 (2014-03-27)
changeset 56300 8346b664fa7a
parent 56299 8201790fdeb9
child 56301 1da7b4c33db9
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]): Markup_Tree =
    24     (empty /: trees)(_ ++ _)
    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: Document.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   override def toString =
   120     branches.toList.map(_._2) match {
   121       case Nil => "Empty"
   122       case list => list.mkString("Tree(", ",", ")")
   123     }
   124 
   125   private def overlapping(range: Text.Range): Branches.T =
   126   {
   127     val start = Text.Range(range.start)
   128     val stop = Text.Range(range.stop)
   129     val bs = branches.range(start, stop)
   130     branches.get(stop) match {
   131       case Some(end) if range overlaps end.range => bs + (end.range -> end)
   132       case _ => bs
   133     }
   134   }
   135 
   136   def + (new_markup: Text.Markup): Markup_Tree =
   137   {
   138     val new_range = new_markup.range
   139 
   140     branches.get(new_range) match {
   141       case None => new Markup_Tree(branches, Entry(new_markup, empty))
   142       case Some(entry) =>
   143         if (entry.range == new_range)
   144           new Markup_Tree(branches, entry + new_markup)
   145         else if (entry.range.contains(new_range))
   146           new Markup_Tree(branches, entry \ new_markup)
   147         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
   148           new Markup_Tree(Branches.empty, Entry(new_markup, this))
   149         else {
   150           val body = overlapping(new_range)
   151           if (body.forall(e => new_range.contains(e._1)))
   152             new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
   153           else {
   154             System.err.println("Ignored overlapping markup information: " + new_markup +
   155               body.filter(e => !new_range.contains(e._1)).mkString("\n"))
   156             this
   157           }
   158         }
   159     }
   160   }
   161 
   162   def ++ (other: Markup_Tree): Markup_Tree =
   163     if (this eq other) this
   164     else if (branches.isEmpty) other
   165     else
   166       (this /: other.branches)({ case (tree, (range, entry)) =>
   167         ((tree ++ entry.subtree) /: entry.markup)(
   168           { case (t, elem) => t + Text.Info(range, elem) }) })
   169 
   170   def to_XML(root_range: Text.Range, text: CharSequence, elements: Document.Elements): XML.Body =
   171   {
   172     def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
   173       if (start == stop) Nil
   174       else List(XML.Text(text.subSequence(start, stop).toString))
   175 
   176     def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
   177       (body /: rev_markups) {
   178         case (b, elem) =>
   179           if (!elements(elem.name)) b
   180           else if (elem.body.isEmpty) List(XML.Elem(elem.markup, b))
   181           else List(XML.Wrapped_Elem(elem.markup, elem.body, b))
   182       }
   183 
   184     def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
   185       : XML.Body =
   186     {
   187       val body = new mutable.ListBuffer[XML.Tree]
   188       var last = elem_range.start
   189       for ((range, entry) <- entries) {
   190         val subrange = range.restrict(elem_range)
   191         body ++= make_text(last, subrange.start)
   192         body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
   193         last = subrange.stop
   194       }
   195       body ++= make_text(last, elem_range.stop)
   196       make_elems(elem_markup, body.toList)
   197     }
   198    make_body(root_range, Nil, overlapping(root_range))
   199   }
   200 
   201   def cumulate[A](root_range: Text.Range, root_info: A, elements: Document.Elements,
   202     result: (A, Text.Markup) => Option[A]): List[Text.Info[A]] =
   203   {
   204     def results(x: A, entry: Entry): Option[A] =
   205     {
   206       var y = x
   207       var changed = false
   208       for {
   209         elem <- entry.filter_markup(elements)
   210         y1 <- result(y, Text.Info(entry.range, elem))
   211       } { y = y1; changed = true }
   212       if (changed) Some(y) else None
   213     }
   214 
   215     def traverse(
   216       last: Text.Offset,
   217       stack: List[(Text.Info[A], List[(Text.Range, Entry)])]): List[Text.Info[A]] =
   218     {
   219       stack match {
   220         case (parent, (range, entry) :: more) :: rest =>
   221           val subrange = range.restrict(root_range)
   222           val subtree = entry.subtree.overlapping(subrange).toList
   223           val start = subrange.start
   224 
   225           results(parent.info, entry) match {
   226             case Some(res) =>
   227               val next = Text.Info(subrange, res)
   228               val nexts = traverse(start, (next, subtree) :: (parent, more) :: rest)
   229               if (last < start) parent.restrict(Text.Range(last, start)) :: nexts
   230               else nexts
   231             case None => traverse(last, (parent, subtree ::: more) :: rest)
   232           }
   233 
   234         case (parent, Nil) :: rest =>
   235           val stop = parent.range.stop
   236           val nexts = traverse(stop, rest)
   237           if (last < stop) parent.restrict(Text.Range(last, stop)) :: nexts
   238           else nexts
   239 
   240         case Nil =>
   241           val stop = root_range.stop
   242           if (last < stop) List(Text.Info(Text.Range(last, stop), root_info))
   243           else Nil
   244       }
   245     }
   246     traverse(root_range.start,
   247       List((Text.Info(root_range, root_info), overlapping(root_range).toList)))
   248   }
   249 }
   250