src/Tools/jEdit/src/proofdocument/ProofDocument.scala
author wenzelm
Tue Jan 20 22:30:54 2009 +0100 (2009-01-20)
changeset 34494 47f9303db81d
parent 34491 20e9d420dbbb
child 34496 1b2995592bb9
permissions -rw-r--r--
misc tuning -- de-camelization;
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
wenzelm@34485
     5
 * @author Makarius
wenzelm@34407
     6
 */
wenzelm@34407
     7
wenzelm@34318
     8
package isabelle.proofdocument
wenzelm@34318
     9
wenzelm@34483
    10
wenzelm@34318
    11
import java.util.regex.Pattern
wenzelm@34485
    12
import isabelle.prover.{Prover, Command}
wenzelm@34318
    13
wenzelm@34318
    14
wenzelm@34494
    15
case class StructureChange(
wenzelm@34494
    16
  val before_change: Command,
wenzelm@34494
    17
  val added_commands: List[Command],
wenzelm@34494
    18
  val removed_commands: List[Command])
wenzelm@34485
    19
wenzelm@34483
    20
object ProofDocument
wenzelm@34483
    21
{
wenzelm@34485
    22
  // Be carefully when changing this regex. Not only must it handle the 
wenzelm@34318
    23
  // spurious end of a token but also:  
wenzelm@34318
    24
  // Bug ID: 5050507 Pattern.matches throws StackOverflow Error
wenzelm@34318
    25
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507
wenzelm@34318
    26
  
wenzelm@34483
    27
  val token_pattern = 
wenzelm@34318
    28
    Pattern.compile(
wenzelm@34318
    29
      "\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" +
wenzelm@34318
    30
      "\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" +
wenzelm@34318
    31
      "(\\?'?|')[A-Za-z_0-9.]*|" + 
wenzelm@34318
    32
      "[A-Za-z_0-9.]+|" + 
wenzelm@34318
    33
      "[!#$%&*+-/<=>?@^_|~]+|" +
wenzelm@34318
    34
      "\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" +
wenzelm@34318
    35
      "`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" +
wenzelm@34318
    36
      "[()\\[\\]{}:;]", Pattern.MULTILINE)
wenzelm@34485
    37
wenzelm@34318
    38
}
wenzelm@34318
    39
wenzelm@34485
    40
