src/Pure/PIDE/protocol_message.scala
author wenzelm
Sun, 29 Mar 2020 21:57:40 +0200
changeset 71623 b3bddebe44ca
parent 71620 5a4ccef7f310
child 71631 3f02bc5a5a03
permissions -rw-r--r--
clarified signature: more explicit type Protocol_Message.Marker;
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
{
71620
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    12
  /* message markers */
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    13
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    14
  object Marker
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    15
  {
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    16
    def apply(a: String): Marker =
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    17
      new Marker { override def name: String = a }
71623
b3bddebe44ca clarified signature: more explicit type Protocol_Message.Marker;
wenzelm
parents: 71620
diff changeset
    18
b3bddebe44ca clarified signature: more explicit type Protocol_Message.Marker;
wenzelm
parents: 71620
diff changeset
    19
    def test(line: String): Boolean = line.startsWith("\f")
71620
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    20
  }
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    21
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    22
  abstract class Marker private
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    23
  {
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    24
    def name: String
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    25
    val prefix: String = "\f" + name + " = "
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    26
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    27
    def apply(text: String): String = prefix + Library.encode_lines(text)
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    28
    def apply(props: Properties.T): String = apply(YXML.string_of_body(XML.Encode.properties(props)))
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    29
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    30
    def test(line: String): Boolean = line.startsWith(prefix)
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    31
    def test_yxml(line: String): Boolean = test(line) && YXML.detect(line)
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    32
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    33
    def unapply(line: String): Option[String] =
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    34
      Library.try_unprefix(prefix, line).map(Library.decode_lines)
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    35
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    36
    override def toString: String = "Marker(" + quote(name) + ")"
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    37
  }
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    38
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    39
59713
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    40
  /* inlined reports */
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    41
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    42
  private val report_elements =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    43
    Markup.Elements(Markup.REPORT, Markup.NO_REPORT)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    44
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    45
  def clean_reports(body: XML.Body): XML.Body =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    46
    body filter {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    47
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !report_elements(name)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    48
      case XML.Elem(Markup(name, _), _) => !report_elements(name)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    49
      case _ => true
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    50
    } map {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    51
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_reports(ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    52
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_reports(ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    53
      case t => t
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    54
    }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    55
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    56
  def reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    57
    body flatMap {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    58
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    59
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    60
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    61
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    62
      case XML.Wrapped_Elem(_, _, ts) => reports(props, ts)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    63
      case XML.Elem(_, ts) => reports(props, ts)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    64
      case XML.Text(_) => Nil
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    65
    }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    66
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    67
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    68
  /* reported positions */
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    69
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    70
  private val position_elements =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    71
    Markup.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    72
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    73
  def positions(
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    74
    self_id: Document_ID.Generic => Boolean,
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    75
    command_position: Position.T,
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    76
    chunk_name: Symbol.Text_Chunk.Name,
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    77
    chunk: Symbol.Text_Chunk,
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    78
    message: XML.Elem): Set[Text.Range] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    79
  {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    80
    def elem(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    81
      props match {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    82
        case Position.Identified(id, name) if self_id(id) && name == chunk_name =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    83
          val opt_range =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    84
            Position.Range.unapply(props) orElse {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    85
              if (name == Symbol.Text_Chunk.Default)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    86
                Position.Range.unapply(command_position)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    87
              else None
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    88
            }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    89
          opt_range match {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    90
            case Some(symbol_range) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    91
              chunk.incorporate(symbol_range) match {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    92
                case Some(range) => set + range
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    93
                case _ => set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    94
              }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    95
            case None => set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    96
          }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    97
        case _ => set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    98
      }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    99
    def tree(set: Set[Text.Range], t: XML.Tree): Set[Text.Range] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   100
      t match {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   101
        case XML.Wrapped_Elem(Markup(name, props), _, body) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   102
          body.foldLeft(if (position_elements(name)) elem(props, set) else set)(tree)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   103
        case XML.Elem(Markup(name, props), body) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   104
          body.foldLeft(if (position_elements(name)) elem(props, set) else set)(tree)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   105
        case XML.Text(_) => set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   106
      }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   107
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   108
    val set = tree(Set.empty, message)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   109
    if (set.isEmpty) elem(message.markup.properties, set)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   110
    else set
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   111
  }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
   112
}