src/Pure/Thy/command.scala
author wenzelm
Mon Mar 29 22:55:57 2010 +0200 (2010-03-29 ago)
changeset 36012 0614676f14d4
parent 34871 e596a0b71f3c
permissions -rw-r--r--
replaced some deprecated methods;
wenzelm@34407
     1
/*
wenzelm@34407
     2
 * Prover commands with semantic state
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Johannes Hölzl, TU Munich
wenzelm@34407
     5
 * @author Fabian Immler, TU Munich
wenzelm@34407
     6
 */
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34451
    10
wenzelm@34699
    11
import scala.actors.Actor, Actor._
wenzelm@34497
    12
import scala.collection.mutable
wenzelm@34486
    13
wenzelm@34451
    14
wenzelm@34637
    15
object Command
wenzelm@34637
    16
{
wenzelm@34637
    17
  object Status extends Enumeration
wenzelm@34637
    18
  {
wenzelm@34318
    19
    val UNPROCESSED = Value("UNPROCESSED")
wenzelm@34318
    20
    val FINISHED = Value("FINISHED")
wenzelm@34318
    21
    val FAILED = Value("FAILED")
wenzelm@34318
    22
  }
wenzelm@34707
    23
wenzelm@34707
    24
  case class HighlightInfo(highlight: String) { override def toString = highlight }
wenzelm@34717
    25
  case class TypeInfo(ty: String)
wenzelm@34707
    26
  case class RefInfo(file: Option[String], line: Option[Int],
wenzelm@34717
    27
    command_id: Option[String], offset: Option[Int])
wenzelm@34318
    28
}
wenzelm@34318
    29
wenzelm@34451
    30
wenzelm@34697
    31
class Command(
wenzelm@34813
    32
    val id: Isar_Document.Command_ID,
wenzelm@34859
    33
    val span: Thy_Syntax.Span)
wenzelm@34815
    34
  extends Session.Entity
wenzelm@34451
    35
{
wenzelm@34859
    36
  /* classification */
wenzelm@34500
    37
wenzelm@36012
    38
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
    39
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
    40
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    41
wenzelm@36012
    42
  def name: String = if (is_command) span.head.content else ""
wenzelm@34859
    43
  override def toString = if (is_command) name else if (is_ignored) "<ignored>" else "<malformed>"
wenzelm@34495
    44
wenzelm@34859
    45
wenzelm@34859
    46
  /* source text */
wenzelm@34451
    47
wenzelm@34859
    48
  val source: String = span.map(_.source).mkString
wenzelm@34859
    49
  def source(i: Int, j: Int): String = source.substring(i, j)
wenzelm@34859
    50
  def length: Int = source.length
wenzelm@34855
    51
wenzelm@34859
    52
  lazy val symbol_index = new Symbol.Index(source)
immler@34593
    53
wenzelm@34815
    54
wenzelm@34815
    55
  /* accumulated messages */
wenzelm@34815
    56
wenzelm@34815
    57
  @volatile protected var state = new State(this)
wenzelm@34815
    58
  def current_state: State = state
wenzelm@34815
    59
wenzelm@34815
    60
  private case class Consume(session: Session, message: XML.Tree)
wenzelm@34832
    61
  private case object Assign
wenzelm@34815
    62
wenzelm@34815
    63
  private val accumulator = actor {
wenzelm@34832
    64
    var assigned = false
wenzelm@34815
    65
    loop {
wenzelm@34815
    66
      react {
wenzelm@34832
    67
        case Consume(session: Session, message: XML.Tree) if !assigned =>
wenzelm@34815
    68
          state = state.+(session, message)
wenzelm@34815
    69
wenzelm@34832
    70
        case Assign =>
wenzelm@34835
    71
          assigned = true  // single assignment
wenzelm@34832
    72
          reply(())
wenzelm@34815
    73
wenzelm@34815
    74
        case bad => System.err.println("command accumulator: ignoring bad message " + bad)
wenzelm@34815
    75
      }
wenzelm@34815
    76
    }
wenzelm@34815
    77
  }
wenzelm@34815
    78
wenzelm@34815
    79
  def consume(session: Session, message: XML.Tree) { accumulator ! Consume(session, message) }
wenzelm@34815
    80
wenzelm@34832
    81
  def assign_state(state_id: Isar_Document.State_ID): Command =
wenzelm@34815
    82
  {
wenzelm@34859
    83
    val cmd = new Command(state_id, span)
wenzelm@34832
    84
    accumulator !? Assign
wenzelm@34815
    85
    cmd.state = current_state
wenzelm@34815
    86
    cmd
wenzelm@34815
    87
  }
immler@34676
    88
immler@34676
    89
immler@34676
    90
  /* markup */
wenzelm@34508
    91
wenzelm@34859
    92
  lazy val empty_markup = new Markup_Text(Nil, source)
immler@34676
    93
wenzelm@34717
    94
  def markup_node(begin: Int, end: Int, info: Any): Markup_Tree =
wenzelm@34699
    95
  {
wenzelm@34703
    96
    val start = symbol_index.decode(begin)
wenzelm@34703
    97
    val stop = symbol_index.decode(end)
wenzelm@34717
    98
    new Markup_Tree(new Markup_Node(start, stop, info), Nil)
wenzelm@34500
    99
  }
immler@34676
   100
}