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