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