src/Pure/PIDE/markup_tree.scala
author wenzelm
Tue Nov 29 21:29:53 2011 +0100 (2011-11-29)
changeset 45673 cd41e3903fbf
parent 45667 546d78f0d81f
child 46165 0e131ca93a49
permissions -rw-r--r--
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
     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 
    16 
    17 object Markup_Tree
    18 {
    19   sealed case class Cumulate[A](
    20     info: A, result: PartialFunction[(A, Text.Markup), A], elements: Option[Set[String]])
    21   sealed case class Select[A](
    22     result: PartialFunction[Text.Markup, A], elements: Option[Set[String]])
    23 
    24   val empty: Markup_Tree = new Markup_Tree(Branches.empty)
    25 
    26   object Entry
    27   {
    28     def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
    29       Entry(markup.range, List(markup.info), Set(markup.info.markup.name), subtree)
    30   }
    31 
    32   sealed case class Entry(
    33     range: Text.Range,
    34     rev_markup: List[XML.Elem],
    35     elements: Set[String],
    36     subtree: Markup_Tree)
    37   {
    38     def + (info: XML.Elem): Entry =
    39       if (elements(info.markup.name)) copy(rev_markup = info :: rev_markup)
    40       else copy(rev_markup = info :: rev_markup, elements = elements + info.markup.name)
    41 
    42     def markup: List[XML.Elem] = rev_markup.reverse
    43   }
    44 
    45   object Branches
    46   {
    47     type T = SortedMap[Text.Range, Entry]
    48     val empty: T = SortedMap.empty(Text.Range.Ordering)
    49   }
    50 }
    51 
    52 
    53 class Markup_Tree private(branches: Markup_Tree.Branches.T)
    54 {
    55   private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
    56     this(branches + (entry.range -> entry))
    57 
    58 
    59   import Markup_Tree._
    60 
    61   override def toString =
    62     branches.toList.map(_._2) match {
    63       case Nil => "Empty"
    64       case list => list.mkString("Tree(", ",", ")")
    65     }
    66 
    67   private def overlapping(range: Text.Range): Branches.T =
    68   {
    69     val start = Text.Range(range.start)
    70     val stop = Text.Range(range.stop)
    71     val bs = branches.range(start, stop)
    72     // FIXME check after Scala 2.8.x
    73     branches.get(stop) match {
    74       case Some(end) if range overlaps end.range => bs + (end.range -> end)
    75       case _ => bs
    76     }
    77   }
    78 
    79   def + (new_markup: Text.Markup): Markup_Tree =
    80   {
    81     val new_range = new_markup.range
    82 
    83     branches.get(new_range) match {
    84       case None => new Markup_Tree(branches, Entry(new_markup, empty))
    85       case Some(entry) =>
    86         if (entry.range == new_range)
    87           new Markup_Tree(branches, entry + new_markup.info)
    88         else if (entry.range.contains(new_range))
    89           new Markup_Tree(branches, entry.copy(subtree = entry.subtree + new_markup))
    90         else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
    91           new Markup_Tree(Branches.empty, Entry(new_markup, this))
    92         else {
    93           val body = overlapping(new_range)
    94           if (body.forall(e => new_range.contains(e._1))) {
    95             val rest = // branches -- body, modulo workarounds for Redblack in Scala 2.8.0 FIXME
    96               if (body.size > 1)
    97                 (Branches.empty /: branches)((rest, entry) =>
    98                   if (body.isDefinedAt(entry._1)) rest else rest + entry)
    99               else branches
   100             new Markup_Tree(rest, Entry(new_markup, new Markup_Tree(body)))
   101           }
   102           else { // FIXME split markup!?
   103             System.err.println("Ignored overlapping markup information: " + new_markup)
   104             this
   105           }
   106         }
   107     }
   108   }
   109 
   110   def cumulate[A](root_range: Text.Range)(body: Cumulate[A]): Stream[Text.Info[A]] =
   111   {
   112     val root_info = body.info
   113 
   114     def result(x: A, entry: Entry): Option[A] =
   115       if (body.elements match { case Some(es) => es.exists(entry.elements) case None => true }) {
   116         val (y, changed) =
   117           (entry.markup :\ (x, false))((info, res) =>
   118             {
   119               val (y, changed) = res
   120               val arg = (x, Text.Info(entry.range, info))
   121               if (body.result.isDefinedAt(arg)) (body.result(arg), true)
   122               else res
   123             })
   124         if (changed) Some(y) else None
   125       }
   126       else None
   127 
   128     def stream(
   129       last: Text.Offset,
   130       stack: List[(Text.Info[A], Stream[(Text.Range, Entry)])]): Stream[Text.Info[A]] =
   131     {
   132       stack match {
   133         case (parent, (range, entry) #:: more) :: rest =>
   134           val subrange = range.restrict(root_range)
   135           val subtree = entry.subtree.overlapping(subrange).toStream
   136           val start = subrange.start
   137 
   138           result(parent.info, entry) match {
   139             case Some(res) =>
   140               val next = Text.Info(subrange, res)
   141               val nexts = stream(start, (next, subtree) :: (parent, more) :: rest)
   142               if (last < start) parent.restrict(Text.Range(last, start)) #:: nexts
   143               else nexts
   144             case None => stream(last, (parent, subtree #::: more) :: rest)
   145           }
   146 
   147         case (parent, Stream.Empty) :: rest =>
   148           val stop = parent.range.stop
   149           val nexts = stream(stop, rest)
   150           if (last < stop) parent.restrict(Text.Range(last, stop)) #:: nexts
   151           else nexts
   152 
   153         case Nil =>
   154           val stop = root_range.stop
   155           if (last < stop) Stream(Text.Info(Text.Range(last, stop), root_info))
   156           else Stream.empty
   157       }
   158     }
   159     stream(root_range.start,
   160       List((Text.Info(root_range, root_info), overlapping(root_range).toStream)))
   161   }
   162 
   163   def swing_tree(parent: DefaultMutableTreeNode)
   164     (swing_node: Text.Markup => DefaultMutableTreeNode)
   165   {
   166     for ((_, entry) <- branches) {
   167       var current = parent
   168       for (info <- entry.markup) {
   169         val node = swing_node(Text.Info(entry.range, info))
   170         current.add(node)
   171         current = node
   172       }
   173       entry.subtree.swing_tree(current)(swing_node)
   174     }
   175   }
   176 }
   177