src/Pure/PIDE/protocol_message.scala
author wenzelm
Wed, 08 Apr 2015 11:52:35 +0200
changeset 59952 550b74e9b08c
parent 59713 6da3efec20ca
child 71620 5a4ccef7f310
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59713
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/PIDE/protocol_message.scala
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     3
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     4
Auxiliary operations on protocol messages.
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     5
*/
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     6
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     8
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
     9
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    10
object Protocol_Message
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    11
{
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    12
  /* inlined reports */
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    13
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    14
  private val report_elements =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    15
    Markup.Elements(Markup.REPORT, Markup.NO_REPORT)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    16
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    17
  def clean_reports(body: XML.Body): XML.Body =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    18
    body filter {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    19
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !report_elements(name)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    20
      case XML.Elem(Markup(name, _), _) => !report_elements(name)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    21
      case _ => true
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    22
    } map {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    23
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_reports(ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    24
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_reports(ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    25
      case t => t
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    26
    }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    27
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    28
  def reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    29
    body flatMap {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    30
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    31
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    32
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    33
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    34
      case XML.Wrapped_Elem(_, _, ts) => reports(props, ts)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    35
      case XML.Elem(_, ts) => reports(props, ts)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    36
      case XML.Text(_) => Nil
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    37
    }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    38
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    39
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    40
  /* reported positions */
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    41
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    42
  private val position_elements =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    43
    Markup.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    44
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    45
  def positions(
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    46
    self_id: Document_ID.Generic => Boolean,
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    47
    command_position: Position.T,
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    48
    chunk_name: Symbol.Text_Chunk.Name,
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    49
    chunk: Symbol.Text_Chunk,
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    50
    message: XML.Elem): Set[Text.Range] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    51
  {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    52
    def elem(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    53
      props match {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    54
        case Position.Identified(id, name) if self_id(id) && name == chunk_name =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    55
          val opt_range =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    56
            Position.Range.unapply(props) orElse {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    57
              if (name == Symbol.Text_Chunk.Default)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    58
                Position.Range.unapply(command_position)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    59
              else None
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    60
            }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    61
          opt_range match {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    62
            case Some(symbol_range) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    63
              chunk.incorporate(symbol_range) match {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    64
                case Some(range) => set + range
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    65
                case _ => set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    66
              }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    67
            case None => set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    68
          }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    69
        case _ => set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    70
      }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    71
    def tree(set: Set[Text.Range], t: XML.Tree): Set[Text.Range] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    72
      t match {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    73
        case XML.Wrapped_Elem(Markup(name, props), _, body) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    74
          body.foldLeft(if (position_elements(name)) elem(props, set) else set)(tree)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    75
        case XML.Elem(Markup(name, props), body) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    76
          body.foldLeft(if (position_elements(name)) elem(props, set) else set)(tree)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    77
        case XML.Text(_) => set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    78
      }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    79
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    80
    val set = tree(Set.empty, message)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    81
    if (set.isEmpty) elem(message.markup.properties, set)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    82
    else set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    83
  }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    84
}