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