src/Pure/PIDE/protocol.scala
changeset 45709 87017fcbad83
parent 45672 a497c5d4a523
child 46121 30a69cd8a9a0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/PIDE/protocol.scala	Thu Dec 01 14:29:14 2011 +0100
     1.3 @@ -0,0 +1,231 @@
     1.4 +/*  Title:      Pure/PIDE/protocol.scala
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Protocol message formats for interactive proof documents.
     1.8 +*/
     1.9 +
    1.10 +package isabelle
    1.11 +
    1.12 +
    1.13 +object Protocol
    1.14 +{
    1.15 +  /* document editing */
    1.16 +
    1.17 +  object Assign
    1.18 +  {
    1.19 +    def unapply(text: String): Option[(Document.Version_ID, Document.Assign)] =
    1.20 +      try {
    1.21 +        import XML.Decode._
    1.22 +        val body = YXML.parse_body(text)
    1.23 +        Some(pair(long, pair(list(pair(long, option(long))), list(pair(string, option(long)))))(body))
    1.24 +      }
    1.25 +      catch {
    1.26 +        case ERROR(_) => None
    1.27 +        case _: XML.XML_Atom => None
    1.28 +        case _: XML.XML_Body => None
    1.29 +      }
    1.30 +  }
    1.31 +
    1.32 +  object Removed
    1.33 +  {
    1.34 +    def unapply(text: String): Option[List[Document.Version_ID]] =
    1.35 +      try {
    1.36 +        import XML.Decode._
    1.37 +        Some(list(long)(YXML.parse_body(text)))
    1.38 +      }
    1.39 +      catch {
    1.40 +        case ERROR(_) => None
    1.41 +        case _: XML.XML_Atom => None
    1.42 +        case _: XML.XML_Body => None
    1.43 +      }
    1.44 +  }
    1.45 +
    1.46 +
    1.47 +  /* toplevel transactions */
    1.48 +
    1.49 +  sealed abstract class Status
    1.50 +  case object Unprocessed extends Status
    1.51 +  case class Forked(forks: Int) extends Status
    1.52 +  case object Finished extends Status
    1.53 +  case object Failed extends Status
    1.54 +
    1.55 +  def command_status(markup: List[Markup]): Status =
    1.56 +  {
    1.57 +    val forks = (0 /: markup) {
    1.58 +      case (i, Markup(Isabelle_Markup.FORKED, _)) => i + 1
    1.59 +      case (i, Markup(Isabelle_Markup.JOINED, _)) => i - 1
    1.60 +      case (i, _) => i
    1.61 +    }
    1.62 +    if (forks != 0) Forked(forks)
    1.63 +    else if (markup.exists(_.name == Isabelle_Markup.FAILED)) Failed
    1.64 +    else if (markup.exists(_.name == Isabelle_Markup.FINISHED)) Finished
    1.65 +    else Unprocessed
    1.66 +  }
    1.67 +
    1.68 +  sealed case class Node_Status(unprocessed: Int, running: Int, finished: Int, failed: Int)
    1.69 +  {
    1.70 +    def total: Int = unprocessed + running + finished + failed
    1.71 +  }
    1.72 +
    1.73 +  def node_status(
    1.74 +    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
    1.75 +  {
    1.76 +    var unprocessed = 0
    1.77 +    var running = 0
    1.78 +    var finished = 0
    1.79 +    var failed = 0
    1.80 +    node.commands.foreach(command =>
    1.81 +      command_status(state.command_state(version, command).status) match {
    1.82 +        case Unprocessed => unprocessed += 1
    1.83 +        case Forked(_) => running += 1
    1.84 +        case Finished => finished += 1
    1.85 +        case Failed => failed += 1
    1.86 +      })
    1.87 +    Node_Status(unprocessed, running, finished, failed)
    1.88 +  }
    1.89 +
    1.90 +
    1.91 +  /* result messages */
    1.92 +
    1.93 +  def clean_message(body: XML.Body): XML.Body =
    1.94 +    body filter { case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false case _ => true } map
    1.95 +      { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
    1.96 +
    1.97 +  def message_reports(msg: XML.Tree): List[XML.Elem] =
    1.98 +    msg match {
    1.99 +      case elem @ XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => List(elem)
   1.100 +      case XML.Elem(_, body) => body.flatMap(message_reports)
   1.101 +      case XML.Text(_) => Nil
   1.102 +    }
   1.103 +
   1.104 +
   1.105 +  /* specific messages */
   1.106 +
   1.107 +  def is_ready(msg: XML.Tree): Boolean =
   1.108 +    msg match {
   1.109 +      case XML.Elem(Markup(Isabelle_Markup.STATUS, _),
   1.110 +        List(XML.Elem(Markup(Isabelle_Markup.READY, _), _))) => true
   1.111 +      case _ => false
   1.112 +    }
   1.113 +
   1.114 + def is_tracing(msg: XML.Tree): Boolean =
   1.115 +    msg match {
   1.116 +      case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
   1.117 +      case _ => false
   1.118 +    }
   1.119 +
   1.120 +  def is_warning(msg: XML.Tree): Boolean =
   1.121 +    msg match {
   1.122 +      case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
   1.123 +      case _ => false
   1.124 +    }
   1.125 +
   1.126 +  def is_error(msg: XML.Tree): Boolean =
   1.127 +    msg match {
   1.128 +      case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
   1.129 +      case _ => false
   1.130 +    }
   1.131 +
   1.132 +  def is_state(msg: XML.Tree): Boolean =
   1.133 +    msg match {
   1.134 +      case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
   1.135 +        List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
   1.136 +      case _ => false
   1.137 +    }
   1.138 +
   1.139 +
   1.140 +  /* reported positions */
   1.141 +
   1.142 +  private val include_pos =
   1.143 +    Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
   1.144 +      Isabelle_Markup.POSITION)
   1.145 +
   1.146 +  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   1.147 +  {
   1.148 +    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   1.149 +      tree match {
   1.150 +        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
   1.151 +        if include_pos(name) && id == command.id =>
   1.152 +          val range = command.decode(raw_range).restrict(command.range)
   1.153 +          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   1.154 +        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
   1.155 +        case _ => set
   1.156 +      }
   1.157 +    val set = positions(Set.empty, message)
   1.158 +    if (set.isEmpty && !is_state(message))
   1.159 +      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   1.160 +    else set
   1.161 +  }
   1.162 +}
   1.163 +
   1.164 +
   1.165 +trait Protocol extends Isabelle_Process
   1.166 +{
   1.167 +  /* commands */
   1.168 +
   1.169 +  def define_command(command: Command): Unit =
   1.170 +    input("Document.define_command",
   1.171 +      Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
   1.172 +
   1.173 +
   1.174 +  /* document versions */
   1.175 +
   1.176 +  def cancel_execution()
   1.177 +  {
   1.178 +    input("Document.cancel_execution")
   1.179 +  }
   1.180 +
   1.181 +  def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
   1.182 +    name: Document.Node.Name, perspective: Command.Perspective)
   1.183 +  {
   1.184 +    val ids =
   1.185 +    { import XML.Encode._
   1.186 +      list(long)(perspective.commands.map(_.id)) }
   1.187 +
   1.188 +    input("Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
   1.189 +      name.node, YXML.string_of_body(ids))
   1.190 +  }
   1.191 +
   1.192 +  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
   1.193 +    edits: List[Document.Edit_Command])
   1.194 +  {
   1.195 +    val edits_yxml =
   1.196 +    { import XML.Encode._
   1.197 +      def id: T[Command] = (cmd => long(cmd.id))
   1.198 +      def encode_edit(dir: String)
   1.199 +          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   1.200 +        variant(List(
   1.201 +          { case Document.Node.Clear() => (Nil, Nil) },
   1.202 +          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   1.203 +          { case Document.Node.Header(Exn.Res(Thy_Header(a, b, c))) =>
   1.204 +              (Nil,
   1.205 +                triple(pair(string, string), list(string), list(pair(string, bool)))(
   1.206 +                  (dir, a), b, c)) },
   1.207 +          { case Document.Node.Header(Exn.Exn(e)) => (List(Exn.message(e)), Nil) },
   1.208 +          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   1.209 +      def encode: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   1.210 +      {
   1.211 +        val (name, edit) = node_edit
   1.212 +        val dir = Isabelle_System.posix_path(name.dir)
   1.213 +        pair(string, encode_edit(dir))(name.node, edit)
   1.214 +      })
   1.215 +      YXML.string_of_body(encode(edits)) }
   1.216 +    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
   1.217 +  }
   1.218 +
   1.219 +  def remove_versions(versions: List[Document.Version])
   1.220 +  {
   1.221 +    val versions_yxml =
   1.222 +      { import XML.Encode._
   1.223 +        YXML.string_of_body(list(long)(versions.map(_.id))) }
   1.224 +    input("Document.remove_versions", versions_yxml)
   1.225 +  }
   1.226 +
   1.227 +
   1.228 +  /* method invocation service */
   1.229 +
   1.230 +  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
   1.231 +  {
   1.232 +    input("Document.invoke_scala", id, tag.toString, res)
   1.233 +  }
   1.234 +}