src/Pure/PIDE/command.scala
author wenzelm
Fri Nov 11 14:24:38 2011 +0100 (2011-11-11 ago)
changeset 45455 4f974c0c5c2f
parent 44959 9476c856c4b9
child 45644 8634b4e61b88
permissions -rw-r--r--
prefer statically typed Text.Markup;
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@45455
    28
    def add_markup(m: Text.Markup): State = copy(markup = markup + m)
wenzelm@38872
    29
    def add_result(serial: Long, result: XML.Tree): State =
wenzelm@38872
    30
      copy(results = results + (serial -> result))
wenzelm@38361
    31
wenzelm@45455
    32
    def root_info: Text.Markup =
wenzelm@45455
    33
      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@45455
    56
                val info: Text.Markup = Text.Info(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@44959
    83
    new Command(Document.no_id, Document.Node.Name.empty,
wenzelm@44615
    84
      List(Token(Token.Kind.UNPARSED, source)))
wenzelm@40454
    85
wenzelm@44615
    86
  def span(node_name: Document.Node.Name, toks: List[Token]): Command =
wenzelm@44607
    87
    new Command(Document.no_id, node_name, toks)
wenzelm@44384
    88
wenzelm@44384
    89
wenzelm@44384
    90
  /* perspective */
wenzelm@44384
    91
wenzelm@44474
    92
  object Perspective
wenzelm@44474
    93
  {
wenzelm@44474
    94
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
    95
  }
wenzelm@44385
    96
wenzelm@44474
    97
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
    98
  {
wenzelm@44474
    99
    def same(that: Perspective): Boolean =
wenzelm@44474
   100
    {
wenzelm@44474
   101
      val cmds1 = this.commands
wenzelm@44474
   102
      val cmds2 = that.commands
wenzelm@44474
   103
      require(cmds1.forall(_.is_defined))
wenzelm@44474
   104
      require(cmds2.forall(_.is_defined))
wenzelm@44474
   105
      cmds1.length == cmds2.length &&
wenzelm@44474
   106
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   107
    }
wenzelm@44385
   108
  }
wenzelm@34318
   109
}
wenzelm@34318
   110
wenzelm@38361
   111
wenzelm@34697
   112
class Command(
wenzelm@38150
   113
    val id: Document.Command_ID,
wenzelm@44615
   114
    val node_name: Document.Node.Name,
wenzelm@38373
   115
    val span: List[Token])
wenzelm@34451
   116
{
wenzelm@34859
   117
  /* classification */
wenzelm@34500
   118
wenzelm@44385
   119
  def is_defined: Boolean = id != Document.no_id
wenzelm@44385
   120
wenzelm@36012
   121
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
   122
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
   123
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
   124
wenzelm@36012
   125
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
   126
  override def toString =
wenzelm@37373
   127
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   128
wenzelm@34859
   129
wenzelm@34859
   130
  /* source text */
wenzelm@34451
   131
wenzelm@34859
   132
  val source: String = span.map(_.source).mkString
wenzelm@38426
   133
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
   134
  def length: Int = source.length
wenzelm@38572
   135
wenzelm@38877
   136
  val newlines =
wenzelm@38877
   137
    (0 /: Symbol.iterator(source)) {
wenzelm@38877
   138
      case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
wenzelm@38877
   139
wenzelm@38479
   140
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
   141
wenzelm@34859
   142
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   143
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   144
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   145
wenzelm@38370
   146
wenzelm@38370
   147
  /* accumulated results */
wenzelm@38370
   148
wenzelm@38872
   149
  val empty_state: Command.State = Command.State(this)
immler@34676
   150
}