class ProofDocument(text: Text, prover: Prover)
wenzelm@34483
    41
{
wenzelm@34318
    42
  private var active = false 
wenzelm@34456
    43
  def activate() {
wenzelm@34456
    44
    if (!active) {
wenzelm@34318
    45
      active = true
wenzelm@34485
    46
      text_changed(0, text.length, 0)
wenzelm@34318
    47
    }
wenzelm@34456
    48
  }
wenzelm@34485
    49
wenzelm@34485
    50
  text.changes += (changed => text_changed(changed.start, changed.added, changed.removed))
wenzelm@34485
    51
wenzelm@34485
    52
wenzelm@34485
    53
wenzelm@34485
    54
  /** token view **/
wenzelm@34485
    55
wenzelm@34494
    56
  private var first_token: Token = null
wenzelm@34494
    57
  private var last_token: Token = null
wenzelm@34318
    58
  
wenzelm@34494
    59
  private def tokens(start: Token, stop: Token) = new Iterator[Token] {
wenzelm@34318
    60
      var current = start
wenzelm@34483
    61
      def hasNext = current != stop && current != null
wenzelm@34494
    62
      def next() = { val save = current; current = current.next; save }
wenzelm@34318
    63
    }
wenzelm@34494
    64
  private def tokens(start: Token): Iterator[Token] = tokens(start, null)
wenzelm@34494
    65
  private def tokens(): Iterator[Token] = tokens(first_token, null)
wenzelm@34485
    66
wenzelm@34318
    67
wenzelm@34494
    68
  private def text_changed(start: Int, added_len: Int, removed_len: Int)
wenzelm@34485
    69
  {
wenzelm@34485
    70
    if (!active)
wenzelm@34318
    71
      return
wenzelm@34485
    72
wenzelm@34494
    73
    var before_change =
wenzelm@34494
    74
      if (Token.check_stop(first_token, _ < start)) Token.scan(first_token, _.stop >= start)
wenzelm@34318
    75
      else null
wenzelm@34318
    76
    
wenzelm@34494
    77
    var first_removed =
wenzelm@34494
    78
      if (before_change != null) before_change.next
wenzelm@34494
    79
      else if (Token.check_start(first_token, _ <= start + removed_len)) first_token
wenzelm@34318
    80
      else null 
wenzelm@34318
    81
wenzelm@34494
    82
    var last_removed = Token.scan(first_removed, _.start > start + removed_len)
wenzelm@34318
    83
wenzelm@34494
    84
    var shift_token =
wenzelm@34494
    85
      if (last_removed != null) last_removed
wenzelm@34494
    86
      else if (Token.check_start(first_token, _ > start)) first_token
wenzelm@34318
    87
      else null
wenzelm@34318
    88
    
wenzelm@34494
    89
    while (shift_token != null) {
wenzelm@34494
    90
      shift_token.shift(added_len - removed_len, start)
wenzelm@34494
    91
      shift_token = shift_token.next
wenzelm@34318
    92
    }
wenzelm@34318
    93
    
wenzelm@34318
    94
    // scan changed tokens until the end of the text or a matching token is
wenzelm@34318
    95
    // found which is beyond the changed area
wenzelm@34494
    96
    val match_start = if (before_change == null) 0 else before_change.stop
wenzelm@34494
    97
    var first_added: Token = null
wenzelm@34494
    98
    var last_added: Token = null
wenzelm@34318
    99
wenzelm@34494
   100
    val matcher = ProofDocument.token_pattern.matcher(text.content(match_start, text.length))
wenzelm@34318
   101
    var finished = false
wenzelm@34318
   102
    var position = 0 
wenzelm@34494
   103
    while (matcher.find(position) && !finished) {
wenzelm@34494
   104
      position = matcher.end
wenzelm@34485
   105
			val kind =
wenzelm@34494
   106
        if (prover.is_command_keyword(matcher.group))
wenzelm@34485
   107
          Token.Kind.COMMAND_START
wenzelm@34494
   108
        else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*")
wenzelm@34485
   109
          Token.Kind.COMMENT
wenzelm@34485
   110
        else
wenzelm@34485
   111
          Token.Kind.OTHER
wenzelm@34494
   112
      val new_token = new Token(matcher.start + match_start, matcher.end + match_start, kind)
wenzelm@34318
   113
wenzelm@34494
   114
      if (first_added == null)
wenzelm@34494
   115
        first_added = new_token
wenzelm@34318
   116
      else {
wenzelm@34494
   117
        new_token.prev = last_added
wenzelm@34494
   118
        last_added.next = new_token
wenzelm@34318
   119
      }
wenzelm@34494
   120
      last_added = new_token
wenzelm@34318
   121
      
wenzelm@34494
   122
      while (Token.check_stop(last_removed, _ < new_token.stop) &&
wenzelm@34494
   123
              last_removed.next != null)
wenzelm@34494
   124
        last_removed = last_removed.next
wenzelm@34318
   125
			
wenzelm@34494
   126
      if (new_token.stop >= start + added_len &&
wenzelm@34494
   127
            Token.check_stop(last_removed, _ == new_token.stop))
wenzelm@34318
   128
        finished = true
wenzelm@34318
   129
    }
wenzelm@34318
   130
wenzelm@34494
   131
    var after_change = if (last_removed != null) last_removed.next else null
wenzelm@34318
   132
		
wenzelm@34318
   133
    // remove superflous first token-change
wenzelm@34494
   134
    if (first_added != null && first_added == first_removed &&
wenzelm@34494
   135
          first_added.stop < start) {
wenzelm@34494
   136
      before_change = first_removed
wenzelm@34494
   137
      if (last_removed == first_removed) {
wenzelm@34494
   138
        last_removed = null
wenzelm@34494
   139
        first_removed = null
wenzelm@34318
   140
      }
wenzelm@34318
   141
      else {
wenzelm@34494
   142
        first_removed = first_removed.next
wenzelm@34494
   143
        assert(first_removed != null)
wenzelm@34318
   144
      }
wenzelm@34318
   145
wenzelm@34494
   146
      if (last_added == first_added) {
wenzelm@34494
   147
        last_added = null
wenzelm@34494
   148
        first_added = null
wenzelm@34318
   149
      }
wenzelm@34494
   150
      if (first_added != null)
wenzelm@34494
   151
        first_added = first_added.next
wenzelm@34318
   152
    }
wenzelm@34318
   153
    
wenzelm@34318
   154
    // remove superflous last token-change
wenzelm@34494
   155
    if (last_added != null && last_added == last_removed &&
wenzelm@34494
   156
          last_added.start > start + added_len) {
wenzelm@34494
   157
      after_change = last_removed
wenzelm@34494
   158
      if (first_removed == last_removed) {
wenzelm@34494
   159
        first_removed = null
wenzelm@34494
   160
        last_removed = null
wenzelm@34318
   161
      }
wenzelm@34318
   162
      else {
wenzelm@34494
   163
        last_removed = last_removed.prev
wenzelm@34494
   164
        assert(last_removed != null)
wenzelm@34318
   165
      }
wenzelm@34318
   166
      
wenzelm@34494
   167
      if (last_added == first_added) {
wenzelm@34494
   168
        last_added = null
wenzelm@34494
   169
        first_added = null
wenzelm@34318
   170
      }
wenzelm@34318
   171
      else
wenzelm@34494
   172
        last_added = last_added.prev
wenzelm@34318
   173
    }
wenzelm@34318
   174
    
wenzelm@34494
   175
    if (first_removed == null && first_added == null)
wenzelm@34318
   176
      return
wenzelm@34318
   177
    
wenzelm@34494
   178
    if (first_token == null) {
wenzelm@34494
   179
      first_token = first_added
wenzelm@34494
   180
      last_token = last_added
wenzelm@34318
   181
    }
wenzelm@34318
   182
    else {
wenzelm@34318
   183
      // cut removed tokens out of list
wenzelm@34494
   184
      if (first_removed != null)
wenzelm@34494
   185
        first_removed.prev = null
wenzelm@34494
   186
      if (last_removed != null)
wenzelm@34494
   187
        last_removed.next = null
wenzelm@34318
   188
      
wenzelm@34494
   189
      if (first_token == first_removed)
wenzelm@34494
   190
        if (first_added != null)
wenzelm@34494
   191
          first_token = first_added
wenzelm@34318
   192
        else
wenzelm@34494
   193
          first_token = after_change
wenzelm@34318
   194
      
wenzelm@34494
   195
      if (last_token == last_removed)
wenzelm@34494
   196
        if (last_added != null)
wenzelm@34494
   197
          last_token = last_added
wenzelm@34318
   198
        else
wenzelm@34494
   199
          last_token = before_change
wenzelm@34318
   200
      
wenzelm@34318
   201
      // insert new tokens into list
wenzelm@34494
   202
      if (first_added != null) {
wenzelm@34494
   203
        first_added.prev = before_change
wenzelm@34494
   204
        if (before_change != null)
wenzelm@34494
   205
          before_change.next = first_added
wenzelm@34318
   206
        else
wenzelm@34494
   207
          first_token = first_added
wenzelm@34318
   208
      }
wenzelm@34494
   209
      else if (before_change != null)
wenzelm@34494
   210
        before_change.next = after_change
wenzelm@34318
   211
      
wenzelm@34494
   212
      if (last_added != null) {
wenzelm@34494
   213
        last_added.next = after_change
wenzelm@34494
   214
        if (after_change != null)
wenzelm@34494
   215
          after_change.prev = last_added
wenzelm@34318
   216
        else
wenzelm@34494
   217
          last_token = last_added
wenzelm@34318
   218
      }
wenzelm@34494
   219
      else if (after_change != null)
wenzelm@34494
   220
        after_change.prev = before_change
wenzelm@34318
   221
    }
wenzelm@34318
   222
    
wenzelm@34494
   223
    token_changed(before_change, after_change, first_removed)
wenzelm@34318
   224
  }
wenzelm@34318
   225
  
wenzelm@34485
   226
wenzelm@34318
   227
  
wenzelm@34485
   228
  /** command view **/
wenzelm@34485
   229
wenzelm@34485
   230
  val structural_changes = new EventBus[StructureChange]
wenzelm@34485
   231
wenzelm@34485
   232
  def commands = new Iterator[Command] {
wenzelm@34494
   233
    var current = first_token
wenzelm@34485
   234
    def hasNext = current != null
wenzelm@34485
   235
    def next() = { val s = current.command ; current = s.last.next ; s }
wenzelm@34485
   236
  }
wenzelm@34485
   237
wenzelm@34485
   238
  def getNextCommandContaining(pos: Int): Command = {
wenzelm@34485
   239
    for (cmd <- commands) { if (pos < cmd.stop) return cmd }
wenzelm@34485
   240
    return null
wenzelm@34485
   241
  }
wenzelm@34485
   242
wenzelm@34485
   243
  private def token_changed(start: Token, stop: Token, removed: Token)
wenzelm@34485
   244
  {
wenzelm@34494
   245
    var removed_commands: List[Command] = Nil
wenzelm@34485
   246
    var first: Command = null
wenzelm@34485
   247
    var last: Command = null
wenzelm@34485
   248
wenzelm@34485
   249
    for(t <- tokens(removed)) {
wenzelm@34485
   250
      if (first == null)
wenzelm@34485
   251
        first = t.command
wenzelm@34485
   252
      if (t.command != last)
wenzelm@34494
   253
        removed_commands = t.command :: removed_commands
wenzelm@34485
   254
      last = t.command
wenzelm@34485
   255
    }
wenzelm@34485
   256
wenzelm@34485
   257
    var before: Command = null
wenzelm@34494
   258
    if (!removed_commands.isEmpty) {
wenzelm@34485
   259
      if (first.first == removed) {
wenzelm@34485
   260
        if (start == null)
wenzelm@34485
   261
          before = null
wenzelm@34485
   262
        else
wenzelm@34485
   263
          before = start.command
wenzelm@34485
   264
      }
wenzelm@34485
   265
      else
wenzelm@34485
   266
        before = first.prev
wenzelm@34485
   267
    }
wenzelm@34485
   268
wenzelm@34494
   269
    var added_commands: List[Command] = Nil
wenzelm@34485
   270
    var scan: Token = null
wenzelm@34485
   271
    if (start != null) {
wenzelm@34485
   272
      val next = start.next
wenzelm@34485
   273
      if (first != null && first.first != removed) {
wenzelm@34485
   274
        scan = first.first
wenzelm@34485
   275
        if (before == null)
wenzelm@34485
   276
          before = first.prev
wenzelm@34485
   277
      }
wenzelm@34485
   278
      else if (next != null && next != stop) {
wenzelm@34485
   279
        if (next.kind == Token.Kind.COMMAND_START) {
wenzelm@34485
   280
          before = start.command
wenzelm@34485
   281
          scan = next
wenzelm@34485
   282
        }
wenzelm@34485
   283
        else if (first == null || first.first == removed) {
wenzelm@34485
   284
          first = start.command
wenzelm@34494
   285
          removed_commands = first :: removed_commands
wenzelm@34485
   286
          scan = first.first
wenzelm@34485
   287
          if (before == null)
wenzelm@34485
   288
            before = first.prev
wenzelm@34485
   289
        }
wenzelm@34485
   290
        else {
wenzelm@34485
   291
          scan = first.first
wenzelm@34485
   292
          if (before == null)
wenzelm@34485
   293
            before = first.prev
wenzelm@34485
   294
        }
wenzelm@34485
   295
      }
wenzelm@34485
   296
    }
wenzelm@34485
   297
    else
wenzelm@34494
   298
      scan = first_token
wenzelm@34485
   299
wenzelm@34485
   300
    var stopScan: Token = null
wenzelm@34485
   301
    if (stop != null) {
wenzelm@34485
   302
      if (stop == stop.command.first)
wenzelm@34485
   303
        stopScan = stop
wenzelm@34485
   304
      else
wenzelm@34485
   305
        stopScan = stop.command.last.next
wenzelm@34485
   306
    }
wenzelm@34485
   307
    else if (last != null)
wenzelm@34485
   308
      stopScan = last.last.next
wenzelm@34485
   309
    else
wenzelm@34485
   310
      stopScan = null
wenzelm@34485
   311
wenzelm@34485
   312
    var cmdStart: Token = null
wenzelm@34485
   313
    var cmdStop: Token = null
wenzelm@34485
   314
    var overrun = false
wenzelm@34485
   315
    var finished = false
wenzelm@34485
   316
    while (scan != null && !finished) {
wenzelm@34485
   317
      if (scan == stopScan) {
wenzelm@34485
   318
        if (scan.kind == Token.Kind.COMMAND_START)
wenzelm@34485
   319
          finished = true
wenzelm@34485
   320
        else
wenzelm@34485
   321
          overrun = true
wenzelm@34485
   322
      }
wenzelm@34485
   323
wenzelm@34485
   324
      if (scan.kind == Token.Kind.COMMAND_START && cmdStart != null && !finished) {
wenzelm@34485
   325
        if (!overrun) {
wenzelm@34494
   326
          added_commands = new Command(text, cmdStart, cmdStop) :: added_commands
wenzelm@34485
   327
          cmdStart = scan
wenzelm@34485
   328
          cmdStop = scan
wenzelm@34485
   329
        }
wenzelm@34485
   330
        else
wenzelm@34485
   331
          finished = true
wenzelm@34485
   332
      }
wenzelm@34485
   333
      else if (!finished) {
wenzelm@34485
   334
        if (cmdStart == null)
wenzelm@34485
   335
          cmdStart = scan
wenzelm@34485
   336
        cmdStop = scan
wenzelm@34485
   337
      }
wenzelm@34485
   338
      if (overrun && !finished) {
wenzelm@34485
   339
        if (scan.command != last)
wenzelm@34494
   340
          removed_commands = scan.command :: removed_commands
wenzelm@34485
   341
        last = scan.command
wenzelm@34485
   342
      }
wenzelm@34485
   343
wenzelm@34485
   344
      if (!finished)
wenzelm@34485
   345
        scan = scan.next
wenzelm@34485
   346
    }
wenzelm@34485
   347
wenzelm@34485
   348
    if (cmdStart != null)
wenzelm@34494
   349
      added_commands = new Command(text, cmdStart, cmdStop) :: added_commands
wenzelm@34485
   350
wenzelm@34485
   351
    // relink commands
wenzelm@34494
   352
    added_commands = added_commands.reverse
wenzelm@34494
   353
    removed_commands = removed_commands.reverse
wenzelm@34485
   354
wenzelm@34494
   355
    structural_changes.event(new StructureChange(before, added_commands, removed_commands))
wenzelm@34485
   356
  }
wenzelm@34318
   357
}