src/Pure/PIDE/command.scala
author wenzelm
Sat Jul 13 12:39:45 2013 +0200 (2013-07-13 ago)
changeset 52642 84eb792224a8
parent 52536 3a35ce87a55c
child 52849 199e9fa5a5c2
permissions -rw-r--r--
full merge of Command.State, which enables Command.prints to augment markup as well (assuming that these dynamic overlays are relatively few);
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@52536
     5
Prover commands with accumulated results from execution.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34451
    10
wenzelm@45644
    11
import scala.collection.mutable
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@50507
    19
  /* results */
wenzelm@50507
    20
wenzelm@50507
    21
  object Results
wenzelm@50507
    22
  {
wenzelm@51496
    23
    type Entry = (Long, XML.Tree)
wenzelm@50507
    24
    val empty = new Results(SortedMap.empty)
wenzelm@51496
    25
    def make(es: Iterable[Results.Entry]): Results = (empty /: es.iterator)(_ + _)
wenzelm@50507
    26
    def merge(rs: Iterable[Results]): Results = (empty /: rs.iterator)(_ ++ _)
wenzelm@50507
    27
  }
wenzelm@50507
    28
wenzelm@51494
    29
  final class Results private(private val rep: SortedMap[Long, XML.Tree])
wenzelm@50507
    30
  {
wenzelm@50507
    31
    def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
wenzelm@50507
    32
    def get(serial: Long): Option[XML.Tree] = rep.get(serial)
wenzelm@51496
    33
    def entries: Iterator[Results.Entry] = rep.iterator
wenzelm@50508
    34
wenzelm@51496
    35
    def + (entry: Results.Entry): Results =
wenzelm@50508
    36
      if (defined(entry._1)) this
wenzelm@50508
    37
      else new Results(rep + entry)
wenzelm@50508
    38
wenzelm@50508
    39
    def ++ (other: Results): Results =
wenzelm@50508
    40
      if (this eq other) this
wenzelm@50508
    41
      else if (rep.isEmpty) other
wenzelm@50508
    42
      else (this /: other.entries)(_ + _)
wenzelm@50540
    43
wenzelm@51494
    44
    override def hashCode: Int = rep.hashCode
wenzelm@51494
    45
    override def equals(that: Any): Boolean =
wenzelm@51494
    46
      that match {
wenzelm@51494
    47
        case other: Results => rep == other.rep
wenzelm@51494
    48
        case _ => false
wenzelm@51494
    49
      }
wenzelm@50540
    50
    override def toString: String = entries.mkString("Results(", ", ", ")")
wenzelm@50507
    51
  }
wenzelm@50507
    52
wenzelm@50507
    53
wenzelm@50507
    54
  /* state */
wenzelm@50501
    55
wenzelm@43714
    56
  sealed case class State(
wenzelm@50501
    57
    command: Command,
wenzelm@50501
    58
    status: List[Markup] = Nil,
wenzelm@50507
    59
    results: Results = Results.empty,
wenzelm@50501
    60
    markup: Markup_Tree = Markup_Tree.empty)
