author | wenzelm |
Sat, 09 Jan 2021 18:56:53 +0100 | |
changeset 73115 | a8e5d7c9a834 |
parent 72692 | 22aeec526ffd |
child 75393 | 87ebf5a50283 |
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 |
||
10 |
object Protocol_Message |
|
11 |
{ |
|
71620 | 12 |
/* message markers */ |
13 |
||
14 |
object Marker |
|
15 |
{ |
|
16 |
def apply(a: String): Marker = |
|
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 | 20 |
} |
21 |
||
22 |
abstract class Marker private |
|
23 |
{ |
|
24 |
def name: String |
|
25 |
val prefix: String = "\f" + name + " = " |
|
26 |
||
27 |
def apply(text: String): String = prefix + Library.encode_lines(text) |
|
28 |
def apply(props: Properties.T): String = apply(YXML.string_of_body(XML.Encode.properties(props))) |
|
29 |
||
30 |
def test(line: String): Boolean = line.startsWith(prefix) |
|
31 |
def test_yxml(line: String): Boolean = test(line) && YXML.detect(line) |
|
32 |
||
33 |
def unapply(line: String): Option[String] = |
|
34 |
Library.try_unprefix(prefix, line).map(Library.decode_lines) |
|
35 |
||
36 |
override def toString: String = "Marker(" + quote(name) + ")" |
|
37 |
} |
|
38 |
||
39 |
||
59713 | 40 |
/* inlined reports */ |
41 |
||
42 |
private val report_elements = |
|
43 |
Markup.Elements(Markup.REPORT, Markup.NO_REPORT) |
|
44 |
||
45 |
def clean_reports(body: XML.Body): XML.Body = |
|
46 |
body filter { |
|
47 |
case XML.Wrapped_Elem(Markup(name, _), _, _) => !report_elements(name) |
|
48 |
case XML.Elem(Markup(name, _), _) => !report_elements(name) |
|
49 |
case _ => true |
|
50 |
} map { |
|
51 |
case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_reports(ts)) |
|
52 |
case XML.Elem(markup, ts) => XML.Elem(markup, clean_reports(ts)) |
|
53 |
case t => t |
|
54 |
} |
|
55 |
||
71631 | 56 |
def expose_no_reports(body: XML.Body): XML.Body = |
57 |
body flatMap { |
|
58 |
case XML.Wrapped_Elem(markup, body, ts) => List(XML.Wrapped_Elem(markup, body, expose_no_reports(ts))) |
|
59 |
case XML.Elem(Markup(Markup.NO_REPORT, _), ts) => ts |
|
60 |
case XML.Elem(markup, ts) => List(XML.Elem(markup, expose_no_reports(ts))) |
|
61 |
case t => List(t) |
|
62 |
} |
|
63 |
||
59713 | 64 |
def reports(props: Properties.T, body: XML.Body): List[XML.Elem] = |
65 |
body flatMap { |
|
66 |
case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) => |
|
67 |
List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts)) |
|
68 |
case XML.Elem(Markup(Markup.REPORT, ps), ts) => |
|
69 |
List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts)) |
|
70 |
case XML.Wrapped_Elem(_, _, ts) => reports(props, ts) |
|
71 |
case XML.Elem(_, ts) => reports(props, ts) |
|
72 |
case XML.Text(_) => Nil |
|
73 |
} |
|
74 |
} |