src/Tools/jEdit/src/proofdocument/document.scala
author wenzelm
Mon Jan 11 01:40:18 2010 +0100 (2010-01-11)
changeset 34859 f986d84dd44b
parent 34855 81d0410dc3ac
child 34860 847c00f5535a
permissions -rw-r--r--
renamed Command.content to source;
reorganization of document parsing, using command spans etc. -- initial setup;
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@34818
    12
import scala.actors.Actor._
wenzelm@34824
    13
import scala.collection.mutable
wenzelm@34818
    14
wenzelm@34318
    15
import java.util.regex.Pattern
wenzelm@34703
    16
wenzelm@34318
    17
wenzelm@34823
    18
object Document
wenzelm@34483
    19
{
wenzelm@34859
    20
  /* command start positions */
wenzelm@34818
    21
wenzelm@34859
    22
  def command_starts(commands: Linear_Set[Command]): Iterator[(Command, Int)] =
wenzelm@34859
    23
  {
wenzelm@34859
    24
    var offset = 0
wenzelm@34859
    25
    for (cmd <- commands.elements) yield {
wenzelm@34859
    26
      val start = offset
wenzelm@34859
    27
      offset += cmd.length
wenzelm@34859
    28
      (cmd, start)
wenzelm@34859
    29
    }
wenzelm@34859
    30
  }
wenzelm@34859
    31
wenzelm@34859
    32
wenzelm@34859
    33
  /* empty document */
wenzelm@34485
    34
wenzelm@34823
    35
  def empty(id: Isar_Document.Document_ID): Document =
wenzelm@34835
    36
  {
wenzelm@34859
    37
    val doc = new Document(id, Linear_Set(), Map())
wenzelm@34835
    38
    doc.assign_states(Nil)
wenzelm@34835
    39
    doc
wenzelm@34835
    40
  }
immler@34660
    41
wenzelm@34859
    42
wenzelm@34859
    43
  // FIXME
wenzelm@34859
    44
  var phase0: List[Text_Edit] = null
wenzelm@34859
    45
  var phase1: Linear_Set[Command] = null
wenzelm@34859
    46
  var phase2: Linear_Set[Command] = null
wenzelm@34859
    47
  var phase3: List[Edit] = null
wenzelm@34859
    48
  var raw_source = ""
wenzelm@34859
    49
wenzelm@34859
    50
wenzelm@34859
    51
wenzelm@34859
    52
  /** document edits **/
wenzelm@34859
    53
wenzelm@34853
    54
  type Edit = (Option[Command], Option[Command])
wenzelm@34824
    55
wenzelm@34824
    56
  def text_edits(session: Session, old_doc: Document, new_id: Isar_Document.Document_ID,
wenzelm@34853
    57
    edits: List[Text_Edit]): (List[Edit], Document) =
wenzelm@34824
    58
  {
wenzelm@34840
    59
    require(old_doc.assignment.is_finished)
wenzelm@34840
    60
wenzelm@34859
    61
    System.err.println(edits)
wenzelm@34859
    62
    phase0 = edits
wenzelm@34859
    63
wenzelm@34859
    64
wenzelm@34859
    65
    /* unparsed dummy commands */
immler@34538
    66
wenzelm@34859
    67
    def unparsed(source: String) =
wenzelm@34859
    68
      new Command(session.create_id(), List(Outer_Lex.Token(Outer_Lex.Token_Kind.UNPARSED, source)))
wenzelm@34859
    69
wenzelm@34859
    70
    def is_unparsed(command: Command) = command.span.exists(_.is_unparsed)
wenzelm@34835
    71
wenzelm@34859
    72
    require(!old_doc.commands.exists(is_unparsed))
wenzelm@34859
    73
wenzelm@34859
    74
wenzelm@34859
    75
    /* phase 1: edit individual command source */
wenzelm@34859
    76
wenzelm@34859
    77
    var commands = old_doc.commands
wenzelm@34485
    78
wenzelm@34859
    79
    def edit_text(eds: List[Text_Edit]): Unit =
wenzelm@34859
    80
    {
wenzelm@34859
    81
      eds match {
wenzelm@34859
    82
        case e :: es =>
wenzelm@34859
    83
          command_starts(commands).find {   // FIXME relative search!
wenzelm@34859
    84
            case (cmd, cmd_start) => e.can_edit(cmd.length, cmd_start)
wenzelm@34859
    85
          } match {
wenzelm@34859
    86
            case Some((cmd, cmd_start)) =>
wenzelm@34859
    87
              val (rest, source) = e.edit(cmd.source, cmd_start)
wenzelm@34859
    88
              // FIXME Linear_Set.edit (?)
wenzelm@34859
    89
              commands = commands.insert_after(Some(cmd), unparsed(source))
wenzelm@34859
    90
              commands -= cmd
wenzelm@34859
    91
              edit_text(rest.toList ::: es)
wenzelm@34318
    92
wenzelm@34859
    93
            case None =>
wenzelm@34859
    94
              require(e.isInstanceOf[Text_Edit.Insert] && e.start == 0)
wenzelm@34859
    95
              commands = commands.insert_after(None, unparsed(e.text))
wenzelm@34859
    96
              edit_text(es)
wenzelm@34859
    97
          }
wenzelm@34859
    98
        case Nil =>
wenzelm@34318
    99
      }
wenzelm@34318
   100
    }
wenzelm@34859
   101
    edit_text(edits)
wenzelm@34859
   102
wenzelm@34859
   103
    phase1 = commands
wenzelm@34582
   104
wenzelm@34818
   105
wenzelm@34859
   106
    /* phase 2: command range edits */
wenzelm@34859
   107
wenzelm@34859
   108
    def edit_commands(): Unit =
wenzelm@34859
   109
    {
wenzelm@34859
   110
      // FIXME relative search!
wenzelm@34859
   111
      commands.elements.find(is_unparsed) match {
wenzelm@34859
   112
        case Some(first_unparsed) =>
wenzelm@34859
   113
          val prefix = commands.prev(first_unparsed)
wenzelm@34859
   114
          val body = commands.elements(first_unparsed).takeWhile(is_unparsed).toList
wenzelm@34859
   115
          val suffix = commands.next(body.last)
wenzelm@34859
   116
wenzelm@34859
   117
          val source =
wenzelm@34859
   118
            (prefix.toList ::: body ::: suffix.toList).flatMap(_.span.map(_.source)).mkString
wenzelm@34859
   119
          raw_source = source
wenzelm@34859
   120
          
wenzelm@34859
   121
          val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(source))
wenzelm@34485
   122
wenzelm@34859
   123
          val (before_edit, spans1) =
wenzelm@34859
   124
            if (!spans0.isEmpty && Some(spans0.first) == prefix.map(_.span))
wenzelm@34859
   125
              (prefix, spans0.tail)
wenzelm@34859
   126
            else (if (prefix.isDefined) commands.prev(prefix.get) else None, spans0)
immler@34544
   127
wenzelm@34859
   128
          val (after_edit, spans2) =
wenzelm@34859
   129
            if (!spans1.isEmpty && Some(spans1.last) == suffix.map(_.span))
wenzelm@34859
   130
              (suffix, spans1.take(spans1.length - 1))
wenzelm@34859
   131
            else (if (suffix.isDefined) commands.next(suffix.get) else None, spans1)
wenzelm@34859
   132
wenzelm@34859
   133
          val new_commands = spans2.map(span => new Command(session.create_id(), span))
wenzelm@34485
   134
wenzelm@34859
   135
          commands = commands.delete_between(before_edit, after_edit)
wenzelm@34859
   136
          commands = commands.append_after(before_edit, new_commands)
immler@34554
   137
wenzelm@34859
   138
          edit_commands()
wenzelm@34859
   139
wenzelm@34859
   140
        case None =>
wenzelm@34485
   141
      }
wenzelm@34485
   142
    }
wenzelm@34485
   143
wenzelm@34859
   144
    phase2 = commands
wenzelm@34739
   145
immler@34554
   146
wenzelm@34859
   147
    /* phase 3: resulting document edits */
wenzelm@34859
   148
wenzelm@34859
   149
    val removed_commands = old_doc.commands.elements.filter(!commands.contains(_)).toList
wenzelm@34859
   150
    val inserted_commands = commands.elements.filter(!old_doc.commands.contains(_)).toList
immler@34660
   151
wenzelm@34859
   152
    // FIXME tune
wenzelm@34859
   153
    val doc_edits =
wenzelm@34859
   154
      removed_commands.reverse.map(cmd => (Some(cmd), None)) :::
wenzelm@34859
   155
      inserted_commands.map(cmd => (commands.prev(cmd), Some(cmd)))
immler@34660
   156
wenzelm@34859
   157
    val former_states = old_doc.assignment.join -- removed_commands
wenzelm@34859
   158
wenzelm@34859
   159
    phase3 = doc_edits
wenzelm@34859
   160
    (doc_edits, new Document(new_id, commands, former_states))
wenzelm@34485
   161
  }