wenzelm@38361
    61
  {
wenzelm@49645
    62
    def markup_to_XML(filter: XML.Elem => Boolean): XML.Body =
wenzelm@49645
    63
      markup.to_XML(command.range, command.source, filter)
wenzelm@49614
    64
wenzelm@49614
    65
wenzelm@51494
    66
    /* content */
wenzelm@51494
    67
wenzelm@51494
    68
    def eq_content(other: State): Boolean =
wenzelm@51494
    69
      command.source == other.command.source &&
wenzelm@51494
    70
      status == other.status &&
wenzelm@51494
    71
      results == other.results &&
wenzelm@51494
    72
      markup == other.markup
wenzelm@38361
    73
wenzelm@46152
    74
    private def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@46152
    75
    private def add_markup(m: Text.Markup): State = copy(markup = markup + m)
wenzelm@38361
    76
wenzelm@52531
    77
    def + (alt_id: Document_ID.Generic, message: XML.Elem): State =
wenzelm@38361
    78
      message match {
wenzelm@50201
    79
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    80
          (this /: msgs)((state, msg) =>
wenzelm@38714
    81
            msg match {
wenzelm@46152
    82
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@50499
    83
                state.add_status(markup)
wenzelm@50499
    84
                  .add_markup(Text.Info(command.proper_range, elem))  // FIXME cumulation order!?
wenzelm@46152
    85
wenzelm@52536
    86
              case _ =>
wenzelm@52536
    87
                java.lang.System.err.println("Ignored status message: " + msg)
wenzelm@52536
    88
                state
wenzelm@38714
    89
            })
wenzelm@38581
    90
wenzelm@50201
    91
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    92
          (this /: msgs)((state, msg) =>
wenzelm@38572
    93
            msg match {
wenzelm@39173
    94
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@49527
    95
              if (id == command.id || id == alt_id) &&
wenzelm@49527
    96
                  command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    97
                val range = command.decode(raw_range)
wenzelm@38872
    98
                val props = Position.purge(atts)
wenzelm@45455
    99
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@38723
   100
                state.add_markup(info)
wenzelm@49526
   101
              case XML.Elem(Markup(name, atts), args)
wenzelm@50201
   102
              if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@49037
   103
                val range = command.proper_range
wenzelm@49037
   104
                val props = Position.purge(atts)
wenzelm@49037
   105
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@49037
   106
                state.add_markup(info)
wenzelm@40572
   107
              case _ =>
wenzelm@52536
   108
                // FIXME java.lang.System.err.println("Ignored report message: " + msg)
wenzelm@40572
   109
                state
wenzelm@38361
   110
            })
wenzelm@38872
   111
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
   112
          atts match {
wenzelm@50201
   113
            case Markup.Serial(i) =>
wenzelm@50163
   114
              val props = Position.purge(atts)
wenzelm@50201
   115
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   116
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   117
wenzelm@50163
   118
              val st0 = copy(results = results + (i -> message1))
wenzelm@39441
   119
              val st1 =
wenzelm@50500
   120
                if (Protocol.is_inlined(message))
wenzelm@45709
   121
                  (st0 /: Protocol.message_positions(command, message))(
wenzelm@50163
   122
                    (st, range) => st.add_markup(Text.Info(range, message2)))
wenzelm@50500
   123
                else st0
wenzelm@49445
   124
wenzelm@49445
   125
              st1
wenzelm@52536
   126
            case _ =>
wenzelm@52536
   127
              java.lang.System.err.println("Ignored message without serial number: " + message)
wenzelm@52536
   128
              this
wenzelm@38872
   129
          }
wenzelm@38361
   130
      }
wenzelm@52527
   131
wenzelm@52527
   132
    def ++ (other: State): State =
wenzelm@52642
   133
      copy(
wenzelm@52642
   134
        status = other.status ::: status,
wenzelm@52642
   135
        results = results ++ other.results,
wenzelm@52642
   136
        markup = markup ++ other.markup)
wenzelm@38361
   137
  }
wenzelm@38367
   138
wenzelm@38367
   139
wenzelm@45644
   140
  /* make commands */
wenzelm@45644
   141
wenzelm@48745
   142
  type Span = List[Token]
wenzelm@48745
   143
wenzelm@52524
   144
  def apply(
wenzelm@52530
   145
    id: Document_ID.Command,
wenzelm@52524
   146
    node_name: Document.Node.Name,
wenzelm@52524
   147
    span: Span,
wenzelm@52524
   148
    results: Results = Results.empty,
wenzelm@52524
   149
    markup: Markup_Tree = Markup_Tree.empty): Command =
