src/Tools/jEdit/src/proofdocument/command.scala
author wenzelm
Wed, 30 Dec 2009 17:48:58 +0100
changeset 34813 f0107bc96961
parent 34778 8eccd35e975e
child 34815 6bae73cd8e33
permissions -rw-r--r--
more explicit modeling of Command and Command_State as Session.Entity; misc tuning;
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
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
     8
package isabelle.proofdocument
34318
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
34699
wenzelm
parents: 34698
diff changeset
    11
import scala.actors.Actor, Actor._
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    12
34497
184fda8cce04 more explicit indication of mutable collections;
wenzelm
parents: 34495
diff changeset
    13
import scala.collection.mutable
34486
7985efd78aa1 tuned handling of accumulated results;
wenzelm
parents: 34485
diff changeset
    14
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents: 34769
diff changeset
    15
import isabelle.jedit.Isabelle
34476
e2b1fb731241 tuned import;
wenzelm
parents: 34458
diff changeset
    16
import isabelle.XML
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    17
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    18
34675
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    19
trait Accumulator extends Actor
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    20
{
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    21
  start() // start actor
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    22
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    23
  protected var _state: State
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    24
  def state = _state
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    25
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    26
  override def act() {
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    27
    loop {
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    28
      react {
34777
91d6089cef88 class Session models full session, with or without prover process (cf. heaps, browser_info);
wenzelm
parents: 34769
diff changeset
    29
        case (session: Session, message: XML.Tree) => _state = _state.+(session, message)
34769
826525fc5285 more precise messages;
wenzelm
parents: 34760
diff changeset
    30
        case bad => System.err.println("Accumulator: ignoring bad message " + bad)
34675
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    31
      }
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    32
    }
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    33
  }
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    34
}
5427df0f6bcb trait Accumulator;
immler@in.tum.de
parents: 34674
diff changeset
    35
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    36
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    37
object Command
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    38
{
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    39
  object Status extends Enumeration
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    40
  {
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    41
    val UNPROCESSED = Value("UNPROCESSED")
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    42
    val FINISHED = Value("FINISHED")
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    43
    val FAILED = Value("FAILED")
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    44
  }
34707
cc5d388fcbf2 eliminated MarkupInfo, moved particular variants into object Command;
wenzelm
parents: 34705
diff changeset
    45
cc5d388fcbf2 eliminated MarkupInfo, moved particular variants into object Command;
wenzelm
parents: 34705
diff changeset
    46
  case class HighlightInfo(highlight: String) { override def toString = highlight }
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
    47
  case class TypeInfo(ty: String)
34707
cc5d388fcbf2 eliminated MarkupInfo, moved particular variants into object Command;
wenzelm
parents: 34705
diff changeset
    48
  case class RefInfo(file: Option[String], line: Option[Int],
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
    49
    command_id: Option[String], offset: Option[Int])
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    50
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    51
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    52
34697
3d4874198e62 State: immutable;
wenzelm
parents: 34688
diff changeset
    53
class Command(
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
    54
    val id: Isar_Document.Command_ID,
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
    55
    val tokens: List[Token],
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
    56
    val starts: Map[Token, Int]) extends Accumulator with Session.Entity
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    57
{
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    58
  require(!tokens.isEmpty)
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    59
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
    60
  // FIXME override equality as well
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34637
diff changeset
    61
  override def hashCode = id.hashCode
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    62
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
    63
  def consume(session: Session, message: XML.Tree) { this ! (session, message) }
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
    64
34674
f9b71bcf2eb7 Command notifies changes
immler@in.tum.de
parents: 34662
diff changeset
    65
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    66
  /* content */
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    67
34495
722533c532da Command: added name field and toString;
wenzelm
parents: 34491
diff changeset
    68
  override def toString = name
722533c532da Command: added name field and toString;
wenzelm
parents: 34491
diff changeset
    69
34526
b504abb6eff6 tokens and commands as lists
immler@in.tum.de
parents: 34511
diff changeset
    70
  val name = tokens.head.content
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34577
diff changeset
    71
  val content: String = Token.string_from_tokens(tokens, starts)
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
    72
  def content(i: Int, j: Int): String = content.substring(i, j)
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    73
  val symbol_index = new Symbol.Index(content)
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    74
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
    75
  def start(doc: Proof_Document) = doc.token_start(tokens.first)
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
    76
  def stop(doc: Proof_Document) = doc.token_start(tokens.last) + tokens.last.length
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    77
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34577
diff changeset
    78
  def contains(p: Token) = tokens.contains(p)
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34577
diff changeset
    79
34697
3d4874198e62 State: immutable;
wenzelm
parents: 34688
diff changeset
    80
  protected override var _state = new State(this)
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    81
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    82
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    83
  /* markup */
34508
422a43b76f77 eliminated Command.Status.REMOVE/REMOVED;
wenzelm
parents: 34500
diff changeset
    84
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
    85
  lazy val empty_markup = new Markup_Text(Nil, content)
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    86
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
    87
  def markup_node(begin: Int, end: Int, info: Any): Markup_Tree =
34699
wenzelm
parents: 34698
diff changeset
    88
  {
34703
ff037c17332a minor tuning;
wenzelm
parents: 34699
diff changeset
    89
    val start = symbol_index.decode(begin)
ff037c17332a minor tuning;
wenzelm
parents: 34699
diff changeset
    90
    val stop = symbol_index.decode(end)
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
    91
    new Markup_Tree(new Markup_Node(start, stop, info), Nil)
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    92
  }
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    93
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    94
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    95
  /* results, markup, ... */
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    96
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
    97
  private val empty_cmd_state = new Command_State("", this)  // FIXME ?
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
    98
  private def cmd_state(doc: Proof_Document) =  // FIXME clarify
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    99
    doc.states.getOrElse(this, empty_cmd_state)
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
   100
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   101
  def status(doc: Proof_Document) = cmd_state(doc).state.status
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   102
  def results(doc: Proof_Document) = cmd_state(doc).results
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   103
  def markup_root(doc: Proof_Document) = cmd_state(doc).markup_root
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   104
  def highlight(doc: Proof_Document) = cmd_state(doc).highlight
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   105
  def type_at(doc: Proof_Document, offset: Int) = cmd_state(doc).type_at(offset)
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
   106
  def ref_at(doc: Proof_Document, offset: Int) = cmd_state(doc).ref_at(offset)
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
   107
}
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
   108
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
   109
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
   110
class Command_State(
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
   111
  override val id: Isar_Document.State_ID,
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
   112
  val command: Command) extends Accumulator with Session.Entity
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
   113
{
34698
wenzelm
parents: 34697
diff changeset
   114
  protected override var _state = new State(command)
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
   115
34813
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
   116
  def consume(session: Session, message: XML.Tree) { this ! (session, message) }
f0107bc96961 more explicit modeling of Command and Command_State as Session.Entity;
wenzelm
parents: 34778
diff changeset
   117
34746
94ef0ff39c21 plain results, no markup here;
wenzelm
parents: 34744
diff changeset
   118
  def results: List[XML.Tree] =
94ef0ff39c21 plain results, no markup here;
wenzelm
parents: 34744
diff changeset
   119
    command.state.results ::: state.results
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
   120
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
   121
  def markup_root: Markup_Text =
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
   122
    (command.state.markup_root /: state.markup_root.markup)(_ + _)
34557
647a2430d1cd immutable markup-nodes;
immler@in.tum.de
parents: 34556
diff changeset
   123
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
   124
  def type_at(pos: Int): Option[String] = state.type_at(pos)
34556
09a5984250a2 seperate node for syntax-highlighting
immler@in.tum.de
parents: 34555
diff changeset
   125
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
   126
  def ref_at(pos: Int): Option[Markup_Node] = state.ref_at(pos)
34568
b517d0607297 implemented IsabelleHyperlinkSource (only links inside the current buffer)
immler@in.tum.de
parents: 34564
diff changeset
   127
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
   128
  def highlight: Markup_Text =
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
   129
    (command.state.highlight /: state.highlight.markup)(_ + _)
34688
1310dc269b4a lazy fields
immler@in.tum.de
parents: 34676
diff changeset
   130
}