src/Tools/jEdit/src/proofdocument/document.scala
author wenzelm
Wed, 06 Jan 2010 23:46:00 +0100
changeset 34840 6c5560d48561
parent 34838 08a72dc4868e
child 34853 32b49207ca20
permissions -rw-r--r--
more precise treatment of document/state assigment;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34390
diff changeset
     1
/*
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
     2
 * Document as list of commands, consisting of lists of tokens
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34390
diff changeset
     3
 *
aad6834ba380 added some headers and comments;
wenzelm
parents: 34390
diff changeset
     4
 * @author Johannes Hölzl, TU Munich
34532
aaafe9c4180b ProofDocument without state
immler@in.tum.de
parents: 34531
diff changeset
     5
 * @author Fabian Immler, TU Munich
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
     6
 * @author Makarius
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34390
diff changeset
     7
 */
aad6834ba380 added some headers and comments;
wenzelm
parents: 34390
diff changeset
     8
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     9
package isabelle.proofdocument
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    10
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
    11
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
    12
import scala.actors.Actor._
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    13
import scala.collection.mutable
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
    14
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    15
import java.util.regex.Pattern
34703
ff037c17332a minor tuning;
wenzelm
parents: 34693
diff changeset
    16
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    17
34823
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34819
diff changeset
    18
object Document
34483
0923926022d7 superficial tuning;
wenzelm
parents: 34482
diff changeset
    19
{
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
    20
  // Be careful when changing this regex. Not only must it handle the
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
    21
  // spurious end of a token but also:
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    22
  // Bug ID: 5050507 Pattern.matches throws StackOverflow Error
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    23
  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
    24
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
    25
  val token_pattern =
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    26
    Pattern.compile(
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    27
      "\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" +
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    28
      "\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" +
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
    29
      "(\\?'?|')[A-Za-z_0-9.]*|" +
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
    30
      "[A-Za-z_0-9.]+|" +
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    31
      "[!#$%&*+-/<=>?@^_|~]+|" +
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    32
      "\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" +
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    33
      "`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" +
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    34
      "[()\\[\\]{}:;]", Pattern.MULTILINE)
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
    35
34823
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34819
diff changeset
    36
  def empty(id: Isar_Document.Document_ID): Document =
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34832
diff changeset
    37
  {
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34832
diff changeset
    38
    val doc = new Document(id, Linear_Set(), Map(), Linear_Set(), Map())
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34832
diff changeset
    39
    doc.assign_states(Nil)
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34832
diff changeset
    40
    doc
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34832
diff changeset
    41
  }
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
    42
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    43
  type Structure_Edit = (Option[Command], Option[Command])
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    44
  type Structure_Change = List[Structure_Edit]
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    45
  type Result = (Document, List[Structure_Edit])
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    46
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    47
  def text_edits(session: Session, old_doc: Document, new_id: Isar_Document.Document_ID,
34838
08a72dc4868e use Text_Edit provided by Isabelle;
wenzelm
parents: 34835
diff changeset
    48
    edits: List[Text_Edit]): Result =
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    49
  {
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    50
    require(old_doc.assignment.is_finished)
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    51
    val doc0 =
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    52
      Document_Body(old_doc.tokens, old_doc.token_start, old_doc.commands, old_doc.assignment.join)
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    53
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    54
    val changes = new mutable.ListBuffer[Structure_Edit]
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    55
    val doc = (doc0 /: edits)((doc1: Document_Body, edit: Text_Edit) =>
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    56
      {
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    57
        val (doc2, chgs) = doc1.text_edit(session, edit)
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    58
        changes ++ chgs
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    59
        doc2
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    60
      })
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    61
    val new_doc = new Document(new_id, doc.tokens, doc.token_start, doc.commands, doc.states)
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    62
    (new_doc, changes.toList)
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
    63
  }
34778
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34760
diff changeset
    64
}
34538
20bfcca24658 Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents: 34532
diff changeset
    65
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    66
private case class Document_Body(
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    67
  val tokens: Linear_Set[Token],   // FIXME plain List, inside Command
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    68
  val token_start: Map[Token, Int],  // FIXME eliminate
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    69
  val commands: Linear_Set[Command],
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    70
  val states: Map[Command, Command])
