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