author | wenzelm |
Sun, 14 Nov 2021 20:15:28 +0100 | |
changeset 74785 | 4671d29feb00 |
parent 74231 | b3c65c984210 |
child 74789 | a5c23da73fca |
permissions | -rw-r--r-- |
44698 | 1 |
(* Title: Pure/PIDE/xml.ML |
2 |
Author: David Aspinall |
|
3 |
Author: Stefan Berghofer |
|
4 |
Author: Makarius |
|
24264 | 5 |
|
46840 | 6 |
Untyped XML trees and representation of ML values. |
24264 | 7 |
*) |
8 |
||
43767 | 9 |
signature XML_DATA_OPS = |
10 |
sig |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
11 |
type 'a A |
43767 | 12 |
type 'a T |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
13 |
type 'a V |
70828 | 14 |
type 'a P |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
15 |
val int_atom: int A |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
16 |
val bool_atom: bool A |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
17 |
val unit_atom: unit A |
43767 | 18 |
val properties: Properties.T T |
19 |
val string: string T |
|
20 |
val int: int T |
|
21 |
val bool: bool T |
|
22 |
val unit: unit T |
|
23 |
val pair: 'a T -> 'b T -> ('a * 'b) T |
|
24 |
val triple: 'a T -> 'b T -> 'c T -> ('a * 'b * 'c) T |
|
25 |
val list: 'a T -> 'a list T |
|
26 |
val option: 'a T -> 'a option T |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
27 |
val variant: 'a V list -> 'a T |
43767 | 28 |
end; |
29 |
||
24264 | 30 |
signature XML = |
31 |
sig |
|
46837
5bdd68f380b3
clarified XML signature (again) -- coincide with basic Markup without explicit dependency;
wenzelm
parents:
45155
diff
changeset
|
32 |
type attributes = (string * string) list |
24264 | 33 |
datatype tree = |
46837
5bdd68f380b3
clarified XML signature (again) -- coincide with basic Markup without explicit dependency;
wenzelm
parents:
45155
diff
changeset
|
34 |
Elem of (string * attributes) * tree list |
24264 | 35 |
| Text of string |
38266
492d377ecfe2
type XML.body as basic data representation language;
wenzelm
parents:
38228
diff
changeset
|
36 |
type body = tree list |
74785 | 37 |
val string: string -> body |
70991
f9f7c34b7dd4
more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents:
70828
diff
changeset
|
38 |
val blob: string list -> body |
70994 | 39 |
val is_empty: tree -> bool |
40 |
val is_empty_body: body -> bool |
|
69234 | 41 |
val xml_elemN: string |
42 |
val xml_nameN: string |
|
43 |
val xml_bodyN: string |
|
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
44 |
val wrap_elem: ((string * attributes) * tree list) * tree list -> tree |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
45 |
val unwrap_elem: tree -> (((string * attributes) * tree list) * tree list) option |
26546 | 46 |
val add_content: tree -> Buffer.T -> Buffer.T |
39555
ccb223a4d49c
added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents:
38474
diff
changeset
|
47 |
val content_of: body -> string |
56059
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents:
49650
diff
changeset
|
48 |
val trim_blanks: body -> body |
26546 | 49 |
val header: string |
50 |
val text: string -> string |
|
51 |
val element: string -> attributes -> string list -> string |
|
69345
6bd63c94cf62
tuned signature (see also src/Tools/Haskell/Markup.hs);
wenzelm
parents:
69234
diff
changeset
|
52 |
val output_markup: Markup.T -> Markup.output |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
53 |
val string_of: tree -> string |
43791 | 54 |
val pretty: int -> tree -> Pretty.T |
26984
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
55 |
val parse_comments: string list -> unit * string list |
24264 | 56 |
val parse_string : string -> string option |
26546 | 57 |
val parse_element: string list -> tree * string list |
26984
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
58 |
val parse_document: string list -> tree * string list |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
59 |
val parse: string -> tree |
43767 | 60 |
exception XML_ATOM of string |
61 |
exception XML_BODY of body |
|
65333 | 62 |
structure Encode: |
63 |
sig |
|
64 |
include XML_DATA_OPS |
|
65 |
val tree: tree T |
|
66 |
end |
|
67 |
structure Decode: |
|
68 |
sig |
|
69 |
include XML_DATA_OPS |
|
70 |
val tree: tree T |
|
71 |
end |
|
24264 | 72 |
end; |
73 |
||
74 |
structure XML: XML = |
|
75 |
struct |
|
76 |
||
26546 | 77 |
(** XML trees **) |
78 |
||
70991
f9f7c34b7dd4
more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents:
70828
diff
changeset
|
79 |
open Output_Primitives.XML; |
26546 | 80 |
|
74785 | 81 |
fun string "" = [] |
82 |
| string s = [Text s]; |
|
83 |
||
70991
f9f7c34b7dd4
more scalable protocol_message: use XML.body directly (Output.output hook is not required);
wenzelm
parents:
70828
diff
changeset
|
84 |
val blob = map Text; |
38266
492d377ecfe2
type XML.body as basic data representation language;
wenzelm
parents:
38228
diff
changeset
|
85 |
|
70994 | 86 |
fun is_empty (Text "") = true |
87 |
| is_empty _ = false; |
|
88 |
||
89 |
val is_empty_body = forall is_empty; |
|
90 |
||
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
91 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
92 |
(* wrapped elements *) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
93 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
94 |
val xml_elemN = "xml_elem"; |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
95 |
val xml_nameN = "xml_name"; |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
96 |
val xml_bodyN = "xml_body"; |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
97 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
98 |
fun wrap_elem (((a, atts), body1), body2) = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
99 |
Elem ((xml_elemN, (xml_nameN, a) :: atts), Elem ((xml_bodyN, []), body1) :: body2); |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
100 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
101 |
fun unwrap_elem (Elem ((name, (n, a) :: atts), Elem ((name', atts'), body1) :: body2)) = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
102 |
if name = xml_elemN andalso n = xml_nameN andalso name' = xml_bodyN andalso null atts' |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
103 |
then SOME (((a, atts), body1), body2) else NONE |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
104 |
| unwrap_elem _ = NONE; |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
105 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
106 |
|
69224 | 107 |
(* text content *) |
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
108 |
|
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
109 |
fun add_content tree = |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
110 |
(case unwrap_elem tree of |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
111 |
SOME (_, ts) => fold add_content ts |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
112 |
| NONE => |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
113 |
(case tree of |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
114 |
Elem (_, ts) => fold add_content ts |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49599
diff
changeset
|
115 |
| Text s => Buffer.add s)); |
26546 | 116 |
|
74231 | 117 |
val content_of = Buffer.build_content o fold add_content; |
39555
ccb223a4d49c
added XML.content_of convenience -- cover XML.body, which is the general situation;
wenzelm
parents:
38474
diff
changeset
|
118 |
|
26546 | 119 |
|
56059
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents:
49650
diff
changeset
|
120 |
(* trim blanks *) |
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents:
49650
diff
changeset
|
121 |
|
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents:
49650
diff
changeset
|
122 |
fun trim_blanks trees = |
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents:
49650
diff
changeset
|
123 |
trees |> maps |
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents:
49650
diff
changeset
|
124 |
(fn Elem (markup, body) => [Elem (markup, trim_blanks body)] |
74785 | 125 |
| Text s => s |> raw_explode |> trim Symbol.is_blank |> implode |> string); |
56059
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents:
49650
diff
changeset
|
126 |
|
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents:
49650
diff
changeset
|
127 |
|
24264 | 128 |
|
26525 | 129 |
(** string representation **) |
130 |
||
69806 | 131 |
val header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; |
24264 | 132 |
|
133 |
||
26546 | 134 |
(* escaped text *) |
24264 | 135 |
|
136 |
fun decode "<" = "<" |
|
137 |
| decode ">" = ">" |
|
138 |
| decode "&" = "&" |
|
139 |
| decode "'" = "'" |
|
140 |
| decode """ = "\"" |
|
141 |
| decode c = c; |
|
142 |
||
143 |
fun encode "<" = "<" |
|
144 |
| encode ">" = ">" |
|
145 |
| encode "&" = "&" |
|
146 |
| encode "'" = "'" |
|
147 |
| encode "\"" = """ |
|
148 |
| encode c = c; |
|
149 |
||
25838 | 150 |
val text = translate_string encode; |
24264 | 151 |
|
152 |
||
153 |
(* elements *) |
|
154 |
||
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
155 |
fun elem name atts = |
26551 | 156 |
space_implode " " (name :: map (fn (a, x) => a ^ "=\"" ^ text x ^ "\"") atts); |
24264 | 157 |
|
26525 | 158 |
fun element name atts body = |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
159 |
let val b = implode body in |
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
160 |
if b = "" then enclose "<" "/>" (elem name atts) |
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
161 |
else enclose "<" ">" (elem name atts) ^ b ^ enclose "</" ">" name |
24264 | 162 |
end; |
163 |
||
27884 | 164 |
fun output_markup (markup as (name, atts)) = |
38474
e498dc2eb576
uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents:
38266
diff
changeset
|
165 |
if Markup.is_empty markup then Markup.no_output |
27884 | 166 |
else (enclose "<" ">" (elem name atts), enclose "</" ">" name); |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
167 |
|
24264 | 168 |
|
74231 | 169 |
(* output content *) |
24264 | 170 |
|
74231 | 171 |
fun content_depth depth = |
24264 | 172 |
let |
43791 | 173 |
fun traverse _ (Elem ((name, atts), [])) = |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
174 |
Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add "/>" |
43791 | 175 |
| traverse d (Elem ((name, atts), ts)) = |
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
176 |
Buffer.add "<" #> Buffer.add (elem name atts) #> Buffer.add ">" #> |
43791 | 177 |
traverse_body d ts #> |
26525 | 178 |
Buffer.add "</" #> Buffer.add name #> Buffer.add ">" |
43791 | 179 |
| traverse _ (Text s) = Buffer.add (text s) |
180 |
and traverse_body 0 _ = Buffer.add "..." |
|
181 |
| traverse_body d ts = fold (traverse (d - 1)) ts; |
|
74231 | 182 |
in Buffer.build_content o traverse depth end; |
24264 | 183 |
|
74231 | 184 |
val string_of = content_depth ~1; |
43791 | 185 |
|
74231 | 186 |
fun pretty depth tree = Pretty.str (content_depth (Int.max (0, depth)) tree); |
25838 | 187 |
|
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62663
diff
changeset
|
188 |
val _ = ML_system_pp (fn depth => fn _ => Pretty.to_polyml o pretty (FixedInt.toInt depth)); |
62663 | 189 |
|
24264 | 190 |
|
191 |
||
44698 | 192 |
(** XML parsing **) |
26546 | 193 |
|
194 |
local |
|
24264 | 195 |
|
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43844
diff
changeset
|
196 |
fun err msg (xs, _) = |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43844
diff
changeset
|
197 |
fn () => "XML parsing error: " ^ msg () ^ "\nfound: " ^ quote (Symbol.beginning 100 xs); |
24264 | 198 |
|
26984
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
199 |
fun ignored _ = []; |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
200 |
|
45155
3216d65d8f34
slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents:
44809
diff
changeset
|
201 |
fun name_start_char c = Symbol.is_ascii_letter c orelse c = ":" orelse c = "_"; |
3216d65d8f34
slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents:
44809
diff
changeset
|
202 |
fun name_char c = name_start_char c orelse Symbol.is_ascii_digit c orelse c = "-" orelse c = "."; |
3216d65d8f34
slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents:
44809
diff
changeset
|
203 |
val parse_name = Scan.one name_start_char ::: Scan.many name_char; |
3216d65d8f34
slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents:
44809
diff
changeset
|
204 |
|
26551 | 205 |
val blanks = Scan.many Symbol.is_blank; |
45155
3216d65d8f34
slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents:
44809
diff
changeset
|
206 |
val special = $$ "&" ^^ (parse_name >> implode) ^^ $$ ";" >> decode; |
58854 | 207 |
val regular = Scan.one Symbol.not_eof; |
208 |
fun regular_except x = Scan.one (fn c => Symbol.not_eof c andalso c <> x); |
|
24264 | 209 |
|
26551 | 210 |
val parse_chars = Scan.repeat1 (special || regular_except "<") >> implode; |
24264 | 211 |
|
26551 | 212 |
val parse_cdata = |
213 |
Scan.this_string "<![CDATA[" |-- |
|
214 |
(Scan.repeat (Scan.unless (Scan.this_string "]]>") regular) >> implode) --| |
|
215 |
Scan.this_string "]]>"; |
|
24264 | 216 |
|
217 |
val parse_att = |
|
45155
3216d65d8f34
slightly more standard-conformant XML parsing (see also 94033767ef9b);
wenzelm
parents:
44809
diff
changeset
|
218 |
((parse_name >> implode) --| (blanks -- $$ "=" -- blanks)) -- |
26551 | 219 |
(($$ "\"" || $$ "'") :|-- (fn s => |
220 |
(Scan.repeat (special || regular_except s) >> implode) --| $$ s)); |
|
24264 | 221 |
|
26551 | 222 |
val parse_comment = |
223 |
Scan.this_string "<!--" -- |
|
224 |
Scan.repeat (Scan.unless (Scan.this_string "-->") regular) -- |
|
26984
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
225 |
Scan.this_string "-->" >> ignored; |
24264 | 226 |
|
26551 | 227 |
val parse_processing_instruction = |
228 |
Scan.this_string "<?" -- |
|
229 |
Scan.repeat (Scan.unless (Scan.this_string "?>") regular) -- |
|
26984
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
230 |
Scan.this_string "?>" >> ignored; |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
231 |
|
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
232 |
val parse_doctype = |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
233 |
Scan.this_string "<!DOCTYPE" -- |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
234 |
Scan.repeat (Scan.unless ($$ ">") regular) -- |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
235 |
$$ ">" >> ignored; |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
236 |
|
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
237 |
val parse_misc = |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
238 |
Scan.one Symbol.is_blank >> ignored || |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
239 |
parse_processing_instruction || |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
240 |
parse_comment; |
26551 | 241 |
|
242 |
val parse_optional_text = |
|
243 |
Scan.optional (parse_chars >> (single o Text)) []; |
|
24264 | 244 |
|
26546 | 245 |
in |
246 |
||
26984
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
247 |
val parse_comments = |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
248 |
blanks -- Scan.repeat (parse_comment -- blanks >> K ()) >> K (); |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
249 |
|
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40131
diff
changeset
|
250 |
val parse_string = Scan.read Symbol.stopper parse_chars o raw_explode; |
26546 | 251 |
|
24264 | 252 |
fun parse_content xs = |
26551 | 253 |
(parse_optional_text @@@ |
61476 | 254 |
Scan.repeats |
26551 | 255 |
((parse_element >> single || |
256 |
parse_cdata >> (single o Text) || |
|
26984
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
257 |
parse_processing_instruction || |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
258 |
parse_comment) |
61476 | 259 |
@@@ parse_optional_text)) xs |
24264 | 260 |
|
26546 | 261 |
and parse_element xs = |
43949
94033767ef9b
more precise parse_name according to XML standard;
wenzelm
parents:
43947
diff
changeset
|
262 |
($$ "<" |-- parse_name -- Scan.repeat (blanks |-- parse_att) --| blanks :-- |
94033767ef9b
more precise parse_name according to XML standard;
wenzelm
parents:
43947
diff
changeset
|
263 |
(fn (name, _) => |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43844
diff
changeset
|
264 |
!! (err (fn () => "Expected > or />")) |
43949
94033767ef9b
more precise parse_name according to XML standard;
wenzelm
parents:
43947
diff
changeset
|
265 |
($$ "/" -- $$ ">" >> ignored || |
94033767ef9b
more precise parse_name according to XML standard;
wenzelm
parents:
43947
diff
changeset
|
266 |
$$ ">" |-- parse_content --| |
94033767ef9b
more precise parse_name according to XML standard;
wenzelm
parents:
43947
diff
changeset
|
267 |
!! (err (fn () => "Expected </" ^ implode name ^ ">")) |
94033767ef9b
more precise parse_name according to XML standard;
wenzelm
parents:
43947
diff
changeset
|
268 |
($$ "<" -- $$ "/" -- Scan.this name -- blanks -- $$ ">"))) |
94033767ef9b
more precise parse_name according to XML standard;
wenzelm
parents:
43947
diff
changeset
|
269 |
>> (fn ((name, atts), body) => Elem ((implode name, atts), body))) xs; |
24264 | 270 |
|
26984
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
271 |
val parse_document = |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
272 |
(Scan.repeat parse_misc -- Scan.option parse_doctype -- Scan.repeat parse_misc) |
d0e098e206f3
added parse_document (optional unchecked header material);
wenzelm
parents:
26554
diff
changeset
|
273 |
|-- parse_element; |
24264 | 274 |
|
26539
a0754be538ab
added output_markup (from Tools/isabelle_process.ML);
wenzelm
parents:
26525
diff
changeset
|
275 |
fun parse s = |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43844
diff
changeset
|
276 |
(case Scan.finite Symbol.stopper (Scan.error (!! (err (fn () => "Malformed element")) |
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40131
diff
changeset
|
277 |
(blanks |-- parse_document --| blanks))) (raw_explode s) of |
24264 | 278 |
(x, []) => x |
48769 | 279 |
| (_, ys) => error ("XML parsing error: unprocessed input\n" ^ Symbol.beginning 100 ys)); |
24264 | 280 |
|
281 |
end; |
|
26546 | 282 |
|
43767 | 283 |
|
284 |
||
285 |
(** XML as data representation language **) |
|
286 |
||
287 |
exception XML_ATOM of string; |
|
288 |
exception XML_BODY of tree list; |
|
289 |
||
290 |
||
291 |
structure Encode = |
|
292 |
struct |
|
293 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
294 |
type 'a A = 'a -> string; |
43767 | 295 |
type 'a T = 'a -> body; |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
296 |
type 'a V = 'a -> string list * body; |
70828 | 297 |
type 'a P = 'a -> string list; |
43767 | 298 |
|
299 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
300 |
(* atomic values *) |
43767 | 301 |
|
69574 | 302 |
fun int_atom i = Value.print_int i; |
43767 | 303 |
|
304 |
fun bool_atom false = "0" |
|
305 |
| bool_atom true = "1"; |
|
306 |
||
307 |
fun unit_atom () = ""; |
|
308 |
||
309 |
||
310 |
(* structural nodes *) |
|
311 |
||
312 |
fun node ts = Elem ((":", []), ts); |
|
313 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
314 |
fun vector xs = map_index (fn (i, x) => (int_atom i, x)) xs; |
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
315 |
|
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
316 |
fun tagged (tag, (xs, ts)) = Elem ((int_atom tag, vector xs), ts); |
43767 | 317 |
|
318 |
||
319 |
(* representation of standard types *) |
|
320 |
||
65333 | 321 |
fun tree (t: tree) = [t]; |
322 |
||
43767 | 323 |
fun properties props = [Elem ((":", props), [])]; |
324 |
||
325 |
fun string "" = [] |
|
326 |
| string s = [Text s]; |
|
327 |
||
328 |
val int = string o int_atom; |
|
329 |
||
330 |
val bool = string o bool_atom; |
|
331 |
||
332 |
val unit = string o unit_atom; |
|
333 |
||
334 |
fun pair f g (x, y) = [node (f x), node (g y)]; |
|
335 |
||
336 |
fun triple f g h (x, y, z) = [node (f x), node (g y), node (h z)]; |
|
337 |
||
338 |
fun list f xs = map (node o f) xs; |
|
339 |
||
340 |
fun option _ NONE = [] |
|
341 |
| option f (SOME x) = [node (f x)]; |
|
342 |
||
47199
15ede9f1da3f
more specific notion of partiality (cf. Scala version);
wenzelm
parents:
46840
diff
changeset
|
343 |
fun variant fs x = |
15ede9f1da3f
more specific notion of partiality (cf. Scala version);
wenzelm
parents:
46840
diff
changeset
|
344 |
[tagged (the (get_index (fn f => SOME (f x) handle General.Match => NONE) fs))]; |
43767 | 345 |
|
26546 | 346 |
end; |
43767 | 347 |
|
348 |
||
349 |
structure Decode = |
|
350 |
struct |
|
351 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
352 |
type 'a A = string -> 'a; |
43767 | 353 |
type 'a T = body -> 'a; |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
354 |
type 'a V = string list * body -> 'a; |
70828 | 355 |
type 'a P = string list -> 'a; |
43767 | 356 |
|
357 |
||
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
358 |
(* atomic values *) |
43767 | 359 |
|
360 |
fun int_atom s = |
|
63806 | 361 |
Value.parse_int s |
43797
fad7758421bf
more precise integer Markup.properties/XML.attributes: disallow ML-style ~ minus;
wenzelm
parents:
43791
diff
changeset
|
362 |
handle Fail _ => raise XML_ATOM s; |
43767 | 363 |
|
364 |
fun bool_atom "0" = false |
|
365 |
| bool_atom "1" = true |
|
366 |
| bool_atom s = raise XML_ATOM s; |
|
367 |
||
368 |
fun unit_atom "" = () |
|
369 |
| unit_atom s = raise XML_ATOM s; |
|
370 |
||
371 |
||
372 |
(* structural nodes *) |
|
373 |
||
374 |
fun node (Elem ((":", []), ts)) = ts |
|
375 |
| node t = raise XML_BODY [t]; |
|
376 |
||
43783 | 377 |
fun vector atts = |
46839
f7232c078fa5
simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents:
46837
diff
changeset
|
378 |
map_index (fn (i, (a, x)) => if int_atom a = i then x else raise XML_ATOM a) atts; |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
379 |
|
43844 | 380 |
fun tagged (Elem ((name, atts), ts)) = (int_atom name, (vector atts, ts)) |
43767 | 381 |
| tagged t = raise XML_BODY [t]; |
382 |
||
383 |
||
384 |
(* representation of standard types *) |
|
385 |
||
65333 | 386 |
fun tree [t] = t |
387 |
| tree ts = raise XML_BODY ts; |
|
388 |
||
43767 | 389 |
fun properties [Elem ((":", props), [])] = props |
390 |
| properties ts = raise XML_BODY ts; |
|
391 |
||
392 |
fun string [] = "" |
|
393 |
| string [Text s] = s |
|
394 |
| string ts = raise XML_BODY ts; |
|
395 |
||
396 |
val int = int_atom o string; |
|
397 |
||
398 |
val bool = bool_atom o string; |
|
399 |
||
400 |
val unit = unit_atom o string; |
|
401 |
||
402 |
fun pair f g [t1, t2] = (f (node t1), g (node t2)) |
|
403 |
| pair _ _ ts = raise XML_BODY ts; |
|
404 |
||
405 |
fun triple f g h [t1, t2, t3] = (f (node t1), g (node t2), h (node t3)) |
|
406 |
| triple _ _ _ ts = raise XML_BODY ts; |
|
407 |
||
408 |
fun list f ts = map (f o node) ts; |
|
409 |
||
410 |
fun option _ [] = NONE |
|
411 |
| option f [t] = SOME (f (node t)) |
|
412 |
| option _ ts = raise XML_BODY ts; |
|
413 |
||
43768 | 414 |
fun variant fs [t] = |
415 |
let |
|
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
416 |
val (tag, (xs, ts)) = tagged t; |
43768 | 417 |
val f = nth fs tag handle General.Subscript => raise XML_BODY [t]; |
43778
ce9189450447
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents:
43768
diff
changeset
|
418 |
in f (xs, ts) end |
43767 | 419 |
| variant _ ts = raise XML_BODY ts; |
420 |
||
421 |
end; |
|
422 |
||
423 |
end; |