author | wenzelm |
Sun, 10 Jul 2011 13:51:21 +0200 | |
changeset 43724 | 4e58485fa320 |
parent 43723 | 8a63c95b1d5b |
child 43725 | bebcfcad12d4 |
permissions | -rw-r--r-- |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/General/xml_data.scala |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
3 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
4 |
XML as basic data representation language. |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
5 |
*/ |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
6 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
8 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
9 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
10 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
11 |
object XML_Data |
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
12 |
{ |
43724 | 13 |
/** make **/ |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
14 |
|
43724 | 15 |
object Make |
16 |
{ |
|
17 |
/* basic values */ |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
18 |
|
43724 | 19 |
private def long_atom(i: Long): String = i.toString |
20 |
||
21 |
private def int_atom(i: Int): String = i.toString |
|
38355
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38269
diff
changeset
|
22 |
|
43724 | 23 |
private def bool_atom(b: Boolean): String = if (b) "1" else "0" |
24 |
||
25 |
private def unit_atom(u: Unit) = "" |
|
38355
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38269
diff
changeset
|
26 |
|
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38269
diff
changeset
|
27 |
|
43724 | 28 |
/* structural nodes */ |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
29 |
|
43724 | 30 |
private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts) |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
31 |
|
43724 | 32 |
private def tagged(tag: Int, ts: XML.Body): XML.Tree = |
33 |
XML.Elem(Markup(int_atom(tag), Nil), ts) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
34 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
35 |
|
43724 | 36 |
/* representation of standard types */ |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
37 |
|
43724 | 38 |
def properties(props: List[(String, String)]): XML.Body = |
39 |
List(XML.Elem(Markup(":", props), Nil)) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
40 |
|
43724 | 41 |
def string(s: String): XML.Body = if (s.isEmpty) Nil else List(XML.Text(s)) |
42 |
||
43 |
def long(i: Long): XML.Body = string(long_atom(i)) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
44 |
|
43724 | 45 |
def int(i: Int): XML.Body = string(int_atom(i)) |
46 |
||
47 |
def bool(b: Boolean): XML.Body = string(bool_atom(b)) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
48 |
|
43724 | 49 |
def unit(u: Unit): XML.Body = string(unit_atom(u)) |
50 |
||
51 |
def pair[A, B](f: A => XML.Body, g: B => XML.Body)(p: (A, B)): XML.Body = |
|
52 |
List(node(f(p._1)), node(g(p._2))) |
|
43698 | 53 |
|
43724 | 54 |
def triple[A, B, C](f: A => XML.Body, g: B => XML.Body, h: C => XML.Body) |
55 |
(p: (A, B, C)): XML.Body = |
|
56 |
List(node(f(p._1)), node(g(p._2)), node(h(p._3))) |
|
43698 | 57 |
|
43724 | 58 |
def list[A](f: A => XML.Body)(xs: List[A]): XML.Body = |
59 |
xs.map((x: A) => node(f(x))) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
60 |
|
43724 | 61 |
def option[A](f: A => XML.Body)(opt: Option[A]): XML.Body = |
62 |
opt match { |
|
63 |
case None => Nil |
|
64 |
case Some(x) => List(node(f(x))) |
|
65 |
} |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
66 |
|
43724 | 67 |
def variant[A](fs: List[PartialFunction[A, XML.Body]])(x: A): XML.Body = |
68 |
{ |
|
69 |
val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get |
|
70 |
List(tagged(tag, f(x))) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
71 |
} |
43724 | 72 |
} |
73 |
||
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
74 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
75 |
|
43724 | 76 |
/** dest **/ |
77 |
||
78 |
class XML_Atom(s: String) extends Exception(s) |
|
79 |
class XML_Body(body: XML.Body) extends Exception |
|
80 |
||
81 |
object Dest |
|
82 |
{ |
|
83 |
/* basic values */ |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
84 |
|
43724 | 85 |
private def long_atom(s: String): Long = |
86 |
try { java.lang.Long.parseLong(s) } |
|
87 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
88 |
||
89 |
private def int_atom(s: String): Int = |
|
90 |
try { Integer.parseInt(s) } |
|
91 |
catch { case e: NumberFormatException => throw new XML_Atom(s) } |
|
92 |
||
93 |
private def bool_atom(s: String): Boolean = |
|
94 |
if (s == "1") true |
|
95 |
else if (s == "0") false |
|
96 |
else throw new XML_Atom(s) |
|
97 |
||
98 |
private def unit_atom(s: String): Unit = |
|
99 |
if (s == "") () else throw new XML_Atom(s) |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
100 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
101 |
|
43724 | 102 |
/* structural nodes */ |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
103 |
|
43724 | 104 |
private def node(t: XML.Tree): XML.Body = |
105 |
t match { |
|
106 |
case XML.Elem(Markup(":", Nil), ts) => ts |
|
107 |
case _ => throw new XML_Body(List(t)) |
|
108 |
} |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
109 |
|
43724 | 110 |
private def tagged(t: XML.Tree): (Int, XML.Body) = |
111 |
t match { |
|
112 |
case XML.Elem(Markup(s, Nil), ts) => (int_atom(s), ts) |
|
113 |
case _ => throw new XML_Body(List(t)) |
|
114 |
} |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
115 |
|
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
116 |
|
43724 | 117 |
/* representation of standard types */ |
118 |
||
119 |
def properties(ts: XML.Body): List[(String, String)] = |
|
120 |
ts match { |
|
121 |
case List(XML.Elem(Markup(":", props), Nil)) => props |
|
122 |
case _ => throw new XML_Body(ts) |
|
123 |
} |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
124 |
|
43724 | 125 |
def string(ts: XML.Body): String = |
126 |
ts match { |
|
127 |
case Nil => "" |
|
128 |
case List(XML.Text(s)) => s |
|
129 |
case _ => throw new XML_Body(ts) |
|
130 |
} |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
131 |
|
43724 | 132 |
def long(ts: XML.Body): Long = long_atom(string(ts)) |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
133 |
|
43724 | 134 |
def int(ts: XML.Body): Int = int_atom(string(ts)) |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
135 |
|
43724 | 136 |
def bool(ts: XML.Body) = bool_atom(string(ts)) |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
137 |
|
43724 | 138 |
def unit(ts: XML.Body): Unit = unit_atom(string(ts)) |
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
139 |
|
43724 | 140 |
def pair[A, B](f: XML.Body => A, g: XML.Body => B)(ts: XML.Body): (A, B) = |
141 |
ts match { |
|
142 |
case List(t1, t2) => (f(node(t1)), g(node(t2))) |
|
143 |
case _ => throw new XML_Body(ts) |
|
144 |
} |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
145 |
|
43724 | 146 |
def triple[A, B, C](f: XML.Body => A, g: XML.Body => B, h: XML.Body => C) |
147 |
(ts: XML.Body): (A, B, C) = |
|
148 |
ts match { |
|
149 |
case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3))) |
|
150 |
case _ => throw new XML_Body(ts) |
|
151 |
} |
|
43698 | 152 |
|
43724 | 153 |
def list[A](f: XML.Body => A)(ts: XML.Body): List[A] = |
154 |
ts.map((t: XML.Tree) => f(node(t))) |
|
43698 | 155 |
|
43724 | 156 |
def option[A](f: XML.Body => A)(ts: XML.Body): Option[A] = |
157 |
ts match { |
|
158 |
case Nil => None |
|
159 |
case List(t) => Some(f(node(t))) |
|
160 |
case _ => throw new XML_Body(ts) |
|
161 |
} |
|
43698 | 162 |
|
43724 | 163 |
def variant[A](fs: List[XML.Body => A])(ts: XML.Body): A = |
164 |
ts match { |
|
165 |
case List(t) => |
|
166 |
val (tag, ts) = tagged(t) |
|
167 |
fs(tag)(ts) |
|
168 |
case _ => throw new XML_Body(ts) |
|
169 |
} |
|
170 |
} |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
diff
changeset
|
171 |
} |