src/Pure/PIDE/command.scala
author wenzelm
Thu Feb 20 14:36:17 2014 +0100 (2014-02-20 ago)
changeset 55618 995162143ef4
parent 55550 a645277885cf
child 55622 ce575c2212fc
permissions -rw-r--r--
tuned imports;
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@55434
    80
      markups == other.markups
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@55618
    97
                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@55618
   104
              def bad(): Unit = 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@55550
   111
                      chunk.decode(raw_range).try_restrict(chunk.range) match {
wenzelm@55550
   112
                        case Some(range) =>
wenzelm@55550
   113
                          if (!range.is_singularity) {
wenzelm@55550
   114
                            val props = Position.purge(atts)
wenzelm@55550
   115
                            state.add_markup(file_name,
wenzelm@55550
   116
                              Text.Info(range, XML.Elem(Markup(name, props), args)))
wenzelm@55550
   117
                          }
wenzelm@55550
   118
                          else state
wenzelm@55550
   119
                        case None => bad(); state
wenzelm@55432
   120
                      }
wenzelm@55550
   121
                    case None => bad(); state
wenzelm@55432
   122
                  }
wenzelm@55432
   123
wenzelm@55432
   124
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   125
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   126
                  val range = command.proper_range
wenzelm@55432
   127
                  val props = Position.purge(atts)
wenzelm@55432
   128
                  val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@55432
   129
                  state.add_markup("", info)
wenzelm@55432
   130
wenzelm@55550
   131
                case _ => /* FIXME bad(); */ state
wenzelm@55432
   132
              }
wenzelm@38361
   133
            })
wenzelm@52930
   134
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   135
          props match {
wenzelm@50201
   136
            case Markup.Serial(i) =>
wenzelm@50201
   137
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   138
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   139
wenzelm@55433
   140
              var st = copy(results = results + (i -> message1))
wenzelm@55433
   141
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   142
                for {
wenzelm@55433
   143
                  (file_name, chunk) <- command.chunks
wenzelm@55433
   144
                  range <- Protocol.message_positions(command.id, alt_id, chunk, message)
wenzelm@55433
   145
                } st = st.add_markup(file_name, Text.Info(range, message2))
wenzelm@55433
   146
              }
wenzelm@55433
   147
              st
wenzelm@55432
   148
wenzelm@52536
   149
            case _ =>
wenzelm@55618
   150
              System.err.println("Ignored message without serial number: " + message)
wenzelm@52536
   151
              this
wenzelm@38872
   152
          }
wenzelm@38361
   153
      }
wenzelm@52527
   154
wenzelm@52527
   155
    def ++ (other: State): State =
wenzelm@52642
   156
      copy(
wenzelm@52642
   157
        status = other.status ::: status,
wenzelm@52642
   158
        results = results ++ other.results,
wenzelm@55432
   159
        markups =
wenzelm@55432
   160
          (markups.keySet ++ other.markups.keySet)
wenzelm@55432
   161
            .map(a => a -> (get_markup(a) ++ other.get_markup(a))).toMap
wenzelm@55432
   162
      )
wenzelm@38361
   163
  }
wenzelm@38367
   164
wenzelm@38367
   165
wenzelm@55431
   166
wenzelm@55431
   167
  /** static content **/
wenzelm@55431
   168
wenzelm@55431
   169
  /* text chunks */
wenzelm@55431
   170
wenzelm@55431
   171
  abstract class Chunk
wenzelm@55431
   172
  {
wenzelm@55431
   173
    def file_name: String
wenzelm@55431
   174
    def length: Int
wenzelm@55431
   175
    def range: Text.Range
wenzelm@55431
   176
    def decode(r: Text.Range): Text.Range
wenzelm@55431
   177
  }
wenzelm@55431
   178
wenzelm@55431
   179
  class File(val file_name: String, text: CharSequence) extends Chunk
wenzelm@55431
   180
  {
wenzelm@55431
   181
    val length = text.length
wenzelm@55431
   182
    val range = Text.Range(0, length)
wenzelm@55431
   183
    private val symbol_index = Symbol.Index(text)
wenzelm@55431
   184
    def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@55431
   185
  }
wenzelm@55431
   186
wenzelm@55431
   187
wenzelm@45644
   188
  /* make commands */
wenzelm@45644
   189
wenzelm@54462
   190
  def name(span: List[Token]): String =
wenzelm@54462
   191
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@48745
   192
wenzelm@55431
   193
  type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, File)])]
wenzelm@54513
   194
wenzelm@52524
   195
  def apply(
wenzelm@52530
   196
    id: Document_ID.Command,
wenzelm@52524
   197
    node_name: Document.Node.Name,
wenzelm@54519
   198
    blobs: List[Blob],
wenzelm@54462
   199
    span: List[Token],
wenzelm@52524
   200
    results: Results = Results.empty,
wenzelm@52524
   201
    markup: Markup_Tree = Markup_Tree.empty): Command =
