src/Pure/PIDE/isar_document.scala
author wenzelm
Fri Aug 26 16:06:58 2011 +0200 (2011-08-26)
changeset 44481 bb42bc831570
parent 44479 9a04e7502e22
child 44612 990ac978854c
permissions -rw-r--r--
tuned signature;
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@44476
    14
  object Assign
wenzelm@44476
    15
  {
wenzelm@44476
    16
    def unapply(msg: XML.Tree): Option[(Document.Version_ID, Document.Assign)] =
wenzelm@38412
    17
      msg match {
wenzelm@44476
    18
        case XML.Elem(Markup(Markup.ASSIGN, List((Markup.VERSION, Document.ID(id)))), body) =>
wenzelm@44476
    19
          try {
wenzelm@44476
    20
            import XML.Decode._
wenzelm@44479
    21
            val a = pair(list(pair(long, option(long))), list(pair(string, option(long))))(body)
wenzelm@44476
    22
            Some(id, a)
wenzelm@44476
    23
          }
wenzelm@44476
    24
          catch {
wenzelm@44476
    25
            case _: XML.XML_Atom => None
wenzelm@44476
    26
            case _: XML.XML_Body => None
wenzelm@44476
    27
          }
wenzelm@38412
    28
        case _ => None
wenzelm@38412
    29
      }
wenzelm@38412
    30
  }
wenzelm@38567
    31
wenzelm@38567
    32
wenzelm@38567
    33
  /* toplevel transactions */
wenzelm@38567
    34
wenzelm@38567
    35
  sealed abstract class Status
wenzelm@38567
    36
  case class Forked(forks: Int) extends Status
wenzelm@38567
    37
  case object Unprocessed extends Status
wenzelm@38567
    38
  case object Finished extends Status
wenzelm@38567
    39
  case object Failed extends Status
wenzelm@38567
    40
wenzelm@38581
    41
  def command_status(markup: List[Markup]): Status =
wenzelm@38567
    42
  {
wenzelm@38567
    43
    val forks = (0 /: markup) {
wenzelm@38581
    44
      case (i, Markup(Markup.FORKED, _)) => i + 1
wenzelm@38581
    45
      case (i, Markup(Markup.JOINED, _)) => i - 1
wenzelm@38567
    46
      case (i, _) => i
wenzelm@38567
    47
    }
wenzelm@38567
    48
    if (forks != 0) Forked(forks)
wenzelm@38581
    49
    else if (markup.exists(_.name == Markup.FAILED)) Failed
wenzelm@38581
    50
    else if (markup.exists(_.name == Markup.FINISHED)) Finished
wenzelm@38567
    51
    else Unprocessed
wenzelm@38567
    52
  }
wenzelm@38887
    53
wenzelm@38887
    54
wenzelm@39441
    55
  /* result messages */
wenzelm@39439
    56
wenzelm@39439
    57
  def clean_message(body: XML.Body): XML.Body =
wenzelm@39439
    58
    body filter { case XML.Elem(Markup(Markup.NO_REPORT, _), _) => false case _ => true } map
