src/Tools/jEdit/src/proofdocument/document.scala
author wenzelm
Mon Jan 04 19:08:10 2010 +0100 (2010-01-04)
changeset 34835 67733fd0e3fa
parent 34832 d785f72ef388
child 34838 08a72dc4868e
permissions -rw-r--r--
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
recent_document: require finished state assignment;
explicitly typed Session.lookup_command;
wenzelm@34407
     1
/*
wenzelm@34485
     2
 * Document as list of commands, consisting of lists of tokens
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Johannes Hölzl, TU Munich
immler@34532
     5
 * @author Fabian Immler, TU Munich
wenzelm@34485
     6
 * @author Makarius
wenzelm@34407
     7
 */
wenzelm@34407
     8
wenzelm@34318
     9
package isabelle.proofdocument
wenzelm@34318
    10
wenzelm@34760
    11
wenzelm@34818
    12
import scala.actors.Actor._
wenzelm@34824
    13
import scala.collection.mutable
wenzelm@34818
    14
wenzelm@34318
    15
import java.util.regex.Pattern
wenzelm@34703
    16
wenzelm@34318
    17
wenzelm@34823
    18
object Document
wenzelm@34483
    19
{
wenzelm@34582
    20
  // Be careful when changing this regex. Not only must it handle the
wenzelm@34818
    21
  // spurious end of a token but also:
wenzelm@34318
    22
  // Bug ID: 5050507 Pattern.matches throws StackOverflow Error
wenzelm@34318
    23
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507
wenzelm@34818
    24
wenzelm@34818
    25
  val token_pattern =
wenzelm@34318
    26
    Pattern.compile(
wenzelm@34318
    27
      "\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" +
wenzelm@34318
    28
      "\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" +
wenzelm@34818
    29
      "(\\?'?|')[A-Za-z_0-9.]*|" +
wenzelm@34818
    30
      "[A-Za-z_0-9.]+|" +
wenzelm@34318
    31
      "[!#$%&*+-/<=>?@^_|~]+|" +
wenzelm@34318
    32
      "\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" +
wenzelm@34318
    33
      "`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" +
wenzelm@34318
    34
      "[()\\[\\]{}:;]", Pattern.MULTILINE)
wenzelm@34485
    35
wenzelm@34823
    36
  def empty(id: Isar_Document.Document_ID): Document =
wenzelm@34835
    37
  {
wenzelm@34835
    38
    val doc = new Document(id, Linear_Set(), Map(), Linear_Set(), Map())
wenzelm@34835
    39
    doc.assign_states(Nil)
wenzelm@34835
    40
    doc
wenzelm@34835
    41
  }
immler@34660
    42
wenzelm@34824
    43
  type Structure_Edit = (Option[Command], Option[Command])
wenzelm@34824
    44
  type Structure_Change = List[Structure_Edit]
wenzelm@34824
    45
  type Result = (Document, List[Structure_Edit])
wenzelm@34824
    46
wenzelm@34824
    47
  def text_edits(session: Session, old_doc: Document, new_id: Isar_Document.Document_ID,
wenzelm@34824
    48
    edits: List[Edit]): Result =
wenzelm@34824
    49
  {
wenzelm@34824
    50
    val changes = new mutable.ListBuffer[Structure_Edit]
wenzelm@34824
    51
    val new_doc = (old_doc /: edits)((doc1: Document, edit: Edit) =>
wenzelm@34824
    52
      {
wenzelm@34824
    53
        val (doc2, chgs) = doc1.text_edit(session, edit, new_id)  // FIXME odd multiple use of id
wenzelm@34824
    54
        changes ++ chgs
wenzelm@34824
    55
        doc2
wenzelm@34824
    56
      })
wenzelm@34824
    57
    (new_doc, changes.toList)
wenzelm@34824
    58
  }
wenzelm@34778
    59
}
immler@34538
    60
wenzelm@34318
    61
wenzelm@34823
    62
class Document(
wenzelm@34818
    63
    val id: Isar_Document.Document_ID,
wenzelm@34818
    64
    val tokens: Linear_Set[Token],   // FIXME plain List, inside Command
wenzelm@34818
    65
    val token_start: Map[Token, Int],  // FIXME eliminate
wenzelm@34818
    66
    val commands: Linear_Set[Command],
wenzelm@34832
    67
    old_states: Map[Command, Command])
