author | wenzelm |
Mon, 09 Sep 2024 11:12:13 +0200 | |
changeset 80821 | eb383d50564b |
parent 80820 | db114ec720cb |
child 80826 | 7feaa04d332b |
permissions | -rw-r--r-- |
44698 | 1 |
(* Title: Pure/PIDE/yxml.ML |
26528 | 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 |
44698 | 5 |
and Y -- no escaping, may nest marked text verbatim. Suitable for |
6 |
direct inlining into plain text. |
|
26528 | 7 |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
8 |
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
|
9 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
10 |
X Y name Y att=val ... X |
26528 | 11 |
... |
12 |
body |
|
13 |
... |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
14 |
X Y X |
26528 | 15 |
*) |
16 |
||
17 |
signature YXML = |
|
18 |
sig |
|
43777 | 19 |
val X: Symbol.symbol |
20 |
val Y: Symbol.symbol |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43728
diff
changeset
|
21 |
val detect: string -> bool |
70990 | 22 |
val traverse: (string -> 'a -> 'a) -> XML.tree -> 'a -> 'a |
73557 | 23 |
val tree_size: XML.tree -> int |
24 |
val body_size: XML.body -> int |
|
43728 | 25 |
val string_of_body: XML.body -> string |
26528 | 26 |
val string_of: XML.tree -> string |
80563 | 27 |
val bytes_of_body: XML.body -> Bytes.T |
28 |
val bytes_of: XML.tree -> Bytes.T |
|
70991
f9f7c34b7dd4
more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents:
70990
diff
changeset
|
29 |
val write_body: Path.T -> XML.body -> unit |
70989 | 30 |
val output_markup: Markup.T -> string * string |
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
31 |
val output_markup_elem: Markup.T -> (string * string) * string |
80821 | 32 |
val markup_ops: Markup.ops |
38266
492d377ecfe2
type XML.body as basic data representation language;
wenzelm
parents:
38265
diff
changeset
|
33 |
val parse_body: string -> XML.body |
75626 | 34 |
val parse_body_bytes: Bytes.T -> XML.body |
26528 | 35 |
val parse: string -> XML.tree |
75626 | 36 |
val parse_bytes: Bytes.T -> XML.tree |
71456
09c850e82258
more robust pretty printing of broken YXML, e.g. single "\^E";
wenzelm
parents:
70996
diff
changeset
|
37 |
val is_wellformed: string -> bool |
26528 | 38 |
end; |
39 |
||
40 |
structure YXML: YXML = |
|
41 |
struct |
|
42 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
43 |
(** string representation **) |
26528 | 44 |
|
26547 | 45 |
(* markers *) |
46 |
||
64275
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
59433
diff
changeset
|
47 |
val X_char = Char.chr 5; |
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
59433
diff
changeset
|
48 |
val Y_char = Char.chr 6; |
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
59433
diff
changeset
|
49 |
|
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
59433
diff
changeset
|
50 |
val X = String.str X_char; |
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
59433
diff
changeset
|
51 |
val Y = String.str Y_char; |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
52 |
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
|
53 |
val XYX = XY ^ X; |
26528 | 54 |
|
64275
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
59433
diff
changeset
|
55 |
fun detect s = Char.contains s X_char orelse Char.contains s Y_char; |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43728
diff
changeset
|
56 |
|
26528 | 57 |
|
73557 | 58 |
(* traverse *) |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
59 |
|
70990 | 60 |
fun traverse string = |
61 |
let |
|
62 |
fun attrib (a, x) = string Y #> string a #> string "=" #> string x; |
|
73556
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
71456
diff
changeset
|
63 |
fun tree (XML.Elem (markup as (name, atts), ts)) = |
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
71456
diff
changeset
|
64 |
if Markup.is_empty markup then fold tree ts |
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
71456
diff
changeset
|
65 |
else |
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
71456
diff
changeset
|
66 |
string XY #> string name #> fold attrib atts #> string X #> |
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
71456
diff
changeset
|
67 |
fold tree ts #> |
192bcee4f8b8
more robust treatment of empty markup: it allows to produce formal chunks;
wenzelm
parents:
71456
diff
changeset
|
68 |
string XYX |
70990 | 69 |
| tree (XML.Text s) = string s; |
70 |
in tree end; |
|
43728 | 71 |
|
77768 | 72 |
val tree_size = Integer.build o traverse (Integer.add o size); |
73 |
val body_size = Integer.build o fold (Integer.add o tree_size); |
|
73557 | 74 |
|
75 |
||
76 |
(* output *) |
|
77 |
||
74231 | 78 |
val string_of_body = Buffer.build_content o fold (traverse Buffer.add); |
43728 | 79 |
val string_of = string_of_body o single; |
26528 | 80 |
|
80563 | 81 |
val bytes_of_body = Bytes.build o fold (traverse Bytes.add); |
82 |
val bytes_of = bytes_of_body o single; |
|
83 |
||
70991
f9f7c34b7dd4
more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents:
70990
diff
changeset
|
84 |
fun write_body path body = |
75615 | 85 |
path |> File_Stream.open_output (fn file => |
86 |
fold (traverse (fn s => fn () => File_Stream.output file s)) body ()); |
|
70991
f9f7c34b7dd4
more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents:
70990
diff
changeset
|
87 |
|
26528 | 88 |
|
70989 | 89 |
(* markup elements *) |
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
90 |
|
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
91 |
val Z = chr 0; |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
92 |
val Z_text = [XML.Text Z]; |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
93 |
|
70989 | 94 |
fun output_markup (markup as (name, atts)) = |
95 |
if Markup.is_empty markup then Markup.no_output |
|
80504 | 96 |
else (XY ^ name ^ implode (map (fn p => Y ^ Properties.print_eq p) atts) ^ X, XYX); |
70989 | 97 |
|
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
98 |
fun output_markup_elem markup = |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
99 |
let val [bg1, bg2, en] = space_explode Z (string_of (XML.wrap_elem ((markup, Z_text), Z_text))) |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
100 |
in ((bg1, bg2), en) end; |
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
101 |
|
80821 | 102 |
val markup_ops: Markup.ops = {output = output_markup}; |
49656
7ff712de5747
treat wrapped markup elements as raw markup delimiters;
wenzelm
parents:
46832
diff
changeset
|
103 |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
104 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
105 |
(** efficient YXML parsing **) |
26528 | 106 |
|
107 |
local |
|
108 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
109 |
(* splitting *) |
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
110 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
111 |
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
|
112 |
Substring.full #> |
64275
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
59433
diff
changeset
|
113 |
Substring.tokens (fn c => c = X_char) #> |
ac2abc987cf9
accomodate Poly/ML repository version, which treats singleton strings as boxed;
wenzelm
parents:
59433
diff
changeset
|
114 |
map (Substring.fields (fn c => c = Y_char) #> map Substring.string); |
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
115 |
|
75626 | 116 |
val split_bytes = |
117 |
Bytes.space_explode X |
|
80561 | 118 |
#> map_filter (fn "" => NONE | s => SOME (space_explode Y s)); |
75626 | 119 |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
120 |
|
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
121 |
(* structural errors *) |
26528 | 122 |
|
46832 | 123 |
fun err msg = raise Fail ("Malformed YXML: " ^ msg); |
26528 | 124 |
fun err_attribute () = err "bad attribute"; |
125 |
fun err_element () = err "bad element"; |
|
126 |
fun err_unbalanced "" = err "unbalanced element" |
|
127 |
| err_unbalanced name = err ("unbalanced element " ^ quote name); |
|
128 |
||
129 |
||
130 |
(* stack operations *) |
|
131 |
||
132 |
fun add x ((elem, body) :: pending) = (elem, x :: body) :: pending; |
|
133 |
||
134 |
fun push "" _ _ = err_element () |
|
135 |
| push name atts pending = ((name, atts), []) :: pending; |
|
136 |
||
137 |
fun pop ((("", _), _) :: _) = err_unbalanced "" |
|
38228
ada3ab6b9085
simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents:
34095
diff
changeset
|
138 |
| pop ((markup, body) :: pending) = add (XML.Elem (markup, rev body)) pending; |
26528 | 139 |
|
140 |
||
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
141 |
(* parsing *) |
26528 | 142 |
|
143 |
fun parse_attrib s = |
|
28025 | 144 |
(case first_field "=" s of |
28023
92dd3ad302b7
simplified parse_attrib (find_substring instead of space_explode);
wenzelm
parents:
27932
diff
changeset
|
145 |
NONE => err_attribute () |
28025 | 146 |
| SOME ("", _) => err_attribute () |
147 |
| SOME att => att); |
|
26528 | 148 |
|
26540
173d548ce9d2
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm
parents:
26528
diff
changeset
|
149 |
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
|
150 |
| 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
|
151 |
| parse_chunk txts = fold (add o XML.Text) txts; |
26528 | 152 |
|
75626 | 153 |
fun parse_body' chunks = |
154 |
(case fold parse_chunk chunks [(("", []), [])] of |
|
26528 | 155 |
[(("", _), result)] => rev result |
156 |
| ((name, _), _) :: _ => err_unbalanced name); |
|
157 |
||
75626 | 158 |
fun parse' chunks = |
159 |
(case parse_body' chunks of |
|
27798
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
wenzelm
parents:
26684
diff
changeset
|
160 |
[result] => result |
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
wenzelm
parents:
26684
diff
changeset
|
161 |
| [] => XML.Text "" |
b96c73f11a23
YXML.parse: allow text without markup, potentially empty;
wenzelm
parents:
26684
diff
changeset
|
162 |
| _ => err "multiple results"); |
26528 | 163 |
|
75626 | 164 |
in |
165 |
||
166 |
val parse_body = parse_body' o split_string; |
|
167 |
val parse_body_bytes = parse_body' o split_bytes; |
|
168 |
||
169 |
val parse = parse' o split_string; |
|
170 |
val parse_bytes = parse' o split_bytes; |
|
171 |
||
26528 | 172 |
end; |
173 |
||
71456
09c850e82258
more robust pretty printing of broken YXML, e.g. single "\^E";
wenzelm
parents:
70996
diff
changeset
|
174 |
fun is_wellformed s = string_of_body (parse_body s) = s |
09c850e82258
more robust pretty printing of broken YXML, e.g. single "\^E";
wenzelm
parents:
70996
diff
changeset
|
175 |
handle Fail _ => false; |
09c850e82258
more robust pretty printing of broken YXML, e.g. single "\^E";
wenzelm
parents:
70996
diff
changeset
|
176 |
|
26528 | 177 |
end; |