src/Pure/PIDE/isar_document.scala
author wenzelm
Wed Sep 22 22:39:17 2010 +0200 (2010-09-22)
changeset 39622 53365ba766ac
parent 39511 5f318522e6fe
child 39625 fb0c851e4f9d
permissions -rw-r--r--
Command.accumulate: refrain from adding tracing messages to markup tree -- potential scalability problem;
     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   /* result 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   def message_reports(msg: XML.Tree): List[XML.Elem] =
    66     msg match {
    67       case elem @ XML.Elem(Markup(Markup.REPORT, _), _) => List(elem)
    68       case XML.Elem(_, body) => body.flatMap(message_reports)
    69       case XML.Text(_) => Nil
    70     }
    71 
    72 
    73   /* specific messages */
    74 
    75   def is_tracing(msg: XML.Tree): Boolean =
    76     msg match {
    77       case XML.Elem(Markup(Markup.TRACING, _), _) => true
    78       case _ => false
    79     }
    80 
    81   def is_warning(msg: XML.Tree): Boolean =
    82     msg match {
    83       case XML.Elem(Markup(Markup.WARNING, _), _) => true
    84       case _ => false
    85     }
    86 
    87   def is_error(msg: XML.Tree): Boolean =
    88     msg match {
    89       case XML.Elem(Markup(Markup.ERROR, _), _) => true
    90       case _ => false
    91     }
    92 
    93   def is_state(msg: XML.Tree): Boolean =
    94     msg match {
    95       case XML.Elem(Markup(Markup.WRITELN, _), List(XML.Elem(Markup(Markup.STATE, _), _))) => true
    96       case _ => false
    97     }
    98 
    99 
   100   /* reported positions */
   101 
   102   private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   103 
   104   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   105   {
   106     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   107       tree match {
   108         case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
   109         if include_pos(name) && id == command.id =>
   110           val range = command.decode(raw_range).restrict(command.range)
   111           body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   112         case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
   113         case _ => set
   114       }
   115     val set = positions(Set.empty, message)
   116     if (set.isEmpty && !is_state(message))
   117       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   118     else set
   119   }
   120 }
   121 
   122 
   123 trait Isar_Document extends Isabelle_Process
   124 {
   125   import Isar_Document._
   126 
   127 
   128   /* commands */
   129 
   130   def define_command(id: Document.Command_ID, text: String): Unit =
   131     input("Isar_Document.define_command", Document.ID(id), text)
   132 
   133 
   134   /* document versions */
   135 
   136   def edit_version(old_id: Document.Version_ID, new_id: Document.Version_ID,
   137       edits: List[Document.Edit[Document.Command_ID]])
   138   {
   139     val arg =
   140       XML_Data.make_list(
   141         XML_Data.make_pair(XML_Data.make_string)(
   142           XML_Data.make_option(XML_Data.make_list(
   143               XML_Data.make_pair(
   144                 XML_Data.make_option(XML_Data.make_long))(
   145                 XML_Data.make_option(XML_Data.make_long))))))(edits)
   146 
   147     input("Isar_Document.edit_version",
   148       Document.ID(old_id), Document.ID(new_id), YXML.string_of_body(arg))
   149   }
   150 }