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