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