src/Pure/PIDE/command.scala
author wenzelm
Mon Apr 17 12:20:45 2017 +0200 (2017-04-17 ago)
changeset 65488 331f09d9535e
parent 65359 9ca34f0407a9
child 65522 4d7c5df70a14
permissions -rw-r--r--
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@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@65335
   165
    def merge_results(states: List[State]): Results =
wenzelm@56299
   166
      Results.merge(states.map(_.results))
wenzelm@56299
   167
wenzelm@65335
   168
    def merge_markups(states: List[State]): Markups =
wenzelm@65335
   169
      Markups.merge(states.map(_.markups))
wenzelm@65335
   170
wenzelm@56301
   171
    def merge_markup(states: List[State], index: Markup_Index,
wenzelm@56743
   172
        range: Text.Range, elements: Markup.Elements): Markup_Tree =
wenzelm@56301
   173
      Markup_Tree.merge(states.map(_.markup(index)), range, elements)
wenzelm@65335
   174
wenzelm@65335
   175
    def merge(command: Command, states: List[State]): State =
wenzelm@65335
   176
      State(command, states.flatMap(_.status), merge_results(states), merge_markups(states))
wenzelm@65335
   177
wenzelm@65335
   178
wenzelm@65335
   179
    /* XML data representation */
wenzelm@65335
   180
wenzelm@65335
   181
    val encode: XML.Encode.T[State] = (st: State) =>
wenzelm@65335
   182
    {
wenzelm@65335
   183
      import XML.Encode._
wenzelm@65335
   184
wenzelm@65335
   185
      val command = st.command
wenzelm@65335
   186
      val blobs_names = command.blobs_names.map(_.node)
wenzelm@65335
   187
      val blobs_index = command.blobs_index
wenzelm@65335
   188
      require(command.blobs_ok)
wenzelm@65335
   189
wenzelm@65335
   190
      pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.encode,
wenzelm@65335
   191
          pair(list(Markup.encode), pair(Results.encode, Markups.encode(command.source.length)))))))(
wenzelm@65335
   192
        (command.id, (command.node_name.node, ((blobs_names, blobs_index), (command.span,
wenzelm@65335
   193
          (st.status, (st.results, st.markups)))))))
wenzelm@65335
   194
    }
wenzelm@65335
   195
wenzelm@65335
   196
    def decode(node_name: String => Document.Node.Name): XML.Decode.T[State] = (body: XML.Body) =>
wenzelm@65335
   197
    {
wenzelm@65335
   198
      import XML.Decode._
wenzelm@65335
   199
      val (id, (node, ((blobs_names, blobs_index), (span, (status, (results, markups)))))) =
wenzelm@65335
   200
        pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.decode,
wenzelm@65335
   201
          pair(list(Markup.decode), pair(Results.decode, Markups.decode))))))(body)
wenzelm@65335
   202
wenzelm@65335
   203
      val blobs_info: Blobs_Info =
wenzelm@65335
   204
        (blobs_names.map(name => Exn.Res((node_name(name), None)): Blob), blobs_index)
wenzelm@65335
   205
      val command = Command(id, node_name(node), blobs_info, span)
wenzelm@65335
   206
      State(command, status, results, markups)
wenzelm@65335
   207
    }
wenzelm@56299
   208
  }
wenzelm@56299
   209
wenzelm@43714
   210
  sealed case class State(
wenzelm@50501
   211
    command: Command,
wenzelm@50501
   212
    status: List[Markup] = Nil,
wenzelm@50507
   213
    results: Results = Results.empty,
wenzelm@55649
   214
    markups: Markups = Markups.empty)
