author | wenzelm |
Tue, 12 Jul 2011 13:45:05 +0200 | |
changeset 43772 | c825594fd0c1 |
parent 43731 | 70072780e095 |
child 43777 | 22c87ff1b8a9 |
permissions | -rw-r--r-- |
26528 | 1 |
(* Title: Pure/General/yxml.ML |
2 |
Author: Makarius |
|
3 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
4 |
Efficient text representation of XML trees using extra characters X |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
5 |
and Y -- no escaping, may nest marked text verbatim. |
26528 | 6 |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
7 |
Markup <elem att="val" ...>...body...</elem> is encoded as: |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
8 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
9 |
X Y name Y att=val ... X |
26528 | 10 |
... |
11 |
body |
|
12 |
... |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
13 |
X Y X |
26528 | 14 |
*) |
15 |
||
16 |
signature YXML = |
|
17 |
sig |
|
43772
c825594fd0c1
clarified YXML.embed_controls -- this is idempotent and cannot be nested;
wenzelm
parents:
43731
diff
changeset
|
18 |
val embed_controls: string -> string |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43728
diff
changeset
|
19 |
val detect: string -> bool |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
20 |
val output_markup: Markup.T -> string * string |
26528 | 21 |
val element: string -> XML.attributes -> string list -> string |
43728 | 22 |
val string_of_body: XML.body -> string |
26528 | 23 |
val string_of: XML.tree -> string |
38266
492d377ecfe2
type XML.body as basic data representation language;
wenzelm
parents:
38265
diff
changeset
|
24 |
val parse_body: string -> XML.body |
26528 | 25 |
val parse: string -> XML.tree |
26 |
end; |
|
27 |
||
28 |
structure YXML: YXML = |
|
29 |
struct |
|
30 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
31 |
(** string representation **) |
26528 | 32 |
|
38265
cc9fde54311f
renamed YXML.binary_text to YXML.escape_controls to emphasize what it actually does;
wenzelm
parents:
38228
diff
changeset
|
33 |
(* idempotent recoding of certain low ASCII control characters *) |
34095
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
34 |
|
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
35 |
fun pseudo_utf8 c = |
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
36 |
if Symbol.is_ascii_control c |
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
37 |
then chr 192 ^ chr (128 + ord c) |
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
38 |
else c; |
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
39 |
|
43772
c825594fd0c1
clarified YXML.embed_controls -- this is idempotent and cannot be nested;
wenzelm
parents:
43731
diff
changeset
|
40 |
fun embed_controls str = |
34095
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
41 |
if exists_string Symbol.is_ascii_control str |
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
42 |
then translate_string pseudo_utf8 str |
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
43 |
else str; |
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
44 |
|
c2f176a38448
robust representation of low ASCII control characters within XML/YXML text;
wenzelm
parents:
31469
diff
changeset
|
45 |
|
26547 | 46 |
(* markers *) |
47 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
48 |
val X = Symbol.ENQ; |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
49 |
val Y = Symbol.ACK; |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
50 |
val XY = X ^ Y; |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
51 |
val XYX = XY ^ X; |
26528 | 52 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43728
diff
changeset
|
53 |
val detect = exists_string (fn s => s = X); |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43728
diff
changeset
|
54 |
|
26528 | 55 |
|
26547 | 56 |
(* output *) |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
57 |
|
27884 | 58 |
fun output_markup (markup as (name, atts)) = |
38474
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents:
38266
diff
changeset
|
59 |
if Markup.is_empty markup then Markup.no_output |
27884 | 60 |
else (XY ^ name ^ implode (map (fn (a, x) => Y ^ a ^ "=" ^ x) atts) ^ X, XYX); |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
61 |
|
26528 | 62 |
fun element name atts body = |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
63 |
let val (pre, post) = output_markup (name, atts) |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
64 |
in pre ^ implode body ^ post end; |
26528 | 65 |
|
43728 | 66 |
fun string_of_body body = |
26528 | 67 |
let |
68 |
fun attrib (a, x) = |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
69 |
Buffer.add Y #> |
26528 | 70 |
Buffer.add a #> Buffer.add "=" #> Buffer.add x; |
38228
ada3ab6b9085
simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents:
34095
diff
changeset
|
71 |
fun tree (XML.Elem ((name, atts), ts)) = |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
72 |
Buffer.add XY #> Buffer.add name #> fold attrib atts #> Buffer.add X #> |
43728 | 73 |
trees ts #> |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
74 |
Buffer.add XYX |
43728 | 75 |
| tree (XML.Text s) = Buffer.add s |
76 |
and trees ts = fold tree ts; |
|
77 |
in Buffer.empty |> trees body |> Buffer.content end; |
|
78 |
||
79 |
val string_of = string_of_body o single; |
|
26528 | 80 |
|
81 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
82 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
83 |
(** efficient YXML parsing **) |
26528 | 84 |
|
85 |
local |
|
86 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
87 |
(* splitting *) |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
88 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
89 |
fun is_char s c = ord s = Char.ord c; |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
90 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
91 |
val split_string = |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
92 |
Substring.full #> |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
93 |
Substring.tokens (is_char X) #> |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
94 |
map (Substring.fields (is_char Y) #> map Substring.string); |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
95 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
96 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
97 |
(* structural errors *) |
26528 | 98 |
|
99 |
fun err msg = raise Fail ("Malformed YXML encoding: " ^ msg); |
|
100 |
fun err_attribute () = err "bad attribute"; |
|
101 |
fun err_element () = err "bad element"; |
|
102 |
fun err_unbalanced "" = err "unbalanced element" |
|
103 |
| err_unbalanced name = err ("unbalanced element " ^ quote name); |
|
104 |
||
105 |
||
106 |
(* stack operations *) |
|
107 |
||
108 |
fun add x ((elem, body) :: pending) = (elem, x :: body) :: pending; |
|
109 |
||
110 |
fun push "" _ _ = err_element () |
|
111 |
| push name atts pending = ((name, atts), []) :: pending; |
|
112 |
||
113 |
fun pop ((("", _), _) :: _) = err_unbalanced "" |
|
38228
ada3ab6b9085
simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents:
34095
diff
changeset
|
114 |
| pop ((markup, body) :: pending) = add (XML.Elem (markup, rev body)) pending; |
26528 | 115 |
|
116 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
117 |
(* parsing *) |
26528 | 118 |
|
119 |
fun parse_attrib s = |
|
28025 | 120 |
(case first_field "=" s of |
28023
92dd3ad302b7
simplified parse_attrib (find_substring instead of space_explode);
wenzelm
parents:
27932
diff
changeset
|
121 |
NONE => err_attribute () |
28025 | 122 |
| SOME ("", _) => err_attribute () |
123 |
| SOME att => att); |
|
26528 | 124 |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
125 |
fun parse_chunk ["", ""] = pop |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
126 |
| parse_chunk ("" :: name :: atts) = push name (map parse_attrib atts) |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
127 |
| parse_chunk txts = fold (add o XML.Text) txts; |
26528 | 128 |
|
129 |
in |
|
130 |
||
131 |
fun parse_body source = |
|
43615
8e0f6cfa8eb2
reverted ce00462f,b3759dce, 7a165592: unwanted generalisation
noschinl
parents:
42355
diff
changeset
|
132 |
(case fold parse_chunk (split_string source) [(("", []), [])] of |
26528 | 133 |
[(("", _), result)] => rev result |
134 |
| ((name, _), _) :: _ => err_unbalanced name); |
|
135 |
||
136 |
fun parse source = |
|
137 |
(case parse_body source of |
|
27798
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
wenzelm
parents:
26684
diff
changeset
|
138 |
[result] => result |
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
wenzelm
parents:
26684
diff
changeset
|
139 |
| [] => XML.Text "" |
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
wenzelm
parents:
26684
diff
changeset
|
140 |
| _ => err "multiple results"); |
26528 | 141 |
|
142 |
end; |
|
143 |
||
144 |
end; |
|
145 |