src/Pure/PIDE/command.scala
author wenzelm
Wed Feb 12 10:50:49 2014 +0100 (2014-02-12 ago)
changeset 55433 d2960d67f163
parent 55432 9c53198dbb1c
child 55434 aa2918d967f0
permissions -rw-r--r--
clarified message_positions: cover alt_id as well;
tuned;
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@52849
    17
  type Edit = (Option[Command], Option[Command])
wenzelm@52849
    18
wenzelm@52849
    19
wenzelm@38361
    20
  /** accumulated results from prover **/
wenzelm@38361
    21
wenzelm@50507
    22
  /* results */
wenzelm@50507
    23
wenzelm@50507
    24
  object Results
wenzelm@50507
    25
  {
wenzelm@51496
    26
    type Entry = (Long, XML.Tree)
wenzelm@50507
    27
    val empty = new Results(SortedMap.empty)
wenzelm@51496
    28
    def make(es: Iterable[Results.Entry]): Results = (empty /: es.iterator)(_ + _)
wenzelm@50507
    29
    def merge(rs: Iterable[Results]): Results = (empty /: rs.iterator)(_ ++ _)
wenzelm@50507
    30
  }
wenzelm@50507
    31
wenzelm@51494
    32
  final class Results private(private val rep: SortedMap[Long, XML.Tree])
wenzelm@50507
    33
  {
wenzelm@50507
    34
    def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
wenzelm@50507
    35
    def get(serial: Long): Option[XML.Tree] = rep.get(serial)
wenzelm@51496
    36
    def entries: Iterator[Results.Entry] = rep.iterator
wenzelm@50508
    37
wenzelm@51496
    38
    def + (entry: Results.Entry): Results =
wenzelm@50508
    39
      if (defined(entry._1)) this
wenzelm@50508
    40
      else new Results(rep + entry)
wenzelm@50508
    41
wenzelm@50508
    42
    def ++ (other: Results): Results =
wenzelm@50508
    43
      if (this eq other) this
wenzelm@50508
    44
      else if (rep.isEmpty) other
wenzelm@50508
    45
      else (this /: other.entries)(_ + _)
wenzelm@50540
    46
wenzelm@51494
    47
    override def hashCode: Int = rep.hashCode
wenzelm@51494
    48
    override def equals(that: Any): Boolean =
wenzelm@51494
    49
      that match {
wenzelm@51494
    50
        case other: Results => rep == other.rep
wenzelm@51494
    51
        case _ => false
wenzelm@51494
    52
      }
wenzelm@50540
    53
    override def toString: String = entries.mkString("Results(", ", ", ")")
wenzelm@50507
    54
  }
wenzelm@50507
    55
wenzelm@50507
    56
wenzelm@50507
    57
  /* state */
wenzelm@50501
    58
wenzelm@43714
    59
  sealed case class State(
wenzelm@50501
    60
    command: Command,
wenzelm@50501
    61
    status: List[Markup] = Nil,
wenzelm@50507
    62
    results: Results = Results.empty,
wenzelm@55432
    63
    markups: Map[String, Markup_Tree] = Map.empty)