wenzelm@38361
   215
  {
wenzelm@56395
   216
    lazy val protocol_status: Protocol.Status =
wenzelm@56395
   217
    {
wenzelm@56395
   218
      val warnings =
wenzelm@59203
   219
        if (results.iterator.exists(p => Protocol.is_warning(p._2) || Protocol.is_legacy(p._2)))
wenzelm@56395
   220
          List(Markup(Markup.WARNING, Nil))
wenzelm@56395
   221
        else Nil
wenzelm@56395
   222
      val errors =
wenzelm@56395
   223
        if (results.iterator.exists(p => Protocol.is_error(p._2)))
wenzelm@56395
   224
          List(Markup(Markup.ERROR, Nil))
wenzelm@56395
   225
        else Nil
wenzelm@56395
   226
      Protocol.Status.make((warnings ::: errors ::: status).iterator)
wenzelm@56395
   227
    }
wenzelm@55432
   228
wenzelm@55650
   229
    def markup(index: Markup_Index): Markup_Tree = markups(index)
wenzelm@55432
   230
wenzelm@56489
   231
    def redirect(other_command: Command): Option[State] =
wenzelm@56489
   232
    {
wenzelm@56489
   233
      val markups1 = markups.redirect(other_command.id)
wenzelm@56489
   234
      if (markups1.is_empty) None
wenzelm@56489
   235
      else Some(new State(other_command, Nil, Results.empty, markups1))
wenzelm@56489
   236
    }
wenzelm@49614
   237
wenzelm@55649
   238
    private def add_status(st: Markup): State =
wenzelm@55649
   239
      copy(status = st :: status)
wenzelm@55432
   240
wenzelm@56746
   241
    private def add_markup(
wenzelm@56746
   242
      status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
wenzelm@55649
   243
    {
wenzelm@55649
   244
      val markups1 =
wenzelm@56395
   245
        if (status || Protocol.liberal_status_elements(m.info.name))
wenzelm@56462
   246
          markups.add(Markup_Index(true, chunk_name), m)
wenzelm@55649
   247
        else markups
wenzelm@56462
   248
      copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
wenzelm@55649
   249
    }
wenzelm@38361
   250
wenzelm@56470
   251
    def accumulate(
wenzelm@56470
   252
        self_id: Document_ID.Generic => Boolean,
wenzelm@56746
   253
        other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
wenzelm@56470
   254
        message: XML.Elem): State =
wenzelm@38361
   255
      message match {
wenzelm@50201
   256
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
   257
          (this /: msgs)((state, msg) =>
wenzelm@38714
   258
            msg match {
wenzelm@46152
   259
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@55649
   260
                state.
wenzelm@55649
   261
                  add_status(markup).
wenzelm@56746
   262
                  add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
wenzelm@52536
   263
              case _ =>
wenzelm@56782
   264
                Output.warning("Ignored status message: " + msg)
wenzelm@52536
   265
                state
wenzelm@38714
   266
            })
wenzelm@38581
   267
wenzelm@50201
   268
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
   269
          (this /: msgs)((state, msg) =>
wenzelm@55432
   270
            {
wenzelm@56782
   271
              def bad(): Unit = Output.warning("Ignored report message: " + msg)
wenzelm@55432
   272
wenzelm@55432
   273
              msg match {
wenzelm@57911
   274
                case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
wenzelm@56470
   275
wenzelm@56470
   276
                  val target =
wenzelm@56470
   277
                    if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
wenzelm@56470
   278
                      Some((chunk_name, command.chunks(chunk_name)))
wenzelm@56746
   279
                    else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
wenzelm@56470
   280
                    else None
wenzelm@56470
   281
wenzelm@57911
   282
                  (target, atts) match {
wenzelm@57911
   283
                    case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
wenzelm@56470
   284
                      target_chunk.incorporate(symbol_range) match {
wenzelm@55550
   285
                        case Some(range) =>
wenzelm@55822
   286
                          val props = Position.purge(atts)
wenzelm@55822
   287
                          val info = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56470
   288
                          state.add_markup(false, target_name, info)
wenzelm@55550
   289
                        case None => bad(); state
wenzelm@55432
   290
                      }
wenzelm@57911
   291
                    case _ =>
wenzelm@56514
   292
                      // silently ignore excessive reports
wenzelm@56476
   293
                      state
wenzelm@55432
   294
                  }
wenzelm@55432
   295
wenzelm@55432
   296
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   297
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   298
                  val range = command.proper_range
wenzelm@55432
   299
                  val props = Position.purge(atts)
wenzelm@55432
   300
                  val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56746
   301
                  state.add_markup(false, Symbol.Text_Chunk.Default, info)
wenzelm@55432
   302
wenzelm@56470
   303
                case _ => bad(); state
wenzelm@55432
   304
              }
wenzelm@38361
   305
            })
wenzelm@52930
   306
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   307
          props match {
wenzelm@50201
   308
            case Markup.Serial(i) =>
wenzelm@50201
   309
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   310
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   311
wenzelm@55433
   312
              var st = copy(results = results + (i -> message1))
wenzelm@55433
   313
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   314
                for {
wenzelm@56469
   315
                  (chunk_name, chunk) <- command.chunks.iterator
wenzelm@59713
   316
                  range <- Protocol_Message.positions(
wenzelm@59735
   317
                    self_id, command.span.position, chunk_name, chunk, message)
wenzelm@56462
   318
                } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
wenzelm@55433
   319
              }
wenzelm@55433
   320
              st
wenzelm@55432
   321
wenzelm@52536
   322
            case _ =>
wenzelm@56782
   323
              Output.warning("Ignored message without serial number: " + message)
wenzelm@52536
   324
              this
wenzelm@38872
   325
          }
wenzelm@56295
   326
    }
wenzelm@38361
   327
  }
