src/Pure/PIDE/isar_document.scala
author wenzelm
Fri Aug 20 11:57:43 2010 +0200 (2010-08-20)
changeset 38567 b670faa807c9
parent 38483 3d16bebee1d3
child 38581 d503a0912e14
permissions -rw-r--r--
concentrate protocol message formats in Isar_Document;
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@38412
    14
  object Assign {
wenzelm@38414
    15
    def unapply(msg: XML.Tree)
wenzelm@38414
    16
        : Option[(Document.Version_ID, List[(Document.Command_ID, Document.Exec_ID)])] =
wenzelm@38412
    17
      msg match {
wenzelm@38414
    18
        case XML.Elem(Markup(Markup.ASSIGN, List((Markup.VERSION, Document.ID(id)))), edits) =>
wenzelm@38412
    19
          val id_edits = edits.map(Edit.unapply)
wenzelm@38414
    20
          if (id_edits.forall(_.isDefined)) Some((id, id_edits.map(_.get)))
wenzelm@38412
    21
          else None
wenzelm@38412
    22
        case _ => None
wenzelm@38412
    23
      }
wenzelm@38412
    24
  }
wenzelm@38412
    25
wenzelm@38412
    26
  object Edit {
wenzelm@38412
    27
    def unapply(msg: XML.Tree): Option[(Document.Command_ID, Document.Exec_ID)] =
wenzelm@38412
    28
      msg match {
wenzelm@38414
    29
        case XML.Elem(
wenzelm@38414
    30
          Markup(Markup.EDIT,
wenzelm@38414
    31
            List((Markup.ID, Document.ID(i)), (Markup.EXEC, Document.ID(j)))), Nil) => Some((i, j))
wenzelm@38412
    32
        case _ => None
wenzelm@38412
    33
      }
wenzelm@38412
    34
  }
wenzelm@38567
    35
wenzelm@38567
    36
wenzelm@38567
    37
  /* toplevel transactions */
wenzelm@38567
    38
wenzelm@38567
    39
  sealed abstract class Status
wenzelm@38567
    40
  case class Forked(forks: Int) extends Status
wenzelm@38567
    41
  case object Unprocessed extends Status
wenzelm@38567
    42
  case object Finished extends Status
wenzelm@38567
    43
  case object Failed extends Status
wenzelm@38567
    44
wenzelm@38567
    45
  def command_status(markup: XML.Body): Status =
wenzelm@38567
    46
  {
wenzelm@38567
    47
    val forks = (0 /: markup) {
wenzelm@38567
    48
      case (i, XML.Elem(Markup(Markup.FORKED, _), _)) => i + 1
wenzelm@38567
    49
      case (i, XML.Elem(Markup(Markup.JOINED, _), _)) => i - 1
wenzelm@38567
    50
      case (i, _) => i
wenzelm@38567
    51
    }
wenzelm@38567
    52
    if (forks != 0) Forked(forks)
wenzelm@38567
    53
    else if (markup.exists { case XML.Elem(Markup(Markup.FAILED, _), _) => true case _ => false })
wenzelm@38567
    54
      Failed
wenzelm@38567
    55
    else if (markup.exists { case XML.Elem(Markup(Markup.FINISHED, _), _) => true case _ => false })
wenzelm@38567
    56
      Finished
wenzelm@38567
    57
    else Unprocessed
wenzelm@38567
    58
  }
wenzelm@38412
    59
}
wenzelm@38412
    60
wenzelm@38412
    61
wenzelm@38412
    62
trait Isar_Document extends Isabelle_Process
wenzelm@38412
    63
{
wenzelm@38412
    64
  import Isar_Document._
wenzelm@38412
    65
wenzelm@38412
    66
wenzelm@38412
    67
  /* commands */
wenzelm@38412
    68
wenzelm@38412
    69
  def define_command(id: Document.Command_ID, text: String): Unit =
wenzelm@38414
    70
    input("Isar_Document.define_command", Document.ID(id), text)
wenzelm@38412
    71
wenzelm@38412
    72
wenzelm@38417
    73
  /* document versions */
wenzelm@38412
    74
wenzelm@38417
    75
  def edit_version(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@38412
    76
      edits: List[Document.Edit[Document.Command_ID]])
wenzelm@38412
    77
  {
wenzelm@38412
    78
    val arg =
wenzelm@38412
    79
      XML_Data.make_list(
wenzelm@38412
    80
        XML_Data.make_pair(XML_Data.make_string)(
wenzelm@38412
    81
          XML_Data.make_option(XML_Data.make_list(
wenzelm@38448
    82
              XML_Data.make_pair(
wenzelm@38448
    83
                XML_Data.make_option(XML_Data.make_long))(
wenzelm@38448
    84
                XML_Data.make_option(XML_Data.make_long))))))(edits)
wenzelm@38412
    85
wenzelm@38417
    86
    input("Isar_Document.edit_version",
wenzelm@38414
    87
      Document.ID(old_id), Document.ID(new_id), YXML.string_of_body(arg))
wenzelm@38412
    88
  }
wenzelm@38412
    89
}