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