wenzelm@38367
   328
wenzelm@38367
   329
wenzelm@55431
   330
wenzelm@55431
   331
  /** static content **/
wenzelm@55431
   332
wenzelm@45644
   333
  /* make commands */
wenzelm@45644
   334
wenzelm@55648
   335
  def apply(
wenzelm@55648
   336
    id: Document_ID.Command,
wenzelm@55648
   337
    node_name: Document.Node.Name,
wenzelm@59704
   338
    blobs_info: Blobs_Info,
wenzelm@57905
   339
    span: Command_Span.Span): Command =
wenzelm@55648
   340
  {
wenzelm@57901
   341
    val (source, span1) = span.compact_source
wenzelm@59704
   342
    new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
wenzelm@55648
   343
  }
wenzelm@49414
   344
wenzelm@57901
   345
  val empty: Command =
wenzelm@59704
   346
    Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
wenzelm@55648
   347
wenzelm@55648
   348
  def unparsed(
wenzelm@55648
   349
    id: Document_ID.Command,
wenzelm@55648
   350
    source: String,
wenzelm@55648
   351
    results: Results,
wenzelm@55648
   352
    markup: Markup_Tree): Command =
wenzelm@55648
   353
  {
wenzelm@57905
   354
    val (source1, span1) = Command_Span.unparsed(source).compact_source
wenzelm@59704
   355
    new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
wenzelm@55648
   356
  }
wenzelm@49414
   357
wenzelm@65341
   358
  def text(source: String): Command =
wenzelm@52530
   359
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   360
wenzelm@52530
   361
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   362
  {
wenzelm@49466
   363
    val text = XML.content(body)
wenzelm@49466
   364
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   365
    unparsed(id, text, results, markup)
wenzelm@49414
   366
  }
wenzelm@49359
   367
wenzelm@44384
   368
wenzelm@44384
   369
  /* perspective */
wenzelm@44384
   370
wenzelm@44474
   371
  object Perspective
wenzelm@44474
   372
  {
wenzelm@44474
   373
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   374
  }
wenzelm@44385
   375
wenzelm@44474
   376
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   377
  {
wenzelm@57615
   378
    def is_empty: Boolean = commands.isEmpty
wenzelm@57615
   379
wenzelm@44474
   380
    def same(that: Perspective): Boolean =
wenzelm@44474
   381
    {
wenzelm@44474
   382
      val cmds1 = this.commands
wenzelm@44474
   383
      val cmds2 = that.commands
wenzelm@48754
   384
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   385
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   386
      cmds1.length == cmds2.length &&
wenzelm@44474
   387
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   388
    }
wenzelm@44385
   389
  }
