src/Pure/PIDE/command.scala
author wenzelm
Thu Aug 25 11:41:48 2011 +0200 (2011-08-25 ago)
changeset 44474 681447a9ffe5
parent 44385 e7fdb008aa7d
child 44607 274eff0ea12e
permissions -rw-r--r--
slightly more abstract Command.Perspective;
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@43520
    10
import java.lang.System
wenzelm@34451
    11
wenzelm@38872
    12
import scala.collection.immutable.SortedMap
wenzelm@38872
    13
wenzelm@38872
    14
wenzelm@34637
    15
object Command
wenzelm@34637
    16
{
wenzelm@38361
    17
  /** accumulated results from prover **/
wenzelm@38361
    18
wenzelm@43714
    19
  sealed case class State(
wenzelm@38361
    20
    val command: Command,
wenzelm@38872
    21
    val status: List[Markup] = Nil,
wenzelm@38872
    22
    val results: SortedMap[Long, XML.Tree] = SortedMap.empty,
wenzelm@38872
    23
    val markup: Markup_Tree = Markup_Tree.empty)
wenzelm@38361
    24
  {
wenzelm@38361
    25
    /* content */
wenzelm@38361
    26
wenzelm@38714
    27
    def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@38714
    28
    def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
wenzelm@38872
    29
    def add_result(serial: Long, result: XML.Tree): State =
wenzelm@38872
    30
      copy(results = results + (serial -> result))
wenzelm@38361
    31
wenzelm@38658
    32
    def root_info: Text.Info[Any] =
wenzelm@38581
    33
      new Text.Info(command.range,
wenzelm@38658
    34
        XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
wenzelm@38658
    35
    def root_markup: Markup_Tree = markup + root_info
wenzelm@38361
    36
wenzelm@38361
    37
wenzelm@38361
    38
    /* message dispatch */
wenzelm@38361
    39
wenzelm@38872
    40
    def accumulate(message: XML.Elem): Command.State =
wenzelm@38361
    41
      message match {
wenzelm@38714
    42
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    43
          (this /: msgs)((state, msg) =>
wenzelm@38714
    44
            msg match {
wenzelm@38714
    45
              case XML.Elem(markup, Nil) => state.add_status(markup)
wenzelm@38714
    46
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    47
            })
wenzelm@38581
    48
wenzelm@38572
    49
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    50
          (this /: msgs)((state, msg) =>
wenzelm@38572
    51
            msg match {
wenzelm@39173
    52
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@39173
    53
              if id == command.id && command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    54
                val range = command.decode(raw_range)
wenzelm@38872
    55
                val props = Position.purge(atts)
wenzelm@39173
    56
                val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
wenzelm@38723
    57
                state.add_markup(info)
wenzelm@40572
    58
              case _ =>
wenzelm@40572
    59
                // FIXME System.err.println("Ignored report message: " + msg)
wenzelm@40572
    60
                state
wenzelm@38361
    61
            })
wenzelm@38872
    62
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
    63
          atts match {
wenzelm@38872
    64
            case Markup.Serial(i) =>
wenzelm@38887
    65
              val result = XML.Elem(Markup(name, Position.purge(atts)), body)
wenzelm@39622
    66
              val st0 = add_result(i, result)
wenzelm@39441
    67
              val st1 =
wenzelm@39622
    68
                if (Isar_Document.is_tracing(message)) st0
wenzelm@39622
    69
                else
wenzelm@39622
    70
                  (st0 /: Isar_Document.message_positions(command, message))(
wenzelm@39622
    71
                    (st, range) => st.add_markup(Text.Info(range, result)))
wenzelm@39622
    72
              val st2 = (st1 /: Isar_Document.message_reports(message))(_ accumulate _)
wenzelm@39622
    73
              st2
wenzelm@38872
    74
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    75
          }
wenzelm@38361
    76
      }
wenzelm@38361
    77
  }
wenzelm@38367
    78
wenzelm@38367
    79
wenzelm@40454
    80
  /* dummy commands */
wenzelm@38367
    81
wenzelm@40454
    82
  def unparsed(source: String): Command =
wenzelm@43662
    83
    new Command(Document.no_id, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@40454
    84
wenzelm@40454
    85
  def span(toks: List[Token]): Command =
wenzelm@43662
    86
    new Command(Document.no_id, toks)
wenzelm@44384
    87
wenzelm@44384
    88
wenzelm@44384
    89
  /* perspective */
wenzelm@44384
    90
wenzelm@44474
    91
  object Perspective
wenzelm@44474
    92
  {
wenzelm@44474
    93
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
    94
  }
wenzelm@44385
    95
wenzelm@44474
    96
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
    97
  {
wenzelm@44474
    98
    def same(that: Perspective): Boolean =
wenzelm@44474
    99
    {
wenzelm@44474
   100
      val cmds1 = this.commands
wenzelm@44474
   101
      val cmds2 = that.commands
wenzelm@44474
   102
      require(cmds1.forall(_.is_defined))
wenzelm@44474
   103
      require(cmds2.forall(_.is_defined))
wenzelm@44474
   104
      cmds1.length == cmds2.length &&
wenzelm@44474
   105
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   106
    }
wenzelm@44385
   107
  }
wenzelm@34318
   108
}
wenzelm@34318
   109
wenzelm@38361
   110
wenzelm@34697
   111
class Command(
wenzelm@38150
   112
    val id: Document.Command_ID,
wenzelm@38373
   113
    val span: List[Token])
wenzelm@34451
   114
{
wenzelm@34859
   115
  /* classification */
wenzelm@34500
   116
wenzelm@44385
   117
  def is_defined: Boolean = id != Document.no_id
wenzelm@44385
   118
wenzelm@36012
   119
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
   120
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
   121
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
   122
wenzelm@36012
   123
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
   124
  override def toString =
wenzelm@37373
   125
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   126
wenzelm@34859
   127
wenzelm@34859
   128
  /* source text */
wenzelm@34451
   129
wenzelm@34859
   130
  val source: String = span.map(_.source).mkString
wenzelm@38426
   131
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
   132
  def length: Int = source.length
wenzelm@38572
   133
wenzelm@38877
   134
  val newlines =
wenzelm@38877
   135
    (0 /: Symbol.iterator(source)) {
wenzelm@38877
   136
      case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
wenzelm@38877
   137
wenzelm@38479
   138
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
   139
wenzelm@34859
   140
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   141
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   142
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   143
wenzelm@38370
   144
wenzelm@38370
   145
  /* accumulated results */
wenzelm@38370
   146
wenzelm@38872
   147
  val empty_state: Command.State = Command.State(this)
immler@34676
   148
}