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