src/Pure/PIDE/markup_tree.scala
author wenzelm
Thu Feb 20 14:36:17 2014 +0100 (2014-02-20)
changeset 55618 995162143ef4
parent 52900 d29bf6db8a2d
child 55620 19dffae33cde
permissions -rw-r--r--
tuned imports;
wenzelm@38479
     1
/*  Title:      Pure/PIDE/markup_tree.scala
wenzelm@45673
     2
    Module:     PIDE
wenzelm@36676
     3
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     4
    Author:     Makarius
wenzelm@36676
     5
wenzelm@38479
     6
Markup trees over nested / non-overlapping text ranges.
wenzelm@36676
     7
*/
immler@34393
     8
wenzelm@34871
     9
package isabelle
immler@34393
    10
wenzelm@34701
    11
wenzelm@38479
    12
import scala.collection.immutable.SortedMap
wenzelm@49614
    13
import scala.collection.mutable
wenzelm@49467
    14
import scala.annotation.tailrec
wenzelm@38479
    15
wenzelm@38479
    16
wenzelm@38479
    17
object Markup_Tree
wenzelm@38479
    18
{
wenzelm@50551
    19
  /* construct trees */
wenzelm@50551
    20
wenzelm@45456
    21
  val empty: Markup_Tree = new Markup_Tree(Branches.empty)
wenzelm@38479
    22
wenzelm@49467
    23
  def merge_disjoint(trees: List[Markup_Tree]): Markup_Tree =
wenzelm@49467
    24
    trees match {
wenzelm@49467
    25
      case Nil => empty
wenzelm@49467
    26
      case head :: tail =>
wenzelm@49467
    27
        new Markup_Tree(
wenzelm@49467
    28
          (head.branches /: tail) {
wenzelm@49467
    29
            case (branches, tree) =>
wenzelm@49467
    30
              (branches /: tree.branches) {
wenzelm@49467
    31
                case (bs, (r, entry)) =>
wenzelm@49467
    32
                  require(!bs.isDefinedAt(r))
wenzelm@49467
    33
                  bs + (r -> entry)
wenzelm@49467
    34
              }
wenzelm@49467
    35
          })
wenzelm@49467
    36
    }
wenzelm@49467
    37
wenzelm@50551
    38
wenzelm@50551
    39
  /* tree building blocks */
wenzelm@50551
    40
wenzelm@50551
    41
  object Elements
wenzelm@50551
    42
  {
wenzelm@50551
    43
    val empty = new Elements(Set.empty)
wenzelm@50551
    44
  }
wenzelm@50551
    45
wenzelm@50551
    46
  final class Elements private(private val rep: Set[String])
wenzelm@50551
    47
  {
wenzelm@50551
    48
    def contains(name: String): Boolean = rep.contains(name)
wenzelm@50551
    49
wenzelm@50551
    50
    def + (name: String): Elements =
wenzelm@50551
    51
      if (contains(name)) this
wenzelm@50551
    52
      else new Elements(rep + name)
wenzelm@50551
    53
wenzelm@50551
    54
    def + (elem: XML.Elem): Elements = this + elem.markup.name
wenzelm@50551
    55
    def ++ (elems: Iterable[XML.Elem]): Elements = (this /: elems.iterator)(_ + _)
wenzelm@50551
    56
wenzelm@50551
    57
    def ++ (other: Elements): Elements =
wenzelm@50551
    58
      if (this eq other) this
wenzelm@50551
    59
      else if (rep.isEmpty) other
wenzelm@50551
    60
      else (this /: other.rep)(_ + _)
wenzelm@50551
    61
  }
wenzelm@50551
    62
wenzelm@45473
    63
  object Entry
wenzelm@45473
    64
  {
wenzelm@45473
    65
    def apply(markup: Text.Markup, subtree: Markup_Tree): Entry =
wenzelm@50552
    66
      Entry(markup.range, List(markup.info), Elements.empty + markup.info,
wenzelm@50552
    67
        subtree, subtree.make_elements)
wenzelm@49469
    68
wenzelm@49469
    69
    def apply(range: Text.Range, rev_markups: List[XML.Elem], subtree: Markup_Tree): Entry =
wenzelm@50552
    70
      Entry(range, rev_markups, Elements.empty ++ rev_markups,
wenzelm@50552
    71
        subtree, subtree.make_elements)
wenzelm@45473
    72
  }
wenzelm@45473
    73
wenzelm@45474
    74
  sealed case class Entry(
wenzelm@45474
    75
    range: Text.Range,
wenzelm@45474
    76
    rev_markup: List[XML.Elem],
wenzelm@50551
    77
    elements: Elements,
wenzelm@50552
    78
    subtree: Markup_Tree,
wenzelm@50552
    79
    subtree_elements: Elements)
wenzelm@45469
    80
  {
wenzelm@50552
    81
    def markup: List[XML.Elem] = rev_markup.reverse
wenzelm@45474
    82
wenzelm@50552
    83
    def + (markup: Text.Markup): Entry =
wenzelm@50552
    84
      copy(rev_markup = markup.info :: rev_markup, elements = elements + markup.info)
wenzelm@50552
    85
wenzelm@50552
    86
    def \ (markup: Text.Markup): Entry =
wenzelm@50552
    87
      copy(subtree = subtree + markup, subtree_elements = subtree_elements + markup.info)
wenzelm@45469
    88
  }
wenzelm@45469
    89
wenzelm@38479
    90
  object Branches
wenzelm@38479
    91
  {
wenzelm@38578
    92
    type T = SortedMap[Text.Range, Entry]
wenzelm@45456
    93
    val empty: T = SortedMap.empty(Text.Range.Ordering)
wenzelm@38479
    94
  }
wenzelm@49466
    95
wenzelm@49466
    96
wenzelm@49466
    97
  /* XML representation */
wenzelm@49466
    98
wenzelm@49650
    99
  @tailrec private def strip_elems(
wenzelm@49650
   100
      elems: List[XML.Elem], body: XML.Body): (List[XML.Elem], XML.Body) =
wenzelm@49467
   101
    body match {
wenzelm@49650
   102
      case List(XML.Wrapped_Elem(markup1, body1, body2)) =>
wenzelm@49650
   103
        strip_elems(XML.Elem(markup1, body1) :: elems, body2)
wenzelm@49650
   104
      case List(XML.Elem(markup1, body1)) =>
wenzelm@49650
   105
        strip_elems(XML.Elem(markup1, Nil) :: elems, body1)
wenzelm@49650
   106
      case _ => (elems, body)
wenzelm@49467
   107
    }
wenzelm@49467
   108
wenzelm@49467
   109
  private def make_trees(acc: (Int, List[Markup_Tree]), tree: XML.Tree): (Int, List[Markup_Tree]) =
wenzelm@49467
   110
    {
wenzelm@49467
   111
      val (offset, markup_trees) = acc
wenzelm@49467
   112
wenzelm@49467
   113
      strip_elems(Nil, List(tree)) match {
wenzelm@49467
   114
        case (Nil, body) =>
wenzelm@49467
   115
          (offset + XML.text_length(body), markup_trees)
wenzelm@49466
   116
wenzelm@49469
   117
        case (elems, body) =>
wenzelm@49469
   118
          val (end_offset, subtrees) = ((offset, Nil: List[Markup_Tree]) /: body)(make_trees)
wenzelm@50642
   119
          if (offset == end_offset) acc
wenzelm@50642
   120
          else {
wenzelm@50642
   121
            val range = Text.Range(offset, end_offset)
wenzelm@50642
   122
            val entry = Entry(range, elems, merge_disjoint(subtrees))
wenzelm@50642
   123
            (end_offset, new Markup_Tree(Branches.empty, entry) :: markup_trees)
wenzelm@50642
   124
          }
wenzelm@49466
   125
      }
wenzelm@49467
   126
    }
wenzelm@49466
   127
wenzelm@49467
   128
  def from_XML(body: XML.Body): Markup_Tree =
wenzelm@49467
   129
    merge_disjoint(((0, Nil: List[Markup_Tree]) /: body)(make_trees)._2)
wenzelm@38479
   130
}
immler@34554
   131
