src/Pure/PIDE/command.scala
author wenzelm
Tue Apr 08 12:19:33 2014 +0200 (2014-04-08 ago)
changeset 56462 b64b0cb845fe
parent 56395 0546e036d1c0
child 56463 013dfac0811a
permissions -rw-r--r--
more explicit Command.Chunk types, less ooddities;
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@56462
    18
  type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, Chunk.File)])]
wenzelm@55648
    19
wenzelm@52849
    20
wenzelm@52849
    21
wenzelm@38361
    22
  /** accumulated results from prover **/
wenzelm@38361
    23
wenzelm@50507
    24
  /* results */
wenzelm@50507
    25
wenzelm@50507
    26
  object Results
wenzelm@50507
    27
  {
wenzelm@51496
    28
    type Entry = (Long, XML.Tree)
wenzelm@50507
    29
    val empty = new Results(SortedMap.empty)
wenzelm@56299
    30
    def make(es: List[Results.Entry]): Results = (empty /: es)(_ + _)
wenzelm@56299
    31
    def merge(rs: List[Results]): Results = (empty /: rs)(_ ++ _)
wenzelm@50507
    32
  }
wenzelm@50507
    33
wenzelm@51494
    34
  final class Results private(private val rep: SortedMap[Long, XML.Tree])
wenzelm@50507
    35
  {
wenzelm@50507
    36
    def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
wenzelm@50507
    37
    def get(serial: Long): Option[XML.Tree] = rep.get(serial)
wenzelm@56372
    38
    def iterator: Iterator[Results.Entry] = rep.iterator
wenzelm@50508
    39
wenzelm@51496
    40
    def + (entry: Results.Entry): Results =
wenzelm@50508
    41
      if (defined(entry._1)) this
wenzelm@50508
    42
      else new Results(rep + entry)
wenzelm@50508
    43
wenzelm@50508
    44
    def ++ (other: Results): Results =
wenzelm@50508
    45
      if (this eq other) this
wenzelm@50508
    46
      else if (rep.isEmpty) other
wenzelm@56372
    47
      else (this /: other.iterator)(_ + _)
wenzelm@50540
    48
wenzelm@51494
    49
    override def hashCode: Int = rep.hashCode
wenzelm@51494
    50
    override def equals(that: Any): Boolean =
wenzelm@51494
    51
      that match {
wenzelm@51494
    52
        case other: Results => rep == other.rep
wenzelm@51494
    53
        case _ => false
wenzelm@51494
    54
      }
wenzelm@56372
    55
    override def toString: String = iterator.mkString("Results(", ", ", ")")
wenzelm@50507
    56
  }
wenzelm@50507
    57
wenzelm@50507
    58
wenzelm@56462
    59
  /* source chunks */
wenzelm@56462
    60
wenzelm@56462
    61
  abstract class Chunk
wenzelm@56462
    62
  {
wenzelm@56462
    63
    def name: Chunk.Name
wenzelm@56462
    64
    def length: Int
wenzelm@56462
    65
    def range: Text.Range
wenzelm@56462
    66
    def symbol_index: Symbol.Index
wenzelm@56462
    67
wenzelm@56462
    68
    def decode(symbol_offset: Symbol.Offset): Text.Offset = symbol_index.decode(symbol_offset)
wenzelm@56462
    69
    def decode(symbol_range: Symbol.Range): Text.Range = symbol_index.decode(symbol_range)
wenzelm@56462
    70
    def incorporate(symbol_range: Symbol.Range): Option[Text.Range] =
wenzelm@56462
    71
    {
wenzelm@56462
    72
      def in(r: Symbol.Range): Option[Text.Range] =
wenzelm@56462
    73
        range.try_restrict(decode(r)) match {
wenzelm@56462
    74
          case Some(r1) if !r1.is_singularity => Some(r1)
wenzelm@56462
    75
          case _ => None
wenzelm@56462
    76
        }
wenzelm@56462
    77
     in(symbol_range) orElse in(symbol_range - 1)
wenzelm@56462
    78
    }
wenzelm@56462
    79
  }
wenzelm@56462
    80
wenzelm@56462
    81
  object Chunk
