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