src/Pure/PIDE/protocol.scala
author wenzelm
Sun Jan 15 19:09:03 2012 +0100 (2012-01-15)
changeset 46227 4aa84f84d5e8
parent 46209 aad604f74be0
child 46688 134982ee4ecb
permissions -rw-r--r--
more precise rendering of overview_color/gutter_message/squiggly_underline based on cumulation of command status and warning/error messages;
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@44476
    14
  object Assign
wenzelm@44476
    15
  {
wenzelm@44661
    16
    def unapply(text: String): Option[(Document.Version_ID, Document.Assign)] =
wenzelm@44661
    17
      try {
wenzelm@44661
    18
        import XML.Decode._
wenzelm@44661
    19
        val body = YXML.parse_body(text)
wenzelm@44661
    20
        Some(pair(long, pair(list(pair(long, option(long))), list(pair(string, option(long)))))(body))
wenzelm@44661
    21
      }
wenzelm@44661
    22
      catch {
wenzelm@44661
    23
        case ERROR(_) => None
wenzelm@44661
    24
        case _: XML.XML_Atom => None
wenzelm@44661
    25
        case _: XML.XML_Body => None
wenzelm@38412
    26
      }
wenzelm@38412
    27
  }
wenzelm@38567
    28
wenzelm@44676
    29
  object Removed
wenzelm@44676
    30
  {
wenzelm@44676
    31
    def unapply(text: String): Option[List[Document.Version_ID]] =
wenzelm@44676
    32
      try {
wenzelm@44676
    33
        import XML.Decode._
wenzelm@44676
    34
        Some(list(long)(YXML.parse_body(text)))
wenzelm@44676
    35
      }
wenzelm@44676
    36
      catch {
wenzelm@44676
    37
        case ERROR(_) => None
wenzelm@44676
    38
        case _: XML.XML_Atom => None
wenzelm@44676
    39
        case _: XML.XML_Body => None
wenzelm@44676
    40
      }
wenzelm@44676
    41
  }
wenzelm@44676
    42
wenzelm@38567
    43
wenzelm@46209
    44
  /* command status */
wenzelm@38567
    45
wenzelm@46227
    46
  object Status
wenzelm@46227
    47
  {
wenzelm@46227
    48
    val init = Status()
wenzelm@46227
    49
  }
wenzelm@46227
    50
wenzelm@46166
    51
  sealed case class Status(
wenzelm@46166
    52
    private val finished: Boolean = false,
wenzelm@46166
    53
    private val failed: Boolean = false,
wenzelm@46166
    54
    forks: Int = 0)
wenzelm@38567
    55
  {
wenzelm@46166
    56
    def is_unprocessed: Boolean = !finished && !failed && forks == 0
wenzelm@46166
    57
    def is_running: Boolean = forks != 0
wenzelm@46166
    58
    def is_finished: Boolean = finished && forks == 0
wenzelm@46166
    59
    def is_failed: Boolean = failed && forks == 0
wenzelm@46227
    60
    def + (that: Status): Status =
wenzelm@46227
    61
      Status(finished || that.finished, failed || that.failed, forks + that.forks)
wenzelm@46166
    62
  }
wenzelm@46166
    63
wenzelm@46166
    64
  val command_status_markup: Set[String] =
wenzelm@46166
    65
    Set(Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED,
wenzelm@46166
    66
      Isabelle_Markup.FORKED, Isabelle_Markup.JOINED)
wenzelm@46166
    67
