src/Pure/PIDE/protocol.scala
author wenzelm
Sat Mar 01 15:58:47 2014 +0100 (2014-03-01)
changeset 55822 ccf2d784be97
parent 55820 61869776ce1f
child 55884 f2c0eaedd579
permissions -rw-r--r--
incorporate chunk range that is 1 off end-of-input, for improved error positions (NB: command spans are tight, without trailing whitespace);
tuned signature;
wenzelm@45709
     1
/*  Title:      Pure/PIDE/protocol.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@45709
    10
object Protocol
wenzelm@38412
    11
{
wenzelm@38567
    12
  /* document editing */
wenzelm@38412
    13
wenzelm@52563
    14
  object Assign_Update
wenzelm@44476
    15
  {
wenzelm@52563
    16
    def unapply(text: String): Option[(Document_ID.Version, Document.Assign_Update)] =
wenzelm@44661
    17
      try {
wenzelm@44661
    18
        import XML.Decode._
wenzelm@44661
    19
        val body = YXML.parse_body(text)
wenzelm@52527
    20
        Some(pair(long, list(pair(long, list(long))))(body))
wenzelm@44661
    21
      }
wenzelm@44661
    22
      catch {
wenzelm@44661
    23
        case ERROR(_) => None
wenzelm@51987
    24
        case _: XML.Error => None
wenzelm@38412
    25
      }
wenzelm@38412
    26
  }
wenzelm@38567
    27
wenzelm@44676
    28
  object Removed
wenzelm@44676
    29
  {
wenzelm@52530
    30
    def unapply(text: String): Option[List[Document_ID.Version]] =
wenzelm@44676
    31
      try {
wenzelm@44676
    32
        import XML.Decode._
wenzelm@44676
    33
        Some(list(long)(YXML.parse_body(text)))
wenzelm@44676
    34
      }
wenzelm@44676
    35
      catch {
wenzelm@44676
    36
        case ERROR(_) => None
wenzelm@51987
    37
        case _: XML.Error => None
wenzelm@44676
    38
      }
wenzelm@44676
    39
  }
wenzelm@44676
    40
wenzelm@38567
    41
wenzelm@46209
    42
  /* command status */
wenzelm@38567
    43
wenzelm@46227
    44
  object Status
wenzelm@46227
    45
  {
wenzelm@46227
    46
    val init = Status()
wenzelm@46227
    47
  }
wenzelm@46227
    48
wenzelm@46166
    49
  sealed case class Status(
wenzelm@49036
    50
    private val touched: Boolean = false,
wenzelm@47395
    51
    private val accepted: Boolean = false,
wenzelm@46166
    52
    private val failed: Boolean = false,
wenzelm@49036
    53
    forks: Int = 0,
wenzelm@49036
    54
    runs: Int = 0)
wenzelm@38567
    55
  {
wenzelm@46227
    56
    def + (that: Status): Status =
wenzelm@49036
    57
      Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
wenzelm@49036
    58
        forks + that.forks, runs + that.runs)
wenzelm@49036
    59
wenzelm@49036
    60
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    61
    def is_running: Boolean = runs != 0
wenzelm@49039
    62
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@49036
    63
    def is_failed: Boolean = failed
wenzelm@46166
    64
  }
wenzelm@46166
    65
wenzelm@46166
    66
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    67
    markup match {
wenzelm@50201
    68
      case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
wenzelm@50201
    69
      case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
wenzelm@50201
    70
      case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
wenzelm@50201
    71
      case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
wenzelm@50201
    72
      case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
wenzelm@50201
    73
      case Markup(Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    74
      case _ => status
wenzelm@38567
    75
    }
wenzelm@46166
    76
wenzelm@46166
    77
  def command_status(markups: List[Markup]): Status =
wenzelm@46227
    78
    (Status.init /: markups)(command_status(_, _))
wenzelm@38887
    79
wenzelm@55820
    80
  val command_status_elements =
wenzelm@55820
    81
    Document.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@55646
    82
      Markup.FINISHED, Markup.FAILED)
wenzelm@55646
    83
wenzelm@55820
    84
  val status_elements =
wenzelm@55646
    85
    command_status_elements + Markup.WARNING + Markup.ERROR
wenzelm@55646
    86
wenzelm@46209
    87
wenzelm@51818
    88
  /* command timing */
wenzelm@51818
    89
wenzelm@51818
    90
  object Command_Timing
