src/Tools/jEdit/src/proofdocument/ProofDocument.scala
author wenzelm
Fri Dec 19 22:24:32 2008 +0100 (2008-12-19 ago)
changeset 34407 aad6834ba380
parent 34390 fe1afce19eb1
child 34408 ad7b6c4813c8
permissions -rw-r--r--
added some headers and comments;
wenzelm@34407
     1
/*
wenzelm@34407
     2
 * Document -- list of tokens
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Johannes Hölzl, TU Munich
wenzelm@34407
     5
 */
wenzelm@34407
     6
wenzelm@34318
     7
package isabelle.proofdocument
wenzelm@34318
     8
wenzelm@34318
     9
import java.util.regex.Pattern
wenzelm@34318
    10
wenzelm@34318
    11
import isabelle.utils.EventSource
wenzelm@34318
    12
wenzelm@34318
    13
object ProofDocument {
wenzelm@34318
    14
  // Be carefully when changeing this regex. Not only must it handle the 
wenzelm@34318
    15
  // spurious end of a token but also:  
wenzelm@34318
    16
  // Bug ID: 5050507 Pattern.matches throws StackOverflow Error
wenzelm@34318
    17
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507
wenzelm@34318
    18
  
wenzelm@34318
    19
  val tokenPattern = 
wenzelm@34318
    20
    Pattern.compile(
wenzelm@34318
    21
      "\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" +
wenzelm@34318
    22
      "\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" +
wenzelm@34318
    23
      "(\\?'?|')[A-Za-z_0-9.]*|" + 
wenzelm@34318
    24
      "[A-Za-z_0-9.]+|" + 
wenzelm@34318
    25
      "[!#$%&*+-/<=>?@^_|~]+|" +
wenzelm@34318
    26
      "\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" +
wenzelm@34318
    27
      "`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" +
wenzelm@34318
    28
      "[()\\[\\]{}:;]", Pattern.MULTILINE)
wenzelm@34318
    29
wenzelm@34318
    30
}
wenzelm@34318
    31
wenzelm@34318
    32
