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