wenzelm@34840
   162
}
wenzelm@34840
   163
wenzelm@34855
   164
wenzelm@34840
   165
class Document(
wenzelm@34840
   166
    val id: Isar_Document.Document_ID,
wenzelm@34840
   167
    val commands: Linear_Set[Command],
wenzelm@34859
   168
    former_states: Map[Command, Command])
wenzelm@34840
   169
{
wenzelm@34859
   170
  /* command ranges */
wenzelm@34855
   171
wenzelm@34859
   172
  def command_starts: Iterator[(Command, Int)] = Document.command_starts(commands)
wenzelm@34855
   173
wenzelm@34855
   174
  def command_start(cmd: Command): Option[Int] =
wenzelm@34855
   175
    command_starts.find(_._1 == cmd).map(_._2)
wenzelm@34855
   176
wenzelm@34855
   177
  def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@34855
   178
    command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@34855
   179
wenzelm@34855
   180
  def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@34855
   181
    command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@34855
   182
wenzelm@34855
   183
  def command_at(i: Int): Option[(Command, Int)] =
wenzelm@34855
   184
  {
wenzelm@34855
   185
    val range = command_range(i)
wenzelm@34855
   186
    if (range.hasNext) Some(range.next) else None
wenzelm@34855
   187
  }
wenzelm@34855
   188
wenzelm@34855
   189
wenzelm@34855
   190
  /* command state assignment */
wenzelm@34840
   191
wenzelm@34840
   192
  val assignment = Future.promise[Map[Command, Command]]
wenzelm@34853
   193
  def await_assignment { assignment.join }
wenzelm@34840
   194
wenzelm@34859
   195
  @volatile private var tmp_states = former_states
wenzelm@34840
   196
wenzelm@34840
   197
  def assign_states(new_states: List[(Command, Command)])
wenzelm@34840
   198
  {
wenzelm@34840
   199
    assignment.fulfill(tmp_states ++ new_states)
wenzelm@34840
   200
    tmp_states = Map()
wenzelm@34840
   201
  }
wenzelm@34840
   202
wenzelm@34840
   203
  def current_state(cmd: Command): State =
wenzelm@34840
   204
  {
wenzelm@34840
   205
    require(assignment.is_finished)
wenzelm@34840
   206
    (assignment.join)(cmd).current_state
wenzelm@34840
   207
  }
wenzelm@34318
   208
}