author | wenzelm |
Thu, 12 Sep 2024 14:38:19 +0200 | |
changeset 80871 | b71a040ab128 |
parent 80870 | 9a7de3f320d8 |
child 80872 | 320bcbf34849 |
permissions | -rw-r--r-- |
59713 | 1 |
/* Title: Pure/PIDE/protocol_message.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Auxiliary operations on protocol messages. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
75393 | 10 |
object Protocol_Message { |
71620 | 11 |
/* message markers */ |
12 |
||
75393 | 13 |
object Marker { |
71620 | 14 |
def apply(a: String): Marker = |
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 | 18 |
} |
19 |
||
75393 | 20 |
abstract class Marker private { |
71620 | 21 |
def name: String |
22 |
val prefix: String = "\f" + name + " = " |
|
23 |
||
24 |
def apply(text: String): String = prefix + Library.encode_lines(text) |
|
25 |
def apply(props: Properties.T): String = apply(YXML.string_of_body(XML.Encode.properties(props))) |
|
26 |
||
27 |
def test(line: String): Boolean = line.startsWith(prefix) |
|
28 |
def test_yxml(line: String): Boolean = test(line) && YXML.detect(line) |
|
29 |
||
30 |
def unapply(line: String): Option[String] = |
|
31 |
Library.try_unprefix(prefix, line).map(Library.decode_lines) |
|
32 |
||
33 |
override def toString: String = "Marker(" + quote(name) + ")" |
|
34 |
} |
|
35 |
||
36 |
||
59713 | 37 |
/* inlined reports */ |
38 |
||
80870 | 39 |
val report_elements: Markup.Elements = Markup.Elements(Markup.REPORT) |
40 |
val no_report_elements: Markup.Elements = Markup.Elements(Markup.NO_REPORT) |
|
41 |
val any_report_elements: Markup.Elements = Markup.Elements(Markup.REPORT, Markup.NO_REPORT) |
|
59713 | 42 |
|
43 |
def clean_reports(body: XML.Body): XML.Body = |
|
80870 | 44 |
XML.filter_elements(body, remove = any_report_elements) |
59713 | 45 |
|
71631 | 46 |
def expose_no_reports(body: XML.Body): XML.Body = |
80818 | 47 |
XML.filter_elements(body, expose = no_report_elements) |
71631 | 48 |
|
59713 | 49 |
def reports(props: Properties.T, body: XML.Body): List[XML.Elem] = |
50 |
body flatMap { |
|
51 |
case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) => |
|
52 |
List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts)) |
|
53 |
case XML.Elem(Markup(Markup.REPORT, ps), ts) => |
|
54 |
List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts)) |
|
80455 | 55 |
case XML.Wrapped_Elem_Body(ts) => reports(props, ts) |
59713 | 56 |
case XML.Elem(_, ts) => reports(props, ts) |
57 |
case XML.Text(_) => Nil |
|
58 |
} |
|
80817 | 59 |
|
60 |
||
61 |
/* clean output */ |
|
62 |
||
80871 | 63 |
def clean_output(xml: XML.Body): XML.Body = |
64 |
XML.filter_elements(xml, remove = report_elements, expose = no_report_elements) |
|
65 |
||
80817 | 66 |
def clean_output(msg: String): String = |
80871 | 67 |
try { XML.content(clean_output(YXML.parse_body(YXML.Source(msg)))) } |
80817 | 68 |
catch { case ERROR(_) => msg } |
59713 | 69 |
} |