src/Pure/PIDE/protocol.scala
author wenzelm
Sun Mar 04 18:15:45 2012 +0100 (2012-03-04 ago)
changeset 46812 3d55ef732cd7
parent 46770 44c28a33c461
child 46910 3e068ef04b42
permissions -rw-r--r--
clarified special eol treatment and moved to gfx_range -- enables error messages at end of input, e.g. "prop PROP";
     1 /*  Title:      Pure/PIDE/protocol.scala
     2     Author:     Makarius
     3 
     4 Protocol message formats for interactive proof documents.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Protocol
    11 {
    12   /* document editing */
    13 
    14   object Assign
    15   {
    16     def unapply(text: String): Option[(Document.Version_ID, Document.Assign)] =
    17       try {
    18         import XML.Decode._
    19         val body = YXML.parse_body(text)
    20         Some(pair(long, pair(list(pair(long, option(long))), list(pair(string, option(long)))))(body))
    21       }
    22       catch {
    23         case ERROR(_) => None
    24         case _: XML.XML_Atom => None
    25         case _: XML.XML_Body => None
    26       }
    27   }
    28 
    29   object Removed
    30   {
    31     def unapply(text: String): Option[List[Document.Version_ID]] =
    32       try {
    33         import XML.Decode._
    34         Some(list(long)(YXML.parse_body(text)))
    35       }
    36       catch {
    37         case ERROR(_) => None
    38         case _: XML.XML_Atom => None
    39         case _: XML.XML_Body => None
    40       }
    41   }
    42 
    43 
    44   /* command status */
    45 
    46   object Status
    47   {
    48     val init = Status()
    49   }
    50 
    51   sealed case class Status(
    52     private val finished: Boolean = false,
    53     private val failed: Boolean = false,
    54     forks: Int = 0)
    55   {
    56     def is_unprocessed: Boolean = !finished && !failed && forks == 0
    57     def is_running: Boolean = forks != 0
    58     def is_finished: Boolean = finished && forks == 0
    59     def is_failed: Boolean = failed && forks == 0
    60     def + (that: Status): Status =
    61       Status(finished || that.finished, failed || that.failed, forks + that.forks)
    62   }
    63 
    64   val command_status_markup: Set[String] =
    65     Set(Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED,
    66       Isabelle_Markup.FORKED, Isabelle_Markup.JOINED)
    67 
    68   def command_status(status: Status, markup: Markup): Status =
    69     markup match {
    70       case Markup(Isabelle_Markup.FINISHED, _) => status.copy(finished = true)
    71       case Markup(Isabelle_Markup.FAILED, _) => status.copy(failed = true)
    72       case Markup(Isabelle_Markup.FORKED, _) => status.copy(forks = status.forks + 1)
    73       case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
    74       case _ => status
    75     }
    76 
    77   def command_status(markups: List[Markup]): Status =
    78     (Status.init /: markups)(command_status(_, _))
    79 
    80 
    81   /* node status */
    82 
    83   sealed case class Node_Status(
    84     unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
    85   {
    86     def total: Int = unprocessed + running + finished + warned + failed
    87   }
    88 
    89   def node_status(
    90     state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
    91   {
    92     var unprocessed = 0
    93     var running = 0
    94     var finished = 0
    95     var warned = 0
    96     var failed = 0
    97     node.commands.foreach(command =>
    98       {
    99         val st = state.command_state(version, command)
   100         val status = command_status(st.status)
   101         if (status.is_running) running += 1
   102         else if (status.is_finished) {
   103           if (st.results.exists(p => is_warning(p._2))) warned += 1
   104           else finished += 1
   105         }
   106         else if (status.is_failed) failed += 1
   107         else unprocessed += 1
   108       })
   109     Node_Status(unprocessed, running, finished, warned, failed)
   110   }
   111 
   112 
   113   /* result messages */
   114 
   115   def clean_message(body: XML.Body): XML.Body =
   116     body filter { case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false case _ => true } map
   117       { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
   118 
   119   def message_reports(msg: XML.Tree): List[XML.Elem] =
   120     msg match {
   121       case elem @ XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => List(elem)
   122       case XML.Elem(_, body) => body.flatMap(message_reports)
   123       case XML.Text(_) => Nil
   124     }
   125 
   126 
   127   /* specific messages */
   128 
   129  def is_tracing(msg: XML.Tree): Boolean =
   130     msg match {
   131       case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
   132       case _ => false
   133     }
   134 
   135   def is_warning(msg: XML.Tree): Boolean =
   136     msg match {
   137       case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
   138       case _ => false
   139     }
   140 
   141   def is_error(msg: XML.Tree): Boolean =
   142     msg match {
   143       case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
   144       case _ => false
   145     }
   146 
   147   def is_state(msg: XML.Tree): Boolean =
   148     msg match {
   149       case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
   150         List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
   151       case _ => false
   152     }
   153 
   154 
   155   /* reported positions */
   156 
   157   private val include_pos =
   158     Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
   159       Isabelle_Markup.POSITION)
   160 
   161   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   162   {
   163     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   164       tree match {
   165         case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
   166         if include_pos(name) && id == command.id =>
   167           val range = command.decode(raw_range).restrict(command.range)
   168           body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   169         case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
   170         case _ => set
   171       }
   172     val set = positions(Set.empty, message)
   173     if (set.isEmpty && !is_state(message))
   174       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   175     else set
   176   }
   177 }
   178 
   179 
   180 trait Protocol extends Isabelle_Process
   181 {
   182   /* commands */
   183 
   184   def define_command(command: Command): Unit =
   185     input("Document.define_command",
   186       Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
   187 
   188 
   189   /* document versions */
   190 
   191   def cancel_execution()
   192   {
   193     input("Document.cancel_execution")
   194   }
   195 
   196   def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
   197     name: Document.Node.Name, perspective: Command.Perspective)
   198   {
   199     val ids =
   200     { import XML.Encode._
   201       list(long)(perspective.commands.map(_.id)) }
   202 
   203     input("Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
   204       name.node, YXML.string_of_body(ids))
   205   }
   206 
   207   def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
   208     edits: List[Document.Edit_Command])
   209   {
   210     val edits_yxml =
   211     { import XML.Encode._
   212       def id: T[Command] = (cmd => long(cmd.id))
   213       def symbol_string: T[String] = (s => string(Symbol.encode(s)))
   214       def encode_edit(name: Document.Node.Name)
   215           : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   216         variant(List(
   217           { case Document.Node.Clear() => (Nil, Nil) },
   218           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   219           { case Document.Node.Header(Exn.Res(deps)) =>
   220               val dir = Isabelle_System.posix_path(name.dir)
   221               val imports = deps.imports.map(_.node)
   222               // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
   223               val uses = deps.uses
   224               (Nil,
   225                 triple(pair(symbol_string, symbol_string), list(symbol_string),
   226                     list(pair(symbol_string, bool)))((dir, name.theory), imports, uses)) },
   227           { case Document.Node.Header(Exn.Exn(e)) => (List(Symbol.encode(Exn.message(e))), Nil) },
   228           { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   229       def encode: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   230       {
   231         val (name, edit) = node_edit
   232         pair(string, encode_edit(name))(name.node, edit)
   233       })
   234       YXML.string_of_body(encode(edits)) }
   235     input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
   236   }
   237 
   238   def remove_versions(versions: List[Document.Version])
   239   {
   240     val versions_yxml =
   241       { import XML.Encode._
   242         YXML.string_of_body(list(long)(versions.map(_.id))) }
   243     input("Document.remove_versions", versions_yxml)
   244   }
   245 
   246 
   247   /* method invocation service */
   248 
   249   def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
   250   {
   251     input("Document.invoke_scala", id, tag.toString, res)
   252   }
   253 }