wenzelm@46166
    68
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    69
    markup match {
wenzelm@46166
    70
      case Markup(Isabelle_Markup.FINISHED, _) => status.copy(finished = true)
wenzelm@46166
    71
      case Markup(Isabelle_Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    72
      case Markup(Isabelle_Markup.FORKED, _) => status.copy(forks = status.forks + 1)
wenzelm@46166
    73
      case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
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@46209
    80
wenzelm@46209
    81
  /* node status */
wenzelm@46209
    82
wenzelm@44613
    83
  sealed case class Node_Status(unprocessed: Int, running: Int, finished: Int, failed: Int)
wenzelm@44866
    84
  {
wenzelm@44866
    85
    def total: Int = unprocessed + running + finished + failed
wenzelm@44866
    86
  }
wenzelm@44613
    87
wenzelm@44613
    88
  def node_status(
wenzelm@44613
    89
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    90
  {
wenzelm@44613
    91
    var unprocessed = 0
wenzelm@44613
    92
    var running = 0
wenzelm@44613
    93
    var finished = 0
wenzelm@44613
    94
    var failed = 0
wenzelm@44613
    95
    node.commands.foreach(command =>
wenzelm@46166
    96
      {
wenzelm@46166
    97
        val status = command_status(state.command_state(version, command).status)
wenzelm@46166
    98
        if (status.is_running) running += 1
wenzelm@46166
    99
        else if (status.is_finished) finished += 1
wenzelm@46166
   100
        else if (status.is_failed) failed += 1
wenzelm@46166
   101
        else unprocessed += 1
wenzelm@44613
   102
      })
wenzelm@44613
   103
    Node_Status(unprocessed, running, finished, failed)
wenzelm@44613
   104
  }
wenzelm@44613
   105
wenzelm@38887
   106
wenzelm@39441
   107
  /* result messages */
wenzelm@39439
   108
wenzelm@39439
   109
  def clean_message(body: XML.Body): XML.Body =
wenzelm@45666
   110
    body filter { case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false case _ => true } map
wenzelm@39439
   111
      { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
wenzelm@39439
   112
wenzelm@39441
   113
  def message_reports(msg: XML.Tree): List[XML.Elem] =
wenzelm@39441
   114
    msg match {
wenzelm@45666
   115
      case elem @ XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => List(elem)
wenzelm@39441
   116
      case XML.Elem(_, body) => body.flatMap(message_reports)
wenzelm@39441
   117
      case XML.Text(_) => Nil
wenzelm@39441
   118
    }
wenzelm@39441
   119
wenzelm@39439
   120
wenzelm@39511
   121
  /* specific messages */
wenzelm@39511
   122
wenzelm@39625
   123
 def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   124
    msg match {
wenzelm@45666
   125
      case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
wenzelm@39622
   126
      case _ => false
wenzelm@39622
   127
    }
wenzelm@39622
   128
wenzelm@39511
   129
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   130
    msg match {
wenzelm@45666
   131
      case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
wenzelm@39511
   132
      case _ => false
wenzelm@39511
   133
    }
wenzelm@39511
   134
wenzelm@39511
   135
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   136
    msg match {
wenzelm@45666
   137
      case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
wenzelm@39511
   138
      case _ => false
wenzelm@39511
   139
    }
wenzelm@38887
   140
wenzelm@39441
   141
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
   142
    msg match {
wenzelm@45666
   143
      case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
wenzelm@45666
   144
        List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
wenzelm@39170
   145
      case _ => false
wenzelm@39170
   146
    }
wenzelm@39170
   147
wenzelm@39511
   148
wenzelm@39511
   149
  /* reported positions */
wenzelm@39511
   150
wenzelm@39627
   151
  private val include_pos =
wenzelm@45666
   152
    Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
wenzelm@45666
   153
      Isabelle_Markup.POSITION)
wenzelm@39441
   154
wenzelm@39441
   155
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   156
  {
wenzelm@39441
   157
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   158
      tree match {
wenzelm@39172
   159
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
   160
        if include_pos(name) && id == command.id =>
wenzelm@46207
   161
          // FIXME handle message range outside command range (!??!)
wenzelm@39172
   162
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39441
   163
          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@39441
   164
        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
wenzelm@39042
   165
        case _ => set
wenzelm@38887
   166
      }
wenzelm@39441
   167
    val set = positions(Set.empty, message)
wenzelm@39170
   168
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   169
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   170
    else set
wenzelm@38887
   171
  }
wenzelm@38412
   172
}
wenzelm@38412
   173
wenzelm@38412
   174
wenzelm@45709
   175
trait Protocol extends Isabelle_Process
wenzelm@38412
   176
{
wenzelm@38412
   177
  /* commands */
wenzelm@38412
   178
wenzelm@44644
   179
  def define_command(command: Command): Unit =
wenzelm@45709
   180
    input("Document.define_command",
wenzelm@44644
   181
      Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
wenzelm@38412
   182
wenzelm@38412
   183
wenzelm@38417
   184
  /* document versions */
wenzelm@38412
   185
wenzelm@44612
   186
  def cancel_execution()
wenzelm@44612
   187
  {
wenzelm@45709
   188
    input("Document.cancel_execution")
wenzelm@44612
   189
  }
wenzelm@44612
   190
wenzelm@44436
   191
  def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44615
   192
    name: Document.Node.Name, perspective: Command.Perspective)
wenzelm@44436
   193
  {
wenzelm@44436
   194
    val ids =
wenzelm@44436
   195
    { import XML.Encode._
wenzelm@44474
   196
      list(long)(perspective.commands.map(_.id)) }
wenzelm@44436
   197
wenzelm@45709
   198
    input("Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
wenzelm@44615
   199
      name.node, YXML.string_of_body(ids))
wenzelm@44436
   200
  }
wenzelm@44436
   201
wenzelm@44481
   202
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   203
    edits: List[Document.Edit_Command])
wenzelm@38412
   204
  {
wenzelm@44157
   205
    val edits_yxml =
wenzelm@43767
   206
    { import XML.Encode._
wenzelm@44383
   207
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@44979
   208
      def encode_edit(dir: String)
wenzelm@44979
   209
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   210
        variant(List(
wenzelm@44979
   211
          { case Document.Node.Clear() => (Nil, Nil) },
wenzelm@44979
   212
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@44979
   213
          { case Document.Node.Header(Exn.Res(Thy_Header(a, b, c))) =>
wenzelm@44979
   214
              (Nil,
wenzelm@44979
   215
                triple(pair(string, string), list(string), list(pair(string, bool)))(
wenzelm@44979
   216
                  (dir, a), b, c)) },
wenzelm@44979
   217
          { case Document.Node.Header(Exn.Exn(e)) => (List(Exn.message(e)), Nil) },
wenzelm@44979
   218
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@44979
   219
      def encode: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   220
      {
wenzelm@44979
   221
        val (name, edit) = node_edit
wenzelm@44979
   222
        val dir = Isabelle_System.posix_path(name.dir)
wenzelm@44979
   223
        pair(string, encode_edit(dir))(name.node, edit)
wenzelm@44979
   224
      })
wenzelm@44157
   225
      YXML.string_of_body(encode(edits)) }
wenzelm@45709
   226
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   227
  }
wenzelm@43748
   228
wenzelm@44673
   229
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   230
  {
wenzelm@44673
   231
    val versions_yxml =
wenzelm@44673
   232
      { import XML.Encode._
wenzelm@44673
   233
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   234
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   235
  }
wenzelm@44673
   236
wenzelm@43748
   237
wenzelm@43748
   238
  /* method invocation service */
wenzelm@43748
   239
wenzelm@43748
   240
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   241
  {
wenzelm@45709
   242
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   243
  }
wenzelm@38412
   244
}