src/Pure/PIDE/command.scala
author wenzelm
Mon May 07 17:11:01 2018 +0200 (15 months ago ago)
changeset 68104 0699a0bacc50
parent 67826 5ea76b219668
child 68119 ce7f35406f37
permissions -rw-r--r--
store exports within PIDE command state;
Markup.Export.unapply: proper NAME;
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@59704
    18
wenzelm@56746
    19
  type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, Symbol.Text_Chunk)])]
wenzelm@59704
    20
  type Blobs_Info = (List[Blob], Int)
wenzelm@59704
    21
  val no_blobs: Blobs_Info = (Nil, -1)
wenzelm@52849
    22
wenzelm@52849
    23
wenzelm@38361
    24
  /** accumulated results from prover **/
wenzelm@38361
    25
wenzelm@50507
    26
  /* results */
wenzelm@50507
    27
wenzelm@50507
    28
  object Results
wenzelm@50507
    29
  {
wenzelm@51496
    30
    type Entry = (Long, XML.Tree)
wenzelm@68104
    31
    val empty: Results = new Results(SortedMap.empty)
wenzelm@59072
    32
    def make(args: TraversableOnce[Results.Entry]): Results = (empty /: args)(_ + _)
wenzelm@59072
    33
    def merge(args: TraversableOnce[Results]): Results = (empty /: args)(_ ++ _)
wenzelm@65335
    34
wenzelm@65335
    35
wenzelm@65335
    36
    /* XML data representation */
wenzelm@65335
    37
wenzelm@65335
    38
    val encode: XML.Encode.T[Results] = (results: Results) =>
wenzelm@65335
    39
    { import XML.Encode._; list(pair(long, tree))(results.rep.toList) }
wenzelm@65335
    40
wenzelm@65335
    41
    val decode: XML.Decode.T[Results] = (body: XML.Body) =>
wenzelm@65335
    42
    { import XML.Decode._; make(list(pair(long, tree))(body)) }
wenzelm@50507
    43
  }
wenzelm@50507
    44
wenzelm@51494
    45
  final class Results private(private val rep: SortedMap[Long, XML.Tree])
wenzelm@50507
    46
  {
wenzelm@64802
    47
    def is_empty: Boolean = rep.isEmpty
wenzelm@50507
    48
    def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
wenzelm@50507
    49
    def get(serial: Long): Option[XML.Tree] = rep.get(serial)
wenzelm@56372
    50
    def iterator: Iterator[Results.Entry] = rep.iterator
wenzelm@50508
    51
wenzelm@51496
    52
    def + (entry: Results.Entry): Results =
wenzelm@50508
    53
      if (defined(entry._1)) this
wenzelm@50508
    54
      else new Results(rep + entry)
wenzelm@50508
    55
wenzelm@50508
    56
    def ++ (other: Results): Results =
wenzelm@50508
    57
      if (this eq other) this
wenzelm@50508
    58
      else if (rep.isEmpty) other
wenzelm@56372
    59
      else (this /: other.iterator)(_ + _)
wenzelm@50540
    60
wenzelm@51494
    61
    override def hashCode: Int = rep.hashCode
wenzelm@51494
    62
    override def equals(that: Any): Boolean =
wenzelm@51494
    63
      that match {
wenzelm@51494
    64
        case other: Results => rep == other.rep
wenzelm@51494
    65
        case _ => false
wenzelm@51494
    66
      }
wenzelm@56372
    67
    override def toString: String = iterator.mkString("Results(", ", ", ")")
wenzelm@50507
    68
  }
wenzelm@50507
    69
wenzelm@50507
    70
wenzelm@68104
    71
  /* exports */
wenzelm@68104
    72
wenzelm@68104
    73
  object Exports
wenzelm@68104
    74
  {
wenzelm@68104
    75
    type Entry = (Long, Export.Entry)
wenzelm@68104
    76
    val empty: Exports = new Exports(SortedMap.empty)
wenzelm@68104
    77
    def merge(args: TraversableOnce[Exports]): Exports = (empty /: args)(_ ++ _)
wenzelm@68104
    78
  }
wenzelm@68104
    79
wenzelm@68104
    80
  final class Exports private(private val rep: SortedMap[Long, Export.Entry])