34483
0923926022d7 superficial tuning;
wenzelm
parents: 34482
diff changeset
    71
{
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    72
  /* token view */
34835
67733fd0e3fa back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents: 34832
diff changeset
    73
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
    74
  def text_edit(session: Session, e: Text_Edit): (Document_Body, List[Document.Structure_Edit]) =
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
    75
  {
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
    76
    case class TextChange(start: Int, added: String, removed: String)
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
    77
    val change = e match {
34838
08a72dc4868e use Text_Edit provided by Isabelle;
wenzelm
parents: 34835
diff changeset
    78
      case Text_Edit.Insert(s, a) => TextChange(s, a, "")
08a72dc4868e use Text_Edit provided by Isabelle;
wenzelm
parents: 34835
diff changeset
    79
      case Text_Edit.Remove(s, r) => TextChange(s, "", r)
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
    80
    }
34551
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
    81
    //indices of tokens
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
    82
    var start: Map[Token, Int] = token_start
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
    83
    def stop(t: Token) = start(t) + t.length
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
    84
    // split old token lists
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
    85
    val tokens = Nil ++ this.tokens
34551
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
    86
    val (begin, remaining) = tokens.span(stop(_) < change.start)
34648
8213a350fd45 remember removed text
immler@in.tum.de
parents: 34603
diff changeset
    87
    val (removed, end) = remaining.span(token_start(_) <= change.start + change.removed.length)
34551
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
    88
    // update indices
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
    89
    start = end.foldLeft(start)((s, t) =>
34648
8213a350fd45 remember removed text
immler@in.tum.de
parents: 34603
diff changeset
    90
      s + (t -> (s(t) + change.added.length - change.removed.length)))
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
    91
34551
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
    92
    val split_begin = removed.takeWhile(start(_) < change.start).
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
    93
      map (t => {
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
    94
          val split_tok = new Token(t.content.substring(0, change.start - start(t)), t.kind)
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
    95
          start += (split_tok -> start(t))
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
    96
          split_tok
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
    97
        })
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
    98
34648
8213a350fd45 remember removed text
immler@in.tum.de
parents: 34603
diff changeset
    99
    val split_end = removed.dropWhile(stop(_) < change.start + change.removed.length).
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   100
      map (t => {
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   101
          val split_tok =
34648
8213a350fd45 remember removed text
immler@in.tum.de
parents: 34603
diff changeset
   102
            new Token(t.content.substring(change.start + change.removed.length - start(t)), t.kind)
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   103
          start += (split_tok -> start(t))
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   104
          split_tok
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   105
        })
34551
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
   106
    // update indices
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   107
    start = removed.foldLeft (start) ((s, t) => s - t)
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   108
    start = split_end.foldLeft (start) ((s, t) =>
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   109
    s + (t -> (change.start + change.added.length)))
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   110
34551
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
   111
    val ins = new Token(change.added, Token.Kind.OTHER)
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
   112
    start += (ins -> change.start)
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
   113
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   114
    var invalid_tokens = split_begin ::: ins :: split_end ::: end
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   115
    var new_tokens: List[Token] = Nil
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   116
    var old_suffix: List[Token] = Nil
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   117
34551
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
   118
    val match_start = invalid_tokens.firstOption.map(start(_)).getOrElse(0)
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   119
    val matcher =
34823
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34819
diff changeset
   120
      Document.token_pattern.matcher(Token.string_from_tokens(invalid_tokens, start))
34526
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   121
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   122
    while (matcher.find() && invalid_tokens != Nil) {
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   123
			val kind =
34819
86cb7f8e5a0d tuned signature;
wenzelm
parents: 34818
diff changeset
   124
        if (session.current_syntax.is_command(matcher.group))
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   125
          Token.Kind.COMMAND_START
34494
47f9303db81d misc tuning -- de-camelization;
wenzelm
parents: 34491
diff changeset
   126
        else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*")
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   127
          Token.Kind.COMMENT
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   128
        else
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   129
          Token.Kind.OTHER
34551
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
   130
      val new_token = new Token(matcher.group, kind)
bd2b8fde9e25 incomplete changes of immutable tokens and commands
immler@in.tum.de
parents: 34550
diff changeset
   131
      start += (new_token -> (match_start + matcher.start))
34526
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   132
      new_tokens ::= new_token
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   133
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
   134
      invalid_tokens = invalid_tokens dropWhile (stop(_) < stop(new_token))
34526
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   135
      invalid_tokens match {
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   136
        case t :: ts =>
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   137
          if (start(t) == start(new_token) &&
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   138
              start(t) > change.start + change.added.length) {
34597
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   139
          old_suffix = t :: ts
34592
b17ebec3690c ignore unchanged tokens
immler@in.tum.de
parents: 34575
diff changeset
   140
          new_tokens = new_tokens.tail
34526
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   141
          invalid_tokens = Nil
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   142
        }
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   143
        case _ =>
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   144
      }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   145
    }
34526
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   146
    val insert = new_tokens.reverse
34544
56217d219e27 proofdocument-versions get id from changes
immler@in.tum.de
parents: 34541
diff changeset
   147
    val new_token_list = begin ::: insert ::: old_suffix
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   148
    token_changed(session, begin.lastOption, insert,
34597
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   149
      old_suffix.firstOption, new_token_list, start)
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   150
  }
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   151
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
   152
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   153
  /* command view */
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   154
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   155
  private def token_changed(
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
   156
      session: Session,
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
   157
      before_change: Option[Token],
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
   158
      inserted_tokens: List[Token],
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
   159
      after_change: Option[Token],
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
   160
      new_tokens: List[Token],
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   161
      new_token_start: Map[Token, Int]): (Document_Body, Document.Structure_Change) =
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   162
  {
34689
810bf0b27bcb use Linear_Set from Isabelle/Pure.jar;
wenzelm
parents: 34676
diff changeset
   163
    val new_tokenset = Linear_Set[Token]() ++ new_tokens
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   164
    val cmd_before_change = before_change match {
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   165
      case None => None
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   166
      case Some(bc) =>
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   167
        val cmd_with_bc = commands.find(_.contains(bc)).get
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   168
        if (cmd_with_bc.tokens.last == bc) {
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   169
          if (new_tokenset.next(bc).map(_.is_start).getOrElse(true))
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   170
            Some(cmd_with_bc)
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   171
          else commands.prev(cmd_with_bc)
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   172
        }
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   173
        else commands.prev(cmd_with_bc)
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   174
    }
34544
56217d219e27 proofdocument-versions get id from changes
immler@in.tum.de
parents: 34541
diff changeset
   175
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   176
    val cmd_after_change = after_change match {
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   177
      case None => None
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   178
      case Some(ac) =>
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   179
        val cmd_with_ac = commands.find(_.contains(ac)).get
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   180
        if (ac.is_start)
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   181
          Some(cmd_with_ac)
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   182
        else
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   183
          commands.next(cmd_with_ac)
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   184
    }
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   185
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   186
    val removed_commands = commands.dropWhile(Some(_) != cmd_before_change).drop(1).
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   187
      takeWhile(Some(_) != cmd_after_change)
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   188
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   189
    // calculate inserted commands
34526
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   190
    def tokens_to_commands(tokens: List[Token]): List[Command]= {
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   191
      tokens match {
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34516
diff changeset
   192
        case Nil => Nil
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   193
        case t :: ts =>
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   194
          val (cmd, rest) =
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   195
            ts.span(t => t.kind != Token.Kind.COMMAND_START && t.kind != Token.Kind.COMMENT)
34778
8eccd35e975e removed unused Session.prover_logic;
wenzelm
parents: 34760
diff changeset
   196
          new Command(session.create_id(), t :: cmd, new_token_start) :: tokens_to_commands(rest)
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   197
      }
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   198
    }
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   199
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   200
    val split_begin =
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   201
      if (before_change.isDefined) {
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   202
        val changed =
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   203
          if (cmd_before_change.isDefined)
34595
0e0e08aaddb5 lists work faster here
immler@in.tum.de
parents: 34594
diff changeset
   204
            new_tokens.dropWhile(_ != cmd_before_change.get.tokens.last).drop(1)
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   205
          else new_tokenset
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   206
        if (changed.exists(_ == before_change.get))
34597
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   207
          changed.takeWhile(_ != before_change.get).toList :::
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   208
            List(before_change.get)
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   209
        else Nil
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   210
      } else Nil
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   211
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   212
    val split_end =
34667
3f20110dfe2f fixed special case;
immler@in.tum.de
parents: 34660
diff changeset
   213
      if (after_change.isDefined) {
34595
0e0e08aaddb5 lists work faster here
immler@in.tum.de
parents: 34594
diff changeset
   214
        val unchanged = new_tokens.dropWhile(_ != after_change.get)
34667
3f20110dfe2f fixed special case;
immler@in.tum.de
parents: 34660
diff changeset
   215
        if(cmd_after_change.isDefined) {
3f20110dfe2f fixed special case;
immler@in.tum.de
parents: 34660
diff changeset
   216
          if (unchanged.exists(_ == cmd_after_change.get.tokens.first))
3f20110dfe2f fixed special case;
immler@in.tum.de
parents: 34660
diff changeset
   217
            unchanged.takeWhile(_ != cmd_after_change.get.tokens.first).toList
3f20110dfe2f fixed special case;
immler@in.tum.de
parents: 34660
diff changeset
   218
          else Nil
3f20110dfe2f fixed special case;
immler@in.tum.de
parents: 34660
diff changeset
   219
        } else {
3f20110dfe2f fixed special case;
immler@in.tum.de
parents: 34660
diff changeset
   220
          unchanged
3f20110dfe2f fixed special case;
immler@in.tum.de
parents: 34660
diff changeset
   221
        }
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   222
      } else Nil
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   223
34597
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   224
    val rescan_begin =
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   225
      split_begin :::
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   226
        before_change.map(bc => new_tokens.dropWhile(_ != bc).drop(1)).getOrElse(new_tokens)
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34575
diff changeset
   227
    val rescanning_tokens =
34597
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   228
      after_change.map(ac => rescan_begin.takeWhile(_ != ac)).getOrElse(rescan_begin) :::
a0c84b0edb9a merged; resolved superficial conflicts
immler@in.tum.de
parents: 34582 34596
diff changeset
   229
        split_end
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34592
diff changeset
   230
    val inserted_commands = tokens_to_commands(rescanning_tokens.toList)
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   231
34550
171c8c6e5707 prepared proofdocument for only needed changes
immler@in.tum.de
parents: 34544
diff changeset
   232
    // build new document
34739
34b0aadab7ee Linear_Set.append_after;
wenzelm
parents: 34724
diff changeset
   233
    val new_commandset = commands.
34b0aadab7ee Linear_Set.append_after;
wenzelm
parents: 34724
diff changeset
   234
      delete_between(cmd_before_change, cmd_after_change).
34b0aadab7ee Linear_Set.append_after;
wenzelm
parents: 34724
diff changeset
   235
      append_after(cmd_before_change, inserted_commands)
34b0aadab7ee Linear_Set.append_after;
wenzelm
parents: 34724
diff changeset
   236
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34551
diff changeset
   237
34544
56217d219e27 proofdocument-versions get id from changes
immler@in.tum.de
parents: 34541
diff changeset
   238
    val doc =
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   239
      new Document_Body(new_tokenset, new_token_start, new_commandset, states -- removed_commands)
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
   240
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
   241
    val removes =
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
   242
      for (cmd <- removed_commands) yield (cmd_before_change -> None)
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
   243
    val inserts =
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
   244
      for (cmd <- inserted_commands) yield (doc.commands.prev(cmd) -> Some(cmd))
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
   245
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   246
    (doc, removes.toList ++ inserts)
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   247
  }
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   248
}
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   249
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   250
class Document(
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   251
    val id: Isar_Document.Document_ID,
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   252
    val tokens: Linear_Set[Token],   // FIXME plain List, inside Command
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   253
    val token_start: Map[Token, Int],  // FIXME eliminate
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   254
    val commands: Linear_Set[Command],
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   255
    old_states: Map[Command, Command])
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   256
{
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   257
  def content = Token.string_from_tokens(Nil ++ tokens, token_start)
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   258
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   259
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   260
  /* command/state assignment */
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   261
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   262
  val assignment = Future.promise[Map[Command, Command]]
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   263
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   264
  @volatile private var tmp_states = old_states
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   265
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   266
  def assign_states(new_states: List[(Command, Command)])
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   267
  {
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   268
    assignment.fulfill(tmp_states ++ new_states)
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   269
    tmp_states = Map()
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   270
  }
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   271
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   272
  def current_state(cmd: Command): State =
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   273
  {
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   274
    require(assignment.is_finished)
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   275
    (assignment.join)(cmd).current_state
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   276
  }
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   277
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   278
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   279
  val commands_offsets = {
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   280
    var last_stop = 0
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   281
    (for (c <- commands) yield {
34824
ac35eee85f5c renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents: 34823
diff changeset
   282
      val r = c -> (last_stop, c.stop(this))
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   283
      last_stop = c.stop(this)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   284
      r
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   285
    }).toArray
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   286
  }
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   287
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   288
  def command_at(pos: Int): Option[Command] =
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   289
    find_command(pos, 0, commands_offsets.length)
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   290
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   291
  // use a binary search to find commands for a given offset
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   292
  private def find_command(pos: Int, array_start: Int, array_stop: Int): Option[Command] =
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   293
  {
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   294
    val middle_index = (array_start + array_stop) / 2
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   295
    if (middle_index >= commands_offsets.length) return None
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   296
    val (middle, (start, stop)) = commands_offsets(middle_index)
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   297
    // does middle contain pos?
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   298
    if (start <= pos && pos < stop)
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   299
      Some(middle)
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   300
    else if (start > pos)
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   301
      find_command(pos, array_start, middle_index)
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   302
    else if (stop <= pos)
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   303
      find_command(pos, middle_index + 1, array_stop)
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34703
diff changeset
   304
    else error("impossible")
34596
2b46d92e4642 linearset works faster here
immler@in.tum.de
parents: 34595
diff changeset
   305
  }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   306
}