src/Tools/jEdit/src/proofdocument/proof_document.scala
author wenzelm
Fri Dec 11 22:25:28 2009 +0100 (2009-12-11)
changeset 34778 8eccd35e975e
parent 34760 dc7f5e0d9d27
child 34799 0330a4284a9b
permissions -rw-r--r--
removed unused Session.prover_logic;
slightly more robust Session.start/stop;
added Session.create_id (formerly in Isabelle_System);
Change/Command/Proof_Document: uniform id value, not implicity creation;
eliminated Proof_Document.is_command_keyword -- plain method via explicit session;
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@34703
    12
import scala.actors.Actor, Actor._
wenzelm@34703
    13
wenzelm@34318
    14
import java.util.regex.Pattern
wenzelm@34703
    15
wenzelm@34318
    16
wenzelm@34760
    17
object Proof_Document
wenzelm@34483
    18
{
wenzelm@34582
    19
  // Be careful when changing this regex. Not only must it handle the
wenzelm@34318
    20
  // spurious end of a token but also:  
wenzelm@34318
    21
  // Bug ID: 5050507 Pattern.matches throws StackOverflow Error
wenzelm@34318
    22
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507
wenzelm@34318
    23
  
wenzelm@34483
    24
  val token_pattern = 
wenzelm@34318
    25
    Pattern.compile(
wenzelm@34318
    26
      "\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" +
wenzelm@34318
    27
      "\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" +
wenzelm@34318
    28
      "(\\?'?|')[A-Za-z_0-9.]*|" + 
wenzelm@34318
    29
      "[A-Za-z_0-9.]+|" + 
wenzelm@34318
    30
      "[!#$%&*+-/<=>?@^_|~]+|" +
wenzelm@34318
    31
      "\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" +
wenzelm@34318
    32
      "`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" +
wenzelm@34318
    33
      "[()\\[\\]{}:;]", Pattern.MULTILINE)
wenzelm@34485
    34
wenzelm@34778
    35
  def empty(id: String): Proof_Document =
wenzelm@34778
    36
    new Proof_Document(id, Linear_Set(), Map(), Linear_Set(), Map())
immler@34660
    37
immler@34660
    38
  type StructureChange = List[(Option[Command], Option[Command])]
wenzelm@34778
    39
}
immler@34538
    40
wenzelm@34318
    41
wenzelm@34760
    42
class Proof_Document(
wenzelm@34582
    43
  val id: String,
wenzelm@34689
    44
  val tokens: Linear_Set[Token],
wenzelm@34582
    45
  val token_start: Map[Token, Int],
wenzelm@34689
    46
  val commands: Linear_Set[Command],
wenzelm@34778
    47
  var states: Map[Command, Command_State])   // FIXME immutable
