src/Pure/PIDE/command.scala
author wenzelm
Thu, 12 Aug 2010 13:49:08 +0200
changeset 38360 53224a4d2f0e
parent 38355 8cb265fb12fe
child 38361 b609d0b271fa
permissions -rw-r--r--
specific Session.Commands_Changed;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36676
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     1
/*  Title:      Pure/PIDE/command.scala
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     2
    Author:     Johannes Hölzl, TU Munich
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     3
    Author:     Fabian Immler, TU Munich
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     4
    Author:     Makarius
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     5
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     6
Prover commands with semantic state.
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     7
*/
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34401
diff changeset
     8
34871
e596a0b71f3c incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents: 34865
diff changeset
     9
package isabelle
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    10
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    11
34699
wenzelm
parents: 34698
diff changeset
    12
import scala.actors.Actor, Actor._
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
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    15
34637
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    16
object Command
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    17
{
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    18
  object Status extends Enumeration
f3b5d6e248be added symbol_index (presently unused);
wenzelm
parents: 34603
diff changeset
    19
  {
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    20
    val UNPROCESSED = Value("UNPROCESSED")
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    21
    val FINISHED = Value("FINISHED")
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    22
    val FAILED = Value("FAILED")
36990
449628c148cf explicit Command.Status.UNDEFINED -- avoid fragile/cumbersome treatment of Option[State];
wenzelm
parents: 36676
diff changeset
    23
    val UNDEFINED = Value("UNDEFINED")
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    24
  }
34707
cc5d388fcbf2 eliminated MarkupInfo, moved particular variants into object Command;
wenzelm
parents: 34705
diff changeset
    25
37197
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37189
diff changeset
    26
  case class HighlightInfo(kind: String, sub_kind: Option[String]) {
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37189
diff changeset
    27
    override def toString = kind
953fc4983439 more detailed token markup, including command kind as sub_kind;
wenzelm
parents: 37189
diff changeset
    28
  }
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
    29
  case class TypeInfo(ty: String)
34707
cc5d388fcbf2 eliminated MarkupInfo, moved particular variants into object Command;
wenzelm
parents: 34705
diff changeset
    30
  case class RefInfo(file: Option[String], line: Option[Int],
38355
8cb265fb12fe represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents: 38220
diff changeset
    31
    command_id: Option[Document.Command_ID], offset: Option[Int])  // FIXME Command_ID vs. State_ID !?
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    32
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    33
34697
3d4874198e62 State: immutable;
wenzelm
parents: 34688
diff changeset
    34
class Command(
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37373
diff changeset
    35
    val id: Document.Command_ID,
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    36
    val span: Thy_Syntax.Span,
38220
b30aa2dbedca avoid null in Scala;
wenzelm
parents: 38150
diff changeset
    37
    val static_parent: Option[Command] = None)  // FIXME !?
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    38
  extends Session.Entity
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    39
{
34859
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    40
  /* classification */
34500
384427c750c8 state_results: separate buffer for messages from running command;
wenzelm
parents: 34497
diff changeset
    41
36012
0614676f14d4 replaced some deprecated methods;
wenzelm
parents: 34871
diff changeset
    42
  def is_command: Boolean = !span.isEmpty && span.head.is_command
34865
104298db6abf Outer_Lex.is_ignored;
wenzelm
parents: 34860
diff changeset
    43
  def is_ignored: Boolean = span.forall(_.is_ignored)
34859
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    44
  def is_malformed: Boolean = !is_command && !is_ignored
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    45
36012
0614676f14d4 replaced some deprecated methods;
wenzelm
parents: 34871
diff changeset
    46
  def name: String = if (is_command) span.head.content else ""
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    47
  override def toString =
37373
25078ba44436 tuned Command.toString -- preserving uniqueness allows the Scala toplevel to print Linear_Set[Command] results without crashing;
wenzelm
parents: 37197
diff changeset
    48
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
34495
722533c532da Command: added name field and toString;
wenzelm
parents: 34491
diff changeset
    49
34859
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    50
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    51
  /* source text */
34451
3b9d0074ed44 command id via Isabelle.plugin;
wenzelm
parents: 34410
diff changeset
    52
34859
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    53
  val source: String = span.map(_.source).mkString
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    54
  def source(i: Int, j: Int): String = source.substring(i, j)
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    55
  def length: Int = source.length
34855
81d0410dc3ac iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents: 34835
diff changeset
    56
34859
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
    57
  lazy val symbol_index = new Symbol.Index(source)
34593
cf37a9f988bf ignore unchanged commands
immler@in.tum.de
parents: 34577
diff changeset
    58
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    59
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    60
  /* accumulated messages */
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    61
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    62
  @volatile protected var state = new State(this)
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    63
  def current_state: State = state
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    64
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    65
  private case class Consume(message: XML.Tree, forward: Command => Unit)
34832
d785f72ef388 more explicit treatment of command/document state;
wenzelm
parents: 34823
diff changeset
    66
  private case object Assign
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    67
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    68
  private val accumulator = actor {
34832
d785f72ef388 more explicit treatment of command/document state;
wenzelm
parents: 34823
diff changeset
    69
    var assigned = false
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    70
    loop {
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    71
      react {
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    72
        case Consume(message, forward) if !assigned =>
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    73
          val old_state = state
37178
d52f934f8ff6 accumulate only local results -- no proper history support yet;
wenzelm
parents: 37129
diff changeset
    74
          state = old_state.accumulate(message)
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    75
          if (!(state eq old_state)) forward(static_parent getOrElse this)
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    76
34832
d785f72ef388 more explicit treatment of command/document state;
wenzelm
parents: 34823
diff changeset
    77
        case Assign =>
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
    78
          assigned = true  // single assignment
34832
d785f72ef388 more explicit treatment of command/document state;
wenzelm
parents: 34823
diff changeset
    79
          reply(())
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    80
37189
2b4e52ecf6fc tuned messages;
wenzelm
parents: 37178
diff changeset
    81
        case bad => System.err.println("Command accumulator: ignoring bad message " + bad)
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    82
      }
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    83
    }
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    84
  }
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    85
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    86
  def consume(message: XML.Tree, forward: Command => Unit)
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    87
  {
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    88
    accumulator ! Consume(message, forward)
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    89
  }
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    90
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37373
diff changeset
    91
  def assign_state(state_id: Document.State_ID): Command =
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    92
  {
37129
4c83696b340e Command.toString: include id for debugging;
wenzelm
parents: 36990
diff changeset
    93
    val cmd = new Command(state_id, span, Some(this))
34832
d785f72ef388 more explicit treatment of command/document state;
wenzelm
parents: 34823
diff changeset
    94
    accumulator !? Assign
34815
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    95
    cmd.state = current_state
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    96
    cmd
6bae73cd8e33 unified Command and Command_State, eliminated separate Accumulator;
wenzelm
parents: 34813
diff changeset
    97
  }
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    98
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
    99
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
   100
  /* markup */
34508
422a43b76f77 eliminated Command.Status.REMOVE/REMOVED;
wenzelm
parents: 34500
diff changeset
   101
34859
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34858
diff changeset
   102
  lazy val empty_markup = new Markup_Text(Nil, source)
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
   103
34717
3f32e08bbb6c sidekick root data: set buffer length to avoid crash of initial caret move;
wenzelm
parents: 34708
diff changeset
   104
  def markup_node(begin: Int, end: Int, info: Any): Markup_Tree =
34699
wenzelm
parents: 34698
diff changeset
   105
  {
34703
ff037c17332a minor tuning;
wenzelm
parents: 34699
diff changeset
   106
    val start = symbol_index.decode(begin)
ff037c17332a minor tuning;
wenzelm
parents: 34699
diff changeset
   107
    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
   108
    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
   109
  }
34676
9e725d34df7b Command and Command_State handle results from prover as Accumulator
immler@in.tum.de
parents: 34675
diff changeset
   110
}