wenzelm@45644
   150
  {
wenzelm@45644
   151
    val source: String =
wenzelm@48745
   152
      span match {
wenzelm@45644
   153
        case List(tok) => tok.source
wenzelm@48745
   154
        case _ => span.map(_.source).mkString
wenzelm@45644
   155
      }
wenzelm@45644
   156
wenzelm@48745
   157
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
   158
    var i = 0
wenzelm@48745
   159
    for (Token(kind, s) <- span) {
wenzelm@45644
   160
      val n = s.length
wenzelm@45644
   161
      val s1 = source.substring(i, i + n)
wenzelm@48745
   162
      span1 += Token(kind, s1)
wenzelm@45644
   163
      i += n
wenzelm@45644
   164
    }
wenzelm@45644
   165
wenzelm@50501
   166
    new Command(id, node_name, span1.toList, source, results, markup)
wenzelm@45644
   167
  }
wenzelm@45644
   168
wenzelm@52530
   169
  val empty = Command(Document_ID.none, Document.Node.Name.empty, Nil)
wenzelm@49414
   170
wenzelm@52530
   171
  def unparsed(id: Document_ID.Command, source: String, results: Results, markup: Markup_Tree)
wenzelm@50501
   172
      : Command =
wenzelm@50501
   173
    Command(id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)), results, markup)
wenzelm@49414
   174
wenzelm@50501
   175
  def unparsed(source: String): Command =
wenzelm@52530
   176
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   177
wenzelm@52530
   178
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   179
  {
wenzelm@49466
   180
    val text = XML.content(body)
wenzelm@49466
   181
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   182
    unparsed(id, text, results, markup)
wenzelm@49414
   183
  }
wenzelm@49359
   184
wenzelm@44384
   185
wenzelm@44384
   186
  /* perspective */
wenzelm@44384
   187
wenzelm@44474
   188
  object Perspective
wenzelm@44474
   189
  {
wenzelm@44474
   190
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   191
  }
wenzelm@44385
   192
wenzelm@44474
   193
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   194
  {
wenzelm@44474
   195
    def same(that: Perspective): Boolean =
wenzelm@44474
   196
    {
wenzelm@44474
   197
      val cmds1 = this.commands
wenzelm@44474
   198
      val cmds2 = that.commands
wenzelm@48754
   199
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   200
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   201
      cmds1.length == cmds2.length &&
wenzelm@44474
   202
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   203
    }
wenzelm@44385
   204
  }
wenzelm@34318
   205
}
wenzelm@34318
   206
wenzelm@38361
   207
wenzelm@46712
   208
final class Command private(
wenzelm@52530
   209
    val id: Document_ID.Command,
wenzelm@44615
   210
    val node_name: Document.Node.Name,
wenzelm@52535
   211
    val span: List[Token],
wenzelm@49414
   212
    val source: String,
wenzelm@50501
   213
    val init_results: Command.Results,
wenzelm@49414
   214
    val init_markup: Markup_Tree)
wenzelm@34451
   215
{
wenzelm@34859
   216
  /* classification */
wenzelm@34500
   217
wenzelm@52530
   218
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@48754
   219
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   220
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   221
wenzelm@48599
   222
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   223
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   224
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   225
wenzelm@47012
   226
  def name: String =
wenzelm@48718
   227
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@47012
   228
wenzelm@37129
   229
  override def toString =
wenzelm@37373
   230
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   231
wenzelm@34859
   232
wenzelm@52509
   233
  /* source */
wenzelm@34451
   234
wenzelm@46813
   235
  def length: Int = source.length
wenzelm@46813
   236
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   237
wenzelm@46813
   238
  val proper_range: Text.Range =
wenzelm@51048
   239
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   240
wenzelm@38426
   241
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   242
wenzelm@52507
   243
  lazy val symbol_index = Symbol.Index(source)
wenzelm@38579
   244
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   245
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   246
wenzelm@38370
   247
wenzelm@38370
   248
  /* accumulated results */
wenzelm@38370
   249
wenzelm@50501
   250
  val init_state: Command.State =
wenzelm@50501
   251
    Command.State(this, results = init_results, markup = init_markup)
wenzelm@52527
   252
wenzelm@52527
   253
  val empty_state: Command.State = Command.State(this)
immler@34676
   254
}