wenzelm@68104
    81
  {
wenzelm@68104
    82
    def iterator: Iterator[Exports.Entry] = rep.iterator
wenzelm@68104
    83
wenzelm@68104
    84
    def + (entry: Exports.Entry): Exports =
wenzelm@68104
    85
      if (rep.isDefinedAt(entry._1)) this
wenzelm@68104
    86
      else new Exports(rep + entry)
wenzelm@68104
    87
wenzelm@68104
    88
    def ++ (other: Exports): Exports =
wenzelm@68104
    89
      if (this eq other) this
wenzelm@68104
    90
      else if (rep.isEmpty) other
wenzelm@68104
    91
      else (this /: other.iterator)(_ + _)
wenzelm@68104
    92
wenzelm@68104
    93
    override def hashCode: Int = rep.hashCode
wenzelm@68104
    94
    override def equals(that: Any): Boolean =
wenzelm@68104
    95
      that match {
wenzelm@68104
    96
        case other: Exports => rep == other.rep
wenzelm@68104
    97
        case _ => false
wenzelm@68104
    98
      }
wenzelm@68104
    99
    override def toString: String = iterator.mkString("Exports(", ", ", ")")
wenzelm@68104
   100
  }
wenzelm@68104
   101
wenzelm@68104
   102
wenzelm@68104
   103
  /* markups */
wenzelm@55649
   104
wenzelm@55649
   105
  object Markup_Index
wenzelm@55649
   106
  {
wenzelm@56746
   107
    val markup: Markup_Index = Markup_Index(false, Symbol.Text_Chunk.Default)
wenzelm@55649
   108
  }
wenzelm@55649
   109
wenzelm@56746
   110
  sealed case class Markup_Index(status: Boolean, chunk_name: Symbol.Text_Chunk.Name)
wenzelm@55649
   111
wenzelm@55649
   112
  object Markups
wenzelm@55649
   113
  {
wenzelm@55649
   114
    val empty: Markups = new Markups(Map.empty)
wenzelm@65335
   115
    def init(markup: Markup_Tree): Markups = new Markups(Map(Markup_Index.markup -> markup))
wenzelm@65335
   116
    def merge(args: TraversableOnce[Markups]): Markups = (empty /: args)(_ ++ _)
wenzelm@55649
   117
wenzelm@65335
   118
wenzelm@65335
   119
    /* XML data representation */
wenzelm@65335
   120
wenzelm@65335
   121
    def encode(source_length: Int): XML.Encode.T[Markups] = (markups: Markups) =>
wenzelm@65335
   122
    {
wenzelm@65335
   123
      import XML.Encode._
wenzelm@65335
   124
wenzelm@65335
   125
      val markup_index: T[Markup_Index] = (index: Markup_Index) =>
wenzelm@65335
   126
        pair(bool, Symbol.Text_Chunk.encode_name)(index.status, index.chunk_name)
wenzelm@65335
   127
wenzelm@65335
   128
      val markup_tree: T[Markup_Tree] =
wenzelm@65335
   129
        _.to_XML(Text.Range(0, source_length), Symbol.spaces(source_length), Markup.Elements.full)
wenzelm@65335
   130
wenzelm@65335
   131
      list(pair(markup_index, markup_tree))(markups.rep.toList)
wenzelm@65335
   132
    }
wenzelm@65335
   133
wenzelm@65335
   134
    val decode: XML.Decode.T[Markups] = (body: XML.Body) =>
wenzelm@65335
   135
    {
wenzelm@65335
   136
      import XML.Decode._
wenzelm@65335
   137
wenzelm@65335
   138
      val markup_index: T[Markup_Index] = (body: XML.Body) =>
wenzelm@65335
   139
      {
wenzelm@65335
   140
        val (status, chunk_name) = pair(bool, Symbol.Text_Chunk.decode_name)(body)
wenzelm@65335
   141
        Markup_Index(status, chunk_name)
wenzelm@65335
   142
      }
wenzelm@65335
   143
wenzelm@65335
   144
      (Markups.empty /: list(pair(markup_index, Markup_Tree.from_XML(_)))(body))(_ + _)
wenzelm@65335
   145
    }
wenzelm@55649
   146
  }
wenzelm@55649
   147
wenzelm@55649
   148
  final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