abstract class ProofDocument[C](text : Text) {
wenzelm@34318
    33
  import ProofDocument._
wenzelm@34318
    34
  
wenzelm@34318
    35
  protected var firstToken : Token[C] = null
wenzelm@34318
    36
  protected var lastToken : Token[C] = null
wenzelm@34318
    37
  private var active = false 
wenzelm@34318
    38
  
wenzelm@34318
    39
  {
wenzelm@34318
    40
    text.changes.add(e => textChanged(e.start, e.added, e.removed))
wenzelm@34318
    41
  }
wenzelm@34318
    42
	
wenzelm@34318
    43
  def activate() : Unit =
wenzelm@34318
    44
    if (! active) {
wenzelm@34318
    45
      active = true
wenzelm@34318
    46
      textChanged(0, text.length, 0)
wenzelm@34318
    47
    }
wenzelm@34318
    48
  
wenzelm@34318
    49
  protected def tokens(start : Token[C], stop : Token[C]) = 
wenzelm@34318
    50
    new Iterator[Token[C]]() {
wenzelm@34318
    51
      var current = start
wenzelm@34318
    52
      def hasNext() = current != stop && current != null
wenzelm@34318
    53
      def next() = { val save = current ; current = current.next ; save }
wenzelm@34318
    54
    }
wenzelm@34318
    55
  
wenzelm@34318
    56
  protected def tokens(start : Token[C]) : Iterator[Token[C]] = 
wenzelm@34318
    57
    tokens(start, null)
wenzelm@34318
    58
  
wenzelm@34318
    59
  protected def tokens() : Iterator[Token[C]] = 
wenzelm@34318
    60
    tokens(firstToken, null)
wenzelm@34318
    61
wenzelm@34318
    62
  private def textChanged(start : Int, addedLen : Int, removedLen : Int) {
immler@34390
    63
    val checkStart = Token.checkStart[C] _
immler@34390
    64
    val checkStop = Token.checkStop[C] _
immler@34390
    65
    val scan = Token.scan[C] _
wenzelm@34318
    66
    if (active == false)
wenzelm@34318
    67
      return
wenzelm@34318
    68
        
wenzelm@34318
    69
    var beforeChange = 
wenzelm@34318
    70
      if (checkStop(firstToken, _ < start)) scan(firstToken, _.stop >= start) 
wenzelm@34318
    71
      else null
wenzelm@34318
    72
    
wenzelm@34318
    73
    var firstRemoved = 
wenzelm@34318
    74
      if (beforeChange != null) beforeChange.next
wenzelm@34318
    75
      else if (checkStart(firstToken, _ <= start + removedLen)) firstToken
wenzelm@34318
    76
      else null 
wenzelm@34318
    77
wenzelm@34318
    78
    var lastRemoved = scan(firstRemoved, _.start > start + removedLen)
wenzelm@34318
    79
wenzelm@34318
    80
    var shiftToken = 
wenzelm@34318
    81
      if (lastRemoved != null) lastRemoved
wenzelm@34318
    82
      else if (checkStart(firstToken, _ > start)) firstToken
wenzelm@34318
    83
      else null
wenzelm@34318
    84
    
wenzelm@34318
    85
    while(shiftToken != null) {
wenzelm@34318
    86
      shiftToken.shift(addedLen - removedLen, start)
wenzelm@34318
    87
      shiftToken = shiftToken.next
wenzelm@34318
    88
    }
wenzelm@34318
    89
    
wenzelm@34318
    90
    // scan changed tokens until the end of the text or a matching token is
wenzelm@34318
    91
    // found which is beyond the changed area
wenzelm@34318
    92
    val matchStart = if (beforeChange == null) 0 else beforeChange.stop
wenzelm@34318
    93
    var firstAdded : Token[C] = null
wenzelm@34318
    94
    var lastAdded : Token[C] = null
wenzelm@34318
    95
wenzelm@34318
    96
    val matcher = tokenPattern.matcher(text.content(matchStart, text.length))
wenzelm@34318
    97
    var finished = false
wenzelm@34318
    98
    var position = 0 
wenzelm@34318
    99
    while(matcher.find(position) && ! finished) {
wenzelm@34318
   100
      position = matcher.end()
immler@34388
   101
			val kind = if(isStartKeyword(matcher.group())){
immler@34388
   102
        Token.Kind.COMMAND_START
immler@34388
   103
      } else if (matcher.end() - matcher.start() > 2 && matcher.group().substring(0, 2) == "(*"){
immler@34388
   104
        Token.Kind.COMMENT
immler@34388
   105
      } else {""}
wenzelm@34318
   106
      val newToken = new Token[C](matcher.start() + matchStart, 
wenzelm@34318
   107
                                  matcher.end() + matchStart,
immler@34388
   108
                                  kind
immler@34388
   109
                                  )
wenzelm@34318
   110
wenzelm@34318
   111
      if (firstAdded == null)
wenzelm@34318
   112
        firstAdded = newToken
wenzelm@34318
   113
      else {
wenzelm@34318
   114
        newToken.previous = lastAdded
wenzelm@34318
   115
        lastAdded.next = newToken
wenzelm@34318
   116
      }
wenzelm@34318
   117
      lastAdded = newToken
wenzelm@34318
   118
      
wenzelm@34318
   119
      while(checkStop(lastRemoved, _ < newToken.stop) &&
wenzelm@34318
   120
              lastRemoved.next != null)	
wenzelm@34318
   121
        lastRemoved = lastRemoved.next
wenzelm@34318
   122
			
wenzelm@34318
   123
      if (newToken.stop >= start + addedLen && 
wenzelm@34318
   124
            checkStop(lastRemoved, _ == newToken.stop) )
wenzelm@34318
   125
        finished = true
wenzelm@34318
   126
    }
wenzelm@34318
   127
wenzelm@34318
   128
    var afterChange = if (lastRemoved != null) lastRemoved.next else null
wenzelm@34318
   129
		
wenzelm@34318
   130
    // remove superflous first token-change
wenzelm@34318
   131
    if (firstAdded != null && firstAdded == firstRemoved && 
wenzelm@34318
   132
          firstAdded.stop < start) {
wenzelm@34318
   133
      beforeChange = firstRemoved
wenzelm@34318
   134
      if (lastRemoved == firstRemoved) {
wenzelm@34318
   135
        lastRemoved = null
wenzelm@34318
   136
        firstRemoved = null
wenzelm@34318
   137
      }
wenzelm@34318
   138
      else {
wenzelm@34318
   139
        firstRemoved = firstRemoved.next
wenzelm@34318
   140
        if (firstRemoved == null)
wenzelm@34318
   141
          System.err.println("ERROR")
wenzelm@34318
   142
        assert(firstRemoved != null)
wenzelm@34318
   143
      }
wenzelm@34318
   144
wenzelm@34318
   145
      if (lastAdded == firstAdded) {
wenzelm@34318
   146
        lastAdded = null
wenzelm@34318
   147
        firstAdded = null
wenzelm@34318
   148
      }
wenzelm@34318
   149
      if (firstAdded != null)
wenzelm@34318
   150
        firstAdded = firstAdded.next
wenzelm@34318
   151
    }
wenzelm@34318
   152
    
wenzelm@34318
   153
    // remove superflous last token-change
wenzelm@34318
   154
    if (lastAdded != null && lastAdded == lastRemoved &&
wenzelm@34318
   155
          lastAdded.start > start + addedLen) {
wenzelm@34318
   156
      afterChange = lastRemoved
wenzelm@34318
   157
      if (firstRemoved == lastRemoved) {
wenzelm@34318
   158
        firstRemoved = null
wenzelm@34318
   159
        lastRemoved = null
wenzelm@34318
   160
      }
wenzelm@34318
   161
      else {
wenzelm@34318
   162
        lastRemoved = lastRemoved.previous
wenzelm@34318
   163
        if (lastRemoved == null)
wenzelm@34318
   164
          System.err.println("ERROR")
wenzelm@34318
   165
        assert(lastRemoved != null)
wenzelm@34318
   166
      }
wenzelm@34318
   167
      
wenzelm@34318
   168
      if (lastAdded == firstAdded) {
wenzelm@34318
   169
        lastAdded = null
wenzelm@34318
   170
        firstAdded = null
wenzelm@34318
   171
      }
wenzelm@34318
   172
      else
wenzelm@34318
   173
        lastAdded = lastAdded.previous
wenzelm@34318
   174
    }
wenzelm@34318
   175
    
wenzelm@34318
   176
    if (firstRemoved == null && firstAdded == null)
wenzelm@34318
   177
      return
wenzelm@34318
   178
    
wenzelm@34318
   179
    if (firstToken == null) {
wenzelm@34318
   180
      firstToken = firstAdded
wenzelm@34318
   181
      lastToken = lastAdded
wenzelm@34318
   182
    }
wenzelm@34318
   183
    else {
wenzelm@34318
   184
      // cut removed tokens out of list
wenzelm@34318
   185
      if (firstRemoved != null)
wenzelm@34318
   186
        firstRemoved.previous = null
wenzelm@34318
   187
      if (lastRemoved != null)
wenzelm@34318
   188
        lastRemoved.next = null
wenzelm@34318
   189
      
wenzelm@34318
   190
      if (firstToken == firstRemoved)
wenzelm@34318
   191
        if (firstAdded != null)
wenzelm@34318
   192
          firstToken = firstAdded
wenzelm@34318
   193
        else
wenzelm@34318
   194
          firstToken = afterChange
wenzelm@34318
   195
      
wenzelm@34318
   196
      if (lastToken == lastRemoved)
wenzelm@34318
   197
        if (lastAdded != null)
wenzelm@34318
   198
          lastToken = lastAdded
wenzelm@34318
   199
        else
wenzelm@34318
   200
          lastToken = beforeChange
wenzelm@34318
   201
      
wenzelm@34318
   202
      // insert new tokens into list
wenzelm@34318
   203
      if (firstAdded != null) {
wenzelm@34318
   204
        firstAdded.previous = beforeChange
wenzelm@34318
   205
        if (beforeChange != null)
wenzelm@34318
   206
          beforeChange.next = firstAdded
wenzelm@34318
   207
        else
wenzelm@34318
   208
          firstToken = firstAdded
wenzelm@34318
   209
      }
wenzelm@34318
   210
      else if (beforeChange != null)
wenzelm@34318
   211
        beforeChange.next = afterChange
wenzelm@34318
   212
      
wenzelm@34318
   213
      if (lastAdded != null) {
wenzelm@34318
   214
        lastAdded.next = afterChange
wenzelm@34318
   215
        if (afterChange != null)
wenzelm@34318
   216
          afterChange.previous = lastAdded
wenzelm@34318
   217
        else
wenzelm@34318
   218
          lastToken = lastAdded
wenzelm@34318
   219
      }
wenzelm@34318
   220
      else if (afterChange != null)
wenzelm@34318
   221
        afterChange.previous = beforeChange
wenzelm@34318
   222
    }
wenzelm@34318
   223
    
wenzelm@34318
   224
    tokenChanged(beforeChange, afterChange, firstRemoved)
wenzelm@34318
   225
  }
wenzelm@34318
   226
  
wenzelm@34318
   227
  protected def isStartKeyword(str : String) : Boolean;
wenzelm@34318
   228
  
wenzelm@34318
   229
  protected def tokenChanged(start : Token[C], stop : Token[C], 
wenzelm@34318
   230
                             removed : Token[C]) 
wenzelm@34318
   231
}