wenzelm@38361
    64
  {
wenzelm@55432
    65
    def get_markup(file_name: String): Markup_Tree =
wenzelm@55432
    66
      markups.getOrElse(file_name, Markup_Tree.empty)
wenzelm@55432
    67
wenzelm@55432
    68
    def markup: Markup_Tree = get_markup("")
wenzelm@55432
    69
wenzelm@49645
    70
    def markup_to_XML(filter: XML.Elem => Boolean): XML.Body =
wenzelm@49645
    71
      markup.to_XML(command.range, command.source, filter)
wenzelm@49614
    72
wenzelm@49614
    73
wenzelm@51494
    74
    /* content */
wenzelm@51494
    75
wenzelm@51494
    76
    def eq_content(other: State): Boolean =
wenzelm@51494
    77
      command.source == other.command.source &&
wenzelm@51494
    78
      status == other.status &&
wenzelm@51494
    79
      results == other.results &&
wenzelm@51494
    80
      markup == other.markup
wenzelm@38361
    81
wenzelm@46152
    82
    private def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@55432
    83
wenzelm@55432
    84
    private def add_markup(file_name: String, m: Text.Markup): State =
wenzelm@55432
    85
      copy(markups = markups + (file_name -> (get_markup(file_name) + m)))
wenzelm@38361
    86
wenzelm@52531
    87
    def + (alt_id: Document_ID.Generic, message: XML.Elem): State =
wenzelm@38361
    88
      message match {
wenzelm@50201
    89
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    90
          (this /: msgs)((state, msg) =>
wenzelm@38714
    91
            msg match {
wenzelm@46152
    92
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@50499
    93
                state.add_status(markup)
wenzelm@55432
    94
                  .add_markup("", Text.Info(command.proper_range, elem))  // FIXME cumulation order!?
wenzelm@46152
    95
wenzelm@52536
    96
              case _ =>
wenzelm@52536
    97
                java.lang.System.err.println("Ignored status message: " + msg)
wenzelm@52536
    98
                state
wenzelm@38714
    99
            })
wenzelm@38581
   100
wenzelm@50201
   101
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
   102
          (this /: msgs)((state, msg) =>
wenzelm@55432
   103
            {
wenzelm@55432
   104
              def bad(): Unit = java.lang.System.err.println("Ignored report message: " + msg)
wenzelm@55432
   105
wenzelm@55432
   106
              msg match {
wenzelm@55432
   107
                case XML.Elem(Markup(name, atts @ Position.Reported(id, file_name, raw_range)), args)
wenzelm@55432
   108
                if id == command.id || id == alt_id =>
wenzelm@55432
   109
                  command.chunks.get(file_name) match {
wenzelm@55432
   110
                    case Some(chunk) =>
wenzelm@55432
   111
                      val range = chunk.decode(raw_range)
wenzelm@55432
   112
                      if (chunk.range.contains(range)) {
wenzelm@55432
   113
                        val props = Position.purge(atts)
wenzelm@55432
   114
                        val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@55432
   115
                        state.add_markup(file_name, info)
wenzelm@55432
   116
                      }
wenzelm@55432
   117
                      else {
wenzelm@55432
   118
                        bad()
wenzelm@55432
   119
                        state
wenzelm@55432
   120
                      }
wenzelm@55432
   121
                    case None =>
wenzelm@55432
   122
                      bad()
wenzelm@55432
   123
                      state
wenzelm@55432
   124
                  }
wenzelm@55432
   125
wenzelm@55432
   126
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   127
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   128
                  val range = command.proper_range
wenzelm@55432
   129
                  val props = Position.purge(atts)
wenzelm@55432
   130
                  val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@55432
   131
                  state.add_markup("", info)
wenzelm@55432
   132
wenzelm@55432
   133
                case _ =>
wenzelm@55432
   134
                  // FIXME bad()
wenzelm@55432
   135
                  state
wenzelm@55432
   136
              }
wenzelm@38361
   137
            })
wenzelm@52930
   138
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   139
          props match {
wenzelm@50201
   140
            case Markup.Serial(i) =>
wenzelm@50201
   141
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   142
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   143
wenzelm@55433
   144
              var st = copy(results = results + (i -> message1))
wenzelm@55433
   145
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   146
                for {
wenzelm@55433
   147
                  (file_name, chunk) <- command.chunks
wenzelm@55433
   148
                  range <- Protocol.message_positions(command.id, alt_id, chunk, message)
wenzelm@55433
   149
                } st = st.add_markup(file_name, Text.Info(range, message2))
wenzelm@55433
   150
              }
wenzelm@55433
   151
              st
wenzelm@55432
   152
wenzelm@52536
   153
            case _ =>
wenzelm@52536
   154
              java.lang.System.err.println("Ignored message without serial number: " + message)
wenzelm@52536
   155
              this
wenzelm@38872
   156
          }
wenzelm@38361
   157
      }
