src/Pure/PIDE/command.scala
author wenzelm
Mon Mar 31 15:05:24 2014 +0200 (2014-03-31 ago)
changeset 56335 8953d4cc060a
parent 56301 1da7b4c33db9
child 56359 bca016a9a18d
permissions -rw-r--r--
store blob content within document node: aux. files that were once open are made persistent;
proper structural equality for Command.File and Symbol.Index;
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@55648
    18
  type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, 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@51496
    38
    def entries: 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@50508
    47
      else (this /: other.entries)(_ + _)
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@50540
    55
    override def toString: String = entries.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@55649
    63
    val markup: Markup_Index = Markup_Index(false, "")
wenzelm@55649
    64
  }
wenzelm@55649
    65
wenzelm@55649
    66
  sealed case class Markup_Index(status: Boolean, file_name: String)
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@55649
   112
    /* markup */
wenzelm@55432
   113
wenzelm@55650
   114
    def markup(index: Markup_Index): Markup_Tree = markups(index)
wenzelm@55432
   115
wenzelm@49614
   116
wenzelm@51494
   117
    /* content */
wenzelm@51494
   118
wenzelm@51494
   119
    def eq_content(other: State): Boolean =
wenzelm@51494
   120
      command.source == other.command.source &&
wenzelm@51494
   121
      status == other.status &&
wenzelm@51494
   122
      results == other.results &&
wenzelm@55434
   123
      markups == other.markups
wenzelm@38361
   124
wenzelm@55649
   125
    private def add_status(st: Markup): State =
wenzelm@55649
   126
      copy(status = st :: status)
wenzelm@55432
   127
wenzelm@55649
   128
    private def add_markup(status: Boolean, file_name: String, m: Text.Markup): State =
wenzelm@55649
   129
    {
wenzelm@55649
   130
      val markups1 =
wenzelm@55649
   131
        if (status || Protocol.status_elements(m.info.name))
wenzelm@55649
   132
          markups.add(Markup_Index(true, file_name), m)
wenzelm@55649
   133
        else markups
wenzelm@55649
   134
      copy(markups = markups1.add(Markup_Index(false, file_name), m))
wenzelm@55649
   135
    }
wenzelm@38361
   136
wenzelm@56295
   137
    def + (valid_id: Document_ID.Generic => Boolean, message: XML.Elem): State =
wenzelm@38361
   138
      message match {
wenzelm@50201
   139
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
   140
          (this /: msgs)((state, msg) =>
wenzelm@38714
   141
            msg match {
wenzelm@46152
   142
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@55649
   143
                state.
wenzelm@55649
   144
                  add_status(markup).
wenzelm@55649
   145
                  add_markup(true, "", Text.Info(command.proper_range, elem))
wenzelm@52536
   146
              case _ =>
wenzelm@55618
   147
                System.err.println("Ignored status message: " + msg)
wenzelm@52536
   148
                state
wenzelm@38714
   149
            })
wenzelm@38581
   150
wenzelm@50201
   151
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
   152
          (this /: msgs)((state, msg) =>
wenzelm@55432
   153
            {
wenzelm@55618
   154
              def bad(): Unit = System.err.println("Ignored report message: " + msg)
wenzelm@55432
   155
wenzelm@55432
   156
              msg match {
wenzelm@55884
   157
                case XML.Elem(Markup(name,
wenzelm@55884
   158
                  atts @ Position.Reported(id, file_name, symbol_range)), args)
wenzelm@56295
   159
                if valid_id(id) =>
wenzelm@55432
   160
                  command.chunks.get(file_name) match {
wenzelm@55432
   161
                    case Some(chunk) =>
wenzelm@55884
   162
                      chunk.incorporate(symbol_range) match {
wenzelm@55550
   163
                        case Some(range) =>
wenzelm@55822
   164
                          val props = Position.purge(atts)
wenzelm@55822
   165
                          val info = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@55822
   166
                          state.add_markup(false, file_name, info)
wenzelm@55550
   167
                        case None => bad(); state
wenzelm@55432
   168
                      }
wenzelm@55550
   169
                    case None => bad(); state
wenzelm@55432
   170
                  }
wenzelm@55432
   171
wenzelm@55432
   172
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   173
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   174
                  val range = command.proper_range
wenzelm@55432
   175
                  val props = Position.purge(atts)
wenzelm@55432
   176
                  val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@55649
   177
                  state.add_markup(false, "", info)
wenzelm@55432
   178
wenzelm@55550
   179
                case _ => /* FIXME bad(); */ state
wenzelm@55432
   180
              }
wenzelm@38361
   181
            })
wenzelm@52930
   182
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   183
          props match {
wenzelm@50201
   184
            case Markup.Serial(i) =>
wenzelm@50201
   185
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   186
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   187
wenzelm@55433
   188
              var st = copy(results = results + (i -> message1))
wenzelm@55433
   189
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   190
                for {
wenzelm@55433
   191
                  (file_name, chunk) <- command.chunks
wenzelm@56295
   192
                  range <- Protocol.message_positions(valid_id, chunk, message)
wenzelm@55649
   193
                } st = st.add_markup(false, file_name, Text.Info(range, message2))
wenzelm@55433
   194
              }
wenzelm@55433
   195
              st
wenzelm@55432
   196
wenzelm@52536
   197
            case _ =>
wenzelm@55618
   198
              System.err.println("Ignored message without serial number: " + message)
wenzelm@52536
   199
              this
wenzelm@38872
   200
          }