wenzelm@55649
   149
  {
wenzelm@56489
   150
    def is_empty: Boolean = rep.isEmpty
wenzelm@56489
   151
wenzelm@55649
   152
    def apply(index: Markup_Index): Markup_Tree =
wenzelm@55649
   153
      rep.getOrElse(index, Markup_Tree.empty)
wenzelm@55649
   154
wenzelm@55649
   155
    def add(index: Markup_Index, markup: Text.Markup): Markups =
wenzelm@55649
   156
      new Markups(rep + (index -> (this(index) + markup)))
wenzelm@55649
   157
wenzelm@65335
   158
    def + (entry: (Markup_Index, Markup_Tree)): Markups =
wenzelm@65335
   159
    {
wenzelm@65335
   160
      val (index, tree) = entry
wenzelm@65335
   161
      new Markups(rep + (index -> (this(index).merge(tree, Text.Range.full, Markup.Elements.full))))
wenzelm@65335
   162
    }
wenzelm@65335
   163
wenzelm@65335
   164
    def ++ (other: Markups): Markups =
wenzelm@65335
   165
      if (this eq other) this
wenzelm@65335
   166
      else if (rep.isEmpty) other
wenzelm@65335
   167
      else (this /: other.rep.iterator)(_ + _)
wenzelm@65335
   168
wenzelm@56475
   169
    def redirection_iterator: Iterator[Document_ID.Generic] =
wenzelm@56746
   170
      for (Markup_Index(_, Symbol.Text_Chunk.Id(id)) <- rep.keysIterator)
wenzelm@56475
   171
        yield id
wenzelm@56470
   172
wenzelm@56475
   173
    def redirect(other_id: Document_ID.Generic): Markups =
wenzelm@56489
   174
    {
wenzelm@56489
   175
      val rep1 =
wenzelm@56470
   176
        (for {
wenzelm@56746
   177
          (Markup_Index(status, Symbol.Text_Chunk.Id(id)), markup) <- rep.iterator
wenzelm@56470
   178
          if other_id == id
wenzelm@56746
   179
        } yield (Markup_Index(status, Symbol.Text_Chunk.Default), markup)).toMap
wenzelm@56489
   180
      if (rep1.isEmpty) Markups.empty else new Markups(rep1)
wenzelm@56489
   181
    }
wenzelm@56470
   182
wenzelm@55649
   183
    override def hashCode: Int = rep.hashCode
wenzelm@55649
   184
    override def equals(that: Any): Boolean =
wenzelm@55649
   185
      that match {
wenzelm@55649
   186
        case other: Markups => rep == other.rep
wenzelm@55649
   187
        case _ => false
wenzelm@55649
   188
      }
wenzelm@55649
   189
    override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
wenzelm@55649
   190
  }
wenzelm@55649
   191
wenzelm@55649
   192
wenzelm@50507
   193
  /* state */
wenzelm@50501
   194
wenzelm@56299
   195
  object State
wenzelm@56299
   196
  {
wenzelm@67824
   197
    def get_result(states: List[State], serial: Long): Option[XML.Tree] =
wenzelm@67824
   198
      states.find(st => st.results.defined(serial)).map(st => st.results.get(serial).get)
wenzelm@67824
   199
wenzelm@67824
   200
    def get_result_proper(states: List[State], props: Properties.T): Option[Results.Entry] =
wenzelm@67824
   201
      for {
wenzelm@67824
   202
        serial <- Markup.Serial.unapply(props)
wenzelm@67824
   203
        tree @ XML.Elem(_, body) <- get_result(states, serial)
wenzelm@67824
   204
        if body.nonEmpty
wenzelm@67824
   205
      } yield (serial -> tree)
wenzelm@67824
   206
wenzelm@65335
   207
    def merge_results(states: List[State]): Results =
wenzelm@56299
   208
      Results.merge(states.map(_.results))
wenzelm@56299
   209
wenzelm@68104
   210
    def merge_exports(states: List[State]): Exports =
wenzelm@68104
   211
      Exports.merge(states.map(_.exports))
wenzelm@68104
   212
wenzelm@65335
   213
    def merge_markups(states: List[State]): Markups =
wenzelm@65335
   214
      Markups.merge(states.map(_.markups))
wenzelm@65335
   215
wenzelm@56301
   216
    def merge_markup(states: List[State], index: Markup_Index,
wenzelm@56743
   217
        range: Text.Range, elements: Markup.Elements): Markup_Tree =
wenzelm@56301
   218
      Markup_Tree.merge(states.map(_.markup(index)), range, elements)
wenzelm@65335
   219
wenzelm@65335
   220
    def merge(command: Command, states: List[State]): State =
wenzelm@68104
   221
      State(command, states.flatMap(_.status), merge_results(states),
wenzelm@68104
   222
        merge_exports(states), merge_markups(states))
wenzelm@65335
   223
wenzelm@65335
   224
wenzelm@65335
   225
    /* XML data representation */
wenzelm@65335
   226
wenzelm@65335
   227
    val encode: XML.Encode.T[State] = (st: State) =>
wenzelm@65335
   228
    {
wenzelm@65335
   229
      import XML.Encode._
wenzelm@65335
   230
wenzelm@65335
   231
      val command = st.command
wenzelm@65335
   232
      val blobs_names = command.blobs_names.map(_.node)
wenzelm@65335
   233
      val blobs_index = command.blobs_index
wenzelm@65335
   234
      require(command.blobs_ok)
wenzelm@65335
   235
wenzelm@65335
   236
      pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.encode,
wenzelm@65335
   237
          pair(list(Markup.encode), pair(Results.encode, Markups.encode(command.source.length)))))))(
wenzelm@65335
   238
        (command.id, (command.node_name.node, ((blobs_names, blobs_index), (command.span,
wenzelm@65335
   239
          (st.status, (st.results, st.markups)))))))