wenzelm@34483
    68
{
wenzelm@34582
    69
  def content = Token.string_from_tokens(Nil ++ tokens, token_start)
wenzelm@34657
    70
wenzelm@34657
    71
wenzelm@34835
    72
  /* command/state assignment */
wenzelm@34835
    73
wenzelm@34835
    74
  val assignment = Future.promise[Map[Command, Command]]
wenzelm@34835
    75
  def is_assigned = assignment.is_finished
wenzelm@34818
    76
wenzelm@34835
    77
  @volatile private var tmp_states = old_states
wenzelm@34835
    78
wenzelm@34835
    79
  def assign_states(new_states: List[(Command, Command)])
wenzelm@34835
    80
  {
wenzelm@34835
    81
    assignment.fulfill(tmp_states ++ new_states)
wenzelm@34835
    82
    tmp_states = Map()
wenzelm@34835
    83
  }
wenzelm@34832
    84
wenzelm@34832
    85
  def current_state(cmd: Command): State =
wenzelm@34835
    86
  {
wenzelm@34835
    87
    require(assignment.is_finished)
wenzelm@34835
    88
    (assignment.join)(cmd).current_state
wenzelm@34818
    89
  }
wenzelm@34818
    90
wenzelm@34818
    91
wenzelm@34818
    92
wenzelm@34485
    93
  /** token view **/
wenzelm@34485
    94
wenzelm@34824
    95
  def text_edit(session: Session, e: Edit, id: String): Document.Result =
immler@34660
    96
  {
immler@34660
    97
    case class TextChange(start: Int, added: String, removed: String)
immler@34660
    98
    val change = e match {
immler@34660
    99
      case Insert(s, a) => TextChange(s, a, "")
immler@34660
   100
      case Remove(s, r) => TextChange(s, "", r)
immler@34660
   101
    }
immler@34551
   102
    //indices of tokens
immler@34551
   103
    var start: Map[Token, Int] = token_start
immler@34551
   104
    def stop(t: Token) = start(t) + t.length
immler@34551
   105
    // split old token lists
wenzelm@34582
   106
    val tokens = Nil ++ this.tokens
immler@34551
   107
    val (begin, remaining) = tokens.span(stop(_) < change.start)
immler@34648
   108
    val (removed, end) = remaining.span(token_start(_) <= change.start + change.removed.length)
immler@34551
   109
    // update indices
wenzelm@34582
   110
    start = end.foldLeft(start)((s, t) =>
immler@34648
   111
      s + (t -> (s(t) + change.added.length - change.removed.length)))
wenzelm@34485
   112
immler@34551
   113
    val split_begin = removed.takeWhile(start(_) < change.start).
immler@34554
   114
      map (t => {
immler@34554
   115
          val split_tok = new Token(t.content.substring(0, change.start - start(t)), t.kind)
immler@34554
   116
          start += (split_tok -> start(t))
immler@34554
   117
          split_tok
immler@34554
   118
        })
immler@34554
   119
immler@34648
   120
    val split_end = removed.dropWhile(stop(_) < change.start + change.removed.length).
immler@34554
   121
      map (t => {
wenzelm@34582
   122
          val split_tok =
immler@34648
   123
            new Token(t.content.substring(change.start + change.removed.length - start(t)), t.kind)
immler@34554
   124
          start += (split_tok -> start(t))
immler@34554
   125
          split_tok
immler@34554
   126
        })
immler@34551
   127
    // update indices
immler@34554
   128
    start = removed.foldLeft (start) ((s, t) => s - t)
immler@34554
   129
    start = split_end.foldLeft (start) ((s, t) =>
immler@34554
   130
    s + (t -> (change.start + change.added.length)))
wenzelm@34318
   131
immler@34551
   132
    val ins = new Token(change.added, Token.Kind.OTHER)
immler@34551
   133
    start += (ins -> change.start)
wenzelm@34818
   134
wenzelm@34582
   135
    var invalid_tokens = split_begin ::: ins :: split_end ::: end
wenzelm@34582
   136
    var new_tokens: List[Token] = Nil
wenzelm@34582
   137
    var old_suffix: List[Token] = Nil
wenzelm@34318
   138
immler@34551
   139
    val match_start = invalid_tokens.firstOption.map(start(_)).getOrElse(0)
wenzelm@34582
   140
    val matcher =
wenzelm@34823
   141
      Document.token_pattern.matcher(Token.string_from_tokens(invalid_tokens, start))
immler@34526
   142
immler@34526
   143
    while (matcher.find() && invalid_tokens != Nil) {
wenzelm@34485
   144
			val kind =
wenzelm@34819
   145
        if (session.current_syntax.is_command(matcher.group))
wenzelm@34485
   146
          Token.Kind.COMMAND_START
wenzelm@34494
   147
        else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*")
wenzelm@34485
   148
          Token.Kind.COMMENT
wenzelm@34485
   149
        else
wenzelm@34485
   150
          Token.Kind.OTHER
immler@34551
   151
      val new_token = new Token(matcher.group, kind)
immler@34551
   152
      start += (new_token -> (match_start + matcher.start))
immler@34526
   153
      new_tokens ::= new_token
wenzelm@34318
   154
immler@34660
   155
      invalid_tokens = invalid_tokens dropWhile (stop(_) < stop(new_token))
immler@34526
   156
      invalid_tokens match {
wenzelm@34582
   157
        case t :: ts =>
wenzelm@34582
   158
          if (start(t) == start(new_token) &&
wenzelm@34582
   159
              start(t) > change.start + change.added.length) {
immler@34597
   160
          old_suffix = t :: ts
immler@34592
   161
          new_tokens = new_tokens.tail
immler@34526
   162
          invalid_tokens = Nil
immler@34526
   163
        }
immler@34526
   164
        case _ =>
wenzelm@34318
   165
      }
wenzelm@34318
   166
    }
immler@34526
   167
    val insert = new_tokens.reverse
immler@34544
   168
    val new_token_list = begin ::: insert ::: old_suffix
wenzelm@34778
   169
    token_changed(session, id, begin.lastOption, insert,
immler@34597
   170
      old_suffix.firstOption, new_token_list, start)
wenzelm@34318
   171
  }
wenzelm@34582
   172
wenzelm@34818
   173
wenzelm@34485
   174
  /** command view **/
wenzelm@34485
   175
wenzelm@34582
   176
  private def token_changed(
wenzelm@34824
   177
      session: Session,
wenzelm@34824
   178
      new_id: String,
wenzelm@34824
   179
      before_change: Option[Token],
wenzelm@34824
   180
      inserted_tokens: List[Token],
wenzelm@34824
   181
      after_change: Option[Token],
wenzelm@34824
   182
      new_tokens: List[Token],
wenzelm@34824
   183
      new_token_start: Map[Token, Int]):
wenzelm@34824
   184
    Document.Result =
wenzelm@34485
   185
  {
wenzelm@34835
   186
    require(assignment.is_finished)
wenzelm@34835
   187
wenzelm@34689
   188
    val new_tokenset = Linear_Set[Token]() ++ new_tokens
immler@34593
   189
    val cmd_before_change = before_change match {
immler@34593
   190
      case None => None
immler@34593
   191
      case Some(bc) =>
immler@34593
   192
        val cmd_with_bc = commands.find(_.contains(bc)).get
immler@34593
   193
        if (cmd_with_bc.tokens.last == bc) {
immler@34593
   194
          if (new_tokenset.next(bc).map(_.is_start).getOrElse(true))
immler@34593
   195
            Some(cmd_with_bc)
immler@34593
   196
          else commands.prev(cmd_with_bc)
immler@34593
   197
        }
immler@34593
   198
        else commands.prev(cmd_with_bc)
immler@34593
   199
    }
immler@34544
   200
immler@34593
   201
    val cmd_after_change = after_change match {
immler@34593
   202
      case None => None
immler@34593
   203
      case Some(ac) =>
immler@34593
   204
        val cmd_with_ac = commands.find(_.contains(ac)).get
immler@34593
   205
        if (ac.is_start)
immler@34593
   206
          Some(cmd_with_ac)
immler@34593
   207
        else
immler@34593
   208
          commands.next(cmd_with_ac)
immler@34593
   209
    }
wenzelm@34485
   210
immler@34593
   211
    val removed_commands = commands.dropWhile(Some(_) != cmd_before_change).drop(1).
immler@34554
   212
      takeWhile(Some(_) != cmd_after_change)
immler@34554
   213
immler@34554
   214
    // calculate inserted commands
immler@34526
   215
    def tokens_to_commands(tokens: List[Token]): List[Command]= {
immler@34526
   216
      tokens match {
immler@34526
   217
        case Nil => Nil
wenzelm@34582
   218
        case t :: ts =>
wenzelm@34582
   219
          val (cmd, rest) =
wenzelm@34582
   220
            ts.span(t => t.kind != Token.Kind.COMMAND_START && t.kind != Token.Kind.COMMENT)
wenzelm@34778
   221
          new Command(session.create_id(), t :: cmd, new_token_start) :: tokens_to_commands(rest)
wenzelm@34485
   222
      }
wenzelm@34485
   223
    }
wenzelm@34485
   224
immler@34593
   225
    val split_begin =
immler@34593
   226
      if (before_change.isDefined) {
immler@34593
   227
        val changed =
immler@34593
   228
          if (cmd_before_change.isDefined)
immler@34595
   229
            new_tokens.dropWhile(_ != cmd_before_change.get.tokens.last).drop(1)
immler@34593
   230
          else new_tokenset
immler@34593
   231
        if (changed.exists(_ == before_change.get))
immler@34597
   232
          changed.takeWhile(_ != before_change.get).toList :::
immler@34597
   233
            List(before_change.get)
immler@34593
   234
        else Nil
immler@34593
   235
      } else Nil
immler@34554
   236
immler@34593
   237
    val split_end =
immler@34667
   238
      if (after_change.isDefined) {
immler@34595
   239
        val unchanged = new_tokens.dropWhile(_ != after_change.get)
immler@34667
   240
        if(cmd_after_change.isDefined) {
immler@34667
   241
          if (unchanged.exists(_ == cmd_after_change.get.tokens.first))
immler@34667
   242
            unchanged.takeWhile(_ != cmd_after_change.get.tokens.first).toList
immler@34667
   243
          else Nil
immler@34667
   244
        } else {
immler@34667
   245
          unchanged
immler@34667
   246
        }
immler@34593
   247
      } else Nil
immler@34593
   248
immler@34597
   249
    val rescan_begin =
immler@34597
   250
      split_begin :::
immler@34597
   251
        before_change.map(bc => new_tokens.dropWhile(_ != bc).drop(1)).getOrElse(new_tokens)
wenzelm@34582
   252
    val rescanning_tokens =
immler@34597
   253
      after_change.map(ac => rescan_begin.takeWhile(_ != ac)).getOrElse(rescan_begin) :::
immler@34597
   254
        split_end
immler@34593
   255
    val inserted_commands = tokens_to_commands(rescanning_tokens.toList)
immler@34554
   256
immler@34550
   257
    // build new document
wenzelm@34739
   258
    val new_commandset = commands.
wenzelm@34739
   259
      delete_between(cmd_before_change, cmd_after_change).
wenzelm@34739
   260
      append_after(cmd_before_change, inserted_commands)
wenzelm@34739
   261
immler@34554
   262
immler@34544
   263
    val doc =
wenzelm@34823
   264
      new Document(new_id, new_tokenset, new_token_start, new_commandset,
wenzelm@34835
   265
        assignment.join -- removed_commands)
immler@34660
   266
immler@34660
   267
    val removes =
immler@34660
   268
      for (cmd <- removed_commands) yield (cmd_before_change -> None)
immler@34660
   269
    val inserts =
immler@34660
   270
      for (cmd <- inserted_commands) yield (doc.commands.prev(cmd) -> Some(cmd))
immler@34660
   271
immler@34660
   272
    return (doc, removes.toList ++ inserts)
wenzelm@34485
   273
  }
immler@34596
   274
immler@34596
   275
  val commands_offsets = {
immler@34596
   276
    var last_stop = 0
immler@34596
   277
    (for (c <- commands) yield {
wenzelm@34824
   278
      val r = c -> (last_stop, c.stop(this))
immler@34596
   279
      last_stop = c.stop(this)
immler@34596
   280
      r
immler@34596
   281
    }).toArray
immler@34596
   282
  }
immler@34596
   283
wenzelm@34712
   284
  def command_at(pos: Int): Option[Command] =
wenzelm@34712
   285
    find_command(pos, 0, commands_offsets.length)
wenzelm@34712
   286
immler@34596
   287
  // use a binary search to find commands for a given offset
wenzelm@34712
   288
  private def find_command(pos: Int, array_start: Int, array_stop: Int): Option[Command] =
wenzelm@34712
   289
  {
immler@34596
   290
    val middle_index = (array_start + array_stop) / 2
wenzelm@34712
   291
    if (middle_index >= commands_offsets.length) return None
immler@34596
   292
    val (middle, (start, stop)) = commands_offsets(middle_index)
immler@34596
   293
    // does middle contain pos?
wenzelm@34712
   294
    if (start <= pos && pos < stop)
wenzelm@34712
   295
      Some(middle)
immler@34596
   296
    else if (start > pos)
wenzelm@34712
   297
      find_command(pos, array_start, middle_index)
immler@34596
   298
    else if (stop <= pos)
wenzelm@34712
   299
      find_command(pos, middle_index + 1, array_stop)
wenzelm@34712
   300
    else error("impossible")
immler@34596
   301
  }
wenzelm@34318
   302
}