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