src/Pure/PIDE/command.scala
author wenzelm
Sun Aug 22 20:11:17 2010 +0200 (2010-08-22 ago)
changeset 38581 d503a0912e14
parent 38579 ce46a6f55bce
child 38637 03b27bd0505e
permissions -rw-r--r--
simplified Command.status again, reverting most of e5eed57913d0 (note that more complex information can be represented with full markup reports);
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@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@38361
    17
  /** accumulated results from prover **/
wenzelm@38361
    18
wenzelm@38362
    19
  case class State(
wenzelm@38361
    20
    val command: Command,
wenzelm@38581
    21
    val status: List[Markup],
wenzelm@38361
    22
    val reverse_results: List[XML.Tree],
wenzelm@38479
    23
    val markup: Markup_Tree)
wenzelm@38361
    24
  {
wenzelm@38361
    25
    /* content */
wenzelm@38361
    26
wenzelm@38362
    27
    lazy val results = reverse_results.reverse
wenzelm@38361
    28
wenzelm@38362
    29
    def add_result(result: XML.Tree): State = copy(reverse_results = result :: reverse_results)
wenzelm@38361
    30
wenzelm@38577
    31
    def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
wenzelm@38479
    32
wenzelm@38577
    33
    def markup_root_info: Text.Info[Any] =
wenzelm@38581
    34
      new Text.Info(command.range,
wenzelm@38581
    35
        XML.Elem(Markup(Markup.STATUS, Nil), status.map(XML.Elem(_, Nil))))
wenzelm@38577
    36
    def markup_root: Markup_Tree = markup + markup_root_info
wenzelm@38361
    37
wenzelm@38361
    38
wenzelm@38361
    39
    /* message dispatch */
wenzelm@38361
    40
wenzelm@38361
    41
    def accumulate(message: XML.Tree): Command.State =
wenzelm@38361
    42
      message match {
wenzelm@38581
    43
        case XML.Elem(Markup(Markup.STATUS, _), body) =>  // FIXME explicit checks!?
wenzelm@38581
    44
          copy(status = (for (XML.Elem(markup, _) <- body) yield markup) ::: status)
wenzelm@38581
    45
wenzelm@38572
    46
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    47
          (this /: msgs)((state, msg) =>
wenzelm@38572
    48
            msg match {
wenzelm@38572
    49
              case XML.Elem(Markup(name, atts), args)
wenzelm@38572
    50
              if Position.get_id(atts) == Some(command.id) && Position.get_range(atts).isDefined =>
wenzelm@38579
    51
                val range = command.decode(Position.get_range(atts).get)
wenzelm@38572
    52
                val props = atts.filterNot(p => Markup.POSITION_PROPERTIES(p._1))
wenzelm@38577
    53
                val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
wenzelm@38577
    54
                add_markup(info)
wenzelm@38572
    55
              case _ => System.err.println("Ignored report message: " + msg); state
wenzelm@38361
    56
            })
wenzelm@38361
    57
        case _ => add_result(message)
wenzelm@38361
    58
      }
wenzelm@38361
    59
  }
wenzelm@38367
    60
wenzelm@38367
    61
wenzelm@38367
    62
  /* unparsed dummy commands */
wenzelm@38367
    63
wenzelm@38367
    64
  def unparsed(source: String) =
wenzelm@38367
    65
    new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@34318
    66
}
wenzelm@34318
    67
wenzelm@38361
    68
wenzelm@34697
    69
class Command(
wenzelm@38150
    70
    val id: Document.Command_ID,
wenzelm@38373
    71
    val span: List[Token])
wenzelm@34451
    72
{
wenzelm@34859
    73
  /* classification */
wenzelm@34500
    74
wenzelm@36012
    75
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
    76
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
    77
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    78
wenzelm@38367
    79
  def is_unparsed = id == Document.NO_ID
wenzelm@38367
    80
wenzelm@36012
    81
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
    82
  override def toString =
wenzelm@37373
    83
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
    84
wenzelm@34859
    85
wenzelm@34859
    86
  /* source text */
wenzelm@34451
    87
wenzelm@34859
    88
  val source: String = span.map(_.source).mkString
wenzelm@38426
    89
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
    90
  def length: Int = source.length
wenzelm@38572
    91
wenzelm@38479
    92
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
    93
wenzelm@34859
    94
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
    95
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
    96
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
    97
wenzelm@38370
    98
wenzelm@38370
    99
  /* accumulated results */
wenzelm@38370
   100
wenzelm@38479
   101
  val empty_state: Command.State = Command.State(this, Nil, Nil, Markup_Tree.empty)
immler@34676
   102
}