wenzelm@56295
   201
    }
wenzelm@38361
   202
  }
wenzelm@38367
   203
wenzelm@38367
   204
wenzelm@55431
   205
wenzelm@55431
   206
  /** static content **/
wenzelm@55431
   207
wenzelm@55431
   208
  /* text chunks */
wenzelm@55431
   209
wenzelm@55431
   210
  abstract class Chunk
wenzelm@55431
   211
  {
wenzelm@55431
   212
    def file_name: String
wenzelm@55431
   213
    def length: Int
wenzelm@55431
   214
    def range: Text.Range
wenzelm@55884
   215
    def decode(symbol_range: Symbol.Range): Text.Range
wenzelm@55822
   216
wenzelm@55884
   217
    def incorporate(symbol_range: Symbol.Range): Option[Text.Range] =
wenzelm@55822
   218
    {
wenzelm@55884
   219
      def inc(r: Symbol.Range): Option[Text.Range] =
wenzelm@55822
   220
        range.try_restrict(decode(r)) match {
wenzelm@55822
   221
          case Some(r1) if !r1.is_singularity => Some(r1)
wenzelm@55822
   222
          case _ => None
wenzelm@55822
   223
        }
wenzelm@55884
   224
     inc(symbol_range) orElse inc(symbol_range - 1)
wenzelm@55822
   225
    }
wenzelm@55431
   226
  }
wenzelm@55431
   227
wenzelm@56335
   228
  // file name and position information, *without* persistent text
wenzelm@55431
   229
  class File(val file_name: String, text: CharSequence) extends Chunk
wenzelm@55431
   230
  {
wenzelm@55431
   231
    val length = text.length
wenzelm@55431
   232
    val range = Text.Range(0, length)
wenzelm@55431
   233
    private val symbol_index = Symbol.Index(text)
wenzelm@55884
   234
    def decode(symbol_range: Symbol.Range): Text.Range = symbol_index.decode(symbol_range)
wenzelm@55777
   235
wenzelm@56335
   236
    private val hash: Int = (file_name, length, symbol_index).hashCode
wenzelm@56335
   237
    override def hashCode: Int = hash
wenzelm@56335
   238
    override def equals(that: Any): Boolean =
wenzelm@56335
   239
      that match {
wenzelm@56335
   240
        case other: File =>
wenzelm@56335
   241
          hash == other.hash &&
wenzelm@56335
   242
          file_name == other.file_name &&
wenzelm@56335
   243
          length == other.length &&
wenzelm@56335
   244
          symbol_index == other.symbol_index
wenzelm@56335
   245
        case _ => false
wenzelm@56335
   246
      }
wenzelm@55777
   247
    override def toString: String = "Command.File(" + file_name + ")"
wenzelm@55431
   248
  }
wenzelm@55431
   249
wenzelm@55431
   250
wenzelm@45644
   251
  /* make commands */
wenzelm@45644
   252
wenzelm@54462
   253
  def name(span: List[Token]): String =
wenzelm@54462
   254
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@48745
   255
wenzelm@55648
   256
  private def source_span(span: List[Token]): (String, List[Token]) =
wenzelm@45644
   257
  {
wenzelm@45644
   258
    val source: String =
wenzelm@48745
   259
      span match {
wenzelm@45644
   260
        case List(tok) => tok.source
wenzelm@48745
   261
        case _ => span.map(_.source).mkString
wenzelm@45644
   262
      }
wenzelm@45644
   263
wenzelm@48745
   264
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
   265
    var i = 0
wenzelm@48745
   266
    for (Token(kind, s) <- span) {
wenzelm@45644
   267
      val n = s.length
wenzelm@45644
   268
      val s1 = source.substring(i, i + n)
wenzelm@48745
   269
      span1 += Token(kind, s1)
wenzelm@45644
   270
      i += n
wenzelm@45644
   271
    }
wenzelm@55648
   272
    (source, span1.toList)
wenzelm@45644
   273
  }
wenzelm@45644
   274
wenzelm@55648
   275
  def apply(
wenzelm@55648
   276
    id: Document_ID.Command,
wenzelm@55648
   277
    node_name: Document.Node.Name,
wenzelm@55648
   278
    blobs: List[Blob],
wenzelm@55648
   279
    span: List[Token]): Command =
wenzelm@55648
   280
  {
wenzelm@55648
   281
    val (source, span1) = source_span(span)
wenzelm@55648
   282
    new Command(id, node_name, blobs, span1, source, Results.empty, Markup_Tree.empty)
wenzelm@55648
   283
  }
