author | wenzelm |
Sun, 29 Mar 2020 21:57:40 +0200 | |
changeset 71623 | b3bddebe44ca |
parent 71620 | 5a4ccef7f310 |
child 71631 | 3f02bc5a5a03 |
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 |
||
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)) |
|
62 |
case XML.Wrapped_Elem(_, _, ts) => reports(props, ts) |
|
63 |
case XML.Elem(_, ts) => reports(props, ts) |
|
64 |
case XML.Text(_) => Nil |
|
65 |
} |
|
66 |
||
67 |
||
68 |
/* reported positions */ |
|
69 |
||
70 |
private val position_elements = |
|
71 |
Markup.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION) |
|
72 |
||
73 |
def positions( |
|
74 |
self_id: Document_ID.Generic => Boolean, |
|
75 |
command_position: Position.T, |
|
76 |
chunk_name: Symbol.Text_Chunk.Name, |
|
77 |
chunk: Symbol.Text_Chunk, |
|
78 |
message: XML.Elem): Set[Text.Range] = |
|
79 |
{ |
|
80 |
def elem(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] = |
|
81 |
props match { |
|
82 |
case Position.Identified(id, name) if self_id(id) && name == chunk_name => |
|
83 |
val opt_range = |
|
84 |
Position.Range.unapply(props) orElse { |
|
85 |
if (name == Symbol.Text_Chunk.Default) |
|
86 |
Position.Range.unapply(command_position) |
|
87 |
else None |
|
88 |
} |
|
89 |
opt_range match { |
|
90 |
case Some(symbol_range) => |
|
91 |
chunk.incorporate(symbol_range) match { |
|
92 |
case Some(range) => set + range |
|
93 |
case _ => set |
|
94 |
} |
|
95 |
case None => set |
|
96 |
} |
|
97 |
case _ => set |
|
98 |
} |
|
99 |
def tree(set: Set[Text.Range], t: XML.Tree): Set[Text.Range] = |
|
100 |
t match { |
|
101 |
case XML.Wrapped_Elem(Markup(name, props), _, body) => |
|
102 |
body.foldLeft(if (position_elements(name)) elem(props, set) else set)(tree) |
|
103 |
case XML.Elem(Markup(name, props), body) => |
|
104 |
body.foldLeft(if (position_elements(name)) elem(props, set) else set)(tree) |
|
105 |
case XML.Text(_) => set |
|
106 |
} |
|
107 |
||
108 |
val set = tree(Set.empty, message) |
|
109 |
if (set.isEmpty) elem(message.markup.properties, set) |
|
110 |
else set |
|
111 |
} |
|
112 |
} |