src/Pure/PIDE/command.scala
author wenzelm
Wed Aug 25 18:19:04 2010 +0200 (2010-08-25 ago)
changeset 38714 31da698fc4e5
parent 38658 20d82e98bcd7
child 38722 ba31936497c2
permissions -rw-r--r--
more precise Command.State accumulation;
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@34637
    11
object Command
wenzelm@34637
    12
{
wenzelm@38361
    13
  /** accumulated results from prover **/
wenzelm@38361
    14
wenzelm@38362
    15
  case class State(
wenzelm@38361
    16
    val command: Command,
wenzelm@38581
    17
    val status: List[Markup],
wenzelm@38361
    18
    val reverse_results: List[XML.Tree],
wenzelm@38479
    19
    val markup: Markup_Tree)
wenzelm@38361
    20
  {
wenzelm@38361
    21
    /* content */
wenzelm@38361
    22
wenzelm@38362
    23
    lazy val results = reverse_results.reverse
wenzelm@38361
    24
wenzelm@38714
    25
    def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@38714
    26
    def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
wenzelm@38362
    27
    def add_result(result: XML.Tree): State = copy(reverse_results = result :: reverse_results)
wenzelm@38361
    28
wenzelm@38658
    29
    def root_info: Text.Info[Any] =
wenzelm@38581
    30
      new Text.Info(command.range,
wenzelm@38658
    31
        XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
wenzelm@38658
    32
    def root_markup: Markup_Tree = markup + root_info
wenzelm@38361
    33
wenzelm@38361
    34
wenzelm@38361
    35
    /* message dispatch */
wenzelm@38361
    36
wenzelm@38361
    37
    def accumulate(message: XML.Tree): Command.State =
wenzelm@38361
    38
      message match {
wenzelm@38714
    39
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    40
          (this /: msgs)((state, msg) =>
wenzelm@38714
    41
            msg match {
wenzelm@38714
    42
              case XML.Elem(markup, Nil) => state.add_status(markup)
wenzelm@38714
    43
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    44
            })
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@38714
    54
                state.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
}