src/Pure/PIDE/document.scala
author wenzelm
Sat Aug 14 22:45:23 2010 +0200 (2010-08-14)
changeset 38414 49f1f657adc2
parent 38374 7eb0f6991e25
child 38417 b8922ae21111
permissions -rw-r--r--
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
added convenient Markup.Int/Long objects (Scala);
simplified "assign" message format -- explicit version id;
misc tuning and simplification;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/document.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@36676
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@38150
     5
list of commands.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34760
    10
wenzelm@38150
    11
import scala.collection.mutable
wenzelm@37073
    12
wenzelm@37073
    13
wenzelm@34823
    14
object Document
wenzelm@34483
    15
{
wenzelm@38227
    16
  /* formal identifiers */
wenzelm@38150
    17
wenzelm@38363
    18
  type ID = Long
wenzelm@38414
    19
wenzelm@38414
    20
  object ID {
wenzelm@38414
    21
    def apply(id: ID): String = Markup.Long.apply(id)
wenzelm@38414
    22
    def unapply(s: String): Option[ID] = Markup.Long.unapply(s)
wenzelm@38414
    23
  }
wenzelm@38414
    24
wenzelm@38373
    25
  type Version_ID = ID
wenzelm@38363
    26
  type Command_ID = ID
wenzelm@38373
    27
  type Exec_ID = ID
wenzelm@38150
    28
wenzelm@38363
    29
  val NO_ID: ID = 0
wenzelm@38355
    30
wenzelm@38150
    31
wenzelm@34818
    32
wenzelm@38227
    33
  /** named document nodes **/
wenzelm@38151
    34
wenzelm@38151
    35
  object Node
wenzelm@38151
    36
  {
wenzelm@38151
    37
    val empty: Node = new Node(Linear_Set())
wenzelm@38227
    38
wenzelm@38227
    39
    def command_starts(commands: Iterator[Command], offset: Int = 0): Iterator[(Command, Int)] =
wenzelm@38227
    40
    {
wenzelm@38227
    41
      var i = offset
wenzelm@38227
    42
      for (command <- commands) yield {
wenzelm@38227
    43
        val start = i
wenzelm@38227
    44
        i += command.length
wenzelm@38227
    45
        (command, start)
wenzelm@38227
    46
      }
wenzelm@38227
    47
    }
wenzelm@38151
    48
  }
wenzelm@38151
    49
wenzelm@38151
    50
  class Node(val commands: Linear_Set[Command])
wenzelm@38151
    51
  {
wenzelm@38151
    52
    /* command ranges */
wenzelm@38151
    53
wenzelm@38151
    54
    def command_starts: Iterator[(Command, Int)] =
wenzelm@38227
    55
      Node.command_starts(commands.iterator)
wenzelm@38151
    56
wenzelm@38151
    57
    def command_start(cmd: Command): Option[Int] =
wenzelm@38151
    58
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38151
    59
wenzelm@38151
    60
    def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@38151
    61
      command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@38151
    62
wenzelm@38151
    63
    def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@38151
    64
      command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@38151
    65
wenzelm@38151
    66
    def command_at(i: Int): Option[(Command, Int)] =
wenzelm@38151
    67
    {
wenzelm@38151
    68
      val range = command_range(i)
wenzelm@38151
    69
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
    70
    }
wenzelm@38151
    71
wenzelm@38151
    72
    def proper_command_at(i: Int): Option[Command] =
wenzelm@38151
    73
      command_at(i) match {
wenzelm@38151
    74
        case Some((command, _)) =>
wenzelm@38151
    75
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
    76
        case None => None
wenzelm@38151
    77
      }
wenzelm@38151
    78
  }
wenzelm@38151
    79
wenzelm@38151
    80
wenzelm@38150
    81
  /* initial document */
wenzelm@34485
    82
wenzelm@38370
    83
  val init: Document = new Document(NO_ID, Map().withDefaultValue(Node.empty))
immler@34660
    84
wenzelm@34859
    85
wenzelm@34859
    86
wenzelm@38227
    87
  /** changes of plain text, eventually resulting in document edits **/
wenzelm@38151
    88
wenzelm@38151
    89
  type Node_Text_Edit = (String, List[Text_Edit])  // FIXME None: remove
wenzelm@38151
    90
wenzelm@38151
    91
  type Edit[C] =
wenzelm@38151
    92
   (String,  // node name
wenzelm@38151
    93
    Option[List[(Option[C], Option[C])]])  // None: remove, Some: insert/remove commands
wenzelm@34859
    94
wenzelm@38227
    95
  abstract class Snapshot
wenzelm@38227
    96
  {
wenzelm@38227
    97
    val document: Document
wenzelm@38227
    98
    val node: Document.Node
wenzelm@38227
    99
    val is_outdated: Boolean
wenzelm@38227
   100
    def convert(offset: Int): Int
wenzelm@38227
   101
    def revert(offset: Int): Int
wenzelm@38370
   102
    def lookup_command(id: Command_ID): Option[Command]
wenzelm@38361
   103
    def state(command: Command): Command.State
wenzelm@38227
   104
  }
wenzelm@38227
   105
wenzelm@38227
   106
  object Change
wenzelm@38227
   107
  {
wenzelm@38366
   108
    val init = new Change(Future.value(Document.init), Nil, Future.value(Nil, Document.init))
wenzelm@38227
   109
  }
wenzelm@38227
   110
wenzelm@38227
   111
  class Change(
wenzelm@38366
   112
    val prev: Future[Document],
wenzelm@38227
   113
    val edits: List[Node_Text_Edit],
wenzelm@38227
   114
    val result: Future[(List[Edit[Command]], Document)])
wenzelm@38227
   115
  {
wenzelm@38366
   116
    val document: Future[Document] = result.map(_._2)
wenzelm@38366
   117
    def is_finished: Boolean = prev.is_finished && document.is_finished
wenzelm@38227
   118
  }
wenzelm@38227
   119
wenzelm@38227
   120
wenzelm@38227
   121
wenzelm@38370
   122
  /** global state -- accumulated prover results **/
wenzelm@38370
   123
wenzelm@38370
   124
  object State
wenzelm@38370
   125
  {
wenzelm@38373
   126
    class Fail(state: State) extends Exception
wenzelm@38373
   127
wenzelm@38370
   128
    val init = State().define_document(Document.init, Map()).assign(Document.init.id, Nil)
wenzelm@38370
   129
wenzelm@38370
   130
    class Assignment(former_assignment: Map[Command, Exec_ID])
wenzelm@38370
   131
    {
wenzelm@38370
   132
      @volatile private var tmp_assignment = former_assignment
wenzelm@38370
   133
      private val promise = Future.promise[Map[Command, Exec_ID]]
wenzelm@38370
   134
      def is_finished: Boolean = promise.is_finished
wenzelm@38370
   135
      def join: Map[Command, Exec_ID] = promise.join
wenzelm@38370
   136
      def assign(command_execs: List[(Command, Exec_ID)])
wenzelm@38370
   137
      {
wenzelm@38370
   138
        promise.fulfill(tmp_assignment ++ command_execs)
wenzelm@38370
   139
        tmp_assignment = Map()
wenzelm@38150
   140
      }
wenzelm@38370
   141
    }
wenzelm@38370
   142
  }
wenzelm@38370
   143
wenzelm@38370
   144
  case class State(
wenzelm@38373
   145
    val documents: Map[Version_ID, Document] = Map(),
wenzelm@38370
   146
    val commands: Map[Command_ID, Command.State] = Map(),
wenzelm@38370
   147
    val execs: Map[Exec_ID, (Command.State, Set[Document])] = Map(),
wenzelm@38370
   148
    val assignments: Map[Document, State.Assignment] = Map(),
wenzelm@38370
   149
    val disposed: Set[ID] = Set())  // FIXME unused!?
wenzelm@38370
   150
  {
wenzelm@38373
   151
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   152
wenzelm@38370
   153
    def define_document(document: Document, former_assignment: Map[Command, Exec_ID]): State =
wenzelm@38370
   154
    {
wenzelm@38370
   155
      val id = document.id
wenzelm@38370
   156
      if (documents.isDefinedAt(id) || disposed(id)) fail
wenzelm@38370
   157
      copy(documents = documents + (id -> document),
wenzelm@38370
   158
        assignments = assignments + (document -> new State.Assignment(former_assignment)))
wenzelm@38370
   159
    }
wenzelm@38370
   160
wenzelm@38373
   161
    def define_command(command: Command): State =
wenzelm@38373
   162
    {
wenzelm@38373
   163
      val id = command.id
wenzelm@38373
   164
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   165
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   166
    }
wenzelm@38373
   167
wenzelm@38370
   168
    def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
wenzelm@38373
   169
wenzelm@38373
   170
    def the_document(id: Version_ID): Document = documents.getOrElse(id, fail)
wenzelm@38370
   171
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@38370
   172
    def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
wenzelm@38370
   173
    def the_assignment(document: Document): State.Assignment = assignments.getOrElse(document, fail)
wenzelm@38370
   174
wenzelm@38370
   175
    def accumulate(id: ID, message: XML.Tree): (Command.State, State) =
wenzelm@38370
   176
      execs.get(id) match {
wenzelm@38370
   177
        case Some((st, docs)) =>
wenzelm@38370
   178
          val new_st = st.accumulate(message)
wenzelm@38370
   179
          (new_st, copy(execs = execs + (id -> (new_st, docs))))
wenzelm@38370
   180
        case None =>
wenzelm@38370
   181
          commands.get(id) match {
wenzelm@38370
   182
            case Some(st) =>
wenzelm@38370
   183
              val new_st = st.accumulate(message)
wenzelm@38370
   184
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   185
            case None => fail
wenzelm@38370
   186
          }
wenzelm@38370
   187
      }
wenzelm@38370
   188
wenzelm@38370
   189
    def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): State =
wenzelm@38370
   190
    {
wenzelm@38370
   191
      val doc = the_document(id)
wenzelm@38370
   192
      val docs = Set(doc)  // FIXME unused (!?)
wenzelm@38370
   193
wenzelm@38370
   194
      var new_execs = execs
wenzelm@38370
   195
      val assigned_execs =
wenzelm@38370
   196
        for ((cmd_id, exec_id) <- edits) yield {
wenzelm@38370
   197
          val st = the_command(cmd_id)
wenzelm@38370
   198
          if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
wenzelm@38370
   199
          new_execs += (exec_id -> (st, docs))
wenzelm@38370
   200
          (st.command, exec_id)
wenzelm@38370
   201
        }
wenzelm@38370
   202
      the_assignment(doc).assign(assigned_execs)  // FIXME explicit value instead of promise (!?)
wenzelm@38370
   203
      copy(execs = new_execs)
wenzelm@38370
   204
    }
wenzelm@38370
   205
wenzelm@38370
   206
    def is_assigned(document: Document): Boolean =
wenzelm@38370
   207
      assignments.get(document) match {
wenzelm@38370
   208
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   209
        case None => false
wenzelm@38370
   210
      }
wenzelm@38370
   211
wenzelm@38370
   212
    def command_state(document: Document, command: Command): Command.State =
wenzelm@38370
   213
    {
wenzelm@38370
   214
      val assgn = the_assignment(document)
wenzelm@38370
   215
      require(assgn.is_finished)
wenzelm@38370
   216
      the_exec_state(assgn.join.getOrElse(command, fail))
wenzelm@38150
   217
    }
wenzelm@34485
   218
  }
wenzelm@34840
   219
}
wenzelm@34840
   220
wenzelm@34855
   221
wenzelm@34840
   222
class Document(
wenzelm@38150
   223
    val id: Document.Version_ID,
wenzelm@38370
   224
    val nodes: Map[String, Document.Node])
wenzelm@34840
   225