src/Pure/PIDE/protocol.scala
author wenzelm
Mon Nov 19 22:34:17 2012 +0100 (2012-11-19 ago)
changeset 50128 599c935aac82
parent 49650 9fad6480300d
child 50157 76efdb6daab2
permissions -rw-r--r--
alternative completion for outer syntax keywords;
wenzelm@45709
     1
/*  Title:      Pure/PIDE/protocol.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@45709
    10
object Protocol
wenzelm@38412
    11
{
wenzelm@38567
    12
  /* document editing */
wenzelm@38412
    13
wenzelm@44476
    14
  object Assign
wenzelm@44476
    15
  {
wenzelm@44661
    16
    def unapply(text: String): Option[(Document.Version_ID, Document.Assign)] =
wenzelm@44661
    17
      try {
wenzelm@44661
    18
        import XML.Decode._
wenzelm@44661
    19
        val body = YXML.parse_body(text)
wenzelm@47388
    20
        Some(pair(long, list(pair(long, option(long))))(body))
wenzelm@44661
    21
      }
wenzelm@44661
    22
      catch {
wenzelm@44661
    23
        case ERROR(_) => None
wenzelm@44661
    24
        case _: XML.XML_Atom => None
wenzelm@44661
    25
        case _: XML.XML_Body => None
wenzelm@38412
    26
      }
wenzelm@38412
    27
  }
wenzelm@38567
    28
wenzelm@44676
    29
  object Removed
wenzelm@44676
    30
  {
wenzelm@44676
    31
    def unapply(text: String): Option[List[Document.Version_ID]] =
wenzelm@44676
    32
      try {
wenzelm@44676
    33
        import XML.Decode._
wenzelm@44676
    34
        Some(list(long)(YXML.parse_body(text)))
wenzelm@44676
    35
      }
wenzelm@44676
    36
      catch {
wenzelm@44676
    37
        case ERROR(_) => None
wenzelm@44676
    38
        case _: XML.XML_Atom => None
wenzelm@44676
    39
        case _: XML.XML_Body => None
wenzelm@44676
    40
      }
wenzelm@44676
    41
  }
wenzelm@44676
    42
wenzelm@38567
    43
wenzelm@46209
    44
  /* command status */
wenzelm@38567
    45
wenzelm@46227
    46
  object Status
wenzelm@46227
    47
  {
wenzelm@46227
    48
    val init = Status()
wenzelm@46227
    49
  }
wenzelm@46227
    50
wenzelm@46166
    51
  sealed case class Status(
wenzelm@49036
    52
    private val touched: Boolean = false,
wenzelm@47395
    53
    private val accepted: Boolean = false,
wenzelm@46166
    54
    private val failed: Boolean = false,
wenzelm@49036
    55
    forks: Int = 0,
wenzelm@49036
    56
    runs: Int = 0)
wenzelm@38567
    57
  {
wenzelm@46227
    58
    def + (that: Status): Status =
wenzelm@49036
    59
      Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
wenzelm@49036
    60
        forks + that.forks, runs + that.runs)
wenzelm@49036
    61
wenzelm@49036
    62
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    63
    def is_running: Boolean = runs != 0
wenzelm@49039
    64
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@49036
    65
    def is_failed: Boolean = failed
wenzelm@46166
    66
  }
wenzelm@46166
    67
wenzelm@46166
    68
  val command_status_markup: Set[String] =
wenzelm@49036
    69
    Set(Isabelle_Markup.ACCEPTED, Isabelle_Markup.FORKED, Isabelle_Markup.JOINED,
wenzelm@49036
    70
      Isabelle_Markup.RUNNING, Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED)
wenzelm@46166
    71
