src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Sep 27 14:46:34 2012 +0200 (2012-09-27)
changeset 49607 b610c0d52bfa
parent 49469 00c301c8d569
child 49608 ce1c34c98eeb
permissions -rw-r--r--
updated to consolidated SortedMap in scala-2.9.x;
     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.annotation.tailrec
    16 
    17 
    18 object Markup_Tree
    19 {
    20   val empty: Markup_Tree = new Markup_Tree(Branches.empty)
    21 
    22   def merge_disjoint(trees: List[Markup_Tree]): Markup_Tree =
    23     trees match {
    24       case Nil => empty
    25       case head :: tail =>
    26         new Markup_Tree(
    27           (head.branches /: tail) {
    28             case (branches, tree) =>
    29               (branches /: tree.branches) {
    30                 case (bs, (r, entry)) =>
    31                   require(!bs.isDefinedAt(r))
    32                   bs + (r -> entry)
    33               }
    34           })
    35     }
    36 
    37   object Entry
    38   {
    39     def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
    40       Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree)
    41 
    42     def apply(range: Text.Range, rev_markups: List[XML.Elem], subtree: Markup_Tree): Entry =
    43       Entry(range, rev_markups, Set.empty ++ rev_markups.iterator.map(_.markup.name), subtree)
    44   }
    45 
    46   sealed case class Entry(
    47     range: Text.Range,
    48     rev_markup: List[XML.Elem],
    49     elements: Set[String],
    50     subtree: Markup_Tree)
    51   {
    52     def + (info: XML.Elem): Entry =
    53       if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup)
    54       else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name)
    55 
    56     def markup: List[XML.Elem] = rev_markup.reverse
    57   }
    58 
    59   object Branches
    60   {
    61     type T = SortedMap[Text.Range, Entry]
    62     val empty: T = SortedMap.empty(Text.Range.Ordering)
    63   }
    64 
    65 
    66   /* XML representation */
    67 
    68   @tailrec private def strip_elems(markups: List[Markup], body: XML.Body): (List[Markup], XML.Body) =
    69     body match {
    70       case List(XML.Elem(markup1, body1)) => strip_elems(markup1 :: markups, body1)
    71       case _ => (markups, body)
    72     }
    73 
    74   private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) =
    75     {
    76       val (offset, markup_trees) = acc
    77 
    78       strip_elems(Nil, List(tree)) match {
    79         case (Nil, body) =>
    80           (offset + XML.text_length(body), markup_trees)
    81 
    82         case (elems, body) =>
    83           val (end_offset, subtrees) = ((offset, Nil: List[Markup_Tree]) /: body)(make_trees)
    84           val range = Text.Range(offset, end_offset)
    85           val entry = Entry(range, elems.map(XML.Elem(_, Nil)), merge_disjoint(subtrees))
    86           (end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees)
    87       }
    88     }
    89 
    90   def from_XML(body: XML.Body): Markup_Tree =
    91     merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
    92 }
    93 
    94 
    95 final class Markup_Tree private(private val branches: Markup_Tree.Branches.T)
    96 {
    97   import Markup_Tree._
    98 
    99   private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
   100     this(branches + (entry.range -> entry))
   101 
   102   override def toString =
   103     branches.toList.map(_._2) match {
   104       case Nil => "Empty"
   105       case list => list.mkString("Tree(", ",", ")")
   106     }
   107 
   108   private def overlapping(range: Text.Range): Branches.T =
   109   {
   110     val start = Text.Range(range.start)
   111     val stop = Text.Range(range.stop)
   112     val bs = branches.range(start, stop)
   113     branches.get(stop) match {
   114       case Some(end) if range overlaps end.range => bs + (end.range -> end)
   115       case _ => bs
   116     }
   117   }
   118 
   119   def + (new_markup: Text.Markup): Markup_Tree =
   120   {
   121     val new_range = new_markup.range
   122 
   123     branches.get(new_range) match {
   124       case None => new Markup_Tree(branches, Entry(new_markup, empty))
   125       case Some(entry) =>
   126         if (entry.range == new_range)
   127           new Markup_Tree(branches, entry + new_markup.info)
   128         else if (entry.range.contains(new_range))
   129           new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
   130         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
   131           new Markup_Tree(Branches.empty, Entry(new_markup, this))
   132         else {
   133           val body = overlapping(new_range)
   134           if (body.forall(e => new_range.contains(e._1)))
   135             new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
   136           else { // FIXME split markup!?
   137             System.err.println("Ignored overlapping markup information: " + new_markup +
   138               body.filter(e => !new_range.contains(e._1)).mkString("\n"))
   139             this
   140           }
   141         }
   142     }
   143   }
   144 
   145   def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
   146     result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
   147   {
   148     def results(x: A, entry: Entry): Option[A] =
   149       if (result_elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
   150         val (y, changed) =
   151           (entry.markup :\ (x, false))((info, res) =>
   152             {
   153               val (y, changed) = res
   154               val arg = (y, Text.Info(entry.range, info))
   155               if (result.isDefinedAt(arg)) (result(arg), true)
   156               else res
   157             })
   158         if (changed) Some(y) else None
   159       }
   160       else None
   161 
   162     def stream(
   163       last: Text.Offset,
   164       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   165     {
   166       stack match {
   167         case (parent, (range, entry) #:: more) :: rest =>
   168           val subrange = range.restrict(root_range)
   169           val subtree = entry.subtree.overlapping(subrange).toStream
   170           val start = subrange.start
   171 
   172           results(parent.info, entry) match {
   173             case Some(res) =>
   174               val next = Text.Info(subrange, res)
   175               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   176               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   177               else nexts
   178             case None => stream(last, (parent, subtree #::: more) :: rest)
   179           }
   180 
   181         case (parent, Stream.Empty) :: rest =>
   182           val stop = parent.range.stop
   183           val nexts = stream(stop, rest)
   184           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   185           else nexts
   186 
   187         case Nil =>
   188           val stop = root_range.stop
   189           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   190           else Stream.empty
   191       }
   192     }
   193     stream(root_range.start,
   194       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   195   }
   196 
   197   def swing_tree(parent: DefaultMutableTreeNode,
   198     swing_node: Text.Info[List[XML.Elem]] => DefaultMutableTreeNode)
   199   {
   200     for ((_, entry) <- branches) {
   201       val node = swing_node(Text.Info(entry.range, entry.markup))
   202       entry.subtree.swing_tree(node, swing_node)
   203       parent.add(node)
   204     }
   205   }
   206 }
   207