| author | wenzelm | 
| Wed, 13 Nov 2024 20:10:34 +0100 | |
| changeset 81438 | 95c9af7483b1 | 
| parent 81419 | b242c7603e08 | 
| 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  | 
||
| 
81414
 
ed4ff84e9b21
Document.Snapshot: support for multiple snippet_commands;
 
wenzelm 
parents: 
80872 
diff
changeset
 | 
37  | 
/* message serial */  | 
| 
 
ed4ff84e9b21
Document.Snapshot: support for multiple snippet_commands;
 
wenzelm 
parents: 
80872 
diff
changeset
 | 
38  | 
|
| 81419 | 39  | 
def get_serial(msg: XML.Elem): Long =  | 
40  | 
Markup.Serial.get(msg.markup.properties)  | 
|
41  | 
||
| 
81414
 
ed4ff84e9b21
Document.Snapshot: support for multiple snippet_commands;
 
wenzelm 
parents: 
80872 
diff
changeset
 | 
42  | 
def provide_serial(msg: XML.Elem): XML.Elem =  | 
| 81419 | 43  | 
if (get_serial(msg) != 0L) msg  | 
| 
81414
 
ed4ff84e9b21
Document.Snapshot: support for multiple snippet_commands;
 
wenzelm 
parents: 
80872 
diff
changeset
 | 
44  | 
else msg.copy(markup = msg.markup.update_properties(Markup.Serial(Document_ID.make())))  | 
| 
 
ed4ff84e9b21
Document.Snapshot: support for multiple snippet_commands;
 
wenzelm 
parents: 
80872 
diff
changeset
 | 
45  | 
|
| 
 
ed4ff84e9b21
Document.Snapshot: support for multiple snippet_commands;
 
wenzelm 
parents: 
80872 
diff
changeset
 | 
46  | 
|
| 59713 | 47  | 
/* inlined reports */  | 
48  | 
||
| 80870 | 49  | 
val report_elements: Markup.Elements = Markup.Elements(Markup.REPORT)  | 
50  | 
val no_report_elements: Markup.Elements = Markup.Elements(Markup.NO_REPORT)  | 
|
51  | 
val any_report_elements: Markup.Elements = Markup.Elements(Markup.REPORT, Markup.NO_REPORT)  | 
|
| 59713 | 52  | 
|
53  | 
def clean_reports(body: XML.Body): XML.Body =  | 
|
| 80870 | 54  | 
XML.filter_elements(body, remove = any_report_elements)  | 
| 59713 | 55  | 
|
56  | 
def reports(props: Properties.T, body: XML.Body): List[XML.Elem] =  | 
|
57  | 
    body flatMap {
 | 
|
58  | 
case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>  | 
|
59  | 
List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))  | 
|
60  | 
case XML.Elem(Markup(Markup.REPORT, ps), ts) =>  | 
|
61  | 
List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))  | 
|
| 80455 | 62  | 
case XML.Wrapped_Elem_Body(ts) => reports(props, ts)  | 
| 59713 | 63  | 
case XML.Elem(_, ts) => reports(props, ts)  | 
64  | 
case XML.Text(_) => Nil  | 
|
65  | 
}  | 
|
| 80817 | 66  | 
|
67  | 
||
68  | 
/* clean output */  | 
|
69  | 
||
| 80871 | 70  | 
def clean_output(xml: XML.Body): XML.Body =  | 
71  | 
XML.filter_elements(xml, remove = report_elements, expose = no_report_elements)  | 
|
72  | 
||
| 80817 | 73  | 
def clean_output(msg: String): String =  | 
| 80871 | 74  | 
    try { XML.content(clean_output(YXML.parse_body(YXML.Source(msg)))) }
 | 
| 80817 | 75  | 
    catch { case ERROR(_) => msg }
 | 
| 59713 | 76  | 
}  |