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