src/Tools/jEdit/src/prover/Command.scala
author immler@in.tum.de
Thu, 27 Aug 2009 10:51:09 +0200
changeset 34674 f9b71bcf2eb7
parent 34662 ab54955c9eea
child 34675 5427df0f6bcb
permissions -rw-r--r--
Command notifies changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34401
diff changeset
     1
/*
aad6834ba380 added some headers and comments;
wenzelm
parents: 34401
diff changeset
     2
 * Prover commands with semantic state
aad6834ba380 added some headers and comments;
wenzelm
parents: 34401
diff changeset
     3
 *
aad6834ba380 added some headers and comments;
wenzelm
parents: 34401
diff changeset
     4
 * @author Johannes Hölzl, TU Munich
aad6834ba380 added some headers and comments;
wenzelm
parents: 34401
diff changeset
     5
 * @author Fabian Immler, TU Munich
aad6834ba380 added some headers and comments;
wenzelm
parents: 34401
diff changeset
     6
 */
aad6834ba380 added some headers and comments;
wenzelm
parents: 34401
diff changeset
     7
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     8
package isabelle.prover
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     9
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    10
34674
f9b71bcf2eb7 Command notifies changes
immler@in.tum.de
parents: 34662
diff changeset
    11
import scala.actors.Actor
f9b71bcf2eb7 Command notifies changes
immler@in.tum.de
parents: 34662
diff changeset
    12
import scala.actors.Actor._
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    13
34497
184fda8cce04 more explicit indication of mutable collections;
wenzelm
parents: 34495
diff changeset
    14
import scala.collection.mutable
34486
7985efd78aa1 tuned handling of accumulated results;
wenzelm
parents: 34485
diff changeset
    15
34662
ab54955c9eea Text is not present any more
immler@in.tum.de
parents: 34658
diff changeset
    16
import isabelle.proofdocument.{Token, ProofDocument}
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    17
import isabelle.jedit.{Isabelle, Plugin}
34476
e2b1fb731241 tuned import;
wenzelm
parents: 34458
diff changeset
    18