wenzelm@65335
   240
    }
wenzelm@65335
   241
wenzelm@65335
   242
    def decode(node_name: String => Document.Node.Name): XML.Decode.T[State] = (body: XML.Body) =>
wenzelm@65335
   243
    {
wenzelm@65335
   244
      import XML.Decode._
wenzelm@65335
   245
      val (id, (node, ((blobs_names, blobs_index), (span, (status, (results, markups)))))) =
wenzelm@65335
   246
        pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.decode,
wenzelm@65335
   247
          pair(list(Markup.decode), pair(Results.decode, Markups.decode))))))(body)
wenzelm@65335
   248
wenzelm@65335
   249
      val blobs_info: Blobs_Info =
wenzelm@65335
   250
        (blobs_names.map(name => Exn.Res((node_name(name), None)): Blob), blobs_index)
wenzelm@65335
   251
      val command = Command(id, node_name(node), blobs_info, span)
wenzelm@68104
   252
      State(command, status, results, Exports.empty, markups)
wenzelm@65335
   253
    }
wenzelm@56299
   254
  }
wenzelm@56299
   255
wenzelm@43714
   256
  sealed case class State(
wenzelm@50501
   257
    command: Command,
wenzelm@50501
   258
    status: List[Markup] = Nil,
wenzelm@50507
   259
    results: Results = Results.empty,
wenzelm@68104
   260
    exports: Exports = Exports.empty,
wenzelm@55649
   261
    markups: Markups = Markups.empty)
