src/Pure/PIDE/protocol_message.scala
author wenzelm
Sun, 19 May 2024 18:43:45 +0200
changeset 80181 aa92c0f96036
parent 75393 87ebf5a50283
child 80455 99e276c44121
permissions -rw-r--r--
provide scala-3.4.2, but do not activate it: scala-3.3.x is LTS version;
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
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 72692
diff changeset
    10
object Protocol_Message {
71620
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    11
  /* message markers */
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    12
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 72692
diff changeset
    13
  object Marker {
71620
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    14
    def apply(a: String): Marker =
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    15
      new Marker { override def name: String = a }
71623
b3bddebe44ca clarified signature: more explicit type Protocol_Message.Marker;
wenzelm
parents: 71620
diff changeset
    16
b3bddebe44ca clarified signature: more explicit type Protocol_Message.Marker;
wenzelm
parents: 71620
diff changeset
    17
    def test(line: String): Boolean = line.startsWith("\f")
71620
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    18
  }
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    19
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 72692
diff changeset
    20
  abstract class Marker private {
71620
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    21
    def name: String
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    22
    val prefix: String = "\f" + name + " = "
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 apply(text: String): String = prefix + Library.encode_lines(text)
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    25
    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
    26
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    27
    def test(line: String): Boolean = line.startsWith(prefix)
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    28
    def test_yxml(line: String): Boolean = test(line) && YXML.detect(line)
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 unapply(line: String): Option[String] =
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    31
      Library.try_unprefix(prefix, line).map(Library.decode_lines)
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
    override def toString: String = "Marker(" + quote(name) + ")"
5a4ccef7f310 more explicit type Protocol_Message.Marker;
wenzelm
parents: 59713
diff changeset
    34
  }
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
59713
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    37
  /* inlined reports */
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    38
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    39
  private val report_elements =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    40
    Markup.Elements(Markup.REPORT, Markup.NO_REPORT)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    41
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    42
  def clean_reports(body: XML.Body): XML.Body =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    43
    body filter {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    44
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !report_elements(name)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    45
      case XML.Elem(Markup(name, _), _) => !report_elements(name)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    46
      case _ => true
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    47
    } map {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    48
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_reports(ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    49
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_reports(ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    50
      case t => t
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    51
    }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    52
71631
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71623
diff changeset
    53
  def expose_no_reports(body: XML.Body): XML.Body =
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71623
diff changeset
    54
    body flatMap {
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71623
diff changeset
    55
      case XML.Wrapped_Elem(markup, body, ts) => List(XML.Wrapped_Elem(markup, body, expose_no_reports(ts)))
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71623
diff changeset
    56
      case XML.Elem(Markup(Markup.NO_REPORT, _), ts) => ts
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71623
diff changeset
    57
      case XML.Elem(markup, ts) => List(XML.Elem(markup, expose_no_reports(ts)))
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71623
diff changeset
    58
      case t => List(t)
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71623
diff changeset
    59
    }
3f02bc5a5a03 more accurate treatment of errors;
wenzelm
parents: 71623
diff changeset
    60
59713
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    61
  def reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    62
    body flatMap {
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    63
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    64
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    65
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    66
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    67
      case XML.Wrapped_Elem(_, _, ts) => reports(props, ts)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    68
      case XML.Elem(_, ts) => reports(props, ts)
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    69
      case XML.Text(_) => Nil
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    70
    }
6da3efec20ca clarified modules;
wenzelm
parents:
diff changeset
    71
}