wenzelm@56462
    82
  {
wenzelm@56462
    83
    sealed abstract class Name
wenzelm@56462
    84
    case object Self extends Name
wenzelm@56462
    85
    case class File_Name(file_name: String) extends Name
wenzelm@56462
    86
wenzelm@56462
    87
    class File(
wenzelm@56462
    88
      file_name: String,
wenzelm@56462
    89
      text: CharSequence  // non-persistent!
wenzelm@56462
    90
    ) extends Chunk
wenzelm@56462
    91
    {
wenzelm@56462
    92
      val name = Chunk.File_Name(file_name)
wenzelm@56462
    93
      val length = text.length
wenzelm@56462
    94
      val range = Text.Range(0, length)
wenzelm@56462
    95
      val symbol_index = Symbol.Index(text)
wenzelm@56462
    96
wenzelm@56462
    97
      private val hash: Int = (name, length, symbol_index).hashCode
wenzelm@56462
    98
      override def hashCode: Int = hash
wenzelm@56462
    99
      override def equals(that: Any): Boolean =
wenzelm@56462
   100
        that match {
wenzelm@56462
   101
          case other: File =>
wenzelm@56462
   102
            hash == other.hash &&
wenzelm@56462
   103
            name == other.name &&
wenzelm@56462
   104
            length == other.length &&
wenzelm@56462
   105
            symbol_index == other.symbol_index
wenzelm@56462
   106
          case _ => false
wenzelm@56462
   107
        }
wenzelm@56462
   108
      override def toString: String = "Command.Chunk.File(" + file_name + ")"
wenzelm@56462
   109
    }
wenzelm@56462
   110
  }
wenzelm@56462
   111
wenzelm@56462
   112
wenzelm@55649
   113
  /* markup */
wenzelm@55649
   114
wenzelm@55649
   115
  object Markup_Index
wenzelm@55649
   116
  {
wenzelm@56462
   117
    val markup: Markup_Index = Markup_Index(false, Chunk.Self)
wenzelm@55649
   118
  }
wenzelm@55649
   119
wenzelm@56462
   120
  sealed case class Markup_Index(status: Boolean, chunk_name: Chunk.Name)
wenzelm@55649
   121
wenzelm@55649
   122
  object Markups
wenzelm@55649
   123
  {
wenzelm@55649
   124
    val empty: Markups = new Markups(Map.empty)
wenzelm@55649
   125
wenzelm@55649
   126
    def init(markup: Markup_Tree): Markups =
wenzelm@55649
   127
      new Markups(Map(Markup_Index.markup -> markup))
wenzelm@55649
   128
  }
wenzelm@55649
   129
wenzelm@55649
   130
  final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
wenzelm@55649
   131
  {
wenzelm@55649
   132
    def apply(index: Markup_Index): Markup_Tree =
wenzelm@55649
   133
      rep.getOrElse(index, Markup_Tree.empty)
wenzelm@55649
   134
wenzelm@55649
   135
    def add(index: Markup_Index, markup: Text.Markup): Markups =
wenzelm@55649
   136
      new Markups(rep + (index -> (this(index) + markup)))
wenzelm@55649
   137
wenzelm@55649
   138
    override def hashCode: Int = rep.hashCode
wenzelm@55649
   139
    override def equals(that: Any): Boolean =
wenzelm@55649
   140
      that match {
wenzelm@55649
   141
        case other: Markups => rep == other.rep
wenzelm@55649
   142
        case _ => false
wenzelm@55649
   143
      }
wenzelm@55649
   144
    override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
wenzelm@55649
   145
  }
wenzelm@55649
   146
wenzelm@55649
   147
wenzelm@50507
   148
  /* state */
wenzelm@50501
   149
wenzelm@56299
   150
  object State
wenzelm@56299
   151
  {
wenzelm@56299
   152
    def merge_results(states: List[State]): Command.Results =
wenzelm@56299
   153
      Results.merge(states.map(_.results))
wenzelm@56299
   154
wenzelm@56301
   155
    def merge_markup(states: List[State], index: Markup_Index,
wenzelm@56301
   156
        range: Text.Range, elements: Document.Elements): Markup_Tree =
wenzelm@56301
   157
      Markup_Tree.merge(states.map(_.markup(index)), range, elements)
wenzelm@56299
   158
  }
wenzelm@56299
   159