wenzelm@38361
   262
  {
wenzelm@66382
   263
    lazy val consolidated: Boolean =
wenzelm@66382
   264
      status.exists(markup => markup.name == Markup.CONSOLIDATED)
wenzelm@66382
   265
wenzelm@56395
   266
    lazy val protocol_status: Protocol.Status =
wenzelm@56395
   267
    {
wenzelm@56395
   268
      val warnings =
wenzelm@59203
   269
        if (results.iterator.exists(p => Protocol.is_warning(p._2) || Protocol.is_legacy(p._2)))
wenzelm@56395
   270
          List(Markup(Markup.WARNING, Nil))
wenzelm@56395
   271
        else Nil
wenzelm@56395
   272
      val errors =
wenzelm@56395
   273
        if (results.iterator.exists(p => Protocol.is_error(p._2)))
wenzelm@56395
   274
          List(Markup(Markup.ERROR, Nil))
wenzelm@56395
   275
        else Nil
wenzelm@56395
   276
      Protocol.Status.make((warnings ::: errors ::: status).iterator)
wenzelm@56395
   277
    }
wenzelm@55432
   278
wenzelm@55650
   279
    def markup(index: Markup_Index): Markup_Tree = markups(index)
wenzelm@55432
   280
wenzelm@56489
   281
    def redirect(other_command: Command): Option[State] =
wenzelm@56489
   282
    {
wenzelm@56489
   283
      val markups1 = markups.redirect(other_command.id)
wenzelm@56489
   284
      if (markups1.is_empty) None
wenzelm@68104
   285
      else Some(new State(other_command, markups = markups1))
wenzelm@56489
   286
    }
wenzelm@49614
   287
wenzelm@55649
   288
    private def add_status(st: Markup): State =
wenzelm@55649
   289
      copy(status = st :: status)
wenzelm@55432
   290
wenzelm@67826
   291
    private def add_result(entry: Results.Entry): State =
wenzelm@67826
   292
      copy(results = results + entry)
wenzelm@67826
   293
wenzelm@68104
   294
    def add_export(entry: Exports.Entry): State =
wenzelm@68104
   295
      copy(exports = exports + entry)
wenzelm@68104
   296
wenzelm@56746
   297
    private def add_markup(
wenzelm@56746
   298
      status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
wenzelm@55649
   299
    {
wenzelm@55649
   300
      val markups1 =
wenzelm@56395
   301
        if (status || Protocol.liberal_status_elements(m.info.name))
wenzelm@56462
   302
          markups.add(Markup_Index(true, chunk_name), m)
wenzelm@55649
   303
        else markups
wenzelm@56462
   304
      copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
wenzelm@55649
   305
    }
wenzelm@38361
   306
wenzelm@56470
   307
    def accumulate(
wenzelm@56470
   308
        self_id: Document_ID.Generic => Boolean,
wenzelm@56746
   309
        other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
wenzelm@67825
   310
        message: XML.Elem,
wenzelm@67825
   311
        xml_cache: XML.Cache): State =
wenzelm@38361
   312
      message match {
wenzelm@50201
   313
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
   314
          (this /: msgs)((state, msg) =>
wenzelm@38714
   315
            msg match {
wenzelm@46152
   316
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@55649
   317
                state.
wenzelm@55649
   318
                  add_status(markup).
wenzelm@56746
   319
                  add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
wenzelm@52536
   320
              case _ =>
wenzelm@56782
   321
                Output.warning("Ignored status message: " + msg)
wenzelm@52536
   322
                state
wenzelm@38714
   323
            })
wenzelm@38581
   324
wenzelm@50201
   325
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
   326
          (this /: msgs)((state, msg) =>
wenzelm@55432
   327
            {
wenzelm@56782
   328
              def bad(): Unit = Output.warning("Ignored report message: " + msg)
wenzelm@55432
   329
wenzelm@55432
   330
              msg match {
wenzelm@57911
   331
                case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
wenzelm@56470
   332
wenzelm@56470
   333
                  val target =
wenzelm@56470
   334
                    if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
wenzelm@56470
   335
                      Some((chunk_name, command.chunks(chunk_name)))
wenzelm@56746
   336
                    else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
wenzelm@56470
   337
                    else None
wenzelm@56470
   338
wenzelm@57911
   339
                  (target, atts) match {
wenzelm@57911
   340
                    case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
wenzelm@56470
   341
                      target_chunk.incorporate(symbol_range) match {
wenzelm@55550
   342
                        case Some(range) =>
wenzelm@55822
   343
                          val props = Position.purge(atts)
wenzelm@67825
   344
                          val elem = xml_cache.elem(XML.Elem(Markup(name, props), args))
wenzelm@67825
   345
                          state.add_markup(false, target_name, Text.Info(range, elem))
wenzelm@55550
   346
                        case None => bad(); state
wenzelm@55432
   347
                      }
wenzelm@57911
   348
                    case _ =>
wenzelm@56514
   349
                      // silently ignore excessive reports
wenzelm@56476
   350
                      state
wenzelm@55432
   351
                  }
wenzelm@55432
   352
wenzelm@55432
   353
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   354
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   355
                  val range = command.proper_range
wenzelm@55432
   356
                  val props = Position.purge(atts)
wenzelm@67825
   357
                  val elem = xml_cache.elem(XML.Elem(Markup(name, props), args))
wenzelm@67825
   358
                  state.add_markup(false, Symbol.Text_Chunk.Default, Text.Info(range, elem))
wenzelm@55432
   359
wenzelm@56470
   360
                case _ => bad(); state
wenzelm@55432
   361
              }
wenzelm@38361
   362
            })
wenzelm@52930
   363
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   364
          props match {
wenzelm@50201
   365
            case Markup.Serial(i) =>
wenzelm@67825
   366
              val markup_message =
wenzelm@67825
   367
                xml_cache.elem(XML.Elem(Markup(Markup.message(name), props), body))
wenzelm@67825
   368
              val message_markup =
wenzelm@67825
   369
                xml_cache.elem(XML.elem(Markup(name, props.filter(p => p._1 == Markup.SERIAL))))
wenzelm@50163
   370
wenzelm@67826
   371
              var st = add_result(i -> markup_message)
wenzelm@55433
   372
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   373
                for {
wenzelm@56469
   374
                  (chunk_name, chunk) <- command.chunks.iterator
wenzelm@59713
   375
                  range <- Protocol_Message.positions(
wenzelm@59735
   376
                    self_id, command.span.position, chunk_name, chunk, message)
wenzelm@67824
   377
                } st = st.add_markup(false, chunk_name, Text.Info(range, message_markup))
wenzelm@55433
   378
              }
wenzelm@55433
   379
              st
wenzelm@55432
   380
wenzelm@52536
   381
            case _ =>
wenzelm@56782
   382
              Output.warning("Ignored message without serial number: " + message)
wenzelm@52536
   383
              this
wenzelm@38872
   384
          }
wenzelm@68104
   385
      }
wenzelm@38361
   386
  }
