author | wenzelm |
Thu, 27 Sep 2012 15:55:38 +0200 | |
changeset 49613 | 2f6986e2ef06 |
parent 49466 | 99ed1f422635 |
child 49650 | 9fad6480300d |
permissions | -rw-r--r-- |
44698 | 1 |
/* Title: Pure/PIDE/xml.scala |
45673
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents:
45667
diff
changeset
|
2 |
Module: PIDE |
27931 | 3 |
Author: Makarius |
4 |
||
44698 | 5 |
Untyped XML trees and basic data representation. |
27931 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
34108 | 10 |
import java.util.WeakHashMap |
11 |
import java.lang.ref.WeakReference |
|
12 |
import javax.xml.parsers.DocumentBuilderFactory |
|
13 |
||
27947 | 14 |
|
29203 | 15 |
object XML |
16 |
{ |
|
43767 | 17 |
/** XML trees **/ |
18 |
||
27947 | 19 |
/* datatype representation */ |
20 |
||
43780 | 21 |
type Attributes = Properties.T |
27931 | 22 |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
23 |
sealed abstract class Tree { override def toString = string_of_tree(this) } |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
24 |
case class Elem(markup: Markup, body: List[Tree]) extends Tree |
29204 | 25 |
case class Text(content: String) extends Tree |
29203 | 26 |
|
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
27 |
def elem(name: String, body: List[Tree]) = Elem(Markup(name, Nil), body) |
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36817
diff
changeset
|
28 |
def elem(name: String) = Elem(Markup(name, Nil), Nil) |
33999 | 29 |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38263
diff
changeset
|
30 |
type Body = List[Tree] |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38263
diff
changeset
|
31 |
|
29203 | 32 |
|
33 |
/* string representation */ |
|
34 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
35 |
def string_of_body(body: Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
36 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
37 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
38 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
39 |
def text(txt: String) { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
40 |
if (txt == null) s ++= txt |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
41 |
else { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
42 |
for (c <- txt.iterator) c match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
43 |
case '<' => s ++= "<" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
44 |
case '>' => s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
45 |
case '&' => s ++= "&" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
46 |
case '"' => s ++= """ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
47 |
case '\'' => s ++= "'" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
48 |
case _ => s += c |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
49 |
} |
34005 | 50 |
} |
29203 | 51 |
} |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
52 |
def attrib(p: (String, String)) { s ++= " "; s ++= p._1; s ++= "=\""; text(p._2); s ++= "\"" } |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
53 |
def elem(markup: Markup) { s ++= markup.name; markup.properties.foreach(attrib) } |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
54 |
def tree(t: Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
55 |
t match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
56 |
case Elem(markup, Nil) => |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
57 |
s ++= "<"; elem(markup); s ++= "/>" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
58 |
case Elem(markup, ts) => |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
59 |
s ++= "<"; elem(markup); s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
60 |
ts.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
61 |
s ++= "</"; s ++= markup.name; s ++= ">" |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
62 |
case Text(txt) => text(txt) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
63 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
64 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
65 |
s.toString |
29203 | 66 |
} |
67 |
||
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
68 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
27941 | 69 |
|
70 |
||
49466 | 71 |
/* traverse text */ |
27941 | 72 |
|
49466 | 73 |
def traverse_text[A](body: Body)(a: A)(op: (A, String) => A): A = |
74 |
{ |
|
75 |
def traverse(x: A, t: Tree): A = |
|
76 |
t match { |
|
77 |
case Elem(_, ts) => (x /: ts)(traverse) |
|
78 |
case Text(s) => op(x, s) |
|
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
79 |
} |
49466 | 80 |
(a /: body)(traverse) |
49416
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
81 |
} |
1053a564dd25
some actual rich text markup via XML.content_markup;
wenzelm
parents:
46839
diff
changeset
|
82 |
|
49466 | 83 |
def text_length(body: Body): Int = traverse_text(body)(0) { case (n, s) => n + s.length } |
84 |
||
85 |
||
86 |
/* text content */ |
|
87 |
||
88 |
def content(body: Body): String = |
|
89 |
{ |
|
90 |
val text = new StringBuilder(text_length(body)) |
|
91 |
traverse_text(body)(()) { case (_, s) => text.append(s) } |
|
92 |
text.toString |
|
93 |
} |
|
94 |
||
95 |
def content(tree: Tree): String = content(List(tree)) |
|
27941 | 96 |
|
27947 | 97 |
|
44808 | 98 |
|
99 |
/** cache for partial sharing (weak table) **/ |
|
34108 | 100 |
|
43745 | 101 |
class Cache(initial_size: Int = 131071, max_string: Int = 100) |
34108 | 102 |
{ |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
103 |
private var table = new WeakHashMap[Any, WeakReference[Any]](initial_size) |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
104 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
105 |
private def lookup[A](x: A): Option[A] = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
106 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
107 |
val ref = table.get(x) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
108 |
if (ref == null) None |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
109 |
else { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
110 |
val y = ref.asInstanceOf[WeakReference[A]].get |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
111 |
if (y == null) None |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
112 |
else Some(y) |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
113 |
} |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
114 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
115 |
private def store[A](x: A): A = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
116 |
{ |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
117 |
table.put(x, new WeakReference[Any](x)) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
118 |
x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
119 |
} |
34108 | 120 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
121 |
private def trim_bytes(s: String): String = new String(s.toCharArray) |
38869 | 122 |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
123 |
private def _cache_string(x: String): String = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
124 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
125 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
126 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
127 |
val z = trim_bytes(x) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
128 |
if (z.length > max_string) z else store(z) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
129 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
130 |
private def _cache_props(x: Properties.T): Properties.T = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
131 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
132 |
else |
34133 | 133 |
lookup(x) match { |
134 |
case Some(y) => y |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
135 |
case None => store(x.map(p => (trim_bytes(p._1).intern, _cache_string(p._2)))) |
34133 | 136 |
} |
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
137 |
private def _cache_markup(x: Markup): Markup = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
138 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
139 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
140 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
141 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
142 |
case Markup(name, props) => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
143 |
store(Markup(_cache_string(name), _cache_props(props))) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
144 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
145 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
146 |
private def _cache_tree(x: XML.Tree): XML.Tree = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
147 |
lookup(x) match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
148 |
case Some(y) => y |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
149 |
case None => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
150 |
x match { |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
151 |
case XML.Elem(markup, body) => |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
152 |
store(XML.Elem(_cache_markup(markup), _cache_body(body))) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
153 |
case XML.Text(text) => store(XML.Text(_cache_string(text))) |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
154 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
155 |
} |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
156 |
private def _cache_body(x: XML.Body): XML.Body = |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
157 |
if (x.isEmpty) x |
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
158 |
else |
34133 | 159 |
lookup(x) match { |
160 |
case Some(y) => y |
|
44704
528d635ef6f0
synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents:
44698
diff
changeset
|
161 |
case None => x.map(_cache_tree(_)) |
34133 | 162 |
} |
38446
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
163 |
|
9d59dab38fef
XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents:
38268
diff
changeset
|
164 |
// main methods |
44705 | 165 |
def cache_string(x: String): String = synchronized { _cache_string(x) } |
166 |
def cache_markup(x: Markup): Markup = synchronized { _cache_markup(x) } |
|
167 |
def cache_tree(x: XML.Tree): XML.Tree = synchronized { _cache_tree(x) } |
|
168 |
def cache_body(x: XML.Body): XML.Body = synchronized { _cache_body(x) } |
|
34108 | 169 |
} |
170 |
||
171 |
||
43767 | 172 |
|
173 |
/** XML as data representation language **/ |
|
174 |
||
175 |
class XML_Atom(s: String) extends Exception(s) |
|
176 |
class XML_Body(body: XML.Body) extends Exception |
|
177 |
||
178 |
object Encode |
|
179 |
{ |
|
180 |
type T[A] = A => XML.Body |
|
181 |
||
182 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
183 |
/* atomic values */ |
43767 | 184 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
185 |
def long_atom(i: Long): String = i.toString |
43767 | 186 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
187 |
def int_atom(i: Int): String = i.toString |
43767 | 188 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
189 |
def bool_atom(b: Boolean): String = if (b) "1" else "0" |
43767 | 190 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
191 |
def unit_atom(u: Unit) = "" |
43767 | 192 |
|
193 |
||
194 |
/* structural nodes */ |
|
195 |
||
196 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
|
197 |
||
43781 | 198 |
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
|
199 |
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
|
200 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
201 |
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
|
202 |
XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2) |
43767 | 203 |
|
204 |
||
205 |
/* representation of standard types */ |
|
206 |
||
43780 | 207 |
val properties: T[Properties.T] = |
43767 | 208 |
(props => List(XML.Elem(Markup(":", props), Nil))) |
209 |
||
210 |
val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s))) |
|
211 |
||
212 |
val long: T[Long] = (x => string(long_atom(x))) |
|
213 |
||
214 |
val int: T[Int] = (x => string(int_atom(x))) |
|
215 |
||
216 |
val bool: T[Boolean] = (x => string(bool_atom(x))) |
|
217 |
||
218 |
val unit: T[Unit] = (x => string(unit_atom(x))) |
|
219 |
||
220 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
221 |
(x => List(node(f(x._1)), node(g(x._2)))) |
|
222 |
||
223 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
224 |
(x => List(node(f(x._1)), node(g(x._2)), node(h(x._3)))) |
|
225 |
||
226 |
def list[A](f: T[A]): T[List[A]] = |
|
227 |
(xs => xs.map((x: A) => node(f(x)))) |
|
228 |
||
229 |
def option[A](f: T[A]): T[Option[A]] = |
|
230 |
{ |
|
231 |
case None => Nil |
|
232 |
case Some(x) => List(node(f(x))) |
|
233 |
} |
|
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 variant[A](fs: List[PartialFunction[A, (List[String], XML.Body)]]): T[A] = |
43767 | 236 |
{ |
237 |
case x => |
|
238 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
239 |
List(tagged(tag, f(x))) |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
object Decode |
|
244 |
{ |
|
245 |
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
|
246 |
type V[A] = (List[String], XML.Body) => A |
43767 | 247 |
|
248 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
249 |
/* atomic values */ |
43767 | 250 |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
251 |
def long_atom(s: String): Long = |
43767 | 252 |
try { java.lang.Long.parseLong(s) } |
253 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
254 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
255 |
def int_atom(s: String): Int = |
43767 | 256 |
try { Integer.parseInt(s) } |
257 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
258 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
259 |
def bool_atom(s: String): Boolean = |
43767 | 260 |
if (s == "1") true |
261 |
else if (s == "0") false |
|
262 |
else throw new XML_Atom(s) |
|
263 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
264 |
def unit_atom(s: String): Unit = |
43767 | 265 |
if (s == "") () else throw new XML_Atom(s) |
266 |
||
267 |
||
268 |
/* structural nodes */ |
|
269 |
||
270 |
private def node(t: XML.Tree): XML.Body = |
|
271 |
t match { |
|
272 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
273 |
case _ => throw new XML_Body(List(t)) |
|
274 |
} |
|
275 |
||
43781 | 276 |
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
|
277 |
atts.iterator.zipWithIndex.map( |
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
45673
diff
changeset
|
278 |
{ 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
|
279 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
280 |
private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) = |
43767 | 281 |
t match { |
43781 | 282 |
case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts)) |
43767 | 283 |
case _ => throw new XML_Body(List(t)) |
284 |
} |
|
285 |
||
286 |
||
287 |
/* representation of standard types */ |
|
288 |
||
43780 | 289 |
val properties: T[Properties.T] = |
43767 | 290 |
{ |
291 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
292 |
case ts => throw new XML_Body(ts) |
|
293 |
} |
|
294 |
||
295 |
val string: T[String] = |
|
296 |
{ |
|
297 |
case Nil => "" |
|
298 |
case List(XML.Text(s)) => s |
|
299 |
case ts => throw new XML_Body(ts) |
|
300 |
} |
|
301 |
||
302 |
val long: T[Long] = (x => long_atom(string(x))) |
|
303 |
||
304 |
val int: T[Int] = (x => int_atom(string(x))) |
|
305 |
||
306 |
val bool: T[Boolean] = (x => bool_atom(string(x))) |
|
307 |
||
308 |
val unit: T[Unit] = (x => unit_atom(string(x))) |
|
309 |
||
310 |
def pair[A, B](f: T[A], g: T[B]): T[(A, B)] = |
|
311 |
{ |
|
312 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
313 |
case ts => throw new XML_Body(ts) |
|
314 |
} |
|
315 |
||
316 |
def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] = |
|
317 |
{ |
|
318 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
319 |
case ts => throw new XML_Body(ts) |
|
320 |
} |
|
321 |
||
322 |
def list[A](f: T[A]): T[List[A]] = |
|
323 |
(ts => ts.map(t => f(node(t)))) |
|
324 |
||
325 |
def option[A](f: T[A]): T[Option[A]] = |
|
326 |
{ |
|
327 |
case Nil => None |
|
328 |
case List(t) => Some(f(node(t))) |
|
329 |
case ts => throw new XML_Body(ts) |
|
330 |
} |
|
331 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
332 |
def variant[A](fs: List[V[A]]): T[A] = |
43767 | 333 |
{ |
334 |
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
|
335 |
val (tag, (xs, ts)) = tagged(t) |
43768 | 336 |
val f = |
337 |
try { fs(tag) } |
|
338 |
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
|
339 |
f(xs, ts) |
43767 | 340 |
case ts => throw new XML_Body(ts) |
341 |
} |
|
342 |
} |
|
27931 | 343 |
} |