wenzelm@52527
   158
wenzelm@52527
   159
    def ++ (other: State): State =
wenzelm@52642
   160
      copy(
wenzelm@52642
   161
        status = other.status ::: status,
wenzelm@52642
   162
        results = results ++ other.results,
wenzelm@55432
   163
        markups =
wenzelm@55432
   164
          (markups.keySet ++ other.markups.keySet)
wenzelm@55432
   165
            .map(a => a -> (get_markup(a) ++ other.get_markup(a))).toMap
wenzelm@55432
   166
      )
wenzelm@38361
   167
  }
wenzelm@38367
   168
wenzelm@38367
   169
wenzelm@55431
   170
wenzelm@55431
   171
  /** static content **/
wenzelm@55431
   172
wenzelm@55431
   173
  /* text chunks */
wenzelm@55431
   174
wenzelm@55431
   175
  abstract class Chunk
wenzelm@55431
   176
  {
wenzelm@55431
   177
    def file_name: String
wenzelm@55431
   178
    def length: Int
wenzelm@55431
   179
    def range: Text.Range
wenzelm@55431
   180
    def decode(r: Text.Range): Text.Range
wenzelm@55431
   181
  }
wenzelm@55431
   182
wenzelm@55431
   183
  class File(val file_name: String, text: CharSequence) extends Chunk
wenzelm@55431
   184
  {
wenzelm@55431
   185
    val length = text.length
wenzelm@55431
   186
    val range = Text.Range(0, length)
wenzelm@55431
   187
    private val symbol_index = Symbol.Index(text)
wenzelm@55431
   188
    def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@55431
   189
  }
wenzelm@55431
   190
wenzelm@55431
   191
wenzelm@45644
   192
  /* make commands */
wenzelm@45644
   193
wenzelm@54462
   194
  def name(span: List[Token]): String =
wenzelm@54462
   195
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@48745
   196
wenzelm@55431
   197
  type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, File)])]
wenzelm@54513
   198
wenzelm@52524
   199
  def apply(
wenzelm@52530
   200
    id: Document_ID.Command,
wenzelm@52524
   201
    node_name: Document.Node.Name,
wenzelm@54519
   202
    blobs: List[Blob],
wenzelm@54462
   203
    span: List[Token],
wenzelm@52524
   204
    results: Results = Results.empty,
wenzelm@52524
   205
    markup: Markup_Tree = Markup_Tree.empty): Command =
wenzelm@45644
   206
  {
wenzelm@45644
   207
    val source: String =
wenzelm@48745
   208
      span match {
wenzelm@45644
   209
        case List(tok) => tok.source
wenzelm@48745
   210
        case _ => span.map(_.source).mkString
wenzelm@45644
   211
      }
wenzelm@45644
   212
wenzelm@48745
   213
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
   214
    var i = 0
wenzelm@48745
   215
    for (Token(kind, s) <- span) {
wenzelm@45644
   216
      val n = s.length
wenzelm@45644
   217
      val s1 = source.substring(i, i + n)
wenzelm@48745
   218
      span1 += Token(kind, s1)
wenzelm@45644
   219
      i += n
wenzelm@45644
   220
    }
wenzelm@45644
   221
wenzelm@54519
   222
    new Command(id, node_name, blobs, span1.toList, source, results, markup)
wenzelm@45644
   223
  }
wenzelm@45644
   224
wenzelm@54513
   225
  val empty = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
wenzelm@49414
   226
wenzelm@52530
   227
  def unparsed(id: Document_ID.Command, source: String, results: Results, markup: Markup_Tree)
wenzelm@50501
   228
      : Command =
wenzelm@54519
   229
    Command(id, Document.Node.Name.empty, Nil, List(Token(Token.Kind.UNPARSED, source)),
wenzelm@54462
   230
      results, markup)