wenzelm@43714
   160
  sealed case class State(
wenzelm@50501
   161
    command: Command,
wenzelm@50501
   162
    status: List[Markup] = Nil,
wenzelm@50507
   163
    results: Results = Results.empty,
wenzelm@55649
   164
    markups: Markups = Markups.empty)
wenzelm@38361
   165
  {
wenzelm@56395
   166
    lazy val protocol_status: Protocol.Status =
wenzelm@56395
   167
    {
wenzelm@56395
   168
      val warnings =
wenzelm@56395
   169
        if (results.iterator.exists(p => Protocol.is_warning(p._2)))
wenzelm@56395
   170
          List(Markup(Markup.WARNING, Nil))
wenzelm@56395
   171
        else Nil
wenzelm@56395
   172
      val errors =
wenzelm@56395
   173
        if (results.iterator.exists(p => Protocol.is_error(p._2)))
wenzelm@56395
   174
          List(Markup(Markup.ERROR, Nil))
wenzelm@56395
   175
        else Nil
wenzelm@56395
   176
      Protocol.Status.make((warnings ::: errors ::: status).iterator)
wenzelm@56395
   177
    }
wenzelm@55432
   178
wenzelm@55650
   179
    def markup(index: Markup_Index): Markup_Tree = markups(index)
wenzelm@55432
   180
wenzelm@49614
   181
wenzelm@51494
   182
    def eq_content(other: State): Boolean =
wenzelm@51494
   183
      command.source == other.command.source &&
wenzelm@51494
   184
      status == other.status &&
wenzelm@51494
   185
      results == other.results &&
wenzelm@55434
   186
      markups == other.markups
wenzelm@38361
   187
wenzelm@55649
   188
    private def add_status(st: Markup): State =
wenzelm@55649
   189
      copy(status = st :: status)
wenzelm@55432
   190
wenzelm@56462
   191
    private def add_markup(status: Boolean, chunk_name: Command.Chunk.Name, m: Text.Markup): State =
wenzelm@55649
   192
    {
wenzelm@55649
   193
      val markups1 =
wenzelm@56395
   194
        if (status || Protocol.liberal_status_elements(m.info.name))
wenzelm@56462
   195
          markups.add(Markup_Index(true, chunk_name), m)
wenzelm@55649
   196
        else markups
wenzelm@56462
   197
      copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
wenzelm@55649
   198
    }
wenzelm@38361
   199
wenzelm@56295
   200
    def + (valid_id: Document_ID.Generic => Boolean, message: XML.Elem): State =
wenzelm@38361
   201
      message match {
wenzelm@50201
   202
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
   203
          (this /: msgs)((state, msg) =>
wenzelm@38714
   204
            msg match {
wenzelm@46152
   205
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@55649
   206
                state.
wenzelm@55649
   207
                  add_status(markup).
wenzelm@56462
   208
                  add_markup(true, Command.Chunk.Self, Text.Info(command.proper_range, elem))
wenzelm@52536
   209
              case _ =>
wenzelm@55618
   210
                System.err.println("Ignored status message: " + msg)
wenzelm@52536
   211
                state
wenzelm@38714
   212
            })
wenzelm@38581
   213
wenzelm@50201
   214
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
   215
          (this /: msgs)((state, msg) =>
wenzelm@55432
   216
            {
wenzelm@55618
   217
              def bad(): Unit = System.err.println("Ignored report message: " + msg)
wenzelm@55432
   218
wenzelm@55432
   219
              msg match {
wenzelm@55884
   220
                case XML.Elem(Markup(name,
wenzelm@56462
   221
                  atts @ Position.Reported(id, chunk_name, symbol_range)), args)
wenzelm@56295
   222
                if valid_id(id) =>
wenzelm@56462
   223
                  command.chunks.get(chunk_name) match {
wenzelm@55432
   224
                    case Some(chunk) =>
wenzelm@55884
   225
                      chunk.incorporate(symbol_range) match {
wenzelm@55550
   226
                        case Some(range) =>
wenzelm@55822
   227
                          val props = Position.purge(atts)
wenzelm@55822
   228
                          val info = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56462
   229
                          state.add_markup(false, chunk_name, info)
wenzelm@55550
   230
                        case None => bad(); state
wenzelm@55432
   231
                      }
wenzelm@55550
   232
                    case None => bad(); state
wenzelm@55432
   233
                  }
wenzelm@55432
   234
wenzelm@55432
   235
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   236
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   237
                  val range = command.proper_range
wenzelm@55432
   238
                  val props = Position.purge(atts)
wenzelm@55432
   239
                  val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56462
   240
                  state.add_markup(false, Command.Chunk.Self, info)
wenzelm@55432
   241
wenzelm@55550
   242
                case _ => /* FIXME bad(); */ state
wenzelm@55432
   243
              }
wenzelm@38361
   244
            })
wenzelm@52930
   245
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   246
          props match {
wenzelm@50201
   247
            case Markup.Serial(i) =>
wenzelm@50201
   248
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   249
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   250
wenzelm@55433
   251
              var st = copy(results = results + (i -> message1))
wenzelm@55433
   252
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   253
                for {
wenzelm@56462
   254
                  (chunk_name, chunk) <- command.chunks
wenzelm@56295
   255
                  range <- Protocol.message_positions(valid_id, chunk, message)
wenzelm@56462
   256
                } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
wenzelm@55433
   257
              }
wenzelm@55433
   258
              st
wenzelm@55432
   259
wenzelm@52536
   260
            case _ =>
wenzelm@55618
   261
              System.err.println("Ignored message without serial number: " + message)
wenzelm@52536
   262
              this
wenzelm@38872
   263
          }
wenzelm@56295
   264
    }