wenzelm@39439
    59
      { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
wenzelm@39439
    60
wenzelm@39441
    61
  def message_reports(msg: XML.Tree): List[XML.Elem] =
wenzelm@39441
    62
    msg match {
wenzelm@39441
    63
      case elem @ XML.Elem(Markup(Markup.REPORT, _), _) => List(elem)
wenzelm@39441
    64
      case XML.Elem(_, body) => body.flatMap(message_reports)
wenzelm@39441
    65
      case XML.Text(_) => Nil
wenzelm@39441
    66
    }
wenzelm@39441
    67
wenzelm@39439
    68
wenzelm@39511
    69
  /* specific messages */
wenzelm@39511
    70
wenzelm@39625
    71
  def is_ready(msg: XML.Tree): Boolean =
wenzelm@39625
    72
    msg match {
wenzelm@39625
    73
      case XML.Elem(Markup(Markup.STATUS, _),
wenzelm@39625
    74
        List(XML.Elem(Markup(Markup.READY, _), _))) => true
wenzelm@39625
    75
      case _ => false
wenzelm@39625
    76
    }
wenzelm@39625
    77
wenzelm@39625
    78
 def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
    79
    msg match {
wenzelm@39622
    80
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@39622
    81
      case _ => false
wenzelm@39622
    82
    }
wenzelm@39622
    83
wenzelm@39511
    84
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
    85
    msg match {
wenzelm@39511
    86
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@39511
    87
      case _ => false
wenzelm@39511
    88
    }
wenzelm@39511
    89
wenzelm@39511
    90
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
    91
    msg match {
wenzelm@39511
    92
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@39511
    93
      case _ => false
wenzelm@39511
    94
    }
wenzelm@38887
    95
wenzelm@39441
    96
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
    97
    msg match {
wenzelm@39627
    98
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@39627
    99
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@39170
   100
      case _ => false
wenzelm@39170
   101
    }
wenzelm@39170
   102
wenzelm@39511
   103
wenzelm@39511
   104
  /* reported positions */
wenzelm@39511
   105
wenzelm@39627
   106
  private val include_pos =
wenzelm@39627
   107
    Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   108
wenzelm@39441
   109
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   110
  {
wenzelm@39441
   111
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   112
      tree match {
wenzelm@39172
   113
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
   114
        if include_pos(name) && id == command.id =>
wenzelm@39172
   115
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39441
   116
          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@39441
   117
        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
wenzelm@39042
   118
        case _ => set
wenzelm@38887
   119
      }
wenzelm@39441
   120
    val set = positions(Set.empty, message)
wenzelm@39170
   121
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   122
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   123
    else set
wenzelm@38887
   124
  }
wenzelm@38412
   125
}
wenzelm@38412
   126
wenzelm@38412
   127
wenzelm@38412
   128
trait Isar_Document extends Isabelle_Process
wenzelm@38412
   129
{
wenzelm@38412
   130
  /* commands */
wenzelm@38412
   131
wenzelm@38412
   132
  def define_command(id: Document.Command_ID, text: String): Unit =
wenzelm@38414
   133
    input("Isar_Document.define_command", Document.ID(id), text)
wenzelm@38412
   134
wenzelm@38412
   135
wenzelm@38417
   136
  /* document versions */
wenzelm@38412
   137
wenzelm@44436
   138
  def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44436
   139
    name: String, perspective: Command.Perspective)
wenzelm@44436
   140
  {
wenzelm@44436
   141
    val ids =
wenzelm@44436
   142
    { import XML.Encode._
wenzelm@44474
   143
      list(long)(perspective.commands.map(_.id)) }
wenzelm@44436
   144
wenzelm@44436
   145
    input("Isar_Document.update_perspective", Document.ID(old_id), Document.ID(new_id), name,
wenzelm@44436
   146
      YXML.string_of_body(ids))
wenzelm@44436
   147
  }
wenzelm@44436
   148
wenzelm@44481
   149
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   150
    edits: List[Document.Edit_Command])
wenzelm@38412
   151
  {
wenzelm@44157
   152
    val edits_yxml =
wenzelm@43767
   153
    { import XML.Encode._
wenzelm@44383
   154
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@44383
   155
      def encode: T[List[Document.Edit_Command]] =
wenzelm@44156
   156
        list(pair(string,
wenzelm@44156
   157
          variant(List(
wenzelm@44185
   158
            { case Document.Node.Clear() => (Nil, Nil) },
wenzelm@44383
   159
            { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@44182
   160
            { case Document.Node.Header(Exn.Res(Thy_Header(a, b, c))) =>
wenzelm@44185
   161
                (Nil, triple(string, list(string), list(pair(string, bool)))(a, b, c)) },
wenzelm@44384
   162
            { case Document.Node.Header(Exn.Exn(e)) => (List(Exn.message(e)), Nil) },
wenzelm@44474
   163
            { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))))
wenzelm@44157
   164
      YXML.string_of_body(encode(edits)) }
wenzelm@38412
   165
wenzelm@44481
   166
    input("Isar_Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   167
  }
wenzelm@43748
   168
wenzelm@43748
   169
wenzelm@43748
   170
  /* method invocation service */
wenzelm@43748
   171
wenzelm@43748
   172
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   173
  {
wenzelm@43748
   174
    input("Isar_Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   175
  }
wenzelm@38412
   176
}