src/Tools/jEdit/src/proofdocument/ProofDocument.scala
author wenzelm
Tue Jun 02 21:20:22 2009 +0200 (2009-06-02)
changeset 34582 5d5d253c7c29
parent 34575 70d11544685f
child 34597 a0c84b0edb9a
permissions -rw-r--r--
superficial tuning;
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@34526
    11
import scala.collection.mutable.ListBuffer
immler@34532
    12
import scala.actors.Actor
immler@34532
    13
import scala.actors.Actor._
wenzelm@34318
    14
import java.util.regex.Pattern
wenzelm@34485
    15
import isabelle.prover.{Prover, Command}
immler@34531
    16
import isabelle.utils.LinearSet
wenzelm@34318
    17
wenzelm@34494
    18
case class StructureChange(
immler@34526
    19
  val before_change: Option[Command],
wenzelm@34494
    20
  val added_commands: List[Command],
wenzelm@34494
    21
  val removed_commands: List[Command])
wenzelm@34485
    22
wenzelm@34483
    23
object ProofDocument
wenzelm@34483
    24
{
wenzelm@34582
    25
  // Be careful when changing this regex. Not only must it handle the
wenzelm@34318
    26
  // spurious end of a token but also:  
wenzelm@34318
    27
  // Bug ID: 5050507 Pattern.matches throws StackOverflow Error
wenzelm@34318
    28
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507
wenzelm@34318
    29
  
wenzelm@34483
    30
  val token_pattern = 
wenzelm@34318
    31
    Pattern.compile(
wenzelm@34318
    32
      "\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" +
wenzelm@34318
    33
      "\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" +
wenzelm@34318
    34
      "(\\?'?|')[A-Za-z_0-9.]*|" + 
wenzelm@34318
    35
      "[A-Za-z_0-9.]+|" + 
wenzelm@34318
    36
      "[!#$%&*+-/<=>?@^_|~]+|" +
wenzelm@34318
    37
      "\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" +
wenzelm@34318
    38
      "`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" +
wenzelm@34318
    39
      "[()\\[\\]{}:;]", Pattern.MULTILINE)
wenzelm@34485
    40
immler@34544
    41
 val empty =
wenzelm@34582
    42
  new ProofDocument(isabelle.jedit.Isabelle.plugin.id(),
wenzelm@34582
    43
    LinearSet(), Map(), LinearSet(), false, _ => false)
immler@34538
    44
wenzelm@34318
    45
}
wenzelm@34318
    46
wenzelm@34582
    47
class ProofDocument(
wenzelm@34582
    48
  val id: String,
wenzelm@34582
    49
  val tokens: LinearSet[Token],
wenzelm@34582
    50
  val token_start: Map[Token, Int],
wenzelm@34582
    51
  val commands: LinearSet[Command],
wenzelm@34582
    52
  val active: Boolean,
wenzelm@34582
    53
  is_command_keyword: String => Boolean)