wenzelm@38361
   265
  }
wenzelm@38367
   266
wenzelm@38367
   267
wenzelm@55431
   268
wenzelm@55431
   269
  /** static content **/
wenzelm@55431
   270
wenzelm@45644
   271
  /* make commands */
wenzelm@45644
   272
wenzelm@54462
   273
  def name(span: List[Token]): String =
wenzelm@54462
   274
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@48745
   275
wenzelm@55648
   276
  private def source_span(span: List[Token]): (String, List[Token]) =
wenzelm@45644
   277
  {
wenzelm@45644
   278
    val source: String =
wenzelm@48745
   279
      span match {
wenzelm@45644
   280
        case List(tok) => tok.source
wenzelm@48745
   281
        case _ => span.map(_.source).mkString
wenzelm@45644
   282
      }
wenzelm@45644
   283
wenzelm@48745
   284
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
   285
    var i = 0
wenzelm@48745
   286
    for (Token(kind, s) <- span) {
wenzelm@45644
   287
      val n = s.length
wenzelm@45644
   288
      val s1 = source.substring(i, i + n)
wenzelm@48745
   289
      span1 += Token(kind, s1)
wenzelm@45644
   290
      i += n
wenzelm@45644
   291
    }
wenzelm@55648
   292
    (source, span1.toList)
wenzelm@45644
   293
  }
wenzelm@45644
   294
wenzelm@55648
   295
  def apply(
wenzelm@55648
   296
    id: Document_ID.Command,
wenzelm@55648
   297
    node_name: Document.Node.Name,
wenzelm@55648
   298
    blobs: List[Blob],
wenzelm@55648
   299
    span: List[Token]): Command =
wenzelm@55648
   300
  {
wenzelm@55648
   301
    val (source, span1) = source_span(span)
wenzelm@55648
   302
    new Command(id, node_name, blobs, span1, source, Results.empty, Markup_Tree.empty)
wenzelm@55648
   303
  }
wenzelm@49414
   304
wenzelm@55648
   305
  val empty: Command = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
wenzelm@55648
   306
wenzelm@55648
   307
  def unparsed(
wenzelm@55648
   308
    id: Document_ID.Command,
wenzelm@55648
   309
    source: String,
wenzelm@55648
   310
    results: Results,
wenzelm@55648
   311
    markup: Markup_Tree): Command =
wenzelm@55648
   312
  {
wenzelm@55648
   313
    val (source1, span) = source_span(List(Token(Token.Kind.UNPARSED, source)))
wenzelm@55648
   314
    new Command(id, Document.Node.Name.empty, Nil, span, source1, results, markup)
wenzelm@55648
   315
  }
wenzelm@49414
   316
wenzelm@50501
   317
  def unparsed(source: String): Command =
