author | wenzelm |
Wed, 26 Jun 2024 15:22:20 +0200 | |
changeset 80425 | efa212a6699a |
parent 75393 | 87ebf5a50283 |
child 80427 | c92356464bb3 |
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 |
||
75393 | 14 |
object YXML { |
27943
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
15 |
/* chunk markers */ |
27930 | 16 |
|
80425 | 17 |
val X_byte: Byte = 5 |
18 |
val Y_byte: Byte = 6 |
|
19 |
||
20 |
val X = X_byte.toChar |
|
21 |
val Y = Y_byte.toChar |
|
56600 | 22 |
|
73029 | 23 |
val is_X: Char => Boolean = _ == X |
24 |
val is_Y: Char => Boolean = _ == Y |
|
56600 | 25 |
|
71601 | 26 |
val X_string: String = X.toString |
27 |
val Y_string: String = Y.toString |
|
28 |
val XY_string: String = X_string + Y_string |
|
29 |
val XYX_string: String = XY_string + X_string |
|
27930 | 30 |
|
43782 | 31 |
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
|
32 |
def detect_elem(s: String): Boolean = s.startsWith(XY_string) |
43782 | 33 |
|
27930 | 34 |
|
73029 | 35 |
/* string representation */ |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
36 |
|
80425 | 37 |
trait Output { |
38 |
def byte(b: Byte): Unit |
|
39 |
def string(s: String): Unit |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
40 |
def tree(t: XML.Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
41 |
t match { |
73556
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
73340
diff
changeset
|
42 |
case XML.Elem(markup @ Markup(name, atts), ts) => |
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
73340
diff
changeset
|
43 |
if (markup.is_empty) ts.foreach(tree) |
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
73340
diff
changeset
|
44 |
else { |
80425 | 45 |
byte(X_byte) |
46 |
byte(Y_byte) |
|
73556
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
73340
diff
changeset
|
47 |
string(name) |
80425 | 48 |
for ((a, b) <- atts) { |
49 |
byte(Y_byte) |
|
50 |
string(a) |
|
51 |
byte('='.toByte) |
|
52 |
string(b) |
|
53 |
} |
|
54 |
byte(X_byte) |
|
73556
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
73340
diff
changeset
|
55 |
ts.foreach(tree) |
80425 | 56 |
byte(X_byte) |
57 |
byte(Y_byte) |
|
58 |
byte(X_byte) |
|
73556
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
73340
diff
changeset
|
59 |
} |
71534 | 60 |
case XML.Text(text) => string(text) |
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
61 |
} |
80425 | 62 |
def trees(ts: Iterable[XML.Tree]): Unit = ts.foreach(tree) |
71534 | 63 |
} |
64 |
||
80425 | 65 |
class Output_String(builder: StringBuilder) extends Output { |
66 |
override def byte(b: Byte): Unit = { builder += b.toChar } |
|
67 |
override def string(s: String): Unit = { builder ++= s } |
|
68 |
def result(ts: Iterable[XML.Tree]): String = { trees(ts); builder.toString } |
|
69 |
def result(t: XML.Tree): String = { tree(t); builder.toString } |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
70 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
71 |
|
80425 | 72 |
def string_of_body(body: XML.Body): String = |
73 |
new Output_String(new StringBuilder).result(body) |
|
74 |
||
75 |
def string_of_tree(tree: XML.Tree): String = |
|
76 |
new Output_String(new StringBuilder).result(tree) |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
77 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
78 |
|
27930 | 79 |
/* parsing */ |
80 |
||
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27971
diff
changeset
|
81 |
private def err(msg: String) = error("Malformed YXML: " + msg) |
27930 | 82 |
private def err_attribute() = err("bad attribute") |
83 |
private def err_element() = err("bad element") |
|
84 |
private def err_unbalanced(name: String) = |
|
85 |
if (name == "") err("unbalanced element") |
|
44181 | 86 |
else err("unbalanced element " + quote(name)) |
27930 | 87 |
|
73029 | 88 |
private def parse_attrib(source: CharSequence): (String, String) = |
73712 | 89 |
Properties.Eq.unapply(source.toString) getOrElse err_attribute() |
27930 | 90 |
|
91 |
||
75393 | 92 |
def parse_body(source: CharSequence, cache: XML.Cache = XML.Cache.none): XML.Body = { |
27930 | 93 |
/* stack operations */ |
94 |
||
40453 | 95 |
def buffer(): mutable.ListBuffer[XML.Tree] = new mutable.ListBuffer[XML.Tree] |
96 |
var stack: List[(Markup, mutable.ListBuffer[XML.Tree])] = List((Markup.Empty, buffer())) |
|
27930 | 97 |
|
73340 | 98 |
def add(x: XML.Tree): Unit = |
73029 | 99 |
(stack: @unchecked) match { case (_, body) :: _ => body += x } |
27930 | 100 |
|
73340 | 101 |
def push(name: String, atts: XML.Attributes): Unit = |
27930 | 102 |
if (name == "") err_element() |
73030 | 103 |
else stack = (cache.markup(Markup(name, atts)), buffer()) :: stack |
27930 | 104 |
|
73340 | 105 |
def pop(): Unit = |
36684 | 106 |
(stack: @unchecked) match { |
73029 | 107 |
case (Markup.Empty, _) :: _ => err_unbalanced("") |
108 |
case (markup, body) :: pending => |
|
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
109 |
stack = pending |
73030 | 110 |
add(cache.tree0(XML.Elem(markup, body.toList))) |
36684 | 111 |
} |
27930 | 112 |
|
113 |
||
114 |
/* parse chunks */ |
|
115 |
||
56600 | 116 |
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
|
117 |
if (chunk.length == 1 && chunk.charAt(0) == Y) pop() |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
118 |
else { |
56600 | 119 |
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
|
120 |
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
|
121 |
push(name.toString, atts.map(parse_attrib)) |
73030 | 122 |
case txts => for (txt <- txts) add(cache.tree0(XML.Text(cache.string(txt.toString)))) |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
123 |
} |
27930 | 124 |
} |
125 |
} |
|
42719 | 126 |
(stack: @unchecked) match { |
38475 | 127 |
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
|
128 |
case (Markup(name, _), _) :: _ => err_unbalanced(name) |
27930 | 129 |
} |
130 |
} |
|
131 |
||
73030 | 132 |
def parse(source: CharSequence, cache: XML.Cache = XML.Cache.none): XML.Tree = |
133 |
parse_body(source, cache = cache) match { |
|
27930 | 134 |
case List(result) => result |
73028 | 135 |
case Nil => XML.no_text |
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
136 |
case _ => err("multiple XML trees") |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
137 |
} |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
138 |
|
73030 | 139 |
def parse_elem(source: CharSequence, cache: XML.Cache = XML.Cache.none): XML.Tree = |
140 |
parse_body(source, cache = cache) match { |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
141 |
case List(elem: XML.Elem) => elem |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
64370
diff
changeset
|
142 |
case _ => err("single XML element expected") |
27930 | 143 |
} |
27960 | 144 |
|
29521 | 145 |
|
146 |
/* failsafe parsing */ |
|
147 |
||
45666 | 148 |
private def markup_broken(source: CharSequence) = |
55551 | 149 |
XML.Elem(Markup.Broken, List(XML.Text(source.toString))) |
29521 | 150 |
|
75393 | 151 |
def parse_body_failsafe(source: CharSequence, cache: XML.Cache = XML.Cache.none): XML.Body = { |
73030 | 152 |
try { parse_body(source, cache = cache) } |
45666 | 153 |
catch { case ERROR(_) => List(markup_broken(source)) } |
29521 | 154 |
} |
155 |
||
75393 | 156 |
def parse_failsafe(source: CharSequence, cache: XML.Cache = XML.Cache.none): XML.Tree = { |
73030 | 157 |
try { parse(source, cache = cache) } |
45666 | 158 |
catch { case ERROR(_) => markup_broken(source) } |
27960 | 159 |
} |
27930 | 160 |
} |