wenzelm@46166
    72
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    73
    markup match {
wenzelm@47395
    74
      case Markup(Isabelle_Markup.ACCEPTED, _) => status.copy(accepted = true)
wenzelm@49036
    75
      case Markup(Isabelle_Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
wenzelm@49036
    76
      case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
wenzelm@49036
    77
      case Markup(Isabelle_Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
wenzelm@49036
    78
      case Markup(Isabelle_Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
wenzelm@46166
    79
      case Markup(Isabelle_Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    80
      case _ => status
wenzelm@38567
    81
    }
wenzelm@46166
    82
wenzelm@46166
    83
  def command_status(markups: List[Markup]): Status =
wenzelm@46227
    84
    (Status.init /: markups)(command_status(_, _))
wenzelm@38887
    85
wenzelm@46209
    86
wenzelm@46209
    87
  /* node status */
wenzelm@46209
    88
wenzelm@46688
    89
  sealed case class Node_Status(
wenzelm@46688
    90
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
    91
  {
wenzelm@46688
    92
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
    93
  }
wenzelm@44613
    94
wenzelm@44613
    95
  def node_status(
wenzelm@44613
    96
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    97
  {
wenzelm@44613
    98
    var unprocessed = 0
wenzelm@44613
    99
    var running = 0
wenzelm@44613
   100
    var finished = 0
wenzelm@46688
   101
    var warned = 0
wenzelm@44613
   102
    var failed = 0
wenzelm@44613
   103
    node.commands.foreach(command =>
wenzelm@46166
   104
      {
wenzelm@46688
   105
        val st = state.command_state(version, command)
wenzelm@46688
   106
        val status = command_status(st.status)
wenzelm@46166
   107
        if (status.is_running) running += 1
wenzelm@46688
   108
        else if (status.is_finished) {
wenzelm@46688
   109
          if (st.results.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   110
          else finished += 1
wenzelm@46688
   111
        }
wenzelm@46166
   112
        else if (status.is_failed) failed += 1
wenzelm@46166
   113
        else unprocessed += 1
wenzelm@44613
   114
      })
wenzelm@46688
   115
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   116
  }
wenzelm@44613
   117
wenzelm@38887
   118
wenzelm@39441
   119
  /* result messages */
wenzelm@39439
   120
wenzelm@39439
   121
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   122
    body filter {
wenzelm@49445
   123
      case XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => false
wenzelm@49445
   124
      case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false
wenzelm@49445
   125
      case _ => true
wenzelm@49650
   126
    } map {
wenzelm@49650
   127
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   128
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   129
      case t => t
wenzelm@49650
   130
    }
wenzelm@39439
   131
wenzelm@49445
   132
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   133
    body flatMap {
wenzelm@49445
   134
      case XML.Elem(Markup(Isabelle_Markup.REPORT, ps), ts) =>
wenzelm@49445
   135
        List(XML.Elem(Markup(Isabelle_Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   136
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   137
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   138
      case XML.Text(_) => Nil
wenzelm@39441
   139
    }
wenzelm@39441
   140
wenzelm@39439
   141
wenzelm@39511
   142
  /* specific messages */
wenzelm@39511
   143
wenzelm@39625
   144
 def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   145
    msg match {
wenzelm@45666
   146
      case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
wenzelm@49418
   147
      case XML.Elem(Markup(Isabelle_Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   148
      case _ => false
wenzelm@39622
   149
    }
wenzelm@39622
   150
wenzelm@39511
   151
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   152
    msg match {
wenzelm@45666
   153
      case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
wenzelm@49418
   154
      case XML.Elem(Markup(Isabelle_Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   155
      case _ => false
wenzelm@39511
   156
    }
wenzelm@39511
   157
wenzelm@39511
   158
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   159
    msg match {
wenzelm@45666
   160
      case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
wenzelm@49418
   161
      case XML.Elem(Markup(Isabelle_Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   162
      case _ => false
wenzelm@39511
   163
    }
wenzelm@38887
   164
wenzelm@39441
   165
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
   166
    msg match {
wenzelm@45666
   167
      case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
wenzelm@45666
   168
        List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
wenzelm@49418
   169
      case XML.Elem(Markup(Isabelle_Markup.WRITELN_MESSAGE, _),
wenzelm@49418
   170
        List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
wenzelm@39170
   171
      case _ => false
wenzelm@39170
   172
    }
wenzelm@39170
   173
wenzelm@39511
   174
wenzelm@39511
   175
  /* reported positions */
wenzelm@39511
   176
wenzelm@39627
   177
  private val include_pos =
wenzelm@45666
   178
    Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
wenzelm@45666
   179
      Isabelle_Markup.POSITION)
wenzelm@39441
   180
wenzelm@39441
   181
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   182
  {
wenzelm@49650
   183
    def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
wenzelm@49650
   184
      : Set[Text.Range] =
wenzelm@49650
   185
    {
wenzelm@49650
   186
      val range = command.decode(raw_range).restrict(command.range)
wenzelm@49650
   187
      body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@49650
   188
    }
wenzelm@39441
   189
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   190
      tree match {
wenzelm@49650
   191
        case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
wenzelm@49650
   192
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   193
wenzelm@49650
   194
        case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
wenzelm@49650
   195
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   196
wenzelm@49650
   197
        case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
wenzelm@49650
   198
wenzelm@49650
   199
        case XML.Elem(_, body) => body.foldLeft(set)(positions)
wenzelm@49650
   200
wenzelm@39042
   201
        case _ => set
wenzelm@38887
   202
      }
wenzelm@39441
   203
    val set = positions(Set.empty, message)
wenzelm@39170
   204
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   205
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   206
    else set
wenzelm@38887
   207
  }
wenzelm@38412
   208
}
wenzelm@38412
   209
wenzelm@38412
   210
wenzelm@45709
   211
trait Protocol extends Isabelle_Process
wenzelm@38412
   212
{
wenzelm@38412
   213
  /* commands */
wenzelm@38412
   214
wenzelm@44644
   215
  def define_command(command: Command): Unit =
wenzelm@45709
   216
    input("Document.define_command",
wenzelm@48705
   217
      Document.ID(command.id), encode(command.name), encode(command.source))
wenzelm@38412
   218
wenzelm@38412
   219
wenzelm@38417
   220
  /* document versions */
wenzelm@38412
   221
wenzelm@47343
   222
  def discontinue_execution() { input("Document.discontinue_execution") }
wenzelm@47343
   223
wenzelm@47343
   224
  def cancel_execution() { input("Document.cancel_execution") }
wenzelm@44612
   225
wenzelm@44481
   226
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   227
    edits: List[Document.Edit_Command])
wenzelm@38412
   228
  {
wenzelm@44157
   229
    val edits_yxml =
wenzelm@43767
   230
    { import XML.Encode._
wenzelm@44383
   231
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   232
      def encode_edit(name: Document.Node.Name)
wenzelm@44979
   233
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   234
        variant(List(
wenzelm@48755
   235
          { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
wenzelm@44979
   236
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   237
          { case Document.Node.Deps(header) =>
wenzelm@46770
   238
              val dir = Isabelle_System.posix_path(name.dir)
wenzelm@48707
   239
              val imports = header.imports.map(_.node)
wenzelm@50128
   240
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@46770
   241
              // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
wenzelm@48707
   242
              val uses = header.uses
wenzelm@44979
   243
              (Nil,
wenzelm@48707
   244
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   245
                  pair(list(pair(Encode.string,
wenzelm@48864
   246
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@48707
   247
                  pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
wenzelm@50128
   248
                (dir, (name.theory, (imports, (keywords, (uses, header.errors))))))) },
wenzelm@44979
   249
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@48705
   250
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   251
      {
wenzelm@44979
   252
        val (name, edit) = node_edit
wenzelm@46737
   253
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   254
      })
wenzelm@48705
   255
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@45709
   256
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   257
  }
wenzelm@43748
   258
wenzelm@44673
   259
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   260
  {
wenzelm@44673
   261
    val versions_yxml =
wenzelm@44673
   262
      { import XML.Encode._
wenzelm@44673
   263
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   264
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   265
  }
wenzelm@44673
   266
wenzelm@43748
   267
wenzelm@43748
   268
  /* method invocation service */
wenzelm@43748
   269
wenzelm@43748
   270
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   271
  {
wenzelm@45709
   272
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   273
  }
wenzelm@38412
   274
}