src/Pure/PIDE/isar_document.scala
author wenzelm
Tue Sep 07 16:40:30 2010 +0200 (2010-09-07 ago)
changeset 39172 31b95e0da7b7
parent 39170 04ad0fed81f5
child 39175 a08d68e993ea
permissions -rw-r--r--
Isar_Document.reported_positions: slightly more robust treatment of positions outside the command range, notably parsing beyond EOF;
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@38581
    45
  def command_status(markup: List[Markup]): Status =
wenzelm@38567
    46
  {
wenzelm@38567
    47
    val forks = (0 /: markup) {
wenzelm@38581
    48
      case (i, Markup(Markup.FORKED, _)) => i + 1
wenzelm@38581
    49
      case (i, Markup(Markup.JOINED, _)) => i - 1
wenzelm@38567
    50
      case (i, _) => i
wenzelm@38567
    51
    }
wenzelm@38567
    52
    if (forks != 0) Forked(forks)
wenzelm@38581
    53
    else if (markup.exists(_.name == Markup.FAILED)) Failed
wenzelm@38581
    54
    else if (markup.exists(_.name == Markup.FINISHED)) Finished
wenzelm@38567
    55
    else Unprocessed
wenzelm@38567
    56
  }
wenzelm@38887
    57
wenzelm@38887
    58
wenzelm@38887
    59
  /* reported positions */
wenzelm@38887
    60
wenzelm@39042
    61
  private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39042
    62
  private val exclude_pos = Set(Markup.LOCATION)
wenzelm@39042
    63
wenzelm@39170
    64
  private def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
    65
    msg match {
wenzelm@39170
    66
      case XML.Elem(Markup(Markup.WRITELN, _), List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@39170
    67
      case _ => false
wenzelm@39170
    68
    }
wenzelm@39170
    69
wenzelm@39172
    70
  def reported_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
    71
  {
wenzelm@38887
    72
    def reported(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
    73
      tree match {
wenzelm@39172
    74
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
    75
        if include_pos(name) && id == command.id =>
wenzelm@39172
    76
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39172
    77
          body.foldLeft(if (range.is_singularity) set else set + range)(reported)
wenzelm@39042
    78
        case XML.Elem(Markup(name, _), body) if !exclude_pos(name) =>
wenzelm@39042
    79
          body.foldLeft(set)(reported)
wenzelm@39042
    80
        case _ => set
wenzelm@38887
    81
      }
wenzelm@39042
    82
    val set = reported(Set.empty, message)
wenzelm@39170
    83
    if (set.isEmpty && !is_state(message))
wenzelm@39172
    84
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
    85
    else set
wenzelm@38887
    86
  }
wenzelm@38412
    87
}
wenzelm@38412
    88
wenzelm@38412
    89
wenzelm@38412
    90
trait Isar_Document extends Isabelle_Process
wenzelm@38412
    91
{
wenzelm@38412
    92
  import Isar_Document._
wenzelm@38412
    93
wenzelm@38412
    94
wenzelm@38412
    95
  /* commands */
wenzelm@38412
    96
wenzelm@38412
    97
  def define_command(id: Document.Command_ID, text: String): Unit =
wenzelm@38414
    98
    input("Isar_Document.define_command", Document.ID(id), text)
wenzelm@38412
    99
wenzelm@38412
   100
wenzelm@38417
   101
  /* document versions */
wenzelm@38412
   102
wenzelm@38417
   103
  def edit_version(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@38412
   104
      edits: List[Document.Edit[Document.Command_ID]])
wenzelm@38412
   105
  {
wenzelm@38412
   106
    val arg =
wenzelm@38412
   107
      XML_Data.make_list(
wenzelm@38412
   108
        XML_Data.make_pair(XML_Data.make_string)(
wenzelm@38412
   109
          XML_Data.make_option(XML_Data.make_list(
wenzelm@38448
   110
              XML_Data.make_pair(
wenzelm@38448
   111
                XML_Data.make_option(XML_Data.make_long))(
wenzelm@38448
   112
                XML_Data.make_option(XML_Data.make_long))))))(edits)
wenzelm@38412
   113
wenzelm@38417
   114
    input("Isar_Document.edit_version",
wenzelm@38414
   115
      Document.ID(old_id), Document.ID(new_id), YXML.string_of_body(arg))
wenzelm@38412
   116
  }
wenzelm@38412
   117
}