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