src/Pure/PIDE/isar_document.scala
author wenzelm
Sat Sep 03 21:15:35 2011 +0200 (2011-09-03)
changeset 44676 7de87f1ae965
parent 44673 2fa51ac191bc
child 44866 0eb8284a64bd
permissions -rw-r--r--
Document.removed_versions on Scala side;
wenzelm@38483
     1
/*  Title:      Pure/PIDE/isar_document.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@38567
     4
Protocol message formats for interactive Isar documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@38412
    10
object Isar_Document
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@38567
    44
  /* toplevel transactions */
wenzelm@38567
    45
wenzelm@38567
    46
  sealed abstract class Status
wenzelm@44613
    47
  case object Unprocessed extends Status
wenzelm@38567
    48
  case class Forked(forks: Int) extends Status
wenzelm@38567
    49
  case object Finished extends Status
wenzelm@38567
    50
  case object Failed extends Status
wenzelm@38567
    51
wenzelm@38581
    52
  def command_status(markup: List[Markup]): Status =
wenzelm@38567
    53
  {
wenzelm@38567
    54
    val forks = (0 /: markup) {
wenzelm@38581
    55
      case (i, Markup(Markup.FORKED, _)) => i + 1
wenzelm@38581
    56
      case (i, Markup(Markup.JOINED, _)) => i - 1
wenzelm@38567
    57
      case (i, _) => i
wenzelm@38567
    58
    }
wenzelm@38567
    59
    if (forks != 0) Forked(forks)
wenzelm@38581
    60
    else if (markup.exists(_.name == Markup.FAILED)) Failed
wenzelm@38581
    61
    else if (markup.exists(_.name == Markup.FINISHED)) Finished
wenzelm@38567
    62
    else Unprocessed
wenzelm@38567
    63
  }
wenzelm@38887
    64
wenzelm@44613
    65
  sealed case class Node_Status(unprocessed: Int, running: Int, finished: Int, failed: Int)
wenzelm@44613
    66
