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