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