wenzelm@38367
   387
wenzelm@38367
   388
wenzelm@55431
   389
wenzelm@55431
   390
  /** static content **/
wenzelm@55431
   391
wenzelm@45644
   392
  /* make commands */
wenzelm@45644
   393
wenzelm@55648
   394
  def apply(
wenzelm@55648
   395
    id: Document_ID.Command,
wenzelm@55648
   396
    node_name: Document.Node.Name,
wenzelm@59704
   397
    blobs_info: Blobs_Info,
wenzelm@57905
   398
    span: Command_Span.Span): Command =
wenzelm@55648
   399
  {
wenzelm@57901
   400
    val (source, span1) = span.compact_source
wenzelm@59704
   401
    new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
wenzelm@55648
   402
  }
wenzelm@49414
   403
wenzelm@57901
   404
  val empty: Command =
wenzelm@59704
   405
    Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
wenzelm@55648
   406
wenzelm@55648
   407
  def unparsed(
wenzelm@55648
   408
    id: Document_ID.Command,
wenzelm@55648
   409
    source: String,
wenzelm@55648
   410
    results: Results,
wenzelm@55648
   411
    markup: Markup_Tree): Command =
wenzelm@55648
   412
  {
wenzelm@57905
   413
    val (source1, span1) = Command_Span.unparsed(source).compact_source
wenzelm@59704
   414
    new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
wenzelm@55648
   415
  }
wenzelm@49414
   416
wenzelm@65341
   417
  def text(source: String): Command =
wenzelm@52530
   418
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   419
wenzelm@52530
   420
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   421
  {
wenzelm@49466
   422
    val text = XML.content(body)
wenzelm@49466
   423
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   424
    unparsed(id, text, results, markup)
wenzelm@49414
   425
  }
wenzelm@49359
   426
wenzelm@44384
   427
wenzelm@44384
   428
  /* perspective */
wenzelm@44384
   429
wenzelm@44474
   430
  object Perspective
wenzelm@44474
   431
  {
wenzelm@44474
   432
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   433
  }
wenzelm@44385
   434
wenzelm@44474
   435
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   436
  {
wenzelm@57615
   437
    def is_empty: Boolean = commands.isEmpty
wenzelm@57615
   438
wenzelm@44474
   439
    def same(that: Perspective): Boolean =
wenzelm@44474
   440
    {
wenzelm@44474
   441
      val cmds1 = this.commands
wenzelm@44474
   442
      val cmds2 = that.commands
wenzelm@48754
   443
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   444
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   445
      cmds1.length == cmds2.length &&
wenzelm@44474
   446
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   447
    }
wenzelm@44385
   448
  }
wenzelm@59689
   449
wenzelm@59689
   450
wenzelm@59704
   451
  /* blobs: inlined errors and auxiliary files */
wenzelm@59689
   452
wenzelm@59689
   453
  private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
wenzelm@59689
   454
  {
wenzelm@59689
   455
    def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
wenzelm@59689
   456
      toks match {
wenzelm@59689
   457
        case (t1, i1) :: (t2, i2) :: rest =>
wenzelm@67446
   458
          if (t1.is_keyword && t1.source == "%" && t2.is_name) clean(rest)
wenzelm@59689
   459
          else (t1, i1) :: clean((t2, i2) :: rest)
wenzelm@59689
   460
        case _ => toks
wenzelm@59689
   461
      }
wenzelm@59689
   462
    clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
wenzelm@59689
   463
  }