immler@34393
   132
wenzelm@51618
   133
final class Markup_Tree private(val branches: Markup_Tree.Branches.T)
wenzelm@34717
   134
{
wenzelm@49417
   135
  import Markup_Tree._
wenzelm@49417
   136
wenzelm@45469
   137
  private def this(branches: Markup_Tree.Branches.T, entry: Markup_Tree.Entry) =
wenzelm@45469
   138
    this(branches + (entry.range -> entry))
wenzelm@45469
   139
wenzelm@38563
   140
  override def toString =
wenzelm@38563
   141
    branches.toList.map(_._2) match {
wenzelm@38563
   142
      case Nil => "Empty"
wenzelm@38563
   143
      case list => list.mkString("Tree(", ",", ")")
wenzelm@38563
   144
    }
wenzelm@38563
   145
wenzelm@45469
   146
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@45457
   147
  {
wenzelm@45457
   148
    val start = Text.Range(range.start)
wenzelm@45457
   149
    val stop = Text.Range(range.stop)
wenzelm@45457
   150
    val bs = branches.range(start, stop)
wenzelm@45457
   151
    branches.get(stop) match {
wenzelm@45469
   152
      case Some(end) if range overlaps end.range => bs + (end.range -> end)
wenzelm@45457
   153
      case _ => bs
wenzelm@45457
   154
    }
wenzelm@45457
   155
  }
wenzelm@45457
   156
wenzelm@50552
   157
  def make_elements: Elements =
wenzelm@50552
   158
    (Elements.empty /: branches)(
wenzelm@50552
   159
      { case (elements, (_, entry)) => elements ++ entry.subtree_elements ++ entry.elements })
wenzelm@50552
   160
wenzelm@45469
   161
  def + (new_markup: Text.Markup): Markup_Tree =
wenzelm@34703
   162
  {
wenzelm@45469
   163
    val new_range = new_markup.range
wenzelm@45469
   164
wenzelm@38578
   165
    branches.get(new_range) match {
wenzelm@45473
   166
      case None => new Markup_Tree(branches, Entry(new_markup, empty))
wenzelm@45469
   167
      case Some(entry) =>
wenzelm@45469
   168
        if (entry.range == new_range)
wenzelm@50552
   169
          new Markup_Tree(branches, entry + new_markup)
wenzelm@45469
   170
        else if (entry.range.contains(new_range))
wenzelm@50552
   171
          new Markup_Tree(branches, entry \ new_markup)
wenzelm@38578
   172
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@45473
   173
          new Markup_Tree(Branches.empty, Entry(new_markup, this))
wenzelm@38482
   174
        else {
wenzelm@45457
   175
          val body = overlapping(new_range)
wenzelm@49607
   176
          if (body.forall(e => new_range.contains(e._1)))
wenzelm@49607
   177
            new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
wenzelm@49608
   178
          else {
wenzelm@55618
   179
            System.err.println("Ignored overlapping markup information: " + new_markup +
wenzelm@48762
   180
              body.filter(e => !new_range.contains(e._1)).mkString("\n"))
wenzelm@38482
   181
            this
wenzelm@38482
   182
          }
wenzelm@38482
   183
        }
wenzelm@34703
   184
    }
wenzelm@34703
   185
  }
wenzelm@34703
   186
wenzelm@52642
   187
  def ++ (other: Markup_Tree): Markup_Tree =
wenzelm@52642
   188
    (this /: other.branches)({ case (tree, (range, entry)) =>
wenzelm@52642
   189
      ((tree ++ entry.subtree) /: entry.markup)({ case (t, elem) => t + Text.Info(range, elem) }) })
wenzelm@52642
   190
wenzelm@49614
   191
  def to_XML(root_range: Text.Range, text: CharSequence, filter: XML.Elem => Boolean): XML.Body =
wenzelm@49614
   192
  {
wenzelm@49614
   193
    def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
wenzelm@49614
   194
      if (start == stop) Nil
wenzelm@49614
   195
      else List(XML.Text(text.subSequence(start, stop).toString))
wenzelm@49614
   196
wenzelm@49614
   197
    def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
wenzelm@49614
   198
      (body /: rev_markups) {
wenzelm@49650
   199
        case (b, elem) =>
wenzelm@49650
   200
          if (!filter(elem)) b
wenzelm@49650
   201
          else if (elem.body.isEmpty) List(XML.Elem(elem.markup, b))
wenzelm@49650
   202
          else List(XML.Wrapped_Elem(elem.markup, elem.body, b))
wenzelm@49614
   203
      }
wenzelm@49614
   204
wenzelm@49614
   205
    def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
wenzelm@49614
   206
      : XML.Body =
wenzelm@49614
   207
    {
wenzelm@49614
   208
      val body = new mutable.ListBuffer[XML.Tree]
wenzelm@49614
   209
      var last = elem_range.start
wenzelm@49614
   210
      for ((range, entry) <- entries) {
wenzelm@49614
   211
        val subrange = range.restrict(elem_range)
wenzelm@49614
   212
        body ++= make_text(last, subrange.start)
wenzelm@49614
   213
        body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
wenzelm@49614
   214
        last = subrange.stop
wenzelm@49614
   215
      }
wenzelm@49614
   216
      body ++= make_text(last, elem_range.stop)
wenzelm@49614
   217
      make_elems(elem_markup, body.toList)
wenzelm@49614
   218
    }
wenzelm@49614
   219
   make_body(root_range, Nil, overlapping(root_range))
wenzelm@49614
   220
  }
wenzelm@49614
   221
wenzelm@49614
   222
  def to_XML(text: CharSequence): XML.Body =
wenzelm@49614
   223
    to_XML(Text.Range(0, text.length), text, (_: XML.Elem) => true)
wenzelm@49614
   224
wenzelm@46178
   225
  def cumulate[A](root_range: Text.Range, root_info: A, result_elements: Option[Set[String]],
wenzelm@52900
   226
    result: (A, Text.Markup) => Option[A]): List[Text.Info[A]] =
wenzelm@45459
   227
  {
wenzelm@50551
   228
    val notable: Elements => Boolean =
wenzelm@50551
   229
      result_elements match {
wenzelm@50551
   230
        case Some(res) => (elements: Elements) => res.exists(elements.contains)
wenzelm@50551
   231
        case None => (elements: Elements) => true
wenzelm@50551
   232
      }
wenzelm@50551
   233
wenzelm@46178
   234
    def results(x: A, entry: Entry): Option[A] =
wenzelm@50552
   235
    {
wenzelm@52889
   236
      var y = x
wenzelm@52889
   237
      var changed = false
wenzelm@52889
   238
      for {
wenzelm@52889
   239
        info <- entry.rev_markup // FIXME proper cumulation order (including status markup) (!?)
wenzelm@52889
   240
        y1 <- result(y, Text.Info(entry.range, info))
wenzelm@52889
   241
      } { y = y1; changed = true }
wenzelm@50552
   242
      if (changed) Some(y) else None
wenzelm@50552
   243
    }
wenzelm@45467
   244
wenzelm@52900
   245
    def traverse(
wenzelm@45459
   246
      last: Text.Offset,
wenzelm@52900
   247
      stack: List[(Text.Info[A], List[(Text.Range, Entry)])]): List[Text.Info[A]] =
wenzelm@45459
   248
    {
wenzelm@45459
   249
      stack match {
wenzelm@52900
   250
        case (parent, (range, entry) :: more) :: rest =>
wenzelm@45467
   251
          val subrange = range.restrict(root_range)
wenzelm@50552
   252
          val subtree =
wenzelm@50552
   253
            if (notable(entry.subtree_elements))
wenzelm@52900
   254
              entry.subtree.overlapping(subrange).toList
wenzelm@52900
   255
            else Nil
wenzelm@45459
   256
          val start = subrange.start
wenzelm@45459
   257
wenzelm@50552
   258
          (if (notable(entry.elements)) results(parent.info, entry) else None) match {
wenzelm@45469
   259
            case Some(res) =>
wenzelm@45469
   260
              val next = Text.Info(subrange, res)
wenzelm@52900
   261
              val nexts = traverse(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@52900
   262
              if (last < start) parent.restrict(Text.Range(last, start)) :: nexts
wenzelm@45469
   263
              else nexts
wenzelm@52900
   264
            case None => traverse(last, (parent, subtree ::: more) :: rest)
wenzelm@45459
   265
          }
wenzelm@45459
   266
wenzelm@52900
   267
        case (parent, Nil) :: rest =>
wenzelm@45459
   268
          val stop = parent.range.stop
wenzelm@52900
   269
          val nexts = traverse(stop, rest)
wenzelm@52900
   270
          if (last < stop) parent.restrict(Text.Range(last, stop)) :: nexts
wenzelm@45459
   271
          else nexts
wenzelm@45459
   272
wenzelm@45459
   273
        case Nil =>
wenzelm@45467
   274
          val stop = root_range.stop
wenzelm@52900
   275
          if (last < stop) List(Text.Info(Text.Range(last, stop), root_info))
wenzelm@52900
   276
          else Nil
wenzelm@45459
   277
      }
wenzelm@45459
   278
    }
wenzelm@52900
   279
    traverse(root_range.start,
wenzelm@52900
   280
      List((Text.Info(root_range, root_info), overlapping(root_range).toList)))
wenzelm@45459
   281
  }
wenzelm@34717
   282
}
immler@34514
   283