src/Pure/PIDE/command.scala
author wenzelm
Mon Aug 30 13:01:32 2010 +0200 (2010-08-30 ago)
changeset 38872 26c505765024
parent 38723 6a55b8978a56
child 38877 682c4932b3cc
permissions -rw-r--r--
Command.results: ordered by serial number;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/command.scala
wenzelm@36676
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     3
    Author:     Makarius
wenzelm@36676
     4
wenzelm@36676
     5
Prover commands with semantic state.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34451
    10
wenzelm@38872
    11
import scala.collection.immutable.SortedMap
wenzelm@38872
    12
wenzelm@38872
    13
wenzelm@34637
    14
object Command
wenzelm@34637
    15
{
wenzelm@38361
    16
  /** accumulated results from prover **/
wenzelm@38361
    17
wenzelm@38362
    18
  case class State(
wenzelm@38361
    19
    val command: Command,
wenzelm@38872
    20
    val status: List[Markup] = Nil,
wenzelm@38872
    21
    val results: SortedMap[Long, XML.Tree] = SortedMap.empty,
wenzelm@38872
    22
    val markup: Markup_Tree = Markup_Tree.empty)
wenzelm@38361
    23
  {
wenzelm@38361
    24
    /* content */
wenzelm@38361
    25
wenzelm@38714
    26
    def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@38714
    27
    def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
wenzelm@38872
    28
    def add_result(serial: Long, result: XML.Tree): State =
wenzelm@38872
    29
      copy(results = results + (serial -> result))
wenzelm@38361
    30
wenzelm@38658
    31
    def root_info: Text.Info[Any] =
wenzelm@38581
    32
      new Text.Info(command.range,
wenzelm@38658
    33
        XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
wenzelm@38658
    34
    def root_markup: Markup_Tree = markup + root_info
wenzelm@38361
    35
wenzelm@38361
    36
wenzelm@38361
    37
    /* message dispatch */
wenzelm@38361
    38
wenzelm@38872
    39
    def accumulate(message: XML.Elem): Command.State =
wenzelm@38361
    40
      message match {
wenzelm@38714
    41
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    42
          (this /: msgs)((state, msg) =>
wenzelm@38714
    43
            msg match {
wenzelm@38714
    44
              case XML.Elem(markup, Nil) => state.add_status(markup)
wenzelm@38714
    45
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    46
            })
wenzelm@38581
    47
wenzelm@38572
    48
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    49
          (this /: msgs)((state, msg) =>
wenzelm@38572
    50
            msg match {
wenzelm@38723
    51
              case XML.Elem(Markup(name, atts @ Position.Range(range)), args)
wenzelm@38723
    52
              if Position.Id.unapply(atts) == Some(command.id) =>
wenzelm@38872
    53
                val props = Position.purge(atts)
wenzelm@38723
    54
                val info =
wenzelm@38723
    55
                  Text.Info[Any](command.decode(range), XML.Elem(Markup(name, props), args))
wenzelm@38723
    56
                state.add_markup(info)
wenzelm@38572
    57
              case _ => System.err.println("Ignored report message: " + msg); state
wenzelm@38361
    58
            })
wenzelm@38872
    59
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
    60
          atts match {
wenzelm@38872
    61
            case Markup.Serial(i) =>
wenzelm@38872
    62
              add_result(i, XML.Elem(Markup(name, Position.purge(atts)), body))
wenzelm@38872
    63
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    64
          }
wenzelm@38361
    65
      }
wenzelm@38361
    66
  }
wenzelm@38367
    67
wenzelm@38367
    68
wenzelm@38367
    69
  /* unparsed dummy commands */
wenzelm@38367
    70
wenzelm@38367
    71
  def unparsed(source: String) =
wenzelm@38367
    72
    new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@34318
    73
}
wenzelm@34318
    74
wenzelm@38361
    75
wenzelm@34697
    76
class Command(
wenzelm@38150
    77
    val id: Document.Command_ID,
wenzelm@38373
    78
    val span: List[Token])
wenzelm@34451
    79
{
wenzelm@34859
    80
  /* classification */
wenzelm@34500
    81
wenzelm@36012
    82
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
    83
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
    84
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    85
wenzelm@38367
    86
  def is_unparsed = id == Document.NO_ID
wenzelm@38367
    87
wenzelm@36012
    88
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
    89
  override def toString =
wenzelm@37373
    90
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
    91
wenzelm@34859
    92
wenzelm@34859
    93
  /* source text */
wenzelm@34451
    94
wenzelm@34859
    95
  val source: String = span.map(_.source).mkString
wenzelm@38426
    96
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
    97
  def length: Int = source.length
wenzelm@38572
    98
wenzelm@38479
    99
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
   100
wenzelm@34859
   101
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   102
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   103
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   104
wenzelm@38370
   105
wenzelm@38370
   106
  /* accumulated results */
wenzelm@38370
   107
wenzelm@38872
   108
  val empty_state: Command.State = Command.State(this)
immler@34676
   109
}