wenzelm@49414
   231
wenzelm@50501
   232
  def unparsed(source: String): Command =
wenzelm@52530
   233
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   234
wenzelm@52530
   235
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   236
  {
wenzelm@49466
   237
    val text = XML.content(body)
wenzelm@49466
   238
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   239
    unparsed(id, text, results, markup)
wenzelm@49414
   240
  }
wenzelm@49359
   241
wenzelm@44384
   242
wenzelm@44384
   243
  /* perspective */
wenzelm@44384
   244
wenzelm@44474
   245
  object Perspective
wenzelm@44474
   246
  {
wenzelm@44474
   247
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   248
  }
wenzelm@44385
   249
wenzelm@44474
   250
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   251
  {
wenzelm@44474
   252
    def same(that: Perspective): Boolean =
wenzelm@44474
   253
    {
wenzelm@44474
   254
      val cmds1 = this.commands
wenzelm@44474
   255
      val cmds2 = that.commands
wenzelm@48754
   256
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   257
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   258
      cmds1.length == cmds2.length &&
wenzelm@44474
   259
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   260
    }
wenzelm@44385
   261
  }
wenzelm@34318
   262
}
wenzelm@34318
   263
wenzelm@38361
   264
wenzelm@46712
   265
final class Command private(
wenzelm@52530
   266
    val id: Document_ID.Command,
wenzelm@44615
   267
    val node_name: Document.Node.Name,
wenzelm@54519
   268
    val blobs: List[Command.Blob],
wenzelm@52535
   269
    val span: List[Token],
wenzelm@49414
   270
    val source: String,
wenzelm@50501
   271
    val init_results: Command.Results,
wenzelm@49414
   272
    val init_markup: Markup_Tree)
wenzelm@55431
   273
  extends Command.Chunk
wenzelm@34451
   274
{
wenzelm@34859
   275
  /* classification */
wenzelm@34500
   276
wenzelm@52530
   277
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@48754
   278
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   279
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   280
wenzelm@48599
   281
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   282
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   283
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   284
wenzelm@54462
   285
  def name: String = Command.name(span)
wenzelm@47012
   286
wenzelm@37129
   287
  override def toString =
wenzelm@37373
   288
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   289
wenzelm@34859
   290
wenzelm@54519
   291
  /* blobs */
wenzelm@54519
   292
wenzelm@54530
   293
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   294
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   295
wenzelm@54519
   296
  def blobs_digests: List[SHA1.Digest] =
wenzelm@55431
   297
    for (Exn.Res((_, Some((digest, _)))) <- blobs) yield digest
wenzelm@54519
   298
wenzelm@55432
   299
  val chunks: Map[String, Command.Chunk] =
wenzelm@55432
   300
    (("" -> this) ::
wenzelm@55432
   301
      (for (Exn.Res((name, Some((_, file)))) <- blobs) yield (name.node -> file))).toMap
wenzelm@55432
   302
wenzelm@54519
   303
wenzelm@52509
   304
  /* source */
wenzelm@34451
   305
wenzelm@55431
   306
  def file_name: String = ""
wenzelm@55431
   307
wenzelm@46813
   308
  def length: Int = source.length
wenzelm@46813
   309
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   310
wenzelm@46813
   311
  val proper_range: Text.Range =
wenzelm@51048
   312
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   313
wenzelm@38426
   314
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   315
wenzelm@55430
   316
  private lazy val symbol_index = Symbol.Index(source)
wenzelm@38579
   317
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   318
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   319
wenzelm@38370
   320
wenzelm@38370
   321
  /* accumulated results */
wenzelm@38370
   322
wenzelm@50501
   323
  val init_state: Command.State =
wenzelm@55432
   324
    Command.State(this, results = init_results, markups = Map("" -> init_markup))
wenzelm@52527
   325
wenzelm@52527
   326
  val empty_state: Command.State = Command.State(this)
immler@34676
   327
}