author | wenzelm |
Fri, 27 Mar 2020 22:01:27 +0100 | |
changeset 71601 | 97ccf48c2f0c |
parent 71534 | f10bffaa2bae |
child 73028 | 95e0f014cd24 |
permissions | -rw-r--r-- |
44698 | 1 |
/* Title: Pure/PIDE/yxml.scala |
27930 | 2 |
Author: Makarius |
3 |
||
44698 | 4 |
Efficient text representation of XML trees. Suitable for direct |
5 |
inlining into plain text. |
|
27930 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
40453 | 11 |
import scala.collection.mutable |
36684 | 12 |
|
13 |
||
32450 | 14 |
object YXML |
15 |
{ |
|
27943
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
16 |
/* chunk markers */ |
27930 | 17 |
|
56661
ef623f6f036b
avoid octal escape literals -- deprecated in scala-2.11.0;
wenzelm
parents:
56600
diff
changeset
|
18 |
val X = '\u0005' |
ef623f6f036b
avoid octal escape literals -- deprecated in scala-2.11.0;
wenzelm
parents:
56600
diff
changeset
|
19 |
val Y = '\u0006' |
56600 | 20 |
|
21 |
val is_X = (c: Char) => c == X |
|
22 |
val is_Y = (c: Char) => c == Y |
|
23 |
||
71601 | 24 |
val X_string: String = X.toString |
25 |
val Y_string: String = Y.toString |
|
26 |
val XY_string: String = X_string + Y_string |
|
27 |
val XYX_string: String = XY_string + X_string |
|
27930 | 28 |
|
43782 | 29 |
def detect(s: String): Boolean = s.exists(c => c == X || c == Y) |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
30 |
def detect_elem(s: String): Boolean = s.startsWith(XY_string) |
43782 | 31 |
|
27930 | 32 |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
33 |
/* string representation */ // FIXME byte array version with pseudo-utf-8 (!?) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
34 |
|
71534 | 35 |
def traversal(string: String => Unit, body: XML.Body): Unit = |
38268
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 |
def tree(t: XML.Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
38 |
t match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
39 |
case XML.Elem(Markup(name, atts), ts) => |
71534 | 40 |
string(XY_string) |
41 |
string(name) |
|
42 |
for ((a, x) <- atts) { string(Y_string); string(a); string("="); string(x) } |
|
43 |
string(X_string) |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
44 |
ts.foreach(tree) |
71534 | 45 |
string(XYX_string) |
46 |
case XML.Text(text) => string(text) |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
47 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
48 |
body.foreach(tree) |
71534 | 49 |
} |
50 |
||
51 |
def string_of_body(body: XML.Body): String = |
|
52 |
{ |
|
53 |
val s = new StringBuilder |
|
54 |
traversal(str => s ++= str, body) |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
55 |
s.toString |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
56 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
57 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
58 |
def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree)) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
59 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
60 |
|
27930 | 61 |
/* parsing */ |
62 |
||
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27971
diff
changeset
|
63 |
private def err(msg: String) = error("Malformed YXML: " + msg) |
27930 | 64 |
private def err_attribute() = err("bad attribute") |
65 |
private def err_element() = err("bad element") |
|
66 |
private def err_unbalanced(name: String) = |
|
67 |
if (name == "") err("unbalanced element") |
|
44181 | 68 |
else err("unbalanced element " + quote(name)) |
27930 | 69 |
|
27944
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
70 |
private def parse_attrib(source: CharSequence) = { |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
71 |
val s = source.toString |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
72 |
val i = s.indexOf('=') |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
73 |
if (i <= 0) err_attribute() |
38234
4b610fbb2d83
YXML.parse: refrain from interning, let XML.Cache do it (partially);
wenzelm
parents:
38231
diff
changeset
|
74 |
(s.substring(0, i), s.substring(i + 1)) |
27944
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
75 |
} |
27930 | 76 |
|
77 |
||
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38234
diff
changeset
|
78 |
def parse_body(source: CharSequence): XML.Body = |
32450 | 79 |
{ |
27930 | 80 |
/* stack operations */ |
81 |
||
40453 | 82 |
def buffer(): mutable.ListBuffer[XML.Tree] = new mutable.ListBuffer[XML.Tree] |
83 |
var stack: List[(Markup, mutable.ListBuffer[XML.Tree])] = List((Markup.Empty, buffer())) |
|
27930 | 84 |
|
36684 | 85 |
def add(x: XML.Tree) |
86 |
{ |
|
87 |
(stack: @unchecked) match { case ((_, body) :: _) => body += x } |
|
27930 | 88 |
} |
89 |
||
36684 | 90 |
def push(name: String, atts: XML.Attributes) |
91 |
{ |
|
27930 | 92 |
if (name == "") err_element() |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
93 |
else stack = (Markup(name, atts), buffer()) :: stack |
36684 | 94 |
} |
27930 | 95 |
|
36684 | 96 |
def pop() |
97 |
{ |
|
98 |
(stack: @unchecked) match { |
|
38475 | 99 |
case ((Markup.Empty, _) :: _) => err_unbalanced("") |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
100 |
case ((markup, body) :: pending) => |
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
101 |
stack = pending |
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
102 |
add(XML.Elem(markup, body.toList)) |
36684 | 103 |
} |
27930 | 104 |
} |
105 |
||
106 |
||
107 |
/* parse chunks */ |
|
108 |
||
56600 | 109 |
for (chunk <- Library.separated_chunks(is_X, source) if chunk.length != 0) { |
34099
2541de190d92
added decode_chars, with raw character view on byte buffer and adhoc decoding via toString;
wenzelm
parents:
32450
diff
changeset
|
110 |
if (chunk.length == 1 && chunk.charAt(0) == Y) pop() |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
111 |
else { |
56600 | 112 |
Library.separated_chunks(is_Y, chunk).toList match { |
34099
2541de190d92
added decode_chars, with raw character view on byte buffer and adhoc decoding via toString;
wenzelm
parents:
32450
diff
changeset
|
113 |
case ch :: name :: atts if ch.length == 0 => |
38234
4b610fbb2d83
YXML.parse: refrain from interning, let XML.Cache do it (partially);
wenzelm
parents:
38231
diff
changeset
|
114 |
push(name.toString, atts.map(parse_attrib)) |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
115 |
case txts => for (txt <- txts) add(XML.Text(txt.toString)) |
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
116 |
} |
27930 | 117 |
} |
118 |
} |
|
42719 | 119 |
(stack: @unchecked) match { |
38475 | 120 |
case List((Markup.Empty, body)) => body.toList |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
121 |
case (Markup(name, _), _) :: _ => err_unbalanced(name) |
27930 | 122 |
} |
123 |
} |
|
124 |
||
32450 | 125 |
def parse(source: CharSequence): XML.Tree = |
27930 | 126 |
parse_body(source) match { |
127 |
case List(result) => result |
|
128 |
case Nil => XML.Text("") |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
129 |
case _ => err("multiple XML trees") |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
130 |
} |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
131 |
|
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
132 |
def parse_elem(source: CharSequence): XML.Tree = |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
133 |
parse_body(source) match { |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
134 |
case List(elem: XML.Elem) => elem |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
135 |
case _ => err("single XML element expected") |
27930 | 136 |
} |
27960 | 137 |
|
29521 | 138 |
|
139 |
/* failsafe parsing */ |
|
140 |
||
45666 | 141 |
private def markup_broken(source: CharSequence) = |
55551 | 142 |
XML.Elem(Markup.Broken, List(XML.Text(source.toString))) |
29521 | 143 |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38234
diff
changeset
|
144 |
def parse_body_failsafe(source: CharSequence): XML.Body = |
32450 | 145 |
{ |
29521 | 146 |
try { parse_body(source) } |
45666 | 147 |
catch { case ERROR(_) => List(markup_broken(source)) } |
29521 | 148 |
} |
149 |
||
32450 | 150 |
def parse_failsafe(source: CharSequence): XML.Tree = |
151 |
{ |
|
27960 | 152 |
try { parse(source) } |
45666 | 153 |
catch { case ERROR(_) => markup_broken(source) } |
27960 | 154 |
} |
27930 | 155 |
} |