src/Pure/PIDE/command.scala
author wenzelm
Tue Nov 16 22:40:45 2010 +0100 (2010-11-16 ago)
changeset 40572 2315c3daee74
parent 40454 2516ea25a54b
child 43520 cec9b95fa35d
permissions -rw-r--r--
avoid spam;
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@39173
    51
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@39173
    52
              if id == command.id && command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    53
                val range = command.decode(raw_range)
wenzelm@38872
    54
                val props = Position.purge(atts)
wenzelm@39173
    55
                val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
wenzelm@38723
    56
                state.add_markup(info)
wenzelm@40572
    57
              case _ =>
wenzelm@40572
    58
                // FIXME System.err.println("Ignored report message: " + msg)
wenzelm@40572
    59
                state
wenzelm@38361
    60
            })
wenzelm@38872
    61
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
    62
          atts match {
wenzelm@38872
    63
            case Markup.Serial(i) =>
wenzelm@38887
    64
              val result = XML.Elem(Markup(name, Position.purge(atts)), body)
wenzelm@39622
    65
              val st0 = add_result(i, result)
wenzelm@39441
    66
              val st1 =
wenzelm@39622
    67
                if (Isar_Document.is_tracing(message)) st0
wenzelm@39622
    68
                else
wenzelm@39622
    69
                  (st0 /: Isar_Document.message_positions(command, message))(
wenzelm@39622
    70
                    (st, range) => st.add_markup(Text.Info(range, result)))
wenzelm@39622
    71
              val st2 = (st1 /: Isar_Document.message_reports(message))(_ accumulate _)
wenzelm@39622
    72
              st2
wenzelm@38872
    73
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    74
          }
wenzelm@38361
    75
      }
wenzelm@38361
    76
  }
wenzelm@38367
    77
wenzelm@38367
    78
wenzelm@40454
    79
  /* dummy commands */
wenzelm@38367
    80
wenzelm@40454
    81
  def unparsed(source: String): Command =
wenzelm@38367
    82
    new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@40454
    83
wenzelm@40454
    84
  def span(toks: List[Token]): Command =
wenzelm@40454
    85
    new Command(Document.NO_ID, toks)
wenzelm@34318
    86
}
wenzelm@34318
    87
wenzelm@38361
    88
wenzelm@34697
    89
class Command(
wenzelm@38150
    90
    val id: Document.Command_ID,
wenzelm@38373
    91
    val span: List[Token])
wenzelm@34451
    92
{
wenzelm@34859
    93
  /* classification */
wenzelm@34500
    94
wenzelm@36012
    95
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
    96
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
    97
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    98
wenzelm@38367
    99
  def is_unparsed = id == Document.NO_ID
wenzelm@38367
   100
wenzelm@36012
   101
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
   102
  override def toString =
wenzelm@37373
   103
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   104
wenzelm@34859
   105
wenzelm@34859
   106
  /* source text */
wenzelm@34451
   107
wenzelm@34859
   108
  val source: String = span.map(_.source).mkString
wenzelm@38426
   109
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
   110
  def length: Int = source.length
wenzelm@38572
   111
wenzelm@38877
   112
  val newlines =
wenzelm@38877
   113
    (0 /: Symbol.iterator(source)) {
wenzelm@38877
   114
      case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
wenzelm@38877
   115
wenzelm@38479
   116
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
   117
wenzelm@34859
   118
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   119
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   120
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   121
wenzelm@38370
   122
wenzelm@38370
   123
  /* accumulated results */
wenzelm@38370
   124
wenzelm@38872
   125
  val empty_state: Command.State = Command.State(this)
immler@34676
   126
}