src/Pure/PIDE/isar_document.scala
author wenzelm
Fri Sep 17 15:51:11 2010 +0200 (2010-09-17)
changeset 39439 1c294d150ded
parent 39181 2257eded8323
child 39441 4110cc1b8f9f
permissions -rw-r--r--
eliminated markup "location" in favour of more explicit "no_report", which is actually deleted from messages;
     1 /*  Title:      Pure/PIDE/isar_document.scala
     2     Author:     Makarius
     3 
     4 Protocol message formats for interactive Isar documents.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Isar_Document
    11 {
    12   /* document editing */
    13 
    14   object Assign {
    15     def unapply(msg: XML.Tree)
    16         : Option[(Document.Version_ID, List[(Document.Command_ID, Document.Exec_ID)])] =
    17       msg match {
    18         case XML.Elem(Markup(Markup.ASSIGN, List((Markup.VERSION, Document.ID(id)))), edits) =>
    19           val id_edits = edits.map(Edit.unapply)
    20           if (id_edits.forall(_.isDefined)) Some((id, id_edits.map(_.get)))
    21           else None
    22         case _ => None
    23       }
    24   }
    25 
    26   object Edit {
    27     def unapply(msg: XML.Tree): Option[(Document.Command_ID, Document.Exec_ID)] =
    28       msg match {
    29         case XML.Elem(
    30           Markup(Markup.EDIT,
    31             List((Markup.ID, Document.ID(i)), (Markup.EXEC, Document.ID(j)))), Nil) => Some((i, j))
    32         case _ => None
    33       }
    34   }
    35 
    36 
    37   /* toplevel transactions */
    38 
    39   sealed abstract class Status
    40   case class Forked(forks: Int) extends Status
    41   case object Unprocessed extends Status
    42   case object Finished extends Status
    43   case object Failed extends Status
    44 
    45   def command_status(markup: List[Markup]): Status =
    46   {
    47     val forks = (0 /: markup) {
    48       case (i, Markup(Markup.FORKED, _)) => i + 1
    49       case (i, Markup(Markup.JOINED, _)) => i - 1
    50       case (i, _) => i
    51     }
    52     if (forks != 0) Forked(forks)
    53     else if (markup.exists(_.name == Markup.FAILED)) Failed
    54     else if (markup.exists(_.name == Markup.FINISHED)) Finished
    55     else Unprocessed
    56   }
    57 
    58 
    59   /* messages */
    60 
    61   def clean_message(body: XML.Body): XML.Body =
    62     body filter { case XML.Elem(Markup(Markup.NO_REPORT, _), _) => false case _ => true } map
    63       { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
    64 
    65 
    66   /* reported positions */
    67 
    68   private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
    69 
    70   private def is_state(msg: XML.Tree): Boolean =
    71     msg match {
    72       case XML.Elem(Markup(Markup.WRITELN, _), List(XML.Elem(Markup(Markup.STATE, _), _))) => true
    73       case _ => false
    74     }
    75 
    76   def reported_positions(command: Command, message: XML.Elem): Set[Text.Range] =
    77   {
    78     def reported(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
    79       tree match {
    80         case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
    81         if include_pos(name) && id == command.id =>
    82           val range = command.decode(raw_range).restrict(command.range)
    83           body.foldLeft(if (range.is_singularity) set else set + range)(reported)
    84         case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(reported)
    85         case _ => set
    86       }
    87     val set = reported(Set.empty, message)
    88     if (set.isEmpty && !is_state(message))
    89       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
    90     else set
    91   }
    92 }
    93 
    94 
    95 trait Isar_Document extends Isabelle_Process
    96 {
    97   import Isar_Document._
    98 
    99 
   100   /* commands */
   101 
   102   def define_command(id: Document.Command_ID, text: String): Unit =
   103     input("Isar_Document.define_command", Document.ID(id), text)
   104 
   105 
   106   /* document versions */
   107 
   108   def edit_version(old_id: Document.Version_ID, new_id: Document.Version_ID,
   109       edits: List[Document.Edit[Document.Command_ID]])
   110   {
   111     val arg =
   112       XML_Data.make_list(
   113         XML_Data.make_pair(XML_Data.make_string)(
   114           XML_Data.make_option(XML_Data.make_list(
   115               XML_Data.make_pair(
   116                 XML_Data.make_option(XML_Data.make_long))(
   117                 XML_Data.make_option(XML_Data.make_long))))))(edits)
   118 
   119     input("Isar_Document.edit_version",
   120       Document.ID(old_id), Document.ID(new_id), YXML.string_of_body(arg))
   121   }
   122 }