wenzelm@49414
   284
wenzelm@55648
   285
  val empty: Command = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
wenzelm@55648
   286
wenzelm@55648
   287
  def unparsed(
wenzelm@55648
   288
    id: Document_ID.Command,
wenzelm@55648
   289
    source: String,
wenzelm@55648
   290
    results: Results,
wenzelm@55648
   291
    markup: Markup_Tree): Command =
wenzelm@55648
   292
  {
wenzelm@55648
   293
    val (source1, span) = source_span(List(Token(Token.Kind.UNPARSED, source)))
wenzelm@55648
   294
    new Command(id, Document.Node.Name.empty, Nil, span, source1, results, markup)
wenzelm@55648
   295
  }
wenzelm@49414
   296
wenzelm@50501
   297
  def unparsed(source: String): Command =
wenzelm@52530
   298
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   299
wenzelm@52530
   300
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   301
  {
wenzelm@49466
   302
    val text = XML.content(body)
wenzelm@49466
   303
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   304
    unparsed(id, text, results, markup)
wenzelm@49414
   305
  }
wenzelm@49359
   306
wenzelm@44384
   307
wenzelm@44384
   308
  /* perspective */
wenzelm@44384
   309
wenzelm@44474
   310
  object Perspective
wenzelm@44474
   311
  {
wenzelm@44474
   312
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   313
  }
wenzelm@44385
   314
wenzelm@44474
   315
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   316
  {
wenzelm@44474
   317
    def same(that: Perspective): Boolean =
wenzelm@44474
   318
    {
wenzelm@44474
   319
      val cmds1 = this.commands
wenzelm@44474
   320
      val cmds2 = that.commands
wenzelm@48754
   321
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   322
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   323
      cmds1.length == cmds2.length &&
wenzelm@44474
   324
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   325
    }
wenzelm@44385
   326
  }
wenzelm@34318
   327
}
wenzelm@34318
   328
wenzelm@38361
   329
wenzelm@46712
   330
final class Command private(
wenzelm@52530
   331
    val id: Document_ID.Command,
wenzelm@44615
   332
    val node_name: Document.Node.Name,
wenzelm@54519
   333
    val blobs: List[Command.Blob],
wenzelm@52535
   334
    val span: List[Token],
wenzelm@49414
   335
    val source: String,
wenzelm@50501
   336
    val init_results: Command.Results,
wenzelm@49414
   337
    val init_markup: Markup_Tree)
wenzelm@55431
   338
  extends Command.Chunk
wenzelm@34451
   339
{
wenzelm@34859
   340
  /* classification */
wenzelm@34500
   341
wenzelm@52530
   342
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@48754
   343
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   344
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   345
wenzelm@48599
   346
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   347
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   348
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   349
wenzelm@54462
   350
  def name: String = Command.name(span)
wenzelm@47012
   351
wenzelm@37129
   352
  override def toString =
wenzelm@37373
   353
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   354
wenzelm@34859
   355
wenzelm@54519
   356
  /* blobs */
wenzelm@54519
   357
wenzelm@55785
   358
  def blobs_changed(doc_blobs: Document.Blobs): Boolean =
wenzelm@55785
   359
    blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
wenzelm@55785
   360
wenzelm@54530
   361
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   362
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   363
wenzelm@54519
   364
  def blobs_digests: List[SHA1.Digest] =
wenzelm@55431
   365
    for (Exn.Res((_, Some((digest, _)))) <- blobs) yield digest
wenzelm@54519
   366
wenzelm@55432
   367
  val chunks: Map[String, Command.Chunk] =
wenzelm@55432
   368
    (("" -> this) ::
wenzelm@55432
   369
      (for (Exn.Res((name, Some((_, file)))) <- blobs) yield (name.node -> file))).toMap
wenzelm@55432
   370
wenzelm@54519
   371
wenzelm@52509
   372
  /* source */
wenzelm@34451
   373
wenzelm@55431
   374
  def file_name: String = ""
wenzelm@55431
   375
wenzelm@46813
   376
  def length: Int = source.length
wenzelm@46813
   377
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   378
wenzelm@46813
   379
  val proper_range: Text.Range =
wenzelm@51048
   380
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   381
wenzelm@38426
   382
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   383
wenzelm@55430
   384
  private lazy val symbol_index = Symbol.Index(source)
wenzelm@55884
   385
  def decode(symbol_offset: Symbol.Offset): Text.Offset = symbol_index.decode(symbol_offset)
wenzelm@55884
   386
  def decode(symbol_range: Symbol.Range): Text.Range = symbol_index.decode(symbol_range)
wenzelm@38370
   387
wenzelm@38370
   388
wenzelm@38370
   389
  /* accumulated results */
wenzelm@38370
   390
wenzelm@50501
   391
  val init_state: Command.State =
wenzelm@55649
   392
    Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
wenzelm@52527
   393
wenzelm@52527
   394
  val empty_state: Command.State = Command.State(this)
immler@34676
   395
}