wenzelm@34483
    48
{
wenzelm@34760
    49
  import Proof_Document.StructureChange
immler@34653
    50
wenzelm@34582
    51
  def content = Token.string_from_tokens(Nil ++ tokens, token_start)
wenzelm@34657
    52
wenzelm@34657
    53
wenzelm@34693
    54
  
wenzelm@34485
    55
  /** token view **/
wenzelm@34485
    56
wenzelm@34778
    57
  def text_changed(session: Session, change: Change): (Proof_Document, StructureChange) =
wenzelm@34485
    58
  {
wenzelm@34760
    59
    def edit_doc(doc_chgs: (Proof_Document, StructureChange), edit: Edit) = {
immler@34660
    60
      val (doc, chgs) = doc_chgs
wenzelm@34778
    61
      val (new_doc, chg) = doc.text_edit(session, edit, change.id)
immler@34660
    62
      (new_doc, chgs ++ chg)
immler@34660
    63
    }
wenzelm@34693
    64
    ((this, Nil: StructureChange) /: change.edits)(edit_doc)
immler@34660
    65
  }
immler@34660
    66
wenzelm@34778
    67
  def text_edit(session: Session, e: Edit, id: String): (Proof_Document, StructureChange) =
immler@34660
    68
  {
immler@34660
    69
    case class TextChange(start: Int, added: String, removed: String)
immler@34660
    70
    val change = e match {
immler@34660
    71
      case Insert(s, a) => TextChange(s, a, "")
immler@34660
    72
      case Remove(s, r) => TextChange(s, "", r)
immler@34660
    73
    }
immler@34551
    74
    //indices of tokens
immler@34551
    75
    var start: Map[Token, Int] = token_start
immler@34551
    76
    def stop(t: Token) = start(t) + t.length
immler@34551
    77
    // split old token lists
wenzelm@34582
    78
    val tokens = Nil ++ this.tokens
immler@34551
    79
    val (begin, remaining) = tokens.span(stop(_) < change.start)
immler@34648
    80
    val (removed, end) = remaining.span(token_start(_) <= change.start + change.removed.length)
immler@34551
    81
    // update indices
wenzelm@34582
    82
    start = end.foldLeft(start)((s, t) =>
immler@34648
    83
      s + (t -> (s(t) + change.added.length - change.removed.length)))
wenzelm@34485
    84
immler@34551
    85
    val split_begin = removed.takeWhile(start(_) < change.start).
immler@34554
    86
      map (t => {
immler@34554
    87
          val split_tok = new Token(t.content.substring(0, change.start - start(t)), t.kind)
immler@34554
    88
          start += (split_tok -> start(t))
immler@34554
    89
          split_tok
immler@34554
    90
        })
immler@34554
    91
immler@34648
    92
    val split_end = removed.dropWhile(stop(_) < change.start + change.removed.length).
immler@34554
    93
      map (t => {
wenzelm@34582
    94
          val split_tok =
immler@34648
    95
            new Token(t.content.substring(change.start + change.removed.length - start(t)), t.kind)
immler@34554
    96
          start += (split_tok -> start(t))
immler@34554
    97
          split_tok
immler@34554
    98
        })
immler@34551
    99
    // update indices
immler@34554
   100
    start = removed.foldLeft (start) ((s, t) => s - t)
immler@34554
   101
    start = split_end.foldLeft (start) ((s, t) =>
immler@34554
   102
    s + (t -> (change.start + change.added.length)))
wenzelm@34318
   103
immler@34551
   104
    val ins = new Token(change.added, Token.Kind.OTHER)
immler@34551
   105
    start += (ins -> change.start)
immler@34551
   106
    
wenzelm@34582
   107
    var invalid_tokens = split_begin ::: ins :: split_end ::: end
wenzelm@34582
   108
    var new_tokens: List[Token] = Nil
wenzelm@34582
   109
    var old_suffix: List[Token] = Nil
wenzelm@34318
   110
immler@34551
   111
    val match_start = invalid_tokens.firstOption.map(start(_)).getOrElse(0)
wenzelm@34582
   112
    val matcher =
wenzelm@34760
   113
      Proof_Document.token_pattern.matcher(Token.string_from_tokens(invalid_tokens, start))
immler@34526
   114
immler@34526
   115
    while (matcher.find() && invalid_tokens != Nil) {
wenzelm@34485
   116
			val kind =
wenzelm@34778
   117
        if (session.is_command_keyword(matcher.group))
wenzelm@34485
   118
          Token.Kind.COMMAND_START
wenzelm@34494
   119
        else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*")
wenzelm@34485
   120
          Token.Kind.COMMENT
wenzelm@34485
   121
        else
wenzelm@34485
   122
          Token.Kind.OTHER
immler@34551
   123
      val new_token = new Token(matcher.group, kind)
immler@34551
   124
      start += (new_token -> (match_start + matcher.start))
immler@34526
   125
      new_tokens ::= new_token
wenzelm@34318
   126
immler@34660
   127
      invalid_tokens = invalid_tokens dropWhile (stop(_) < stop(new_token))
immler@34526
   128
      invalid_tokens match {
wenzelm@34582
   129
        case t :: ts =>
wenzelm@34582
   130
          if (start(t) == start(new_token) &&
wenzelm@34582
   131
              start(t) > change.start + change.added.length) {
immler@34597
   132
          old_suffix = t :: ts
immler@34592
   133
          new_tokens = new_tokens.tail
immler@34526
   134
          invalid_tokens = Nil
immler@34526
   135
        }
immler@34526
   136
        case _ =>
wenzelm@34318
   137
      }
wenzelm@34318
   138
    }
immler@34526
   139
    val insert = new_tokens.reverse
immler@34544
   140
    val new_token_list = begin ::: insert ::: old_suffix
wenzelm@34778
   141
    token_changed(session, id, begin.lastOption, insert,
immler@34597
   142
      old_suffix.firstOption, new_token_list, start)
wenzelm@34318
   143
  }
wenzelm@34582
   144
wenzelm@34318
   145
  
wenzelm@34485
   146
  /** command view **/
wenzelm@34485
   147
wenzelm@34582
   148
  private def token_changed(
wenzelm@34778
   149
    session: Session,
wenzelm@34582
   150
    new_id: String,
wenzelm@34582
   151
    before_change: Option[Token],
wenzelm@34582
   152
    inserted_tokens: List[Token],
wenzelm@34582
   153
    after_change: Option[Token],
immler@34597
   154
    new_tokens: List[Token],
immler@34660
   155
    new_token_start: Map[Token, Int]):
wenzelm@34760
   156
  (Proof_Document, StructureChange) =
wenzelm@34485
   157
  {
wenzelm@34689
   158
    val new_tokenset = Linear_Set[Token]() ++ new_tokens
immler@34593
   159
    val cmd_before_change = before_change match {
immler@34593
   160
      case None => None
immler@34593
   161
      case Some(bc) =>
immler@34593
   162
        val cmd_with_bc = commands.find(_.contains(bc)).get
immler@34593
   163
        if (cmd_with_bc.tokens.last == bc) {
immler@34593
   164
          if (new_tokenset.next(bc).map(_.is_start).getOrElse(true))
immler@34593
   165
            Some(cmd_with_bc)
immler@34593
   166
          else commands.prev(cmd_with_bc)
immler@34593
   167
        }
immler@34593
   168
        else commands.prev(cmd_with_bc)
immler@34593
   169
    }
immler@34544
   170
immler@34593
   171
    val cmd_after_change = after_change match {
immler@34593
   172
      case None => None
immler@34593
   173
      case Some(ac) =>
immler@34593
   174
        val cmd_with_ac = commands.find(_.contains(ac)).get
immler@34593
   175
        if (ac.is_start)
immler@34593
   176
          Some(cmd_with_ac)
immler@34593
   177
        else
immler@34593
   178
          commands.next(cmd_with_ac)
immler@34593
   179
    }
wenzelm@34485
   180
immler@34593
   181
    val removed_commands = commands.dropWhile(Some(_) != cmd_before_change).drop(1).
immler@34554
   182
      takeWhile(Some(_) != cmd_after_change)
immler@34554
   183
immler@34554
   184
    // calculate inserted commands
immler@34526
   185
    def tokens_to_commands(tokens: List[Token]): List[Command]= {
immler@34526
   186
      tokens match {
immler@34526
   187
        case Nil => Nil
wenzelm@34582
   188
        case t :: ts =>
wenzelm@34582
   189
          val (cmd, rest) =
wenzelm@34582
   190
            ts.span(t => t.kind != Token.Kind.COMMAND_START && t.kind != Token.Kind.COMMENT)
wenzelm@34778
   191
          new Command(session.create_id(), t :: cmd, new_token_start) :: tokens_to_commands(rest)
wenzelm@34485
   192
      }
wenzelm@34485
   193
    }
wenzelm@34485
   194
immler@34593
   195
    val split_begin =
immler@34593
   196
      if (before_change.isDefined) {
immler@34593
   197
        val changed =
immler@34593
   198
          if (cmd_before_change.isDefined)
immler@34595
   199
            new_tokens.dropWhile(_ != cmd_before_change.get.tokens.last).drop(1)
immler@34593
   200
          else new_tokenset
immler@34593
   201
        if (changed.exists(_ == before_change.get))
immler@34597
   202
          changed.takeWhile(_ != before_change.get).toList :::
immler@34597
   203
            List(before_change.get)
immler@34593
   204
        else Nil
immler@34593
   205
      } else Nil
immler@34554
   206
immler@34593
   207
    val split_end =
immler@34667
   208
      if (after_change.isDefined) {
immler@34595
   209
        val unchanged = new_tokens.dropWhile(_ != after_change.get)
immler@34667
   210
        if(cmd_after_change.isDefined) {
immler@34667
   211
          if (unchanged.exists(_ == cmd_after_change.get.tokens.first))
immler@34667
   212
            unchanged.takeWhile(_ != cmd_after_change.get.tokens.first).toList
immler@34667
   213
          else Nil
immler@34667
   214
        } else {
immler@34667
   215
          unchanged
immler@34667
   216
        }
immler@34593
   217
      } else Nil
immler@34593
   218
immler@34597
   219
    val rescan_begin =
immler@34597
   220
      split_begin :::
immler@34597
   221
        before_change.map(bc => new_tokens.dropWhile(_ != bc).drop(1)).getOrElse(new_tokens)
wenzelm@34582
   222
    val rescanning_tokens =
immler@34597
   223
      after_change.map(ac => rescan_begin.takeWhile(_ != ac)).getOrElse(rescan_begin) :::
immler@34597
   224
        split_end
immler@34593
   225
    val inserted_commands = tokens_to_commands(rescanning_tokens.toList)
immler@34554
   226
immler@34550
   227
    // build new document
wenzelm@34739
   228
    val new_commandset = commands.
wenzelm@34739
   229
      delete_between(cmd_before_change, cmd_after_change).
wenzelm@34739
   230
      append_after(cmd_before_change, inserted_commands)
wenzelm@34739
   231
immler@34554
   232
immler@34544
   233
    val doc =
wenzelm@34760
   234
      new Proof_Document(new_id, new_tokenset, new_token_start, new_commandset,
wenzelm@34778
   235
        states -- removed_commands)
immler@34660
   236
immler@34660
   237
    val removes =
immler@34660
   238
      for (cmd <- removed_commands) yield (cmd_before_change -> None)
immler@34660
   239
    val inserts =
immler@34660
   240
      for (cmd <- inserted_commands) yield (doc.commands.prev(cmd) -> Some(cmd))
immler@34660
   241
immler@34660
   242
    return (doc, removes.toList ++ inserts)
wenzelm@34485
   243
  }
immler@34596
   244
immler@34596
   245
  val commands_offsets = {
immler@34596
   246
    var last_stop = 0
immler@34596
   247
    (for (c <- commands) yield {
immler@34596
   248
      val r = c -> (last_stop,c.stop(this))
immler@34596
   249
      last_stop = c.stop(this)
immler@34596
   250
      r
immler@34596
   251
    }).toArray
immler@34596
   252
  }
immler@34596
   253
wenzelm@34712
   254
  def command_at(pos: Int): Option[Command] =
wenzelm@34712
   255
    find_command(pos, 0, commands_offsets.length)
wenzelm@34712
   256
immler@34596
   257
  // use a binary search to find commands for a given offset
wenzelm@34712
   258
  private def find_command(pos: Int, array_start: Int, array_stop: Int): Option[Command] =
wenzelm@34712
   259
  {
immler@34596
   260
    val middle_index = (array_start + array_stop) / 2
wenzelm@34712
   261
    if (middle_index >= commands_offsets.length) return None
immler@34596
   262
    val (middle, (start, stop)) = commands_offsets(middle_index)
immler@34596
   263
    // does middle contain pos?
wenzelm@34712
   264
    if (start <= pos && pos < stop)
wenzelm@34712
   265
      Some(middle)
immler@34596
   266
    else if (start > pos)
wenzelm@34712
   267
      find_command(pos, array_start, middle_index)
immler@34596
   268
    else if (stop <= pos)
wenzelm@34712
   269
      find_command(pos, middle_index + 1, array_stop)
wenzelm@34712
   270
    else error("impossible")
immler@34596
   271
  }
wenzelm@34318
   272
}