wenzelm@44613
    67
  def node_status(
wenzelm@44613
    68
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    69
  {
wenzelm@44613
    70
    var unprocessed = 0
wenzelm@44613
    71
    var running = 0
wenzelm@44613
    72
    var finished = 0
wenzelm@44613
    73
    var failed = 0
wenzelm@44613
    74
    node.commands.foreach(command =>
wenzelm@44613
    75
      command_status(state.command_state(version, command).status) match {
wenzelm@44613
    76
        case Unprocessed => unprocessed += 1
wenzelm@44613
    77
        case Forked(_) => running += 1
wenzelm@44613
    78
        case Finished => finished += 1
wenzelm@44613
    79
        case Failed => failed += 1
wenzelm@44613
    80
      })
wenzelm@44613
    81
    Node_Status(unprocessed, running, finished, failed)
wenzelm@44613
    82
  }
wenzelm@44613
    83
wenzelm@38887
    84
wenzelm@39441
    85
  /* result messages */
wenzelm@39439
    86
wenzelm@39439
    87
  def clean_message(body: XML.Body): XML.Body =
wenzelm@39439
    88
    body filter { case XML.Elem(Markup(Markup.NO_REPORT, _), _) => false case _ => true } map
wenzelm@39439
    89
      { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
wenzelm@39439
    90
wenzelm@39441
    91
  def message_reports(msg: XML.Tree): List[XML.Elem] =
wenzelm@39441
    92
    msg match {
wenzelm@39441
    93
      case elem @ XML.Elem(Markup(Markup.REPORT, _), _) => List(elem)
wenzelm@39441
    94
      case XML.Elem(_, body) => body.flatMap(message_reports)
wenzelm@39441
    95
      case XML.Text(_) => Nil
wenzelm@39441
    96
    }
wenzelm@39441
    97
wenzelm@39439
    98
wenzelm@39511
    99
  /* specific messages */
wenzelm@39511
   100
wenzelm@39625
   101
  def is_ready(msg: XML.Tree): Boolean =
wenzelm@39625
   102
    msg match {
wenzelm@39625
   103
      case XML.Elem(Markup(Markup.STATUS, _),
wenzelm@39625
   104
        List(XML.Elem(Markup(Markup.READY, _), _))) => true
wenzelm@39625
   105
      case _ => false
wenzelm@39625
   106
    }
wenzelm@39625
   107
wenzelm@39625
   108
 def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   109
    msg match {
wenzelm@39622
   110
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@39622
   111
      case _ => false
wenzelm@39622
   112
    }
wenzelm@39622
   113
wenzelm@39511
   114
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   115
    msg match {
wenzelm@39511
   116
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@39511
   117
      case _ => false
wenzelm@39511
   118
    }
wenzelm@39511
   119
wenzelm@39511
   120
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   121
    msg match {
wenzelm@39511
   122
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@39511
   123
      case _ => false
wenzelm@39511
   124
    }
wenzelm@38887
   125
wenzelm@39441
   126
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
   127
    msg match {
wenzelm@39627
   128
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@39627
   129
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@39170
   130
      case _ => false
wenzelm@39170
   131
    }
wenzelm@39170
   132
wenzelm@39511
   133
wenzelm@39511
   134
  /* reported positions */
wenzelm@39511
   135
wenzelm@39627
   136
  private val include_pos =
wenzelm@39627
   137
    Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   138
wenzelm@39441
   139
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   140
  {
wenzelm@39441
   141
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   142
      tree match {
wenzelm@39172
   143
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
   144
        if include_pos(name) && id == command.id =>
wenzelm@39172
   145
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39441
   146
          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@39441
   147
        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
wenzelm@39042
   148
        case _ => set
wenzelm@38887
   149
      }
wenzelm@39441
   150
    val set = positions(Set.empty, message)
wenzelm@39170
   151
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   152
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   153
    else set
wenzelm@38887
   154
  }
wenzelm@38412
   155
}
wenzelm@38412
   156
wenzelm@38412
   157
wenzelm@38412
   158
trait Isar_Document extends Isabelle_Process
wenzelm@38412
   159
{
wenzelm@38412
   160
  /* commands */
wenzelm@38412
   161
wenzelm@44644
   162
  def define_command(command: Command): Unit =
wenzelm@44644
   163
    input("Isar_Document.define_command",
wenzelm@44644
   164
      Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
wenzelm@38412
   165
wenzelm@38412
   166
wenzelm@38417
   167
  /* document versions */
wenzelm@38412
   168
wenzelm@44612
   169
  def cancel_execution()
wenzelm@44612
   170
  {
wenzelm@44612
   171
    input("Isar_Document.cancel_execution")
wenzelm@44612
   172
  }
wenzelm@44612
   173
wenzelm@44436
   174
  def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44615
   175
    name: Document.Node.Name, perspective: Command.Perspective)
wenzelm@44436
   176
  {
wenzelm@44436
   177
    val ids =
wenzelm@44436
   178
    { import XML.Encode._
wenzelm@44474
   179
      list(long)(perspective.commands.map(_.id)) }
wenzelm@44436
   180
wenzelm@44615
   181
    input("Isar_Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
wenzelm@44615
   182
      name.node, YXML.string_of_body(ids))
wenzelm@44436
   183
  }
wenzelm@44436
   184
wenzelm@44481
   185
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   186
    edits: List[Document.Edit_Command])
wenzelm@38412
   187
  {
wenzelm@44157
   188
    val edits_yxml =
wenzelm@43767
   189
    { import XML.Encode._
wenzelm@44383
   190
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@44383
   191
      def encode: T[List[Document.Edit_Command]] =
wenzelm@44615
   192
        list(pair((name => string(name.node)),
wenzelm@44156
   193
          variant(List(
wenzelm@44185
   194
            { case Document.Node.Clear() => (Nil, Nil) },
wenzelm@44383
   195
            { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@44182
   196
            { case Document.Node.Header(Exn.Res(Thy_Header(a, b, c))) =>
wenzelm@44185
   197
                (Nil, triple(string, list(string), list(pair(string, bool)))(a, b, c)) },
wenzelm@44384
   198
            { case Document.Node.Header(Exn.Exn(e)) => (List(Exn.message(e)), Nil) },
wenzelm@44474
   199
            { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))))
wenzelm@44157
   200
      YXML.string_of_body(encode(edits)) }
wenzelm@38412
   201
wenzelm@44481
   202
    input("Isar_Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   203
  }
wenzelm@43748
   204
wenzelm@44673
   205
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   206
  {
wenzelm@44673
   207
    val versions_yxml =
wenzelm@44673
   208
      { import XML.Encode._
wenzelm@44673
   209
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@44673
   210
    input("Isar_Document.remove_versions", versions_yxml)
wenzelm@44673
   211
  }
wenzelm@44673
   212
wenzelm@43748
   213
wenzelm@43748
   214
  /* method invocation service */
wenzelm@43748
   215
wenzelm@43748
   216
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   217
  {
wenzelm@43748
   218
    input("Isar_Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   219
  }
wenzelm@38412
   220
}