wenzelm@34483
    54
{
wenzelm@34485
    55
immler@34544
    56
  def mark_active: ProofDocument =
immler@34551
    57
    new ProofDocument(id, tokens, token_start, commands, true, is_command_keyword)
immler@34538
    58
  def activate: (ProofDocument, StructureChange) = {
immler@34544
    59
    val (doc, change) =
immler@34544
    60
      text_changed(new Text.Change(isabelle.jedit.Isabelle.plugin.id(), 0, content, content.length))
immler@34538
    61
    return (doc.mark_active, change)
wenzelm@34456
    62
  }
immler@34532
    63
  def set_command_keyword(f: String => Boolean): ProofDocument =
immler@34551
    64
    new ProofDocument(id, tokens, token_start, commands, active, f)
wenzelm@34485
    65
wenzelm@34582
    66
  def content = Token.string_from_tokens(Nil ++ tokens, token_start)
wenzelm@34485
    67
  /** token view **/
wenzelm@34485
    68
immler@34538
    69
  def text_changed(change: Text.Change): (ProofDocument, StructureChange) =
wenzelm@34485
    70
  {
immler@34551
    71
    //indices of tokens
immler@34551
    72
    var start: Map[Token, Int] = token_start
immler@34551
    73
    def stop(t: Token) = start(t) + t.length
immler@34551
    74
    // split old token lists
wenzelm@34582
    75
    val tokens = Nil ++ this.tokens
immler@34551
    76
    val (begin, remaining) = tokens.span(stop(_) < change.start)
immler@34551
    77
    val (removed, end) = remaining.span(token_start(_) <= change.start + change.removed)
immler@34551
    78
    // update indices
wenzelm@34582
    79
    start = end.foldLeft(start)((s, t) =>
wenzelm@34582
    80
      s + (t -> (s(t) + change.added.length - change.removed)))
wenzelm@34485
    81
immler@34551
    82
    val split_begin = removed.takeWhile(start(_) < change.start).
immler@34554
    83
      map (t => {
immler@34554
    84
          val split_tok = new Token(t.content.substring(0, change.start - start(t)), t.kind)
immler@34554
    85
          start += (split_tok -> start(t))
immler@34554
    86
          split_tok
immler@34554
    87
        })
immler@34554
    88
immler@34551
    89
    val split_end = removed.dropWhile(stop(_) < change.start + change.removed).
immler@34554
    90
      map (t => {
wenzelm@34582
    91
          val split_tok =
wenzelm@34582
    92
            new Token(t.content.substring(change.start + change.removed - start(t)), t.kind)
immler@34554
    93
          start += (split_tok -> start(t))
immler@34554
    94
          split_tok
immler@34554
    95
        })
immler@34551
    96
    // update indices
immler@34554
    97
    start = removed.foldLeft (start) ((s, t) => s - t)
immler@34554
    98
    start = split_end.foldLeft (start) ((s, t) =>
immler@34554
    99
    s + (t -> (change.start + change.added.length)))
wenzelm@34318
   100
immler@34551
   101
    val ins = new Token(change.added, Token.Kind.OTHER)
immler@34551
   102
    start += (ins -> change.start)
immler@34551
   103
    
wenzelm@34582
   104
    var invalid_tokens = split_begin ::: ins :: split_end ::: end
wenzelm@34582
   105
    var new_tokens: List[Token] = Nil
wenzelm@34582
   106
    var old_suffix: List[Token] = Nil
wenzelm@34318
   107
immler@34551
   108
    val match_start = invalid_tokens.firstOption.map(start(_)).getOrElse(0)
wenzelm@34582
   109
    val matcher =
wenzelm@34582
   110
      ProofDocument.token_pattern.matcher(Token.string_from_tokens(invalid_tokens, start))
immler@34526
   111
immler@34526
   112
    while (matcher.find() && invalid_tokens != Nil) {
wenzelm@34485
   113
			val kind =
wenzelm@34505
   114
        if (is_command_keyword(matcher.group))
wenzelm@34485
   115
          Token.Kind.COMMAND_START
wenzelm@34494
   116
        else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*")
wenzelm@34485
   117
          Token.Kind.COMMENT
wenzelm@34485
   118
        else
wenzelm@34485
   119
          Token.Kind.OTHER
immler@34551
   120
      val new_token = new Token(matcher.group, kind)
immler@34551
   121
      start += (new_token -> (match_start + matcher.start))
immler@34526
   122
      new_tokens ::= new_token
wenzelm@34318
   123
immler@34551
   124
      invalid_tokens = invalid_tokens dropWhile (stop(_) < stop(new_token))
immler@34526
   125
      invalid_tokens match {
wenzelm@34582
   126
        case t :: ts =>
wenzelm@34582
   127
          if (start(t) == start(new_token) &&
wenzelm@34582
   128
              start(t) > change.start + change.added.length) {
immler@34526
   129
          old_suffix = ts
immler@34526
   130
          invalid_tokens = Nil
immler@34526
   131
        }
immler@34526
   132
        case _ =>
wenzelm@34318
   133
      }
wenzelm@34318
   134
    }
immler@34526
   135
    val insert = new_tokens.reverse
immler@34544
   136
    val new_token_list = begin ::: insert ::: old_suffix
immler@34554
   137
    val new_tokenset = (LinearSet() ++ new_token_list).asInstanceOf[LinearSet[Token]]
wenzelm@34582
   138
    token_changed(change.id, begin.lastOption, insert,
wenzelm@34582
   139
      old_suffix.firstOption, new_tokenset, start)
wenzelm@34318
   140
  }
wenzelm@34582
   141
wenzelm@34318
   142
  
wenzelm@34485
   143
  /** command view **/
wenzelm@34485
   144
wenzelm@34496
   145
  def find_command_at(pos: Int): Command = {
immler@34554
   146
    for (cmd <- commands) { if (pos < cmd.stop(this)) return cmd }
wenzelm@34485
   147
    return null
wenzelm@34485
   148
  }
wenzelm@34485
   149
wenzelm@34582
   150
  private def token_changed(
wenzelm@34582
   151
    new_id: String,
wenzelm@34582
   152
    before_change: Option[Token],
wenzelm@34582
   153
    inserted_tokens: List[Token],
wenzelm@34582
   154
    after_change: Option[Token],
wenzelm@34582
   155
    new_tokenset: LinearSet[Token],
wenzelm@34582
   156
    new_token_start: Map[Token, Int]): (ProofDocument, StructureChange) =
wenzelm@34485
   157
  {
immler@34554
   158
    val cmd_first_changed =
immler@34554
   159
      if (before_change.isDefined) commands.find(_.tokens.contains(before_change.get))
immler@34554
   160
      else None
immler@34554
   161
    val cmd_last_changed =
immler@34554
   162
      if (after_change.isDefined) commands.find(_.tokens.contains(after_change.get))
immler@34554
   163
      else None
immler@34544
   164
immler@34554
   165
    val cmd_before_change =
immler@34554
   166
      if (cmd_first_changed.isDefined) commands.prev(cmd_first_changed.get)
immler@34554
   167
      else None
immler@34554
   168
    val cmd_after_change =
immler@34554
   169
      if (cmd_last_changed.isDefined) commands.next(cmd_last_changed.get)
immler@34554
   170
      else None
wenzelm@34485
   171
immler@34554
   172
    val removed_commands = commands.dropWhile(Some(_) != cmd_first_changed).
immler@34554
   173
      takeWhile(Some(_) != cmd_after_change)
immler@34554
   174
immler@34554
   175
    // calculate inserted commands
immler@34526
   176
    def tokens_to_commands(tokens: List[Token]): List[Command]= {
immler@34526
   177
      tokens match {
immler@34526
   178
        case Nil => Nil
wenzelm@34582
   179
        case t :: ts =>
wenzelm@34582
   180
          val (cmd, rest) =
wenzelm@34582
   181
            ts.span(t => t.kind != Token.Kind.COMMAND_START && t.kind != Token.Kind.COMMENT)
wenzelm@34582
   182
          new Command(t :: cmd, new_token_start) :: tokens_to_commands(rest)
wenzelm@34485
   183
      }
wenzelm@34485
   184
    }
wenzelm@34485
   185
immler@34554
   186
    val split_begin_tokens =
immler@34554
   187
      if (!cmd_first_changed.isDefined || !before_change.isDefined) Nil
immler@34554
   188
      else {
wenzelm@34582
   189
        cmd_first_changed.get.tokens.takeWhile(_ != before_change.get) :::
wenzelm@34582
   190
          List(before_change.get)
immler@34554
   191
      }
immler@34554
   192
    val split_end_tokens =
immler@34554
   193
      if (!cmd_last_changed.isDefined || !after_change.isDefined) Nil
immler@34554
   194
      else {
immler@34554
   195
        cmd_last_changed.get.tokens.dropWhile(_ != after_change.get)
immler@34554
   196
      }
immler@34554
   197
wenzelm@34582
   198
    val rescanning_tokens =
wenzelm@34582
   199
      split_begin_tokens ::: inserted_tokens ::: split_end_tokens
immler@34554
   200
    val inserted_commands = tokens_to_commands(rescanning_tokens)
immler@34554
   201
immler@34554
   202
    val change = new StructureChange(cmd_before_change, inserted_commands, removed_commands.toList)
immler@34550
   203
    // build new document
immler@34554
   204
    val new_commandset = commands.delete_between(cmd_before_change, cmd_after_change).
immler@34554
   205
        insert_after(cmd_before_change, inserted_commands)
immler@34554
   206
immler@34544
   207
    val doc =
wenzelm@34582
   208
      new ProofDocument(new_id, new_tokenset, new_token_start,
wenzelm@34582
   209
        new_commandset, active, is_command_keyword)
immler@34538
   210
    return (doc, change)
wenzelm@34485
   211
  }
wenzelm@34318
   212
}