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