wenzelm@59689
   464
wenzelm@59689
   465
  private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
wenzelm@59924
   466
    if (tokens.exists({ case (t, _) => t.is_command })) {
wenzelm@59924
   467
      tokens.dropWhile({ case (t, _) => !t.is_command }).
wenzelm@64471
   468
        collectFirst({ case (t, i) if t.is_embedded => (t.content, i) })
wenzelm@59689
   469
    }
wenzelm@59924
   470
    else None
wenzelm@59689
   471
wenzelm@63584
   472
  def span_files(syntax: Outer_Syntax, span: Command_Span.Span): (List[String], Int) =
wenzelm@59735
   473
    syntax.load_command(span.name) match {
wenzelm@59735
   474
      case Some(exts) =>
wenzelm@59735
   475
        find_file(clean_tokens(span.content)) match {
wenzelm@59735
   476
          case Some((file, i)) =>
wenzelm@59735
   477
            if (exts.isEmpty) (List(file), i)
wenzelm@59735
   478
            else (exts.map(ext => file + "." + ext), i)
wenzelm@59689
   479
          case None => (Nil, -1)
wenzelm@59689
   480
        }
wenzelm@59735
   481
      case None => (Nil, -1)
wenzelm@59689
   482
    }
wenzelm@59689
   483
wenzelm@59704
   484
  def blobs_info(
wenzelm@59701
   485
    resources: Resources,
wenzelm@63584
   486
    syntax: Outer_Syntax,
wenzelm@59701
   487
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@59704
   488
    can_import: Document.Node.Name => Boolean,
wenzelm@59701
   489
    node_name: Document.Node.Name,
wenzelm@59704
   490
    span: Command_Span.Span): Blobs_Info =
wenzelm@59689
   491
  {
wenzelm@59735
   492
    span.name match {
wenzelm@59704
   493
      // inlined errors
wenzelm@59735
   494
      case Thy_Header.THEORY =>
wenzelm@64825
   495
        val reader = Scan.char_reader(Token.implode(span.content))
wenzelm@66768
   496
        val imports = resources.check_thy_reader(node_name, reader).imports
wenzelm@66768
   497
        val raw_imports =
wenzelm@66768
   498
          try {
wenzelm@66768
   499
            val imports1 = Thy_Header.read(reader, Token.Pos.none).imports
wenzelm@66768
   500
            if (imports.length == imports1.length) imports1.map(_._1) else error("")
wenzelm@66768
   501
          }
wenzelm@66768
   502
          catch { case exn: Throwable => List.fill(imports.length)("") }
wenzelm@66768
   503
wenzelm@59715
   504
        val errors =
wenzelm@66768
   505
          for { ((import_name, pos), s) <- imports zip raw_imports if !can_import(import_name) }
wenzelm@66768
   506
          yield {
wenzelm@66768
   507
            val completion =
wenzelm@66768
   508
              if (Thy_Header.is_base_name(s)) {
wenzelm@66768
   509
                val completed = Completion.completed(import_name.theory_base_name)
wenzelm@66966
   510
                val qualifier = resources.session_base.theory_qualifier(node_name)
wenzelm@66768
   511
                val dir = node_name.master_dir
wenzelm@66768
   512
                for {
wenzelm@66768
   513
                  (_, known_name) <- resources.session_base.known.theories.toList
wenzelm@66768
   514
                  if completed(known_name.theory_base_name)
wenzelm@66966
   515
                }
wenzelm@66966
   516
                yield {
wenzelm@66966
   517
                  resources.standard_import(
wenzelm@66966
   518
                    resources.session_base, qualifier, dir, known_name.theory)
wenzelm@66966
   519
                }
wenzelm@66768
   520
              }.sorted
wenzelm@66768
   521
              else Nil
wenzelm@59715
   522
            val msg =
wenzelm@59715
   523
              "Bad theory import " +
wenzelm@66768
   524
                Markup.Path(import_name.node).markup(quote(import_name.toString)) +
wenzelm@66768
   525
                Position.here(pos) + Completion.report_theories(pos, completion)
wenzelm@59715
   526
            Exn.Exn(ERROR(msg)): Command.Blob
wenzelm@59710
   527
          }
wenzelm@59715
   528
        (errors, -1)
wenzelm@59704
   529
wenzelm@59704
   530
      // auxiliary files
wenzelm@59704
   531
      case _ =>
wenzelm@59704
   532
        val (files, index) = span_files(syntax, span)
wenzelm@59704
   533
        val blobs =
wenzelm@59704
   534
          files.map(file =>
wenzelm@59704
   535
            (Exn.capture {
wenzelm@65488
   536
              val name = Document.Node.Name(resources.append(node_name, Path.explode(file)))
wenzelm@59704
   537
              val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
wenzelm@59704
   538
              (name, blob)
wenzelm@59704
   539
            }).user_error)
wenzelm@59704
   540
        (blobs, index)
wenzelm@59704
   541
    }
wenzelm@59689
   542
  }