wenzelm@51818
    91
  {
wenzelm@52531
    92
    def unapply(props: Properties.T): Option[(Document_ID.Generic, isabelle.Timing)] =
wenzelm@51818
    93
      props match {
wenzelm@51818
    94
        case (Markup.FUNCTION, Markup.COMMAND_TIMING) :: args =>
wenzelm@51818
    95
          (args, args) match {
wenzelm@51818
    96
            case (Position.Id(id), Markup.Timing_Properties(timing)) => Some((id, timing))
wenzelm@51818
    97
            case _ => None
wenzelm@51818
    98
          }
wenzelm@51818
    99
        case _ => None
wenzelm@51818
   100
      }
wenzelm@51818
   101
  }
wenzelm@51818
   102
wenzelm@51818
   103
wenzelm@46209
   104
  /* node status */
wenzelm@46209
   105
wenzelm@46688
   106
  sealed case class Node_Status(
wenzelm@46688
   107
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
   108
  {
wenzelm@46688
   109
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
   110
  }
wenzelm@44613
   111
wenzelm@44613
   112
  def node_status(
wenzelm@44613
   113
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
   114
  {
wenzelm@44613
   115
    var unprocessed = 0
wenzelm@44613
   116
    var running = 0
wenzelm@44613
   117
    var finished = 0
wenzelm@46688
   118
    var warned = 0
wenzelm@44613
   119
    var failed = 0
wenzelm@44613
   120
    node.commands.foreach(command =>
wenzelm@46166
   121
      {
wenzelm@46688
   122
        val st = state.command_state(version, command)
wenzelm@46688
   123
        val status = command_status(st.status)
wenzelm@46166
   124
        if (status.is_running) running += 1
wenzelm@46688
   125
        else if (status.is_finished) {
wenzelm@50507
   126
          if (st.results.entries.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   127
          else finished += 1
wenzelm@46688
   128
        }
wenzelm@46166
   129
        else if (status.is_failed) failed += 1
wenzelm@46166
   130
        else unprocessed += 1
wenzelm@44613
   131
      })
wenzelm@46688
   132
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   133
  }
wenzelm@44613
   134
wenzelm@38887
   135
wenzelm@51533
   136
  /* node timing */
wenzelm@51533
   137
wenzelm@51533
   138
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
wenzelm@51533
   139
wenzelm@51533
   140
  val empty_node_timing = Node_Timing(0.0, Map.empty)
wenzelm@51533
   141
wenzelm@51533
   142
  def node_timing(
wenzelm@51533
   143
    state: Document.State,
wenzelm@51533
   144
    version: Document.Version,
wenzelm@51533
   145
    node: Document.Node,
wenzelm@51533
   146
    threshold: Double): Node_Timing =
wenzelm@51533
   147
  {
wenzelm@51533
   148
    var total = 0.0
wenzelm@51533
   149
    var commands = Map.empty[Command, Double]
wenzelm@51533
   150
    for {
wenzelm@51533
   151
      command <- node.commands.iterator
wenzelm@51533
   152
      st = state.command_state(version, command)
wenzelm@51533
   153
      command_timing =
wenzelm@51533
   154
        (0.0 /: st.status)({
wenzelm@51533
   155
          case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@51533
   156
          case (timing, _) => timing
wenzelm@51533
   157
        })
wenzelm@51533
   158
    } {
wenzelm@51533
   159
      total += command_timing
wenzelm@51533
   160
      if (command_timing >= threshold) commands += (command -> command_timing)
wenzelm@51533
   161
    }
wenzelm@51533
   162
    Node_Timing(total, commands)
wenzelm@51533
   163
  }
wenzelm@51533
   164
wenzelm@51533
   165
wenzelm@39441
   166
  /* result messages */
wenzelm@39439
   167
wenzelm@55820
   168
  private val clean_elements =
wenzelm@55820
   169
    Document.Elements(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   170
wenzelm@39439
   171
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   172
    body filter {
wenzelm@55646
   173
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean_elements(name)
wenzelm@55646
   174
      case XML.Elem(Markup(name, _), _) => !clean_elements(name)
wenzelm@49445
   175
      case _ => true
wenzelm@49650
   176
    } map {
wenzelm@49650
   177
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   178
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   179
      case t => t
wenzelm@49650
   180
    }
wenzelm@39439
   181
wenzelm@49445
   182
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   183
    body flatMap {
wenzelm@50450
   184
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   185
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   186
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   187
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   188
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   189
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   190
      case XML.Text(_) => Nil
wenzelm@39441
   191
    }
wenzelm@39441
   192
wenzelm@39439
   193
wenzelm@39511
   194
  /* specific messages */
wenzelm@39511
   195
wenzelm@50500
   196
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@50500
   197
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@50500
   198
wenzelm@50500
   199
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   200
    msg match {
wenzelm@50500
   201
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   202
      case _ => false
wenzelm@50500
   203
    }
wenzelm@50500
   204
wenzelm@50157
   205
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   206
    msg match {
wenzelm@50201
   207
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   208
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   209
      case _ => false
wenzelm@39622
   210
    }
wenzelm@39622
   211
wenzelm@52650
   212
  def is_writeln_markup(msg: XML.Tree, name: String): Boolean =
wenzelm@50500
   213
    msg match {
wenzelm@50500
   214
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@52650
   215
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   216
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@52650
   217
        List(XML.Elem(markup, _))) => markup.name == name
wenzelm@50500
   218
      case _ => false
wenzelm@50500
   219
    }
wenzelm@50500
   220
wenzelm@52650
   221
  def is_state(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.STATE)
wenzelm@52650
   222
  def is_information(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.INFORMATION)
wenzelm@52650
   223
wenzelm@39511
   224
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   225
    msg match {
wenzelm@50201
   226
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   227
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   228
      case _ => false
wenzelm@39511
   229
    }
wenzelm@39511
   230
wenzelm@39511
   231
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   232
    msg match {
wenzelm@50201
   233
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   234
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   235
      case _ => false
wenzelm@39511
   236
    }
wenzelm@38887
   237
wenzelm@50500
   238
wenzelm@50500
   239
  /* dialogs */
wenzelm@50500
   240
wenzelm@50500
   241
  object Dialog_Args
wenzelm@50500
   242
  {
wenzelm@52531
   243
    def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   244
      (props, props, props) match {
wenzelm@50500
   245
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   246
          Some((id, serial, result))
wenzelm@50500
   247
        case _ => None
wenzelm@50500
   248
      }
wenzelm@50500
   249
  }
wenzelm@50500
   250
wenzelm@50500
   251
  object Dialog
wenzelm@50500
   252
  {
wenzelm@52531
   253
    def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
wenzelm@50500
   254
      tree match {
wenzelm@50500
   255
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   256
          Some((id, serial, result))
wenzelm@50500
   257
        case _ => None
wenzelm@50500
   258
      }
wenzelm@50500
   259
  }
wenzelm@50500
   260
wenzelm@50500
   261
  object Dialog_Result
wenzelm@50500
   262
  {
wenzelm@52531
   263
    def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
wenzelm@50501
   264
    {
wenzelm@50501
   265
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   266
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   267
    }
wenzelm@50500
   268
wenzelm@50501
   269
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   270
      tree match {
wenzelm@50501
   271
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   272
        case _ => None
wenzelm@50500
   273
      }
wenzelm@50500
   274
  }
wenzelm@39170
   275
wenzelm@39511
   276
wenzelm@39511
   277
  /* reported positions */
wenzelm@39511
   278
wenzelm@55646
   279
  private val position_elements =
wenzelm@55820
   280
    Document.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   281
wenzelm@55433
   282
  def message_positions(
wenzelm@55433
   283
    command_id: Document_ID.Command,
wenzelm@55433
   284
    alt_id: Document_ID.Generic,
wenzelm@55433
   285
    chunk: Command.Chunk,
wenzelm@55433
   286
    message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   287
  {
wenzelm@55433
   288
    def elem_positions(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
wenzelm@55433
   289
      props match {
wenzelm@55548
   290
        case Position.Reported(id, file_name, raw_range)
wenzelm@55433
   291
        if (id == command_id || id == alt_id) && file_name == chunk.file_name =>
wenzelm@55822
   292
          chunk.incorporate(raw_range) match {
wenzelm@55822
   293
            case Some(range) => set + range
wenzelm@55548
   294
            case _ => set
wenzelm@55548
   295
          }
wenzelm@55433
   296
        case _ => set
wenzelm@55433
   297
      }
wenzelm@50157
   298
wenzelm@39441
   299
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   300
      tree match {
wenzelm@55433
   301
        case XML.Wrapped_Elem(Markup(name, props), _, body) =>
wenzelm@55646
   302
          body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
wenzelm@55433
   303
        case XML.Elem(Markup(name, props), body) =>
wenzelm@55646
   304
          body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
wenzelm@55433
   305
        case XML.Text(_) => set
wenzelm@38887
   306
      }
wenzelm@50157
   307
wenzelm@39441
   308
    val set = positions(Set.empty, message)
wenzelm@55433
   309
    if (set.isEmpty) elem_positions(message.markup.properties, set)
wenzelm@39042
   310
    else set
wenzelm@38887
   311
  }
wenzelm@38412
   312
}
wenzelm@38412
   313
wenzelm@38412
   314
wenzelm@45709
   315
trait Protocol extends Isabelle_Process
wenzelm@38412
   316
{
wenzelm@54519
   317
  /* inlined files */
wenzelm@54519
   318
wenzelm@55783
   319
  def define_blob(blob: Document.Blob): Unit =
wenzelm@55783
   320
    protocol_command_raw(
wenzelm@55783
   321
      "Document.define_blob", Bytes(blob.bytes.sha1_digest.toString), blob.bytes)
wenzelm@54519
   322
wenzelm@54519
   323
wenzelm@38412
   324
  /* commands */
wenzelm@38412
   325
wenzelm@44644
   326
  def define_command(command: Command): Unit =
wenzelm@54519
   327
  {
wenzelm@54519
   328
    val blobs_yxml =
wenzelm@54519
   329
    { import XML.Encode._
wenzelm@54519
   330
      val encode_blob: T[Command.Blob] =
wenzelm@54519
   331
        variant(List(
wenzelm@54526
   332
          { case Exn.Res((a, b)) =>
wenzelm@55431
   333
              (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
wenzelm@54526
   334
          { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
wenzelm@54519
   335
      YXML.string_of_body(list(encode_blob)(command.blobs))
wenzelm@54519
   336
    }
wenzelm@52582
   337
    protocol_command("Document.define_command",
wenzelm@54519
   338
      Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
wenzelm@54519
   339
  }
wenzelm@38412
   340
wenzelm@38412
   341
wenzelm@52931
   342
  /* execution */
wenzelm@52931
   343
wenzelm@52931
   344
  def discontinue_execution(): Unit =
wenzelm@52931
   345
    protocol_command("Document.discontinue_execution")
wenzelm@38412
   346
wenzelm@52931
   347
  def cancel_exec(id: Document_ID.Exec): Unit =
wenzelm@52931
   348
    protocol_command("Document.cancel_exec", Document_ID(id))
wenzelm@52931
   349
wenzelm@52931
   350
wenzelm@52931
   351
  /* document versions */
wenzelm@47343
   352
wenzelm@52530
   353
  def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
wenzelm@44383
   354
    edits: List[Document.Edit_Command])
wenzelm@38412
   355
  {
wenzelm@44157
   356
    val edits_yxml =
wenzelm@43767
   357
    { import XML.Encode._
wenzelm@44383
   358
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   359
      def encode_edit(name: Document.Node.Name)
wenzelm@52849
   360
          : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
wenzelm@44979
   361
        variant(List(
wenzelm@44979
   362
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   363
          { case Document.Node.Deps(header) =>
wenzelm@54515
   364
              val master_dir = Isabelle_System.posix_path(name.master_dir)
wenzelm@48707
   365
              val imports = header.imports.map(_.node)
wenzelm@50128
   366
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@44979
   367
              (Nil,
wenzelm@48707
   368
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   369
                  pair(list(pair(Encode.string,
wenzelm@48864
   370
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@51294
   371
                  list(Encode.string)))))(
wenzelm@54515
   372
                (master_dir, (name.theory, (imports, (keywords, header.errors)))))) },
wenzelm@52849
   373
          { case Document.Node.Perspective(a, b, c) =>
wenzelm@52849
   374
              (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
wenzelm@52862
   375
                list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
wenzelm@48705
   376
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   377
      {
wenzelm@44979
   378
        val (name, edit) = node_edit
wenzelm@46737
   379
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   380
      })
wenzelm@48705
   381
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@52582
   382
    protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
wenzelm@38412
   383
  }
wenzelm@43748
   384
wenzelm@44673
   385
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   386
  {
wenzelm@44673
   387
    val versions_yxml =
wenzelm@44673
   388
      { import XML.Encode._
wenzelm@44673
   389
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@52582
   390
    protocol_command("Document.remove_versions", versions_yxml)
wenzelm@44673
   391
  }
wenzelm@44673
   392
wenzelm@43748
   393
wenzelm@50498
   394
  /* dialog via document content */
wenzelm@50498
   395
wenzelm@52931
   396
  def dialog_result(serial: Long, result: String): Unit =
wenzelm@52582
   397
    protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@38412
   398
}