author | wenzelm |
Thu, 14 Feb 2019 14:44:41 +0100 | |
changeset 69804 | 9efccbad7d42 |
parent 68265 | f0899dad4877 |
child 69805 | a8debe27c36c |
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 |
{ |
23 |
def name: String = markup.name |
|
65753 | 24 |
|
64358 | 25 |
def update_attributes(more_attributes: Attributes): Elem = |
26 |
if (more_attributes.isEmpty) this |
|
27 |
else Elem(markup.update_properties(more_attributes), body) |
|
65753 | 28 |
|
65772 | 29 |
def + (att: Attribute): Elem = Elem(markup + att, body) |
52890 | 30 |
} |
29204 | 31 |
case class Text(content: String) extends Tree |
29203 | 32 |
|
66196 | 33 |
def elem(markup: Markup): XML.Elem = XML.Elem(markup, Nil) |
64354 | 34 |
def elem(name: String, body: Body): XML.Elem = XML.Elem(Markup(name, Nil), body) |
35 |
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
|
36 |
|
29203 | 37 |
|
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
|
38 |
/* 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
|
39 |
|
60215 | 40 |
val XML_ELEM = "xml_elem" |
41 |
val XML_NAME = "xml_name" |
|
42 |
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
|
43 |
|
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
|
44 |
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
|
45 |
{ |
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
|
46 |
def apply(markup: Markup, body1: Body, body2: Body): XML.Elem = |
61026 | 47 |
XML.Elem(Markup(XML_ELEM, (XML_NAME, markup.name) :: markup.properties), |
48 |
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
|
49 |
|
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
|
50 |
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
|
51 |
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
|
52 |
case |
61026 | 53 |
XML.Elem(Markup(XML_ELEM, (XML_NAME, name) :: props), |
54 |
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
|
55 |
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
|
56 |
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
|
57 |
} |
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
|
58 |
} |
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
|
59 |
|
67818
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
60 |
object Root_Elem |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
61 |
{ |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
62 |
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
|
63 |
def unapply(tree: Tree): Option[Body] = |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
64 |
tree match { |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
65 |
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
|
66 |
case _ => None |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
67 |
} |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
68 |
} |
2457bea123e4
convenience to represent XML.Body as single XML.Elem;
wenzelm
parents:
67113
diff
changeset
|
69 |
|
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 |
/* 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
|
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 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
|
74 |
{ |
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
|
75 |
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
|
76 |
t match { |
61026 | 77 |
case XML.Wrapped_Elem(_, _, ts) => (x /: ts)(traverse) |
78 |
case XML.Elem(_, ts) => (x /: ts)(traverse) |
|
79 |
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
|
80 |
} |
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
|
81 |
(a /: body)(traverse) |
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 |
} |
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 |
|
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 |
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
|
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 |
|
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
|
87 |
/* 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
|
88 |
|
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
|
89 |
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
|
90 |
{ |
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 |
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
|
92 |
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
|
93 |
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
|
94 |
} |
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
|
95 |
|
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 |
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
|
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 |
|
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 |
/** string representation **/ |
29203 | 101 |
|
69804 | 102 |
val header: String = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
103 |
||
65990 | 104 |
def output_char(c: Char, s: StringBuilder) |
105 |
{ |
|
106 |
c match { |
|
107 |
case '<' => s ++= "<" |
|
108 |
case '>' => s ++= ">" |
|
109 |
case '&' => s ++= "&" |
|
110 |
case '"' => s ++= """ |
|
111 |
case '\'' => s ++= "'" |
|
112 |
case _ => s += c |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
def output_string(str: String, s: StringBuilder) |
|
117 |
{ |
|
118 |
if (str == null) s ++= str |
|
119 |
else str.iterator.foreach(c => output_char(c, s)) |
|
120 |
} |
|
121 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
122 |
def string_of_body(body: Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
123 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
124 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
125 |
|
65990 | 126 |
def text(txt: String) { output_string(txt, s) } |
65991 | 127 |
def elem(markup: Markup) |
128 |
{ |
|
129 |
s ++= markup.name |
|
130 |
for ((a, b) <- markup.properties) { |
|
131 |
s += ' '; s ++= a; s += '='; s += '"'; text(b); s += '"' |
|
132 |
} |
|
133 |
} |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
134 |
def tree(t: Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
135 |
t match { |
61026 | 136 |
case XML.Elem(markup, Nil) => |
65991 | 137 |
s += '<'; elem(markup); s ++= "/>" |
61026 | 138 |
case XML.Elem(markup, ts) => |
65991 | 139 |
s += '<'; elem(markup); s += '>' |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
140 |
ts.foreach(tree) |
65991 | 141 |
s ++= "</"; s ++= markup.name; s += '>' |
61026 | 142 |
case XML.Text(txt) => text(txt) |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
143 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
144 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
145 |
s.toString |
29203 | 146 |
} |
147 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
148 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
27941 | 149 |
|
150 |
||
44808 | 151 |
|
68265 | 152 |
/** cache **/ |
34108 | 153 |
|
68169 | 154 |
def make_cache(initial_size: Int = 131071, max_string: Int = 100): Cache = |
155 |
new Cache(initial_size, max_string) |
|
156 |
||
157 |
class Cache private[XML](initial_size: Int, max_string: Int) |
|
68265 | 158 |
extends isabelle.Cache(initial_size, max_string) |
34108 | 159 |
{ |
68265 | 160 |
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
|
161 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
162 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
163 |
else |
34133 | 164 |
lookup(x) match { |
165 |
case Some(y) => y |
|
65903 | 166 |
case None => store(x.map(p => (Library.isolate_substring(p._1).intern, cache_string(p._2)))) |
34133 | 167 |
} |
68265 | 168 |
} |
169 |
||
170 |
protected def cache_markup(x: Markup): Markup = |
|
171 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
172 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
173 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
174 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
175 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
176 |
case Markup(name, props) => |
51663 | 177 |
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
|
178 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
179 |
} |
68265 | 180 |
} |
181 |
||
182 |
protected def cache_tree(x: XML.Tree): XML.Tree = |
|
183 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
184 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
185 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
186 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
187 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
188 |
case XML.Elem(markup, body) => |
51663 | 189 |
store(XML.Elem(cache_markup(markup), cache_body(body))) |
190 |
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
|
191 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
192 |
} |
68265 | 193 |
} |
194 |
||
195 |
protected def cache_body(x: XML.Body): XML.Body = |
|
196 |
{ |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
197 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
198 |
else |
34133 | 199 |
lookup(x) match { |
200 |
case Some(y) => y |
|
51663 | 201 |
case None => x.map(cache_tree(_)) |
34133 | 202 |
} |
68265 | 203 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
204 |
|
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
205 |
// main methods |
51663 | 206 |
def props(x: Properties.T): Properties.T = synchronized { cache_props(x) } |
207 |
def markup(x: Markup): Markup = synchronized { cache_markup(x) } |
|
208 |
def tree(x: XML.Tree): XML.Tree = synchronized { cache_tree(x) } |
|
209 |
def body(x: XML.Body): XML.Body = synchronized { cache_body(x) } |
|
210 |
def elem(x: XML.Elem): XML.Elem = synchronized { cache_tree(x).asInstanceOf[XML.Elem] } |
|
34108 | 211 |
} |
212 |
||
213 |
||
43767 | 214 |
|
215 |
/** XML as data representation language **/ |
|
216 |
||
51987 | 217 |
abstract class Error(s: String) extends Exception(s) |
218 |
class XML_Atom(s: String) extends Error(s) |
|
219 |
class XML_Body(body: XML.Body) extends Error("") |
|
43767 | 220 |
|
221 |
object Encode |
|
222 |
{ |
|
223 |
type T[A] = A => XML.Body |
|
65334 | 224 |
type V[A] = PartialFunction[A, (List[String], XML.Body)] |
43767 | 225 |
|
226 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
227 |
/* atomic values */ |
43767 | 228 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
229 |
def long_atom(i: Long): String = Library.signed_string_of_long(i) |
43767 | 230 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
55618
diff
changeset
|
231 |
def int_atom(i: Int): String = Library.signed_string_of_int(i) |
43767 | 232 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
233 |
def bool_atom(b: Boolean): String = if (b) "1" else "0" |
43767 | 234 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
235 |
def unit_atom(u: Unit) = "" |
43767 | 236 |
|
237 |
||
238 |
/* structural nodes */ |
|
239 |
||
240 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
241 |
||
43781 | 242 |
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
|
243 |
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
|
244 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
245 |
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
|
246 |
XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2) |
43767 | 247 |
|
248 |
||
249 |
/* representation of standard types */ |
|
250 |
||
65333 | 251 |
val tree: T[XML.Tree] = (t => List(t)) |
252 |
||
43780 | 253 |
val properties: T[Properties.T] = |
43767 | 254 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
255 |
||
256 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
257 |
||
258 |
val long: T[Long] = (x => string(long_atom(x))) |
|
259 |
||
260 |
val int: T[Int] = (x => string(int_atom(x))) |
|
261 |
||
262 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
263 |
||
264 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
265 |
||
266 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
267 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
268 |
||
269 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
270 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
271 |
||
272 |
def list[A](f: T[A]): T[List[A]] = |
|
273 |
(xs => xs.map((x: A) => node(f(x)))) |
|
274 |
||
275 |
def option[A](f: T[A]): T[Option[A]] = |
|
276 |
{ |
|
277 |
case None => Nil |
|
278 |
case Some(x) => List(node(f(x))) |
|
279 |
} |
|
280 |
||
65334 | 281 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 282 |
{ |
283 |
case x => |
|
284 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
285 |
List(tagged(tag, f(x))) |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
object Decode |
|
290 |
{ |
|
291 |
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
|
292 |
type V[A] = (List[String], XML.Body) => A |
43767 | 293 |
|
294 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
295 |
/* atomic values */ |
43767 | 296 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
297 |
def long_atom(s: String): Long = |
43767 | 298 |
try { java.lang.Long.parseLong(s) } |
299 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
300 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
301 |
def int_atom(s: String): Int = |
43767 | 302 |
try { Integer.parseInt(s) } |
303 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
304 |
||
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 |
def bool_atom(s: String): Boolean = |
43767 | 306 |
if (s == "1") true |
307 |
else if (s == "0") false |
|
308 |
else throw new XML_Atom(s) |
|
309 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
310 |
def unit_atom(s: String): Unit = |
43767 | 311 |
if (s == "") () else throw new XML_Atom(s) |
312 |
||
313 |
||
314 |
/* structural nodes */ |
|
315 |
||
316 |
private def node(t: XML.Tree): XML.Body = |
|
317 |
t match { |
|
318 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
319 |
case _ => throw new XML_Body(List(t)) |
|
320 |
} |
|
321 |
||
43781 | 322 |
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
|
323 |
atts.iterator.zipWithIndex.map( |
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
324 |
{ 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
|
325 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
326 |
private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) = |
43767 | 327 |
t match { |
43781 | 328 |
case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts)) |
43767 | 329 |
case _ => throw new XML_Body(List(t)) |
330 |
} |
|
331 |
||
332 |
||
333 |
/* representation of standard types */ |
|
334 |
||
65333 | 335 |
val tree: T[XML.Tree] = |
336 |
{ |
|
337 |
case List(t) => t |
|
338 |
case ts => throw new XML_Body(ts) |
|
339 |
} |
|
340 |
||
43780 | 341 |
val properties: T[Properties.T] = |
43767 | 342 |
{ |
343 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
344 |
case ts => throw new XML_Body(ts) |
|
345 |
} |
|
346 |
||
347 |
val string: T[String] = |
|
348 |
{ |
|
349 |
case Nil => "" |
|
350 |
case List(XML.Text(s)) => s |
|
351 |
case ts => throw new XML_Body(ts) |
|
352 |
} |
|
353 |
||
354 |
val long: T[Long] = (x => long_atom(string(x))) |
|
355 |
||
356 |
val int: T[Int] = (x => int_atom(string(x))) |
|
357 |
||
358 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
359 |
||
360 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
361 |
||
362 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
363 |
{ |
|
364 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
365 |
case ts => throw new XML_Body(ts) |
|
366 |
} |
|
367 |
||
368 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
369 |
{ |
|
370 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
371 |
case ts => throw new XML_Body(ts) |
|
372 |
} |
|
373 |
||
374 |
def list[A](f: T[A]): T[List[A]] = |
|
375 |
(ts => ts.map(t => f(node(t)))) |
|
376 |
||
377 |
def option[A](f: T[A]): T[Option[A]] = |
|
378 |
{ |
|
379 |
case Nil => None |
|
380 |
case List(t) => Some(f(node(t))) |
|
381 |
case ts => throw new XML_Body(ts) |
|
382 |
} |
|
383 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
384 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 385 |
{ |
386 |
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
|
387 |
val (tag, (xs, ts)) = tagged(t) |
43768 | 388 |
val f = |
389 |
try { fs(tag) } |
|
390 |
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
|
391 |
f(xs, ts) |
43767 | 392 |
case ts => throw new XML_Body(ts) |
393 |
} |
|
394 |
} |
|
27931 | 395 |
} |