author | blanchet |
Mon, 26 Oct 2009 09:14:29 +0100 | |
changeset 33201 | e3d741e9d2fe |
parent 32450 | 375db037f4d2 |
child 34099 | 2541de190d92 |
permissions | -rw-r--r-- |
27930 | 1 |
/* Title: Pure/General/yxml.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Efficient text representation of XML trees. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
32450 | 10 |
object YXML |
11 |
{ |
|
27943
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
12 |
/* chunk markers */ |
27930 | 13 |
|
14 |
private val X = '\5' |
|
15 |
private val Y = '\6' |
|
27960 | 16 |
private val X_string = X.toString |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
17 |
private val Y_string = Y.toString |
27930 | 18 |
|
19 |
||
27943
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
20 |
/* iterate over chunks (resembles space_explode in ML) */ |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
21 |
|
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
22 |
private def chunks(sep: Char, source: CharSequence) = new Iterator[CharSequence] { |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
23 |
private val end = source.length |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
24 |
private var state = if (end == 0) None else get_chunk(-1) |
32450 | 25 |
private def get_chunk(i: Int) = |
26 |
{ |
|
27943
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
27 |
if (i < end) { |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
28 |
var j = i; do j += 1 while (j < end && source.charAt(j) != sep) |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
29 |
Some((source.subSequence(i + 1, j), j)) |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
30 |
} |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
31 |
else None |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
32 |
} |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
33 |
|
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
34 |
def hasNext() = state.isDefined |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
35 |
def next() = state match { |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
36 |
case Some((s, i)) => { state = get_chunk(i); s } |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
37 |
case None => throw new NoSuchElementException("next on empty iterator") |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
38 |
} |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
39 |
} |
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
40 |
|
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
41 |
|
27930 | 42 |
/* parsing */ |
43 |
||
27993
6dd90ef9f927
simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents:
27971
diff
changeset
|
44 |
private def err(msg: String) = error("Malformed YXML: " + msg) |
27930 | 45 |
private def err_attribute() = err("bad attribute") |
46 |
private def err_element() = err("bad element") |
|
47 |
private def err_unbalanced(name: String) = |
|
48 |
if (name == "") err("unbalanced element") |
|
49 |
else err("unbalanced element \"" + name + "\"") |
|
50 |
||
27944
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
51 |
private def parse_attrib(source: CharSequence) = { |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
52 |
val s = source.toString |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
53 |
val i = s.indexOf('=') |
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
54 |
if (i <= 0) err_attribute() |
29563 | 55 |
(s.substring(0, i).intern, s.substring(i + 1)) |
27944
2bf3f30558ed
parse_attrib: more efficient due to indexOf('=');
wenzelm
parents:
27943
diff
changeset
|
56 |
} |
27930 | 57 |
|
58 |
||
32450 | 59 |
def parse_body(source: CharSequence): List[XML.Tree] = |
60 |
{ |
|
27930 | 61 |
/* stack operations */ |
62 |
||
63 |
var stack: List[((String, XML.Attributes), List[XML.Tree])] = null |
|
64 |
||
29180 | 65 |
def add(x: XML.Tree) = (stack: @unchecked) match { |
27930 | 66 |
case ((elem, body) :: pending) => stack = (elem, x :: body) :: pending |
67 |
} |
|
68 |
||
69 |
def push(name: String, atts: XML.Attributes) = |
|
70 |
if (name == "") err_element() |
|
71 |
else stack = ((name, atts), Nil) :: stack |
|
72 |
||
29180 | 73 |
def pop() = (stack: @unchecked) match { |
27930 | 74 |
case ((("", _), _) :: _) => err_unbalanced("") |
75 |
case (((name, atts), body) :: pending) => |
|
76 |
stack = pending; add(XML.Elem(name, atts, body.reverse)) |
|
77 |
} |
|
78 |
||
79 |
||
80 |
/* parse chunks */ |
|
81 |
||
82 |
stack = List((("", Nil), Nil)) |
|
27943
f34ff5e7728f
replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents:
27930
diff
changeset
|
83 |
for (chunk <- chunks(X, source) if chunk != "") { |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
84 |
if (chunk == Y_string) pop() |
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
85 |
else { |
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
86 |
chunks(Y, chunk).toList match { |
29563 | 87 |
case "" :: name :: atts => push(name.toString.intern, atts.map(parse_attrib)) |
27945
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
88 |
case txts => for (txt <- txts) add(XML.Text(txt.toString)) |
d2dc5a1903e8
tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents:
27944
diff
changeset
|
89 |
} |
27930 | 90 |
} |
91 |
} |
|
92 |
stack match { |
|
93 |
case List((("", _), result)) => result.reverse |
|
94 |
case ((name, _), _) :: _ => err_unbalanced(name) |
|
95 |
} |
|
96 |
} |
|
97 |
||
32450 | 98 |
def parse(source: CharSequence): XML.Tree = |
27930 | 99 |
parse_body(source) match { |
100 |
case List(result) => result |
|
101 |
case Nil => XML.Text("") |
|
102 |
case _ => err("multiple results") |
|
103 |
} |
|
27960 | 104 |
|
29521 | 105 |
|
106 |
/* failsafe parsing */ |
|
107 |
||
108 |
private def markup_failsafe(source: CharSequence) = |
|
109 |
XML.Elem (Markup.BAD, Nil, |
|
110 |
List(XML.Text(source.toString.replace(X_string, "\\<^X>").replace(Y_string, "\\<^Y>")))) |
|
111 |
||
32450 | 112 |
def parse_body_failsafe(source: CharSequence): List[XML.Tree] = |
113 |
{ |
|
29521 | 114 |
try { parse_body(source) } |
115 |
catch { case _: RuntimeException => List(markup_failsafe(source)) } |
|
116 |
} |
|
117 |
||
32450 | 118 |
def parse_failsafe(source: CharSequence): XML.Tree = |
119 |
{ |
|
27960 | 120 |
try { parse(source) } |
29521 | 121 |
catch { case _: RuntimeException => markup_failsafe(source) } |
27960 | 122 |
} |
27930 | 123 |
} |