src/Pure/PIDE/markup_tree.scala
author wenzelm
Tue Aug 12 18:36:43 2014 +0200 (2014-08-12)
changeset 57916 2c2c24dbf0a4
parent 57912 dd9550f84106
child 58463 0bf0e9788d54
permissions -rw-r--r--
generic process wrapping in Prover;
clarified module arrangement;
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@56743
    23
  def merge(trees: List[Markup_Tree], range: Text.Range, elements: Markup.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@56743
    57
    def filter_markup(elements: Markup.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@45469
   119
  private def overlapping(range: Text.Range): Branches.T =
wenzelm@56311
   120
    if (branches.isEmpty ||
wenzelm@56313
   121
        (range.contains(branches.firstKey.start) && branches.lastKey.stop <= range.stop))
wenzelm@56311
   122
      branches
wenzelm@56311
   123
    else {
wenzelm@56311
   124
      val start = Text.Range(range.start)
wenzelm@56311
   125
      val stop = Text.Range(range.stop)
wenzelm@56311
   126
      val bs = branches.range(start, stop)
wenzelm@56311
   127
      branches.get(stop) match {
wenzelm@56311
   128
        case Some(end) if range overlaps end.range => bs + (end.range -> end)
wenzelm@56311
   129
        case _ => bs
wenzelm@56311
   130
      }
wenzelm@45457
   131
    }
wenzelm@45457
   132
wenzelm@56307
   133
  def restrict(range: Text.Range): Markup_Tree =
wenzelm@56307
   134
    new Markup_Tree(overlapping(range))
wenzelm@56307
   135
wenzelm@56307
   136
  def is_empty: Boolean = branches.isEmpty
wenzelm@56307
   137
wenzelm@45469
   138
  def + (new_markup: Text.Markup): Markup_Tree =
wenzelm@34703
   139
  {
wenzelm@45469
   140
    val new_range = new_markup.range
wenzelm@45469
   141
wenzelm@38578
   142
    branches.get(new_range) match {
wenzelm@45473
   143
      case None => new Markup_Tree(branches, Entry(new_markup, empty))
wenzelm@45469
   144
      case Some(entry) =>
wenzelm@45469
   145
        if (entry.range == new_range)
wenzelm@50552
   146
          new Markup_Tree(branches, entry + new_markup)
wenzelm@45469
   147
        else if (entry.range.contains(new_range))
wenzelm@50552
   148
          new Markup_Tree(branches, entry \ new_markup)
wenzelm@38578
   149
        else if (new_range.contains(branches.head._1) && new_range.contains(branches.last._1))
wenzelm@45473
   150
          new Markup_Tree(Branches.empty, Entry(new_markup, this))
wenzelm@38482
   151
        else {
wenzelm@45457
   152
          val body = overlapping(new_range)
wenzelm@49607
   153
          if (body.forall(e => new_range.contains(e._1)))
wenzelm@49607
   154
            new Markup_Tree(branches -- body.keys, Entry(new_markup, new Markup_Tree(body)))
wenzelm@49608
   155
          else {
wenzelm@56782
   156
            Output.warning("Ignored overlapping markup information: " + new_markup +
wenzelm@48762
   157
              body.filter(e => !new_range.contains(e._1)).mkString("\n"))
wenzelm@38482
   158
            this
wenzelm@38482
   159
          }
wenzelm@38482
   160
        }
wenzelm@34703
   161
    }
wenzelm@34703
   162
  }
wenzelm@34703
   163
wenzelm@56743
   164
  def merge(other: Markup_Tree, root_range: Text.Range, elements: Markup.Elements): Markup_Tree =
wenzelm@49614
   165
  {
wenzelm@56301
   166
    def merge_trees(tree1: Markup_Tree, tree2: Markup_Tree): Markup_Tree =
wenzelm@56307
   167
      (tree1 /: tree2.branches)(
wenzelm@56307
   168
        { case (tree, (range, entry)) =>
wenzelm@56307
   169
            if (!range.overlaps(root_range)) tree
wenzelm@56307
   170
            else
wenzelm@56307
   171
              (merge_trees(tree, entry.subtree) /: entry.filter_markup(elements))(
wenzelm@56307
   172
                { case (t, elem) => t + Text.Info(range, elem) })
wenzelm@56307
   173
        })
wenzelm@49614
   174
wenzelm@56307
   175
    if (this eq other) this
wenzelm@56307
   176
    else {
wenzelm@56307
   177
      val tree1 = this.restrict(root_range)
wenzelm@56307
   178
      val tree2 = other.restrict(root_range)
wenzelm@56307
   179
      if (tree1.is_empty) tree2
wenzelm@56307
   180
      else merge_trees(tree1, tree2)
wenzelm@56307
   181
    }
wenzelm@49614
   182
  }
wenzelm@49614
   183
wenzelm@56743
   184
  def cumulate[A](root_range: Text.Range, root_info: A, elements: Markup.Elements,
wenzelm@52900
   185
    result: (A, Text.Markup) => Option[A]): List[Text.Info[A]] =
wenzelm@45459
   186
  {
wenzelm@46178
   187
    def results(x: A, entry: Entry): Option[A] =
wenzelm@50552
   188
    {
wenzelm@52889
   189
      var y = x
wenzelm@52889
   190
      var changed = false
wenzelm@52889
   191
      for {
wenzelm@55645
   192
        elem <- entry.filter_markup(elements)
wenzelm@55620
   193
        y1 <- result(y, Text.Info(entry.range, elem))
wenzelm@52889
   194
      } { y = y1; changed = true }
wenzelm@50552
   195
      if (changed) Some(y) else None
wenzelm@50552
   196
    }
wenzelm@45467
   197
wenzelm@52900
   198
    def traverse(
wenzelm@45459
   199
      last: Text.Offset,
wenzelm@52900
   200
      stack: List[(Text.Info[A], List[(Text.Range, Entry)])]): List[Text.Info[A]] =
wenzelm@45459
   201
    {
wenzelm@45459
   202
      stack match {
wenzelm@52900
   203
        case (parent, (range, entry) :: more) :: rest =>
wenzelm@45467
   204
          val subrange = range.restrict(root_range)
wenzelm@55652
   205
          val subtree = entry.subtree.overlapping(subrange).toList
wenzelm@45459
   206
          val start = subrange.start
wenzelm@45459
   207
wenzelm@55652
   208
          results(parent.info, entry) match {
wenzelm@45469
   209
            case Some(res) =>
wenzelm@45469
   210
              val next = Text.Info(subrange, res)
wenzelm@52900
   211
              val nexts = traverse(start, (next, subtree) :: (parent, more) :: rest)
wenzelm@52900
   212
              if (last < start) parent.restrict(Text.Range(last, start)) :: nexts
wenzelm@45469
   213
              else nexts
wenzelm@52900
   214
            case None => traverse(last, (parent, subtree ::: more) :: rest)
wenzelm@45459
   215
          }
wenzelm@45459
   216
wenzelm@52900
   217
        case (parent, Nil) :: rest =>
wenzelm@45459
   218
          val stop = parent.range.stop
wenzelm@52900
   219
          val nexts = traverse(stop, rest)
wenzelm@52900
   220
          if (last < stop) parent.restrict(Text.Range(last, stop)) :: nexts
wenzelm@45459
   221
          else nexts
wenzelm@45459
   222
wenzelm@45459
   223
        case Nil =>
wenzelm@45467
   224
          val stop = root_range.stop
wenzelm@52900
   225
          if (last < stop) List(Text.Info(Text.Range(last, stop), root_info))
wenzelm@52900
   226
          else Nil
wenzelm@45459
   227
      }
wenzelm@45459
   228
    }
wenzelm@52900
   229
    traverse(root_range.start,
wenzelm@52900
   230
      List((Text.Info(root_range, root_info), overlapping(root_range).toList)))
wenzelm@45459
   231
  }
wenzelm@56301
   232
wenzelm@56743
   233
  def to_XML(root_range: Text.Range, text: CharSequence, elements: Markup.Elements): XML.Body =
wenzelm@56301
   234
  {
wenzelm@56301
   235
    def make_text(start: Text.Offset, stop: Text.Offset): XML.Body =
wenzelm@56301
   236
      if (start == stop) Nil
wenzelm@56301
   237
      else List(XML.Text(text.subSequence(start, stop).toString))
wenzelm@56301
   238
wenzelm@56301
   239
    def make_elems(rev_markups: List[XML.Elem], body: XML.Body): XML.Body =
wenzelm@56301
   240
      (body /: rev_markups) {
wenzelm@56301
   241
        case (b, elem) =>
wenzelm@56301
   242
          if (!elements(elem.name)) b
wenzelm@56301
   243
          else if (elem.body.isEmpty) List(XML.Elem(elem.markup, b))
wenzelm@56301
   244
          else List(XML.Wrapped_Elem(elem.markup, elem.body, b))
wenzelm@56301
   245
      }
wenzelm@56301
   246
wenzelm@56301
   247
    def make_body(elem_range: Text.Range, elem_markup: List[XML.Elem], entries: Branches.T)
wenzelm@56301
   248
      : XML.Body =
wenzelm@56301
   249
    {
wenzelm@56301
   250
      val body = new mutable.ListBuffer[XML.Tree]
wenzelm@56301
   251
      var last = elem_range.start
wenzelm@56301
   252
      for ((range, entry) <- entries) {
wenzelm@56301
   253
        val subrange = range.restrict(elem_range)
wenzelm@56301
   254
        body ++= make_text(last, subrange.start)
wenzelm@56301
   255
        body ++= make_body(subrange, entry.rev_markup, entry.subtree.overlapping(subrange))
wenzelm@56301
   256
        last = subrange.stop
wenzelm@56301
   257
      }
wenzelm@56301
   258
      body ++= make_text(last, elem_range.stop)
wenzelm@56301
   259
      make_elems(elem_markup, body.toList)
wenzelm@56301
   260
    }
wenzelm@56301
   261
   make_body(root_range, Nil, overlapping(root_range))
wenzelm@56301
   262
  }
wenzelm@56301
   263
wenzelm@57912
   264
  override def toString: String =
wenzelm@56301
   265
    branches.toList.map(_._2) match {
wenzelm@56301
   266
      case Nil => "Empty"
wenzelm@56301
   267
      case list => list.mkString("Tree(", ",", ")")
wenzelm@56301
   268
    }
wenzelm@34717
   269
}
immler@34514
   270