wenzelm@59689
   390
wenzelm@59689
   391
wenzelm@59704
   392
  /* blobs: inlined errors and auxiliary files */
wenzelm@59689
   393
wenzelm@59689
   394
  private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
wenzelm@59689
   395
  {
wenzelm@61579
   396
    val markers = Set("%", "--", Symbol.comment, Symbol.comment_decoded)
wenzelm@59689
   397
    def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
wenzelm@59689
   398
      toks match {
wenzelm@59689
   399
        case (t1, i1) :: (t2, i2) :: rest =>
wenzelm@61579
   400
          if (t1.is_keyword && markers(t1.source)) clean(rest)
wenzelm@59689
   401
          else (t1, i1) :: clean((t2, i2) :: rest)
wenzelm@59689
   402
        case _ => toks
wenzelm@59689
   403
      }
wenzelm@59689
   404
    clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
wenzelm@59689
   405
  }
wenzelm@59689
   406
wenzelm@59689
   407
  private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
wenzelm@59924
   408
    if (tokens.exists({ case (t, _) => t.is_command })) {
wenzelm@59924
   409
      tokens.dropWhile({ case (t, _) => !t.is_command }).
wenzelm@64471
   410
        collectFirst({ case (t, i) if t.is_embedded => (t.content, i) })
wenzelm@59689
   411
    }
wenzelm@59924
   412
    else None
wenzelm@59689
   413
wenzelm@63584
   414
  def span_files(syntax: Outer_Syntax, span: Command_Span.Span): (List[String], Int) =
wenzelm@59735
   415
    syntax.load_command(span.name) match {
wenzelm@59735
   416
      case Some(exts) =>
wenzelm@59735
   417
        find_file(clean_tokens(span.content)) match {
wenzelm@59735
   418
          case Some((file, i)) =>
wenzelm@59735
   419
            if (exts.isEmpty) (List(file), i)
wenzelm@59735
   420
            else (exts.map(ext => file + "." + ext), i)
wenzelm@59689
   421
          case None => (Nil, -1)
wenzelm@59689
   422
        }
wenzelm@59735
   423
      case None => (Nil, -1)
wenzelm@59689
   424
    }
wenzelm@59689
   425
wenzelm@59704
   426
  def blobs_info(
wenzelm@59701
   427
    resources: Resources,
wenzelm@63584
   428
    syntax: Outer_Syntax,
wenzelm@59701
   429
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@59704
   430
    can_import: Document.Node.Name => Boolean,
wenzelm@59701
   431
    node_name: Document.Node.Name,
wenzelm@59704
   432
    span: Command_Span.Span): Blobs_Info =
wenzelm@59689
   433
  {
wenzelm@59735
   434
    span.name match {
wenzelm@59704
   435
      // inlined errors
wenzelm@59735
   436
      case Thy_Header.THEORY =>
wenzelm@64825
   437
        val reader = Scan.char_reader(Token.implode(span.content))
wenzelm@65359
   438
        val header = resources.check_thy_reader(node_name, reader)
wenzelm@59715
   439
        val errors =
wenzelm@59710
   440
          for ((imp, pos) <- header.imports if !can_import(imp)) yield {
wenzelm@59715
   441
            val msg =
wenzelm@59715
   442
              "Bad theory import " +
wenzelm@59715
   443
                Markup.Path(imp.node).markup(quote(imp.toString)) + Position.here(pos)
wenzelm@59715
   444
            Exn.Exn(ERROR(msg)): Command.Blob
wenzelm@59710
   445
          }
wenzelm@59715
   446
        (errors, -1)
wenzelm@59704
   447
wenzelm@59704
   448
      // auxiliary files
wenzelm@59704
   449
      case _ =>
wenzelm@59704
   450
        val (files, index) = span_files(syntax, span)
wenzelm@59704
   451
        val blobs =
wenzelm@59704
   452
          files.map(file =>
wenzelm@59704
   453
            (Exn.capture {
wenzelm@65488
   454
              val name = Document.Node.Name(resources.append(node_name, Path.explode(file)))
wenzelm@59704
   455
              val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
wenzelm@59704
   456
              (name, blob)
wenzelm@59704
   457
            }).user_error)
wenzelm@59704
   458
        (blobs, index)
wenzelm@59704
   459
    }
wenzelm@59689
   460
  }