wenzelm@52530
   318
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   319
wenzelm@52530
   320
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   321
  {
wenzelm@49466
   322
    val text = XML.content(body)
wenzelm@49466
   323
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   324
    unparsed(id, text, results, markup)
wenzelm@49414
   325
  }
wenzelm@49359
   326
wenzelm@44384
   327
wenzelm@44384
   328
  /* perspective */
wenzelm@44384
   329
wenzelm@44474
   330
  object Perspective
wenzelm@44474
   331
  {
wenzelm@44474
   332
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   333
  }
wenzelm@44385
   334
wenzelm@44474
   335
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   336
  {
wenzelm@44474
   337
    def same(that: Perspective): Boolean =
wenzelm@44474
   338
    {
wenzelm@44474
   339
      val cmds1 = this.commands
wenzelm@44474
   340
      val cmds2 = that.commands
wenzelm@48754
   341
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   342
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   343
      cmds1.length == cmds2.length &&
wenzelm@44474
   344
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   345
    }
wenzelm@44385
   346
  }
wenzelm@34318
   347
}
wenzelm@34318
   348
wenzelm@38361
   349
wenzelm@46712
   350
final class Command private(
wenzelm@52530
   351
    val id: Document_ID.Command,
wenzelm@44615
   352
    val node_name: Document.Node.Name,
wenzelm@54519
   353
    val blobs: List[Command.Blob],
wenzelm@52535
   354
    val span: List[Token],
wenzelm@49414
   355
    val source: String,
wenzelm@50501
   356
    val init_results: Command.Results,
wenzelm@49414
   357
    val init_markup: Markup_Tree)
wenzelm@34451
   358
{
wenzelm@34859
   359
  /* classification */
wenzelm@34500
   360
wenzelm@52530
   361
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@48754
   362
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   363
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   364
wenzelm@48599
   365
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   366
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   367
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   368
wenzelm@54462
   369
  def name: String = Command.name(span)
wenzelm@47012
   370
wenzelm@37129
   371
  override def toString =
wenzelm@37373
   372
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   373
wenzelm@34859
   374
wenzelm@54519
   375
  /* blobs */
wenzelm@54519
   376
wenzelm@55785
   377
  def blobs_changed(doc_blobs: Document.Blobs): Boolean =
wenzelm@55785
   378
    blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
wenzelm@55785
   379
wenzelm@54530
   380
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   381
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   382
wenzelm@54519
   383
  def blobs_digests: List[SHA1.Digest] =
wenzelm@55431
   384
    for (Exn.Res((_, Some((digest, _)))) <- blobs) yield digest
wenzelm@54519
   385
wenzelm@55432
   386
wenzelm@56462
   387
  /* source chunks */
wenzelm@55431
   388
wenzelm@46813
   389
  def length: Int = source.length
wenzelm@46813
   390
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   391
wenzelm@46813
   392
  val proper_range: Text.Range =
wenzelm@51048
   393
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   394
wenzelm@38426
   395
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   396
wenzelm@56462
   397
  val chunk: Command.Chunk =
wenzelm@56462
   398
    new Command.Chunk {
wenzelm@56462
   399
      def name: Command.Chunk.Name = Command.Chunk.Self
wenzelm@56462
   400
      def length: Int = Command.this.length
wenzelm@56462
   401
      def range: Text.Range = Command.this.range
wenzelm@56462
   402
      lazy val symbol_index = Symbol.Index(source)
wenzelm@56462
   403
    }
wenzelm@56462
   404
wenzelm@56462
   405
  val chunks: Map[Command.Chunk.Name, Command.Chunk] =
wenzelm@56462
   406
    ((Command.Chunk.Self -> chunk) ::
wenzelm@56462
   407
      (for (Exn.Res((name, Some((_, file)))) <- blobs)
wenzelm@56462
   408
        yield (Command.Chunk.File_Name(name.node) -> file))).toMap
wenzelm@38370
   409
wenzelm@38370
   410
wenzelm@38370
   411
  /* accumulated results */
wenzelm@38370
   412
wenzelm@50501
   413
  val init_state: Command.State =
wenzelm@55649
   414
    Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
wenzelm@52527
   415
wenzelm@52527
   416
  val empty_state: Command.State = Command.State(this)
immler@34676
   417
}