src/Pure/PIDE/command.scala
author wenzelm
Fri May 28 22:21:08 2010 +0200 (2010-05-28)
changeset 37178 d52f934f8ff6
parent 37129 4c83696b340e
child 37189 2b4e52ecf6fc
permissions -rw-r--r--
accumulate only local results -- no proper history support yet;
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@37129
    16
case class Command_Set(set: Set[Command])
wenzelm@37129
    17
wenzelm@37129
    18
wenzelm@34637
    19
object Command
wenzelm@34637
    20
{
wenzelm@34637
    21
  object Status extends Enumeration
wenzelm@34637
    22
  {
wenzelm@34318
    23
    val UNPROCESSED = Value("UNPROCESSED")
wenzelm@34318
    24
    val FINISHED = Value("FINISHED")
wenzelm@34318
    25
    val FAILED = Value("FAILED")
wenzelm@36990
    26
    val UNDEFINED = Value("UNDEFINED")
wenzelm@34318
    27
  }
wenzelm@34707
    28
wenzelm@34707
    29
  case class HighlightInfo(highlight: String) { override def toString = highlight }
wenzelm@34717
    30
  case class TypeInfo(ty: String)
wenzelm@34707
    31
  case class RefInfo(file: Option[String], line: Option[Int],
wenzelm@34717
    32
    command_id: Option[String], offset: Option[Int])
wenzelm@34318
    33
}
wenzelm@34318
    34
wenzelm@34697
    35
class Command(
wenzelm@34813
    36
    val id: Isar_Document.Command_ID,
wenzelm@37129
    37
    val span: Thy_Syntax.Span,
wenzelm@37129
    38
    val static_parent: Option[Command] = None)
wenzelm@34815
    39
  extends Session.Entity
wenzelm@34451
    40
{
wenzelm@34859
    41
  /* classification */
wenzelm@34500
    42
wenzelm@36012
    43
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
    44
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
    45
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    46
wenzelm@36012
    47
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
    48
  override def toString =
wenzelm@37129
    49
    if (is_command) name + "(" + id + ")"
wenzelm@37129
    50
    else if (is_ignored) "<ignored>" else "<malformed>"
wenzelm@34495
    51
wenzelm@34859
    52
wenzelm@34859
    53
  /* source text */
wenzelm@34451
    54
wenzelm@34859
    55
  val source: String = span.map(_.source).mkString
wenzelm@34859
    56
  def source(i: Int, j: Int): String = source.substring(i, j)
wenzelm@34859
    57
  def length: Int = source.length
wenzelm@34855
    58
wenzelm@34859
    59
  lazy val symbol_index = new Symbol.Index(source)
immler@34593
    60
wenzelm@34815
    61
wenzelm@34815
    62
  /* accumulated messages */
wenzelm@34815
    63
wenzelm@34815
    64
  @volatile protected var state = new State(this)
wenzelm@34815
    65
  def current_state: State = state
wenzelm@34815
    66
wenzelm@37129
    67
  private case class Consume(message: XML.Tree, forward: Command => Unit)
wenzelm@34832
    68
  private case object Assign
wenzelm@34815
    69
wenzelm@34815
    70
  private val accumulator = actor {
wenzelm@34832
    71
    var assigned = false
wenzelm@34815
    72
    loop {
wenzelm@34815
    73
      react {
wenzelm@37129
    74
        case Consume(message, forward) if !assigned =>
wenzelm@37129
    75
          val old_state = state
wenzelm@37178
    76
          state = old_state.accumulate(message)
wenzelm@37129
    77
          if (!(state eq old_state)) forward(static_parent getOrElse this)
wenzelm@34815
    78
wenzelm@34832
    79
        case Assign =>
wenzelm@34835
    80
          assigned = true  // single assignment
wenzelm@34832
    81
          reply(())
wenzelm@34815
    82
wenzelm@34815
    83
        case bad => System.err.println("command accumulator: ignoring bad message " + bad)
wenzelm@34815
    84
      }
wenzelm@34815
    85
    }
wenzelm@34815
    86
  }
wenzelm@34815
    87
wenzelm@37129
    88
  def consume(message: XML.Tree, forward: Command => Unit)
wenzelm@37129
    89
  {
wenzelm@37129
    90
    accumulator ! Consume(message, forward)
wenzelm@37129
    91
  }
wenzelm@34815
    92
wenzelm@34832
    93
  def assign_state(state_id: Isar_Document.State_ID): Command =
wenzelm@34815
    94
  {
wenzelm@37129
    95
    val cmd = new Command(state_id, span, Some(this))
wenzelm@34832
    96
    accumulator !? Assign
wenzelm@34815
    97
    cmd.state = current_state
wenzelm@34815
    98
    cmd
wenzelm@34815
    99
  }
immler@34676
   100
immler@34676
   101
immler@34676
   102
  /* markup */
wenzelm@34508
   103
wenzelm@34859
   104
  lazy val empty_markup = new Markup_Text(Nil, source)
immler@34676
   105
wenzelm@34717
   106
  def markup_node(begin: Int, end: Int, info: Any): Markup_Tree =
wenzelm@34699
   107
  {
wenzelm@34703
   108
    val start = symbol_index.decode(begin)
wenzelm@34703
   109
    val stop = symbol_index.decode(end)
wenzelm@34717
   110
    new Markup_Tree(new Markup_Node(start, stop, info), Nil)
wenzelm@34500
   111
  }
immler@34676
   112
}