wenzelm@34318
   461
}
wenzelm@34318
   462
wenzelm@38361
   463
wenzelm@46712
   464
final class Command private(
wenzelm@52530
   465
    val id: Document_ID.Command,
wenzelm@44615
   466
    val node_name: Document.Node.Name,
wenzelm@59704
   467
    val blobs_info: Command.Blobs_Info,
wenzelm@57905
   468
    val span: Command_Span.Span,
wenzelm@49414
   469
    val source: String,
wenzelm@50501
   470
    val init_results: Command.Results,
wenzelm@49414
   471
    val init_markup: Markup_Tree)
wenzelm@34451
   472
{
wenzelm@57912
   473
  override def toString: String = id + "/" + span.kind.toString
wenzelm@34495
   474
wenzelm@57910
   475
wenzelm@57910
   476
  /* classification */
wenzelm@57910
   477
wenzelm@57905
   478
  def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
wenzelm@57905
   479
  def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
wenzelm@57904
   480
wenzelm@57904
   481
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@57904
   482
  val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
wenzelm@57904
   483
  val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
wenzelm@57904
   484
wenzelm@34859
   485
wenzelm@54519
   486
  /* blobs */
wenzelm@54519
   487
wenzelm@59704
   488
  def blobs: List[Command.Blob] = blobs_info._1
wenzelm@59704
   489
  def blobs_index: Int = blobs_info._2
wenzelm@59704
   490
wenzelm@65335
   491
  def blobs_ok: Boolean =
wenzelm@65335
   492
    blobs.forall({ case Exn.Res(_) => true case _ => false })
wenzelm@65335
   493
wenzelm@54530
   494
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   495
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   496
wenzelm@60916
   497
  def blobs_undefined: List[Document.Node.Name] =
wenzelm@60916
   498
    for (Exn.Res((name, None)) <- blobs) yield name
wenzelm@60916
   499
wenzelm@57842
   500
  def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
wenzelm@57842
   501
    for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
wenzelm@57842
   502
wenzelm@57842
   503
  def blobs_changed(doc_blobs: Document.Blobs): Boolean =
wenzelm@57842
   504
    blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
wenzelm@54519
   505
wenzelm@55432
   506
wenzelm@56462
   507
  /* source chunks */
wenzelm@55431
   508
wenzelm@56746
   509
  val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
wenzelm@56473
   510
wenzelm@56746
   511
  val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
wenzelm@56746
   512
    ((Symbol.Text_Chunk.Default -> chunk) ::
wenzelm@56473
   513
      (for (Exn.Res((name, Some((_, file)))) <- blobs)
wenzelm@60215
   514
        yield Symbol.Text_Chunk.File(name.node) -> file)).toMap
wenzelm@56473
   515
wenzelm@46813
   516
  def length: Int = source.length
wenzelm@56473
   517
  def range: Text.Range = chunk.range
wenzelm@46813
   518
wenzelm@46813
   519
  val proper_range: Text.Range =
wenzelm@57901
   520
    Text.Range(0,
wenzelm@57901
   521
      (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   522
wenzelm@38426
   523
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   524
wenzelm@38370
   525
wenzelm@38370
   526
  /* accumulated results */
wenzelm@38370
   527
wenzelm@50501
   528
  val init_state: Command.State =
wenzelm@55649
   529
    Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
wenzelm@52527
   530
wenzelm@52527
   531
  val empty_state: Command.State = Command.State(this)
immler@34676
   532
}