src/Pure/PIDE/command.scala
author wenzelm
Tue Feb 11 15:05:13 2014 +0100 (2014-02-11 ago)
changeset 55429 4a50f9e70dc1
parent 54530 2c1440f70028
child 55430 8eb6c740ec1a
permissions -rw-r--r--
report (but ignore) markup within auxiliary files;
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@50501
    63
    markup: Markup_Tree = Markup_Tree.empty)
wenzelm@38361
    64
  {
wenzelm@49645
    65
    def markup_to_XML(filter: XML.Elem => Boolean): XML.Body =
wenzelm@49645
    66
      markup.to_XML(command.range, command.source, filter)
wenzelm@49614
    67
wenzelm@49614
    68
wenzelm@51494
    69
    /* content */
wenzelm@51494
    70
wenzelm@51494
    71
    def eq_content(other: State): Boolean =
wenzelm@51494
    72
      command.source == other.command.source &&
wenzelm@51494
    73
      status == other.status &&
wenzelm@51494
    74
      results == other.results &&
wenzelm@51494
    75
      markup == other.markup
wenzelm@38361
    76
wenzelm@46152
    77
    private def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@46152
    78
    private def add_markup(m: Text.Markup): State = copy(markup = markup + m)
wenzelm@38361
    79
wenzelm@52531
    80
    def + (alt_id: Document_ID.Generic, message: XML.Elem): State =
wenzelm@38361
    81
      message match {
wenzelm@50201
    82
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    83
          (this /: msgs)((state, msg) =>
wenzelm@38714
    84
            msg match {
wenzelm@46152
    85
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@50499
    86
                state.add_status(markup)
wenzelm@50499
    87
                  .add_markup(Text.Info(command.proper_range, elem))  // FIXME cumulation order!?
wenzelm@46152
    88
wenzelm@52536
    89
              case _ =>
wenzelm@52536
    90
                java.lang.System.err.println("Ignored status message: " + msg)
wenzelm@52536
    91
                state
wenzelm@38714
    92
            })
wenzelm@38581
    93
wenzelm@50201
    94
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    95
          (this /: msgs)((state, msg) =>
wenzelm@38572
    96
            msg match {
wenzelm@55429
    97
              case XML.Elem(Markup(name, atts @ Position.Reported(id, file, raw_range)), args)
wenzelm@55429
    98
              if (id == command.id || id == alt_id) && file == "" &&
wenzelm@49527
    99
                  command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
   100
                val range = command.decode(raw_range)
wenzelm@38872
   101
                val props = Position.purge(atts)
wenzelm@45455
   102
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@38723
   103
                state.add_markup(info)
wenzelm@49526
   104
              case XML.Elem(Markup(name, atts), args)
wenzelm@50201
   105
              if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@49037
   106
                val range = command.proper_range
wenzelm@49037
   107
                val props = Position.purge(atts)
wenzelm@49037
   108
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@49037
   109
                state.add_markup(info)
wenzelm@40572
   110
              case _ =>
wenzelm@52536
   111
                // FIXME java.lang.System.err.println("Ignored report message: " + msg)
wenzelm@40572
   112
                state
wenzelm@38361
   113
            })
wenzelm@52930
   114
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   115
          props match {
wenzelm@50201
   116
            case Markup.Serial(i) =>
wenzelm@50201
   117
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   118
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   119
wenzelm@50163
   120
              val st0 = copy(results = results + (i -> message1))
wenzelm@39441
   121
              val st1 =
wenzelm@50500
   122
                if (Protocol.is_inlined(message))
wenzelm@45709
   123
                  (st0 /: Protocol.message_positions(command, message))(
wenzelm@50163
   124
                    (st, range) => st.add_markup(Text.Info(range, message2)))
wenzelm@50500
   125
                else st0
wenzelm@49445
   126
wenzelm@49445
   127
              st1
wenzelm@52536
   128
            case _ =>
wenzelm@52536
   129
              java.lang.System.err.println("Ignored message without serial number: " + message)
wenzelm@52536
   130
              this
wenzelm@38872
   131
          }
wenzelm@38361
   132
      }
wenzelm@52527
   133
wenzelm@52527
   134
    def ++ (other: State): State =
wenzelm@52642
   135
      copy(
wenzelm@52642
   136
        status = other.status ::: status,
wenzelm@52642
   137
        results = results ++ other.results,
wenzelm@52642
   138
        markup = markup ++ other.markup)
wenzelm@38361
   139
  }
wenzelm@38367
   140
wenzelm@38367
   141
wenzelm@45644
   142
  /* make commands */
wenzelm@45644
   143
wenzelm@54462
   144
  def name(span: List[Token]): String =
wenzelm@54462
   145
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@48745
   146
wenzelm@54524
   147
  type Blob = Exn.Result[(Document.Node.Name, Option[SHA1.Digest])]
wenzelm@54513
   148