import isabelle.XML
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    19
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    20
import sidekick.{SideKickParsedData, IAsset}
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    21
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    22
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    23
object Command
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    24
{
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    25
  object Status extends Enumeration
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    26
  {
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    27
    val UNPROCESSED = Value("UNPROCESSED")
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    28
    val FINISHED = Value("FINISHED")
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    29
    val FAILED = Value("FAILED")
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    30
  }
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
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    33
34674
f9b71bcf2eb7 Command notifies changes
immler@in.tum.de
parents: 34662
diff changeset
    34
class Command(val tokens: List[Token], val starts: Map[Token, Int], chg_rec: Actor)
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    35
{
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    36
  require(!tokens.isEmpty)
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    37
34603
83a37e3b8c9c produce ids via Isabelle.system (http://isabelle.in.tum.de/repos/isabelle/rev/c23663825e23);
wenzelm
parents: 34597
diff changeset
    38
  val id = Isabelle.system.id()
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    39
  override def hashCode = id.hashCode
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    40
34674
f9b71bcf2eb7 Command notifies changes
immler@in.tum.de
parents: 34662
diff changeset
    41
  def changed() = chg_rec ! this
f9b71bcf2eb7 Command notifies changes
immler@in.tum.de
parents: 34662
diff changeset
    42
f9b71bcf2eb7 Command notifies changes
immler@in.tum.de
parents: 34662
diff changeset
    43
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    44
  /* content */
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    45
34495
722533c532da Command: added name field and toString;
wenzelm
parents: 34491
diff changeset
    46
  override def toString = name
722533c532da Command: added name field and toString;
wenzelm
parents: 34491
diff changeset
    47
34526
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34511
diff changeset
    48
  val name = tokens.head.content
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
    49
  val content: String = Token.string_from_tokens(tokens, starts)
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    50
  val symbol_index = new Symbol.Index(content)
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    51
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34539
diff changeset
    52
  def start(doc: ProofDocument) = doc.token_start(tokens.first)
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34539
diff changeset
    53
  def stop(doc: ProofDocument) = doc.token_start(tokens.last) + tokens.last.length
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    54
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34577
diff changeset
    55
  def contains(p: Token) = tokens.contains(p)
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34577
diff changeset
    56
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    57
  /* states */
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    58
  val states = mutable.Map[IsarDocument.State_ID, Command_State]()
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    59
  private def state(doc: ProofDocument) = doc.states.get(this)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    60
  
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    61
  /* command status */
34508
422a43b76f77 eliminated Command.Status.REMOVE/REMOVED;
wenzelm
parents: 34500
diff changeset
    62
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    63
  def set_status(state: IsarDocument.State_ID, status: Command.Status.Value) = {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    64
    if (state != null)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    65
      states.getOrElseUpdate(state, new Command_State(this)).status = status
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    66
  }
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    67
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    68
  def status(doc: ProofDocument) =
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    69
    state(doc) match {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    70
      case Some(s) => states.getOrElseUpdate(s, new Command_State(this)).status
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    71
      case _ => Command.Status.UNPROCESSED
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    72
    }
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    73
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    74
  /* results */
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    75
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    76
  private val results = new mutable.ListBuffer[XML.Tree]
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    77
  def add_result(state: IsarDocument.State_ID, tree: XML.Tree) = synchronized {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    78
    (if (state == null) results else states(state).results) += tree
34508
422a43b76f77 eliminated Command.Status.REMOVE/REMOVED;
wenzelm
parents: 34500
diff changeset
    79
  }
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    80
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    81
  def result_document(doc: ProofDocument) = {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    82
    val state_results = state(doc) match {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    83
      case Some(s) =>
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    84
        states.getOrElseUpdate(s, new Command_State(this)).results
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    85
      case _ => Nil}
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    86
    XML.document(
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    87
      results.toList ::: state_results.toList match {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    88
        case Nil => XML.Elem("message", Nil, Nil)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    89
        case List(elem) => elem
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    90
        case elems => XML.Elem("messages", Nil, elems)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    91
      }, "style")
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    92
  }
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    93
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    94
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    95
  /* markup */
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    96
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34556
diff changeset
    97
  val empty_root_node =
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
    98
    new MarkupNode(this, 0, starts(tokens.last) - starts(tokens.first) + tokens.last.length,
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
    99
      Nil, id, content, RootInfo())
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   100
  private var _markup_root = empty_root_node
34656
2740439a86b4 decode offsets with respect to symbols
immler@in.tum.de
parents: 34653
diff changeset
   101
  def add_markup(state: IsarDocument.State_ID, raw_node: MarkupNode) = {
2740439a86b4 decode offsets with respect to symbols
immler@in.tum.de
parents: 34653
diff changeset
   102
    // decode node
2740439a86b4 decode offsets with respect to symbols
immler@in.tum.de
parents: 34653
diff changeset
   103
    val node = raw_node transform symbol_index.decode
34658
3b05426b9318 avoid internal "add" method;
wenzelm
parents: 34656
diff changeset
   104
    if (state == null) _markup_root += node
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   105
    else {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   106
      val cmd_state = states.getOrElseUpdate(state, new Command_State(this))
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   107
      cmd_state.markup_root += node
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   108
    }
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   109
  }
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34556
diff changeset
   110
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   111
  def markup_root(doc: ProofDocument): MarkupNode = {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   112
    state(doc) match {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   113
      case Some(s) =>
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   114
        (_markup_root /: states(s).markup_root.children) (_ + _)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   115
      case _ => _markup_root
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   116
    }
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   117
  }
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
   118
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   119
  def highlight_node(doc: ProofDocument): MarkupNode =
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
   120
  {
34560
08f0d81c6833 reduced to one markup-tree
immler@in.tum.de
parents: 34557
diff changeset
   121
    import MarkupNode._
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   122
    markup_root(doc).filter(_.info match {
34564
850dc36d4926 let MarkupNode carry arbitrary information
immler@in.tum.de
parents: 34562
diff changeset
   123
      case RootInfo() | OuterInfo(_) | HighlightInfo(_) => true
34560
08f0d81c6833 reduced to one markup-tree
immler@in.tum.de
parents: 34557
diff changeset
   124
      case _ => false
08f0d81c6833 reduced to one markup-tree
immler@in.tum.de
parents: 34557
diff changeset
   125
    }).head
08f0d81c6833 reduced to one markup-tree
immler@in.tum.de
parents: 34557
diff changeset
   126
  }
34556
09a5984250a2 seperate node for syntax-highlighting
immler@in.tum.de
parents: 34555
diff changeset
   127
34564
850dc36d4926 let MarkupNode carry arbitrary information
immler@in.tum.de
parents: 34562
diff changeset
   128
  def markup_node(begin: Int, end: Int, info: MarkupInfo) =
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34556
diff changeset
   129
    new MarkupNode(this, begin, end, Nil, id,
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
   130
      if (end <= content.length && begin >= 0) content.substring(begin, end)
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
   131
      else "wrong indices??",
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
   132
      info)
34556
09a5984250a2 seperate node for syntax-highlighting
immler@in.tum.de
parents: 34555
diff changeset
   133
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   134
  def type_at(doc: ProofDocument, pos: Int) =
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   135
    state(doc).map(states(_).type_at(pos)).getOrElse(null)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   136
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   137
  def ref_at(doc: ProofDocument, pos: Int) =
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   138
    state(doc).flatMap(states(_).ref_at(pos))
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   139
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   140
}
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   141
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   142
class Command_State(val cmd: Command) {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   143
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   144
  var status = Command.Status.UNPROCESSED
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   145
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   146
  /* results */
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   147
  val results = new mutable.ListBuffer[XML.Tree]
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   148
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   149
  /* markup */
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   150
  val empty_root_node = cmd.empty_root_node
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   151
  var markup_root = empty_root_node
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
   152
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
   153
  def type_at(pos: Int): String =
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
   154
  {
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
   155
    val types = markup_root.filter(_.info match { case TypeInfo(_) => true case _ => false })
34562
cdf914c78ff2 ML types in tooltip
immler@in.tum.de
parents: 34560
diff changeset
   156
    types.flatten(_.flatten).
cdf914c78ff2 ML types in tooltip
immler@in.tum.de
parents: 34560
diff changeset
   157
      find(t => t.start <= pos && t.stop > pos).
34586
fc5df4a6561b type_at: no quotes;
wenzelm
parents: 34582
diff changeset
   158
      map(t => t.content + ": " + (t.info match { case TypeInfo(i) => i case _ => "" })).
34562
cdf914c78ff2 ML types in tooltip
immler@in.tum.de
parents: 34560
diff changeset
   159
      getOrElse(null)
cdf914c78ff2 ML types in tooltip
immler@in.tum.de
parents: 34560
diff changeset
   160
  }
34568
b517d0607297 implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents: 34564
diff changeset
   161
b517d0607297 implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents: 34564
diff changeset
   162
  def ref_at(pos: Int): Option[MarkupNode] =
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
   163
    markup_root.filter(_.info match { case RefInfo(_, _, _, _) => true case _ => false }).
34568
b517d0607297 implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents: 34564
diff changeset
   164
      flatten(_.flatten).
b517d0607297 implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents: 34564
diff changeset
   165
      find(t => t.start <= pos && t.stop > pos)
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
   166
}