wenzelm@45644
   202
  {
wenzelm@45644
   203
    val source: String =
wenzelm@48745
   204
      span match {
wenzelm@45644
   205
        case List(tok) => tok.source
wenzelm@48745
   206
        case _ => span.map(_.source).mkString
wenzelm@45644
   207
      }
wenzelm@45644
   208
wenzelm@48745
   209
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
   210
    var i = 0
wenzelm@48745
   211
    for (Token(kind, s) <- span) {
wenzelm@45644
   212
      val n = s.length
wenzelm@45644
   213
      val s1 = source.substring(i, i + n)
wenzelm@48745
   214
      span1 += Token(kind, s1)
wenzelm@45644
   215
      i += n
wenzelm@45644
   216
    }
wenzelm@45644
   217
wenzelm@54519
   218
    new Command(id, node_name, blobs, span1.toList, source, results, markup)
wenzelm@45644
   219
  }
wenzelm@45644
   220
wenzelm@54513
   221
  val empty = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
wenzelm@49414
   222
wenzelm@52530
   223
  def unparsed(id: Document_ID.Command, source: String, results: Results, markup: Markup_Tree)
wenzelm@50501
   224
      : Command =
wenzelm@54519
   225
    Command(id, Document.Node.Name.empty, Nil, List(Token(Token.Kind.UNPARSED, source)),
wenzelm@54462
   226
      results, markup)
wenzelm@49414
   227
wenzelm@50501
   228
  def unparsed(source: String): Command =
wenzelm@52530
   229
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   230
wenzelm@52530
   231
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   232
  {
wenzelm@49466
   233
    val text = XML.content(body)
wenzelm@49466
   234
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   235
    unparsed(id, text, results, markup)
wenzelm@49414
   236
  }
wenzelm@49359
   237
wenzelm@44384
   238
wenzelm@44384
   239
  /* perspective */
wenzelm@44384
   240
wenzelm@44474
   241
  object Perspective
wenzelm@44474
   242
  {
wenzelm@44474
   243
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   244
  }
wenzelm@44385
   245
wenzelm@44474
   246
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   247
  {
wenzelm@44474
   248
    def same(that: Perspective): Boolean =
wenzelm@44474
   249
    {
wenzelm@44474
   250
      val cmds1 = this.commands
wenzelm@44474
   251
      val cmds2 = that.commands
wenzelm@48754
   252
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   253
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   254
      cmds1.length == cmds2.length &&
wenzelm@44474
   255
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   256
    }
wenzelm@44385
   257
  }
wenzelm@34318
   258
}
wenzelm@34318
   259
wenzelm@38361
   260
wenzelm@46712
   261
final class Command private(
wenzelm@52530
   262
    val id: Document_ID.Command,
wenzelm@44615
   263
    val node_name: Document.Node.Name,
wenzelm@54519
   264
    val blobs: List[Command.Blob],
wenzelm@52535
   265
    val span: List[Token],
wenzelm@49414
   266
    val source: String,
wenzelm@50501
   267
    val init_results: Command.Results,
wenzelm@49414
   268
    val init_markup: Markup_Tree)
wenzelm@55431
   269
  extends Command.Chunk
wenzelm@34451
   270
{
wenzelm@34859
   271
  /* classification */
wenzelm@34500
   272
wenzelm@52530
   273
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@48754
   274
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   275
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   276
wenzelm@48599
   277
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   278
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   279
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   280
wenzelm@54462
   281
  def name: String = Command.name(span)
wenzelm@47012
   282
wenzelm@37129
   283
  override def toString =
wenzelm@37373
   284
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   285
wenzelm@34859
   286
wenzelm@54519
   287
  /* blobs */
wenzelm@54519
   288
wenzelm@54530
   289
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   290
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   291
wenzelm@54519
   292
  def blobs_digests: List[SHA1.Digest] =
wenzelm@55431
   293
    for (Exn.Res((_, Some((digest, _)))) <- blobs) yield digest
wenzelm@54519
   294
wenzelm@55432
   295
  val chunks: Map[String, Command.Chunk] =
wenzelm@55432
   296
    (("" -> this) ::
wenzelm@55432
   297
      (for (Exn.Res((name, Some((_, file)))) <- blobs) yield (name.node -> file))).toMap
wenzelm@55432
   298
wenzelm@54519
   299
wenzelm@52509
   300
  /* source */
wenzelm@34451
   301
wenzelm@55431
   302
  def file_name: String = ""
wenzelm@55431
   303
wenzelm@46813
   304
  def length: Int = source.length
wenzelm@46813
   305
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   306
wenzelm@46813
   307
  val proper_range: Text.Range =
wenzelm@51048
   308
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   309
wenzelm@38426
   310
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   311
wenzelm@55430
   312
  private lazy val symbol_index = Symbol.Index(source)
wenzelm@38579
   313
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   314
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   315
wenzelm@38370
   316
wenzelm@38370
   317
  /* accumulated results */
wenzelm@38370
   318
wenzelm@50501
   319
  val init_state: Command.State =
wenzelm@55432
   320
    Command.State(this, results = init_results, markups = Map("" -> init_markup))
wenzelm@52527
   321
wenzelm@52527
   322
  val empty_state: Command.State = Command.State(this)
immler@34676
   323
}