wenzelm@52524
   149
  def apply(
wenzelm@52530
   150
    id: Document_ID.Command,
wenzelm@52524
   151
    node_name: Document.Node.Name,
wenzelm@54519
   152
    blobs: List[Blob],
wenzelm@54462
   153
    span: List[Token],
wenzelm@52524
   154
    results: Results = Results.empty,
wenzelm@52524
   155
    markup: Markup_Tree = Markup_Tree.empty): Command =
wenzelm@45644
   156
  {
wenzelm@45644
   157
    val source: String =
wenzelm@48745
   158
      span match {
wenzelm@45644
   159
        case List(tok) => tok.source
wenzelm@48745
   160
        case _ => span.map(_.source).mkString
wenzelm@45644
   161
      }
wenzelm@45644
   162
wenzelm@48745
   163
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
   164
    var i = 0
wenzelm@48745
   165
    for (Token(kind, s) <- span) {
wenzelm@45644
   166
      val n = s.length
wenzelm@45644
   167
      val s1 = source.substring(i, i + n)
wenzelm@48745
   168
      span1 += Token(kind, s1)
wenzelm@45644
   169
      i += n
wenzelm@45644
   170
    }
wenzelm@45644
   171
wenzelm@54519
   172
    new Command(id, node_name, blobs, span1.toList, source, results, markup)
wenzelm@45644
   173
  }
wenzelm@45644
   174
wenzelm@54513
   175
  val empty = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
wenzelm@49414
   176
wenzelm@52530
   177
  def unparsed(id: Document_ID.Command, source: String, results: Results, markup: Markup_Tree)
wenzelm@50501
   178
      : Command =
wenzelm@54519
   179
    Command(id, Document.Node.Name.empty, Nil, List(Token(Token.Kind.UNPARSED, source)),
wenzelm@54462
   180
      results, markup)
wenzelm@49414
   181
wenzelm@50501
   182
  def unparsed(source: String): Command =
wenzelm@52530
   183
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   184
wenzelm@52530
   185
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   186
  {
wenzelm@49466
   187
    val text = XML.content(body)
wenzelm@49466
   188
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   189
    unparsed(id, text, results, markup)
wenzelm@49414
   190
  }
wenzelm@49359
   191
wenzelm@44384
   192
wenzelm@44384
   193
  /* perspective */
wenzelm@44384
   194
wenzelm@44474
   195
  object Perspective
wenzelm@44474
   196
  {
wenzelm@44474
   197
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   198
  }
wenzelm@44385
   199
wenzelm@44474
   200
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   201
  {
wenzelm@44474
   202
    def same(that: Perspective): Boolean =
wenzelm@44474
   203
    {
wenzelm@44474
   204
      val cmds1 = this.commands
wenzelm@44474
   205
      val cmds2 = that.commands
wenzelm@48754
   206
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   207
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   208
      cmds1.length == cmds2.length &&
wenzelm@44474
   209
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   210
    }
wenzelm@44385
   211
  }
wenzelm@34318
   212
}
wenzelm@34318
   213
wenzelm@38361
   214
wenzelm@46712
   215
final class Command private(
wenzelm@52530
   216
    val id: Document_ID.Command,
wenzelm@44615
   217
    val node_name: Document.Node.Name,
wenzelm@54519
   218
    val blobs: List[Command.Blob],
wenzelm@52535
   219
    val span: List[Token],
wenzelm@49414
   220
    val source: String,
wenzelm@50501
   221
    val init_results: Command.Results,
wenzelm@49414
   222
    val init_markup: Markup_Tree)
wenzelm@34451
   223
{
wenzelm@34859
   224
  /* classification */
wenzelm@34500
   225
wenzelm@52530
   226
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@48754
   227
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   228
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   229
wenzelm@48599
   230
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   231
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   232
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   233
wenzelm@54462
   234
  def name: String = Command.name(span)
wenzelm@47012
   235
wenzelm@37129
   236
  override def toString =
wenzelm@37373
   237
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   238
wenzelm@34859
   239
wenzelm@54519
   240
  /* blobs */
wenzelm@54519
   241
wenzelm@54530
   242
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   243
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   244
wenzelm@54519
   245
  def blobs_digests: List[SHA1.Digest] =
wenzelm@54524
   246
    for (Exn.Res((_, Some(digest))) <- blobs) yield digest
wenzelm@54519
   247
wenzelm@54519
   248
wenzelm@52509
   249
  /* source */
wenzelm@34451
   250
wenzelm@46813
   251
  def length: Int = source.length
wenzelm@46813
   252
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   253
wenzelm@46813
   254
  val proper_range: Text.Range =
wenzelm@51048
   255
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   256
wenzelm@38426
   257
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   258
wenzelm@52507
   259
  lazy val symbol_index = Symbol.Index(source)
wenzelm@38579
   260
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   261
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   262
wenzelm@38370
   263
wenzelm@38370
   264
  /* accumulated results */
wenzelm@38370
   265
wenzelm@50501
   266
  val init_state: Command.State =
wenzelm@50501
   267
    Command.State(this, results = init_results, markup = init_markup)
wenzelm@52527
   268
wenzelm@52527
   269
  val empty_state: Command.State = Command.State(this)
immler@34676
   270
}