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