author | wenzelm |
Mon, 28 Nov 2011 22:18:19 +0100 | |
changeset 45667 | 546d78f0d81f |
parent 45666 | d83797ef0d2d |
child 45673 | cd41e3903fbf |
permissions | -rw-r--r-- |
44698 | 1 |
/* Title: Pure/PIDE/yxml.scala |
45667
546d78f0d81f
explicit indication of modules for independent Scala library;
wenzelm
parents:
45666
diff
changeset
|
2 |
Module: Library |
27930 | 3 |
Author: Makarius |
4 |
||
44698 | 5 |
Efficient text representation of XML trees. Suitable for direct |
6 |
inlining into plain text. |
|
27930 | 7 |
*/ |
8 |
||
9 |
package isabelle |
|
10 |
||
11 |
||
40453 | 12 |
import scala.collection.mutable |
36684 | 13 |
|
14 |
||
32450 | 15 |
object YXML |
16 |
{ |
|
27943
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
17 |
/* chunk markers */ |
27930 | 18 |
|
43774
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43746
diff
changeset
|
19 |
val X = '\5' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43746
diff
changeset
|
20 |
val Y = '\6' |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43746
diff
changeset
|
21 |
val X_string = X.toString |
6dfdb70496fe
added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents:
43746
diff
changeset
|
22 |
val Y_string = Y.toString |
27930 | 23 |
|
43782 | 24 |
def detect(s: String): Boolean = s.exists(c => c == X || c == Y) |
25 |
||
27930 | 26 |
|
38268
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
27 |
/* 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
|
28 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
29 |
def string_of_body(body: XML.Body): String = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
30 |
{ |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
31 |
val s = new StringBuilder |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
32 |
def attrib(p: (String, String)) { s += Y; s ++= p._1; s += '='; s ++= p._2 } |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
33 |
def tree(t: XML.Tree): Unit = |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
34 |
t match { |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
35 |
case XML.Elem(Markup(name, atts), ts) => |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
36 |
s += X; s += Y; s++= name; atts.foreach(attrib); s += X |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
37 |
ts.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
38 |
s += X; s += Y; s += X |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
39 |
case XML.Text(text) => s ++= text |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
40 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
41 |
body.foreach(tree) |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
42 |
s.toString |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
43 |
} |
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
44 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
45 |
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
|
46 |
|
beb86b805590
more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents:
38267
diff
changeset
|
47 |
|
27930 | 48 |
/* parsing */ |
49 |
||
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27971
diff
changeset
|
50 |
private def err(msg: String) = error("Malformed YXML: " + msg) |
27930 | 51 |
private def err_attribute() = err("bad attribute") |
52 |
private def err_element() = err("bad element") |
|
53 |
private def err_unbalanced(name: String) = |
|
54 |
if (name == "") err("unbalanced element") |
|
44181 | 55 |
else err("unbalanced element " + quote(name)) |
27930 | 56 |
|
27944
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
57 |
private def parse_attrib(source: CharSequence) = { |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
58 |
val s = source.toString |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
59 |
val i = s.indexOf('=') |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
60 |
if (i <= 0) err_attribute() |
38234
4b610fbb2d83
YXML.parse: refrain from interning, let XML.Cache do it (partially);
wenzelm
parents:
38231
diff
changeset
|
61 |
(s.substring(0, i), s.substring(i + 1)) |
27944
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
62 |
} |
27930 | 63 |
|
64 |
||
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38234
diff
changeset
|
65 |
def parse_body(source: CharSequence): XML.Body = |
32450 | 66 |
{ |
27930 | 67 |
/* stack operations */ |
68 |
||
40453 | 69 |
def buffer(): mutable.ListBuffer[XML.Tree] = new mutable.ListBuffer[XML.Tree] |
70 |
var stack: List[(Markup, mutable.ListBuffer[XML.Tree])] = List((Markup.Empty, buffer())) |
|
27930 | 71 |
|
36684 | 72 |
def add(x: XML.Tree) |
73 |
{ |
|
74 |
(stack: @unchecked) match { case ((_, body) :: _) => body += x } |
|
27930 | 75 |
} |
76 |
||
36684 | 77 |
def push(name: String, atts: XML.Attributes) |
78 |
{ |
|
27930 | 79 |
if (name == "") err_element() |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
80 |
else stack = (Markup(name, atts), buffer()) :: stack |
36684 | 81 |
} |
27930 | 82 |
|
36684 | 83 |
def pop() |
84 |
{ |
|
85 |
(stack: @unchecked) match { |
|
38475 | 86 |
case ((Markup.Empty, _) :: _) => err_unbalanced("") |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
87 |
case ((markup, body) :: pending) => |
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
88 |
stack = pending |
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
36685
diff
changeset
|
89 |
add(XML.Elem(markup, body.toList)) |
36684 | 90 |
} |
27930 | 91 |
} |
92 |
||
93 |
||
94 |
/* parse chunks */ |
|
95 |
||
36685 | 96 |
for (chunk <- Library.chunks(source, X) 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
|
97 |
if (chunk.length == 1 && chunk.charAt(0) == Y) pop() |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
98 |
else { |
36685 | 99 |
Library.chunks(chunk, Y).toList match { |
34099
2541de190d92
added decode_chars, with raw character view on byte buffer and adhoc decoding via toString;
wenzelm
parents:
32450
diff
changeset
|
100 |
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
|
101 |
push(name.toString, atts.map(parse_attrib)) |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
102 |
case txts => for (txt <- txts) add(XML.Text(txt.toString)) |
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
103 |
} |
27930 | 104 |
} |
105 |
} |
|
42719 | 106 |
(stack: @unchecked) match { |
38475 | 107 |
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
|
108 |
case (Markup(name, _), _) :: _ => err_unbalanced(name) |
27930 | 109 |
} |
110 |
} |
|
111 |
||
32450 | 112 |
def parse(source: CharSequence): XML.Tree = |
27930 | 113 |
parse_body(source) match { |
114 |
case List(result) => result |
|
115 |
case Nil => XML.Text("") |
|
116 |
case _ => err("multiple results") |
|
117 |
} |
|
27960 | 118 |
|
29521 | 119 |
|
120 |
/* failsafe parsing */ |
|
121 |
||
45666 | 122 |
private def markup_broken(source: CharSequence) = |
123 |
XML.elem(Markup.Broken.name, List(XML.Text(source.toString))) |
|
29521 | 124 |
|
38267
e50c283dd125
type XML.Body as basic data representation language (Scala version);
wenzelm
parents:
38234
diff
changeset
|
125 |
def parse_body_failsafe(source: CharSequence): XML.Body = |
32450 | 126 |
{ |
29521 | 127 |
try { parse_body(source) } |
45666 | 128 |
catch { case ERROR(_) => List(markup_broken(source)) } |
29521 | 129 |
} |
130 |
||
32450 | 131 |
def parse_failsafe(source: CharSequence): XML.Tree = |
132 |
{ |
|
27960 | 133 |
try { parse(source) } |
45666 | 134 |
catch { case ERROR(_) => markup_broken(source) } |
27960 | 135 |
} |
27930 | 136 |
} |