src/Pure/PIDE/command.scala
author wenzelm
Thu Aug 12 13:49:08 2010 +0200 (2010-08-12)
changeset 38360 53224a4d2f0e
parent 38355 8cb265fb12fe
child 38361 b609d0b271fa
permissions -rw-r--r--
specific Session.Commands_Changed;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/command.scala
wenzelm@36676
     2
    Author:     Johannes Hölzl, TU Munich
wenzelm@36676
     3
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     4
    Author:     Makarius
wenzelm@36676
     5
wenzelm@36676
     6
Prover commands with semantic state.
wenzelm@36676
     7
*/
wenzelm@34407
     8
wenzelm@34871
     9
package isabelle
wenzelm@34318
    10
wenzelm@34451
    11
wenzelm@34699
    12
import scala.actors.Actor, Actor._
wenzelm@34497
    13
import scala.collection.mutable
wenzelm@34486
    14
wenzelm@34451
    15
wenzelm@34637
    16
object Command
wenzelm@34637
    17
{
wenzelm@34637
    18
  object Status extends Enumeration
wenzelm@34637
    19
  {
wenzelm@34318
    20
    val UNPROCESSED = Value("UNPROCESSED")
wenzelm@34318
    21
    val FINISHED = Value("FINISHED")
wenzelm@34318
    22
    val FAILED = Value("FAILED")
wenzelm@36990
    23
    val UNDEFINED = Value("UNDEFINED")
wenzelm@34318
    24
  }
wenzelm@34707
    25
wenzelm@37197
    26
  case class HighlightInfo(kind: String, sub_kind: Option[String]) {
wenzelm@37197
    27
    override def toString = kind
wenzelm@37197
    28
  }
wenzelm@34717
    29
  case class TypeInfo(ty: String)
wenzelm@34707
    30
  case class RefInfo(file: Option[String], line: Option[Int],
wenzelm@38355
    31
    command_id: Option[Document.Command_ID], offset: Option[Int])  // FIXME Command_ID vs. State_ID !?
wenzelm@34318
    32
}
wenzelm@34318
    33
wenzelm@34697
    34
class Command(
wenzelm@38150
    35
    val id: Document.Command_ID,
wenzelm@37129
    36
    val span: Thy_Syntax.Span,
wenzelm@38220
    37
    val static_parent: Option[Command] = None)  // FIXME !?
wenzelm@34815
    38
  extends Session.Entity
wenzelm@34451
    39
{
wenzelm@34859
    40
  /* classification */
wenzelm@34500
    41
wenzelm@36012
    42
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
    43
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
    44
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    45
wenzelm@36012
    46
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
    47
  override def toString =
wenzelm@37373
    48
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
    49
wenzelm@34859
    50
wenzelm@34859
    51
  /* source text */
wenzelm@34451
    52
wenzelm@34859
    53
  val source: String = span.map(_.source).mkString
wenzelm@34859
    54
  def source(i: Int, j: Int): String = source.substring(i, j)
wenzelm@34859
    55
  def length: Int = source.length
wenzelm@34855
    56
wenzelm@34859
    57
  lazy val symbol_index = new Symbol.Index(source)
immler@34593
    58
wenzelm@34815
    59
wenzelm@34815
    60
  /* accumulated messages */
wenzelm@34815
    61
wenzelm@34815
    62
  @volatile protected var state = new State(this)
wenzelm@34815
    63
  def current_state: State = state
wenzelm@34815
    64
wenzelm@37129
    65
  private case class Consume(message: XML.Tree, forward: Command => Unit)
wenzelm@34832
    66
  private case object Assign
wenzelm@34815
    67
wenzelm@34815
    68
  private val accumulator = actor {
wenzelm@34832
    69
    var assigned = false
wenzelm@34815
    70
    loop {
wenzelm@34815
    71
      react {
wenzelm@37129
    72
        case Consume(message, forward) if !assigned =>
wenzelm@37129
    73
          val old_state = state
wenzelm@37178
    74
          state = old_state.accumulate(message)
wenzelm@37129
    75
          if (!(state eq old_state)) forward(static_parent getOrElse this)
wenzelm@34815
    76
wenzelm@34832
    77
        case Assign =>
wenzelm@34835
    78
          assigned = true  // single assignment
wenzelm@34832
    79
          reply(())
wenzelm@34815
    80
wenzelm@37189
    81
        case bad => System.err.println("Command accumulator: ignoring bad message " + bad)
wenzelm@34815
    82
      }
wenzelm@34815
    83
    }
wenzelm@34815
    84
  }
wenzelm@34815
    85
wenzelm@37129
    86
  def consume(message: XML.Tree, forward: Command => Unit)
wenzelm@37129
    87
  {
wenzelm@37129
    88
    accumulator ! Consume(message, forward)
wenzelm@37129
    89
  }
wenzelm@34815
    90
wenzelm@38150
    91
  def assign_state(state_id: Document.State_ID): Command =
wenzelm@34815
    92
  {
wenzelm@37129
    93
    val cmd = new Command(state_id, span, Some(this))
wenzelm@34832
    94
    accumulator !? Assign
wenzelm@34815
    95
    cmd.state = current_state
wenzelm@34815
    96
    cmd
wenzelm@34815
    97
  }
immler@34676
    98
immler@34676
    99
immler@34676
   100
  /* markup */
wenzelm@34508
   101
wenzelm@34859
   102
  lazy val empty_markup = new Markup_Text(Nil, source)
immler@34676
   103
wenzelm@34717
   104
  def markup_node(begin: Int, end: Int, info: Any): Markup_Tree =
wenzelm@34699
   105
  {
wenzelm@34703
   106
    val start = symbol_index.decode(begin)
wenzelm@34703
   107
    val stop = symbol_index.decode(end)
wenzelm@34717
   108
    new Markup_Tree(new Markup_Node(start, stop, info), Nil)
wenzelm@34500
   109
  }
immler@34676
   110
}