author | wenzelm |
Thu, 04 Mar 2021 15:41:46 +0100 | |
changeset 73359 | d8a0e996614b |
parent 73340 | 0ffcad1f6130 |
child 73528 | c337c798f64c |
permissions | -rw-r--r-- |
44698 | 1 |
/* Title: Pure/PIDE/xml.scala |
27931 | 2 |
Author: Makarius |
3 |
||
44698 | 4 |
Untyped XML trees and basic data representation. |
27931 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
55618 | 9 |
|
29203 | 10 |
object XML |
11 |
{ |
|
43767 | 12 |
/** XML trees **/ |
13 |
||
27947 | 14 |
/* datatype representation */ |
15 |
||
65753 | 16 |
type Attribute = Properties.Entry |
43780 | 17 |
type Attributes = Properties.T |
27931 | 18 |
|
57912 | 19 |
sealed abstract class Tree { override def toString: String = string_of_tree(this) } |
64354 | 20 |
type Body = List[Tree] |
21 |
case class Elem(markup: Markup, body: Body) extends Tree |
|
52890 | 22 |
{ |
73032 | 23 |
private lazy val hash: Int = (markup, body).hashCode() |
24 |
override def hashCode(): Int = hash |
|
25 |
||
52890 | 26 |
def name: String = markup.name |
65753 | 27 |
|
64358 | 28 |
def update_attributes(more_attributes: Attributes): Elem = |
29 |
if (more_attributes.isEmpty) this |
|
30 |
else Elem(markup.update_properties(more_attributes), body) |
|
65753 | 31 |
|
65772 | 32 |
def + (att: Attribute): Elem = Elem(markup + att, body) |
52890 | 33 |
} |
29204 | 34 |
case class Text(content: String) extends Tree |
73032 | 35 |
{ |
36 |
private lazy val hash: Int = content.hashCode() |
|
37 |
override def hashCode(): Int = hash |
|
38 |
} |
|
29203 | 39 |
|
66196 | 40 |
def elem(markup: Markup): XML.Elem = XML.Elem(markup, Nil) |
64354 | 41 |
def elem(name: String, body: Body): XML.Elem = XML.Elem(Markup(name, Nil), body) |
42 |
def elem(name: String): XML.Elem = XML.Elem(Markup(name, Nil), Nil) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38263
diff
changeset
|
43 |
|
73028 | 44 |
val no_text: Text = Text("") |
69867 | 45 |
val newline: Text = Text("\n") |
46 |
||
29203 | 47 |
|
69805 | 48 |
/* name space */ |
49 |
||
50 |
object Namespace |
|
51 |
{ |
|
52 |
def apply(prefix: String, target: String): Namespace = |
|
53 |
new Namespace(prefix, target) |
|
54 |
} |
|
55 |
||
56 |
final class Namespace private(prefix: String, target: String) |
|
57 |
{ |
|
58 |
def apply(name: String): String = prefix + ":" + name |
|
59 |
val attribute: XML.Attribute = ("xmlns:" + prefix, target) |
|
60 |
||
61 |
override def toString: String = attribute.toString |
|
62 |
} |
|
63 |
||
64 |
||
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
65 |
/* wrapped elements */ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
66 |
|
60215 | 67 |
val XML_ELEM = "xml_elem" |
68 |
val XML_NAME = "xml_name" |
|
69 |
val XML_BODY = "xml_body" |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
70 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
71 |
object Wrapped_Elem |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
72 |
{ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
73 |
def apply(markup: Markup, body1: Body, body2: Body): XML.Elem = |
61026 | 74 |
XML.Elem(Markup(XML_ELEM, (XML_NAME, markup.name) :: markup.properties), |
75 |
XML.Elem(Markup(XML_BODY, Nil), body1) :: body2) |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
76 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
77 |
def unapply(tree: Tree): Option[(Markup, Body, Body)] = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
78 |
tree match { |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
79 |
case |
61026 | 80 |
XML.Elem(Markup(XML_ELEM, (XML_NAME, name) :: props), |
81 |
XML.Elem(Markup(XML_BODY, Nil), body1) :: body2) => |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
82 |
Some(Markup(name, props), body1, body2) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
83 |
case _ => None |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
84 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
85 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
86 |
|
67818
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
87 |
object Root_Elem |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
88 |
{ |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
89 |
def apply(body: Body): XML.Elem = XML.Elem(Markup(XML_ELEM, Nil), body) |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
90 |
def unapply(tree: Tree): Option[Body] = |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
91 |
tree match { |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
92 |
case XML.Elem(Markup(XML_ELEM, Nil), body) => Some(body) |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
93 |
case _ => None |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
94 |
} |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
95 |
} |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
96 |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
97 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
98 |
/* traverse text */ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
99 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
100 |
def traverse_text[A](body: Body)(a: A)(op: (A, String) => A): A = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
101 |
{ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
102 |
def traverse(x: A, t: Tree): A = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
103 |
t match { |
73359 | 104 |
case XML.Wrapped_Elem(_, _, ts) => ts.foldLeft(x)(traverse) |
105 |
case XML.Elem(_, ts) => ts.foldLeft(x)(traverse) |
|
61026 | 106 |
case XML.Text(s) => op(x, s) |
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
107 |
} |
73359 | 108 |
body.foldLeft(a)(traverse) |
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
109 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
110 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
111 |
def text_length(body: Body): Int = traverse_text(body)(0) { case (n, s) => n + s.length } |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
112 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
113 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
114 |
/* text content */ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
115 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
116 |
def content(body: Body): String = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
117 |
{ |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
118 |
val text = new StringBuilder(text_length(body)) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
119 |
traverse_text(body)(()) { case (_, s) => text.append(s) } |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
120 |
text.toString |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
121 |
} |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
122 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
123 |
def content(tree: Tree): String = content(List(tree)) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
124 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
125 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
126 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
127 |
/** string representation **/ |
29203 | 128 |
|
69804 | 129 |
val header: String = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
130 |
||
73340 | 131 |
def output_char(s: StringBuilder, c: Char, permissive: Boolean = false): Unit = |
65990 | 132 |
{ |
133 |
c match { |
|
134 |
case '<' => s ++= "<" |
|
135 |
case '>' => s ++= ">" |
|
136 |
case '&' => s ++= "&" |
|
73203 | 137 |
case '"' if !permissive => s ++= """ |
138 |
case '\'' if !permissive => s ++= "'" |
|
65990 | 139 |
case _ => s += c |
140 |
} |
|
141 |
} |
|
142 |
||
73340 | 143 |
def output_string(s: StringBuilder, str: String, permissive: Boolean = false): Unit = |
65990 | 144 |
{ |
145 |
if (str == null) s ++= str |
|
73203 | 146 |
else str.iterator.foreach(output_char(s, _, permissive = permissive)) |
65990 | 147 |
} |
148 |
||
73340 | 149 |
def output_elem(s: StringBuilder, markup: Markup, end: Boolean = false): Unit = |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
150 |
{ |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
151 |
s += '<' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
152 |
s ++= markup.name |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
153 |
for ((a, b) <- markup.properties) { |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
154 |
s += ' ' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
155 |
s ++= a |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
156 |
s += '=' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
157 |
s += '"' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
158 |
output_string(s, b) |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
159 |
s += '"' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
160 |
} |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
161 |
if (end) s += '/' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
162 |
s += '>' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
163 |
} |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
164 |
|
73340 | 165 |
def output_elem_end(s: StringBuilder, name: String): Unit = |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
166 |
{ |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
167 |
s += '<' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
168 |
s += '/' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
169 |
s ++= name |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
170 |
s += '>' |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
171 |
} |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
172 |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
173 |
def string_of_body(body: Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
174 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
175 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
176 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
177 |
def tree(t: Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
178 |
t match { |
61026 | 179 |
case XML.Elem(markup, Nil) => |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
180 |
output_elem(s, markup, end = true) |
61026 | 181 |
case XML.Elem(markup, ts) => |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
182 |
output_elem(s, markup) |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
183 |
ts.foreach(tree) |
73204
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
184 |
output_elem_end(s, markup.name) |
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
wenzelm
parents:
73203
diff
changeset
|
185 |
case XML.Text(txt) => output_string(s, txt) |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
186 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
187 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
188 |
s.toString |
29203 | 189 |
} |
190 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
191 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
27941 | 192 |
|
193 |
||
44808 | 194 |
|
68265 | 195 |
/** cache **/ |
34108 | 196 |
|
73024 | 197 |
object Cache |
198 |
{ |
|
199 |
def make( |
|
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
200 |
xz: XZ.Cache = XZ.Cache.make(), |
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
201 |
max_string: Int = isabelle.Cache.default_max_string, |
73024 | 202 |
initial_size: Int = isabelle.Cache.default_initial_size): Cache = |
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
203 |
new Cache(xz, max_string, initial_size) |
68169 | 204 |
|
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
205 |
val none: Cache = make(XZ.Cache.none, max_string = 0) |
73024 | 206 |
} |
207 |
||
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73030
diff
changeset
|
208 |
class Cache private[XML](val xz: XZ.Cache, max_string: Int, initial_size: Int) |
73024 | 209 |
extends isabelle.Cache(max_string, initial_size) |
34108 | 210 |
{ |
68265 | 211 |
protected def cache_props(x: Properties.T): Properties.T = |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
212 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
213 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
214 |
else |
34133 | 215 |
lookup(x) match { |
216 |
case Some(y) => y |
|
65903 | 217 |
case None => store(x.map(p => (Library.isolate_substring(p._1).intern, cache_string(p._2)))) |
34133 | 218 |
} |
68265 | 219 |
} |
220 |
||
221 |
protected def cache_markup(x: Markup): Markup = |
|
222 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
223 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
224 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
225 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
226 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
227 |
case Markup(name, props) => |
51663 | 228 |
store(Markup(cache_string(name), cache_props(props))) |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
229 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
230 |
} |
68265 | 231 |
} |
232 |
||
233 |
protected def cache_tree(x: XML.Tree): XML.Tree = |
|
234 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
235 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
236 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
237 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
238 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
239 |
case XML.Elem(markup, body) => |
51663 | 240 |
store(XML.Elem(cache_markup(markup), cache_body(body))) |
241 |
case XML.Text(text) => store(XML.Text(cache_string(text))) |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
242 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
243 |
} |
68265 | 244 |
} |
245 |
||
246 |
protected def cache_body(x: XML.Body): XML.Body = |
|
247 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
248 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
249 |
else |
34133 | 250 |
lookup(x) match { |
251 |
case Some(y) => y |
|
71601 | 252 |
case None => x.map(cache_tree) |
34133 | 253 |
} |
68265 | 254 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
255 |
|
73030 | 256 |
// support hash-consing |
257 |
def tree0(x: XML.Tree): XML.Tree = |
|
258 |
if (no_cache) x else synchronized { lookup(x) getOrElse store(x) } |
|
259 |
||
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
260 |
// main methods |
73024 | 261 |
def props(x: Properties.T): Properties.T = |
262 |
if (no_cache) x else synchronized { cache_props(x) } |
|
263 |
def markup(x: Markup): Markup = |
|
264 |
if (no_cache) x else synchronized { cache_markup(x) } |
|
265 |
def tree(x: XML.Tree): XML.Tree = |
|
266 |
if (no_cache) x else synchronized { cache_tree(x) } |
|
267 |
def body(x: XML.Body): XML.Body = |
|
268 |
if (no_cache) x else synchronized { cache_body(x) } |
|
269 |
def elem(x: XML.Elem): XML.Elem = |
|
270 |
if (no_cache) x else synchronized { cache_tree(x).asInstanceOf[XML.Elem] } |
|
34108 | 271 |
} |
272 |
||
273 |
||
43767 | 274 |
|
275 |
/** XML as data representation language **/ |
|
276 |
||
51987 | 277 |
abstract class Error(s: String) extends Exception(s) |
278 |
class XML_Atom(s: String) extends Error(s) |
|
279 |
class XML_Body(body: XML.Body) extends Error("") |
|
43767 | 280 |
|
281 |
object Encode |
|
282 |
{ |
|
283 |
type T[A] = A => XML.Body |
|
65334 | 284 |
type V[A] = PartialFunction[A, (List[String], XML.Body)] |
70828 | 285 |
type P[A] = PartialFunction[A, List[String]] |
43767 | 286 |
|
287 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
288 |
/* atomic values */ |
43767 | 289 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
290 |
def long_atom(i: Long): String = Library.signed_string_of_long(i) |
43767 | 291 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
292 |
def int_atom(i: Int): String = Library.signed_string_of_int(i) |
43767 | 293 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
294 |
def bool_atom(b: Boolean): String = if (b) "1" else "0" |
43767 | 295 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
296 |
def unit_atom(u: Unit) = "" |
43767 | 297 |
|
298 |
||
299 |
/* structural nodes */ |
|
300 |
||
301 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
302 |
||
43781 | 303 |
private def vector(xs: List[String]): XML.Attributes = |
46839
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
304 |
xs.zipWithIndex.map({ case (x, i) => (int_atom(i), x) }) |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
305 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
306 |
private def tagged(tag: Int, data: (List[String], XML.Body)): XML.Tree = |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
307 |
XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2) |
43767 | 308 |
|
309 |
||
310 |
/* representation of standard types */ |
|
311 |
||
65333 | 312 |
val tree: T[XML.Tree] = (t => List(t)) |
313 |
||
43780 | 314 |
val properties: T[Properties.T] = |
43767 | 315 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
316 |
||
317 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
318 |
||
319 |
val long: T[Long] = (x => string(long_atom(x))) |
|
320 |
||
321 |
val int: T[Int] = (x => string(int_atom(x))) |
|
322 |
||
323 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
324 |
||
325 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
326 |
||
327 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
328 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
329 |
||
330 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
331 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
332 |
||
333 |
def list[A](f: T[A]): T[List[A]] = |
|
334 |
(xs => xs.map((x: A) => node(f(x)))) |
|
335 |
||
336 |
def option[A](f: T[A]): T[Option[A]] = |
|
337 |
{ |
|
338 |
case None => Nil |
|
339 |
case Some(x) => List(node(f(x))) |
|
340 |
} |
|
341 |
||
65334 | 342 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 343 |
{ |
344 |
case x => |
|
345 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
346 |
List(tagged(tag, f(x))) |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
object Decode |
|
351 |
{ |
|
352 |
type T[A] = XML.Body => A |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
353 |
type V[A] = (List[String], XML.Body) => A |
70828 | 354 |
type P[A] = PartialFunction[List[String], A] |
43767 | 355 |
|
356 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
357 |
/* atomic values */ |
43767 | 358 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
359 |
def long_atom(s: String): Long = |
43767 | 360 |
try { java.lang.Long.parseLong(s) } |
361 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
362 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
363 |
def int_atom(s: String): Int = |
43767 | 364 |
try { Integer.parseInt(s) } |
365 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
366 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
367 |
def bool_atom(s: String): Boolean = |
43767 | 368 |
if (s == "1") true |
369 |
else if (s == "0") false |
|
370 |
else throw new XML_Atom(s) |
|
371 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
372 |
def unit_atom(s: String): Unit = |
43767 | 373 |
if (s == "") () else throw new XML_Atom(s) |
374 |
||
375 |
||
376 |
/* structural nodes */ |
|
377 |
||
378 |
private def node(t: XML.Tree): XML.Body = |
|
379 |
t match { |
|
380 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
381 |
case _ => throw new XML_Body(List(t)) |
|
382 |
} |
|
383 |
||
43781 | 384 |
private def vector(atts: XML.Attributes): List[String] = |
46839
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
385 |
atts.iterator.zipWithIndex.map( |
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
386 |
{ case ((a, x), i) => if (int_atom(a) == i) x else throw new XML_Atom(a) }).toList |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
387 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
388 |
private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) = |
43767 | 389 |
t match { |
43781 | 390 |
case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts)) |
43767 | 391 |
case _ => throw new XML_Body(List(t)) |
392 |
} |
|
393 |
||
394 |
||
395 |
/* representation of standard types */ |
|
396 |
||
65333 | 397 |
val tree: T[XML.Tree] = |
398 |
{ |
|
399 |
case List(t) => t |
|
400 |
case ts => throw new XML_Body(ts) |
|
401 |
} |
|
402 |
||
43780 | 403 |
val properties: T[Properties.T] = |
43767 | 404 |
{ |
405 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
406 |
case ts => throw new XML_Body(ts) |
|
407 |
} |
|
408 |
||
409 |
val string: T[String] = |
|
410 |
{ |
|
411 |
case Nil => "" |
|
412 |
case List(XML.Text(s)) => s |
|
413 |
case ts => throw new XML_Body(ts) |
|
414 |
} |
|
415 |
||
416 |
val long: T[Long] = (x => long_atom(string(x))) |
|
417 |
||
418 |
val int: T[Int] = (x => int_atom(string(x))) |
|
419 |
||
420 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
421 |
||
422 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
423 |
||
424 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
425 |
{ |
|
426 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
427 |
case ts => throw new XML_Body(ts) |
|
428 |
} |
|
429 |
||
430 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
431 |
{ |
|
432 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
433 |
case ts => throw new XML_Body(ts) |
|
434 |
} |
|
435 |
||
436 |
def list[A](f: T[A]): T[List[A]] = |
|
437 |
(ts => ts.map(t => f(node(t)))) |
|
438 |
||
439 |
def option[A](f: T[A]): T[Option[A]] = |
|
440 |
{ |
|
441 |
case Nil => None |
|
442 |
case List(t) => Some(f(node(t))) |
|
443 |
case ts => throw new XML_Body(ts) |
|
444 |
} |
|
445 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
446 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 447 |
{ |
448 |
case List(t) => |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
449 |
val (tag, (xs, ts)) = tagged(t) |
43768 | 450 |
val f = |
451 |
try { fs(tag) } |
|
452 |
catch { case _: IndexOutOfBoundsException => throw new XML_Body(List(t)) } |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
453 |
f(xs, ts) |
43767 | 454 |
case ts => throw new XML_Body(ts) |
455 |
} |
|
456 |
} |
|
27931 | 457 |
} |