wenzelm@34318
   543
}
wenzelm@34318
   544
wenzelm@38361
   545
wenzelm@46712
   546
final class Command private(
wenzelm@52530
   547
    val id: Document_ID.Command,
wenzelm@44615
   548
    val node_name: Document.Node.Name,
wenzelm@59704
   549
    val blobs_info: Command.Blobs_Info,
wenzelm@57905
   550
    val span: Command_Span.Span,
wenzelm@49414
   551
    val source: String,
wenzelm@50501
   552
    val init_results: Command.Results,
wenzelm@49414
   553
    val init_markup: Markup_Tree)
wenzelm@34451
   554
{
wenzelm@57912
   555
  override def toString: String = id + "/" + span.kind.toString
wenzelm@34495
   556
wenzelm@57910
   557
wenzelm@57910
   558
  /* classification */
wenzelm@57910
   559
wenzelm@57905
   560
  def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
wenzelm@57905
   561
  def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
wenzelm@57904
   562
wenzelm@57904
   563
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@57904
   564
  val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
wenzelm@57904
   565
  val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
wenzelm@57904
   566
wenzelm@34859
   567
wenzelm@54519
   568
  /* blobs */
wenzelm@54519
   569
wenzelm@59704
   570
  def blobs: List[Command.Blob] = blobs_info._1
wenzelm@59704
   571
  def blobs_index: Int = blobs_info._2
wenzelm@59704
   572
wenzelm@65335
   573
  def blobs_ok: Boolean =
wenzelm@65335
   574
    blobs.forall({ case Exn.Res(_) => true case _ => false })
wenzelm@65335
   575
wenzelm@54530
   576
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   577
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   578
wenzelm@60916
   579
  def blobs_undefined: List[Document.Node.Name] =
wenzelm@60916
   580
    for (Exn.Res((name, None)) <- blobs) yield name
wenzelm@60916
   581
wenzelm@57842
   582
  def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
wenzelm@57842
   583
    for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
wenzelm@57842
   584
wenzelm@57842
   585
  def blobs_changed(doc_blobs: Document.Blobs): Boolean =
wenzelm@57842
   586
    blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
wenzelm@54519
   587
wenzelm@55432
   588
wenzelm@56462
   589
  /* source chunks */
wenzelm@55431
   590
wenzelm@56746
   591
  val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
wenzelm@56473
   592
wenzelm@56746
   593
  val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
wenzelm@56746
   594
    ((Symbol.Text_Chunk.Default -> chunk) ::
wenzelm@56473
   595
      (for (Exn.Res((name, Some((_, file)))) <- blobs)
wenzelm@60215
   596
        yield Symbol.Text_Chunk.File(name.node) -> file)).toMap
wenzelm@56473
   597
wenzelm@46813
   598
  def length: Int = source.length
wenzelm@56473
   599
  def range: Text.Range = chunk.range
wenzelm@46813
   600
wenzelm@46813
   601
  val proper_range: Text.Range =
wenzelm@57901
   602
    Text.Range(0,
wenzelm@57901
   603
      (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   604
wenzelm@65522
   605
  def source(range: Text.Range): String = range.substring(source)
wenzelm@38572
   606
wenzelm@38370
   607
wenzelm@38370
   608
  /* accumulated results */
wenzelm@38370
   609
wenzelm@50501
   610
  val init_state: Command.State =
wenzelm@55649
   611
    Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
wenzelm@52527
   612
wenzelm@52527
   613
  val empty_state: Command.State = Command.State(this)
immler@34676
   614
}